17
17
18
18
use super :: utils:: * ;
19
19
use async_trait:: async_trait;
20
- use hive_metastore:: { TThriftHiveMetastoreSyncClient , ThriftHiveMetastoreSyncClient } ;
20
+ use hive_metastore:: ThriftHiveMetastoreClient ;
21
+ use hive_metastore:: ThriftHiveMetastoreClientBuilder ;
21
22
use iceberg:: table:: Table ;
22
- use iceberg:: { Catalog , Namespace , NamespaceIdent , Result , TableCommit , TableCreation , TableIdent } ;
23
+ use iceberg:: {
24
+ Catalog , Error , ErrorKind , Namespace , NamespaceIdent , Result , TableCommit , TableCreation ,
25
+ TableIdent ,
26
+ } ;
23
27
use std:: collections:: HashMap ;
24
28
use std:: fmt:: { Debug , Formatter } ;
25
- use std:: sync:: { Arc , Mutex } ;
26
- use thrift:: protocol:: { TBinaryInputProtocol , TBinaryOutputProtocol } ;
27
- use thrift:: transport:: {
28
- ReadHalf , TBufferedReadTransport , TBufferedWriteTransport , TIoChannel , WriteHalf ,
29
- } ;
29
+ use std:: net:: ToSocketAddrs ;
30
30
use typed_builder:: TypedBuilder ;
31
31
32
32
/// Hive metastore Catalog configuration.
@@ -35,24 +35,7 @@ pub struct HmsCatalogConfig {
35
35
address : String ,
36
36
}
37
37
38
- /// TODO: We only support binary protocol for now.
39
- type HmsClientType = ThriftHiveMetastoreSyncClient <
40
- TBinaryInputProtocol < TBufferedReadTransport < ReadHalf < thrift:: transport:: TTcpChannel > > > ,
41
- TBinaryOutputProtocol < TBufferedWriteTransport < WriteHalf < thrift:: transport:: TTcpChannel > > > ,
42
- > ;
43
-
44
- /// # TODO
45
- ///
46
- /// we are using the same connection everytime, we should support connection
47
- /// pool in the future.
48
- struct HmsClient ( Arc < Mutex < HmsClientType > > ) ;
49
-
50
- impl HmsClient {
51
- fn call < T > ( & self , f : impl FnOnce ( & mut HmsClientType ) -> thrift:: Result < T > ) -> Result < T > {
52
- let mut client = self . 0 . lock ( ) . unwrap ( ) ;
53
- f ( & mut client) . map_err ( from_thrift_error)
54
- }
55
- }
38
+ struct HmsClient ( ThriftHiveMetastoreClient ) ;
56
39
57
40
/// Hive metastore Catalog.
58
41
pub struct HmsCatalog {
@@ -71,19 +54,29 @@ impl Debug for HmsCatalog {
71
54
impl HmsCatalog {
72
55
/// Create a new hms catalog.
73
56
pub fn new ( config : HmsCatalogConfig ) -> Result < Self > {
74
- let mut channel = thrift:: transport:: TTcpChannel :: new ( ) ;
75
- channel
76
- . open ( config. address . as_str ( ) )
77
- . map_err ( from_thrift_error) ?;
78
- let ( i_chan, o_chan) = channel. split ( ) . map_err ( from_thrift_error) ?;
79
- let i_chan = TBufferedReadTransport :: new ( i_chan) ;
80
- let o_chan = TBufferedWriteTransport :: new ( o_chan) ;
81
- let i_proto = TBinaryInputProtocol :: new ( i_chan, true ) ;
82
- let o_proto = TBinaryOutputProtocol :: new ( o_chan, true ) ;
83
- let client = ThriftHiveMetastoreSyncClient :: new ( i_proto, o_proto) ;
57
+ let address = config
58
+ . address
59
+ . as_str ( )
60
+ . to_socket_addrs ( )
61
+ . map_err ( from_io_error) ?
62
+ . next ( )
63
+ . ok_or_else ( || {
64
+ Error :: new (
65
+ ErrorKind :: Unexpected ,
66
+ format ! ( "invalid address: {}" , config. address) ,
67
+ )
68
+ } ) ?;
69
+
70
+ let client = ThriftHiveMetastoreClientBuilder :: new ( "hms" )
71
+ . address ( address)
72
+ // Framed thrift rpc is not enabled by default in HMS, we use
73
+ // buffered instead.
74
+ . make_codec ( volo_thrift:: codec:: default:: DefaultMakeCodec :: buffered ( ) )
75
+ . build ( ) ;
76
+
84
77
Ok ( Self {
85
78
config,
86
- client : HmsClient ( Arc :: new ( Mutex :: new ( client) ) ) ,
79
+ client : HmsClient ( client) ,
87
80
} )
88
81
}
89
82
}
@@ -103,10 +96,17 @@ impl Catalog for HmsCatalog {
103
96
let dbs = if parent. is_some ( ) {
104
97
return Ok ( vec ! [ ] ) ;
105
98
} else {
106
- self . client . call ( |client| client. get_all_databases ( ) ) ?
99
+ self . client
100
+ . 0
101
+ . get_all_databases ( )
102
+ . await
103
+ . map_err ( from_thrift_error) ?
107
104
} ;
108
105
109
- Ok ( dbs. into_iter ( ) . map ( NamespaceIdent :: new) . collect ( ) )
106
+ Ok ( dbs
107
+ . into_iter ( )
108
+ . map ( |v| NamespaceIdent :: new ( v. into ( ) ) )
109
+ . collect ( ) )
110
110
}
111
111
112
112
async fn create_namespace (
0 commit comments