@@ -17,26 +17,19 @@ pub(crate) mod local;
17
17
use std:: ops:: Deref ;
18
18
use std:: pin:: Pin ;
19
19
use std:: sync:: Arc ;
20
- use std:: task:: Context ;
21
- use std:: task:: Poll ;
22
20
use std:: time:: Duration ;
23
21
24
22
use databend_common_grpc:: RpcClientConf ;
25
23
use databend_common_meta_client:: errors:: CreationError ;
26
24
use databend_common_meta_client:: ClientHandle ;
27
25
use databend_common_meta_client:: MetaGrpcClient ;
28
- use databend_common_meta_kvapi:: kvapi;
29
- use databend_common_meta_kvapi:: kvapi:: KVStream ;
30
- use databend_common_meta_kvapi:: kvapi:: UpsertKVReply ;
31
26
use databend_common_meta_semaphore:: acquirer:: Permit ;
32
27
use databend_common_meta_semaphore:: errors:: AcquireError ;
33
28
use databend_common_meta_semaphore:: Semaphore ;
34
29
use databend_common_meta_types:: protobuf:: WatchRequest ;
35
30
use databend_common_meta_types:: protobuf:: WatchResponse ;
36
31
use databend_common_meta_types:: MetaError ;
37
- use databend_common_meta_types:: TxnReply ;
38
- use databend_common_meta_types:: TxnRequest ;
39
- use databend_common_meta_types:: UpsertKV ;
32
+ use futures:: stream:: TryStreamExt ;
40
33
pub use local:: LocalMetaService ;
41
34
use log:: info;
42
35
use tokio_stream:: Stream ;
@@ -56,6 +49,17 @@ pub enum MetaStore {
56
49
R ( Arc < ClientHandle > ) ,
57
50
}
58
51
52
+ impl Deref for MetaStore {
53
+ type Target = Arc < ClientHandle > ;
54
+
55
+ fn deref ( & self ) -> & Self :: Target {
56
+ match self {
57
+ MetaStore :: L ( l) => l. deref ( ) ,
58
+ MetaStore :: R ( r) => r,
59
+ }
60
+ }
61
+ }
62
+
59
63
impl MetaStore {
60
64
/// Create a local meta service for testing.
61
65
///
@@ -79,24 +83,16 @@ impl MetaStore {
79
83
}
80
84
}
81
85
82
- pub async fn get_local_addr ( & self ) -> std:: result:: Result < Option < String > , MetaError > {
83
- let client = match self {
84
- MetaStore :: L ( l) => l. deref ( ) . deref ( ) ,
85
- MetaStore :: R ( grpc_client) => grpc_client,
86
- } ;
87
-
88
- let client_info = client. get_client_info ( ) . await ?;
89
- Ok ( Some ( client_info. client_addr ) )
86
+ pub async fn get_local_addr ( & self ) -> Result < String , MetaError > {
87
+ let client_info = self . get_client_info ( ) . await ?;
88
+ Ok ( client_info. client_addr )
90
89
}
91
90
92
91
pub async fn watch ( & self , request : WatchRequest ) -> Result < WatchStream , MetaError > {
93
- let client = match self {
94
- MetaStore :: L ( l) => l. deref ( ) ,
95
- MetaStore :: R ( grpc_client) => grpc_client,
96
- } ;
92
+ let client = self . deref ( ) ;
97
93
98
94
let streaming = client. request ( request) . await ?;
99
- Ok ( Box :: pin ( WatchResponseStream :: create ( streaming ) ) )
95
+ Ok ( Box :: pin ( streaming . map_err ( MetaError :: from ) ) )
100
96
}
101
97
102
98
pub async fn new_acquired (
@@ -106,11 +102,7 @@ impl MetaStore {
106
102
id : impl ToString ,
107
103
lease : Duration ,
108
104
) -> Result < Permit , AcquireError > {
109
- let client = match self {
110
- MetaStore :: L ( l) => l. deref ( ) ,
111
- MetaStore :: R ( grpc_client) => grpc_client,
112
- } ;
113
-
105
+ let client = self . deref ( ) ;
114
106
Semaphore :: new_acquired ( client. clone ( ) , prefix, capacity, id, lease) . await
115
107
}
116
108
@@ -121,48 +113,11 @@ impl MetaStore {
121
113
id : impl ToString ,
122
114
lease : Duration ,
123
115
) -> Result < Permit , AcquireError > {
124
- let client = match self {
125
- MetaStore :: L ( l) => l. deref ( ) ,
126
- MetaStore :: R ( grpc_client) => grpc_client,
127
- } ;
128
-
116
+ let client = self . deref ( ) ;
129
117
Semaphore :: new_acquired_by_time ( client. clone ( ) , prefix, capacity, id, lease) . await
130
118
}
131
119
}
132
120
133
- #[ async_trait:: async_trait]
134
- impl kvapi:: KVApi for MetaStore {
135
- type Error = MetaError ;
136
-
137
- async fn upsert_kv ( & self , act : UpsertKV ) -> Result < UpsertKVReply , Self :: Error > {
138
- match self {
139
- MetaStore :: L ( x) => x. upsert_kv ( act) . await ,
140
- MetaStore :: R ( x) => x. upsert_kv ( act) . await ,
141
- }
142
- }
143
-
144
- async fn get_kv_stream ( & self , keys : & [ String ] ) -> Result < KVStream < Self :: Error > , Self :: Error > {
145
- match self {
146
- MetaStore :: L ( x) => x. get_kv_stream ( keys) . await ,
147
- MetaStore :: R ( x) => x. get_kv_stream ( keys) . await ,
148
- }
149
- }
150
-
151
- async fn list_kv ( & self , prefix : & str ) -> Result < KVStream < Self :: Error > , Self :: Error > {
152
- match self {
153
- MetaStore :: L ( x) => x. list_kv ( prefix) . await ,
154
- MetaStore :: R ( x) => x. list_kv ( prefix) . await ,
155
- }
156
- }
157
-
158
- async fn transaction ( & self , txn : TxnRequest ) -> Result < TxnReply , Self :: Error > {
159
- match self {
160
- MetaStore :: L ( x) => x. transaction ( txn) . await ,
161
- MetaStore :: R ( x) => x. transaction ( txn) . await ,
162
- }
163
- }
164
- }
165
-
166
121
impl MetaStoreProvider {
167
122
pub fn new ( rpc_conf : RpcClientConf ) -> Self {
168
123
MetaStoreProvider { rpc_conf }
@@ -188,37 +143,3 @@ impl MetaStoreProvider {
188
143
}
189
144
}
190
145
}
191
-
192
- pub struct WatchResponseStream < E , S >
193
- where
194
- E : Into < MetaError > + Send + ' static ,
195
- S : Stream < Item = Result < WatchResponse , E > > + Send + Unpin + ' static ,
196
- {
197
- inner : S ,
198
- }
199
-
200
- impl < E , S > WatchResponseStream < E , S >
201
- where
202
- E : Into < MetaError > + Send + ' static ,
203
- S : Stream < Item = Result < WatchResponse , E > > + Send + Unpin + ' static ,
204
- {
205
- pub fn create ( inner : S ) -> WatchResponseStream < E , S > {
206
- WatchResponseStream { inner }
207
- }
208
- }
209
-
210
- impl < E , S > Stream for WatchResponseStream < E , S >
211
- where
212
- E : Into < MetaError > + Send + ' static ,
213
- S : Stream < Item = Result < WatchResponse , E > > + Send + Unpin + ' static ,
214
- {
215
- type Item = Result < WatchResponse , MetaError > ;
216
-
217
- fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
218
- Pin :: new ( & mut self . inner ) . poll_next ( cx) . map ( |x| match x {
219
- None => None ,
220
- Some ( Ok ( resp) ) => Some ( Ok ( resp) ) ,
221
- Some ( Err ( e) ) => Some ( Err ( e. into ( ) ) ) ,
222
- } )
223
- }
224
- }
0 commit comments