@@ -15,6 +15,7 @@ use tonic::Request;
15
15
16
16
use super :: timestamp:: TimestampOracle ;
17
17
use crate :: internal_err;
18
+ use crate :: proto:: keyspacepb;
18
19
use crate :: proto:: pdpb;
19
20
use crate :: Error ;
20
21
use crate :: Result ;
@@ -25,7 +26,7 @@ use crate::Timestamp;
25
26
pub struct Cluster {
26
27
id : u64 ,
27
28
client : pdpb:: pd_client:: PdClient < Channel > ,
28
- endpoint : String ,
29
+ keyspace_client : keyspacepb :: keyspace_client :: KeyspaceClient < Channel > ,
29
30
members : pdpb:: GetMembersResponse ,
30
31
tso : TimestampOracle ,
31
32
}
@@ -94,16 +95,18 @@ impl Cluster {
94
95
req. send ( & mut self . client , timeout) . await
95
96
}
96
97
97
- pub async fn get_keyspace_id ( & self , keyspace : & str ) -> Result < u32 > {
98
- let resp =
99
- reqwest:: get ( format ! ( "{}/pd/api/v2/keyspaces/{keyspace}" , self . endpoint) ) . await ?;
100
- let body = resp. json :: < serde_json:: Value > ( ) . await ?;
101
- let keyspace_id = body
102
- . get ( "id" )
103
- . ok_or_else ( || Error :: UnknownHttpRespond ( body. to_string ( ) ) ) ?
104
- . as_u64 ( )
105
- . ok_or_else ( || Error :: UnknownHttpRespond ( body. to_string ( ) ) ) ?;
106
- Ok ( keyspace_id as u32 )
98
+ pub async fn load_keyspace (
99
+ & mut self ,
100
+ keyspace : & str ,
101
+ timeout : Duration ,
102
+ ) -> Result < keyspacepb:: KeyspaceMeta > {
103
+ let mut req = pd_request ! ( self . id, keyspacepb:: LoadKeyspaceRequest ) ;
104
+ req. name = keyspace. to_owned ( ) ;
105
+ let resp = req. send ( & mut self . keyspace_client , timeout) . await ?;
106
+ let keyspace = resp
107
+ . keyspace
108
+ . ok_or_else ( || Error :: KeyspaceNotFound ( keyspace. to_owned ( ) ) ) ?;
109
+ Ok ( keyspace)
107
110
}
108
111
}
109
112
@@ -123,13 +126,13 @@ impl Connection {
123
126
timeout : Duration ,
124
127
) -> Result < Cluster > {
125
128
let members = self . validate_endpoints ( endpoints, timeout) . await ?;
126
- let ( client, endpoint , members) = self . try_connect_leader ( & members, timeout) . await ?;
129
+ let ( client, keyspace_client , members) = self . try_connect_leader ( & members, timeout) . await ?;
127
130
let id = members. header . as_ref ( ) . unwrap ( ) . cluster_id ;
128
131
let tso = TimestampOracle :: new ( id, & client) ?;
129
132
let cluster = Cluster {
130
133
id,
131
134
client,
132
- endpoint ,
135
+ keyspace_client ,
133
136
members,
134
137
tso,
135
138
} ;
@@ -140,13 +143,13 @@ impl Connection {
140
143
pub async fn reconnect ( & self , cluster : & mut Cluster , timeout : Duration ) -> Result < ( ) > {
141
144
warn ! ( "updating pd client" ) ;
142
145
let start = Instant :: now ( ) ;
143
- let ( client, endpoint , members) =
146
+ let ( client, keyspace_client , members) =
144
147
self . try_connect_leader ( & cluster. members , timeout) . await ?;
145
148
let tso = TimestampOracle :: new ( cluster. id , & client) ?;
146
149
* cluster = Cluster {
147
150
id : cluster. id ,
148
151
client,
149
- endpoint ,
152
+ keyspace_client ,
150
153
members,
151
154
tso,
152
155
} ;
@@ -169,7 +172,7 @@ impl Connection {
169
172
return Err ( internal_err ! ( "duplicated PD endpoint {}" , ep) ) ;
170
173
}
171
174
172
- let ( _, resp) = match self . connect ( ep, timeout) . await {
175
+ let ( _, _ , resp) = match self . connect ( ep, timeout) . await {
173
176
Ok ( resp) => resp,
174
177
// Ignore failed PD node.
175
178
Err ( e) => {
@@ -211,27 +214,42 @@ impl Connection {
211
214
& self ,
212
215
addr : & str ,
213
216
_timeout : Duration ,
214
- ) -> Result < ( pdpb:: pd_client:: PdClient < Channel > , pdpb:: GetMembersResponse ) > {
217
+ ) -> Result < (
218
+ pdpb:: pd_client:: PdClient < Channel > ,
219
+ keyspacepb:: keyspace_client:: KeyspaceClient < Channel > ,
220
+ pdpb:: GetMembersResponse ,
221
+ ) > {
215
222
let mut client = self
216
223
. security_mgr
217
224
. connect ( addr, pdpb:: pd_client:: PdClient :: < Channel > :: new)
218
225
. await ?;
226
+ let keyspace_client = self
227
+ . security_mgr
228
+ . connect (
229
+ addr,
230
+ keyspacepb:: keyspace_client:: KeyspaceClient :: < Channel > :: new,
231
+ )
232
+ . await ?;
219
233
let resp: pdpb:: GetMembersResponse = client
220
234
. get_members ( pdpb:: GetMembersRequest :: default ( ) )
221
235
. await ?
222
236
. into_inner ( ) ;
223
- Ok ( ( client, resp) )
237
+ Ok ( ( client, keyspace_client , resp) )
224
238
}
225
239
226
240
async fn try_connect (
227
241
& self ,
228
242
addr : & str ,
229
243
cluster_id : u64 ,
230
244
timeout : Duration ,
231
- ) -> Result < ( pdpb:: pd_client:: PdClient < Channel > , pdpb:: GetMembersResponse ) > {
232
- let ( client, r) = self . connect ( addr, timeout) . await ?;
245
+ ) -> Result < (
246
+ pdpb:: pd_client:: PdClient < Channel > ,
247
+ keyspacepb:: keyspace_client:: KeyspaceClient < Channel > ,
248
+ pdpb:: GetMembersResponse ,
249
+ ) > {
250
+ let ( client, keyspace_client, r) = self . connect ( addr, timeout) . await ?;
233
251
Connection :: validate_cluster_id ( addr, & r, cluster_id) ?;
234
- Ok ( ( client, r) )
252
+ Ok ( ( client, keyspace_client , r) )
235
253
}
236
254
237
255
fn validate_cluster_id (
@@ -258,7 +276,7 @@ impl Connection {
258
276
timeout : Duration ,
259
277
) -> Result < (
260
278
pdpb:: pd_client:: PdClient < Channel > ,
261
- String ,
279
+ keyspacepb :: keyspace_client :: KeyspaceClient < Channel > ,
262
280
pdpb:: GetMembersResponse ,
263
281
) > {
264
282
let previous_leader = previous. leader . as_ref ( ) . unwrap ( ) ;
@@ -274,7 +292,7 @@ impl Connection {
274
292
{
275
293
for ep in & m. client_urls {
276
294
match self . try_connect ( ep. as_str ( ) , cluster_id, timeout) . await {
277
- Ok ( ( _, r) ) => {
295
+ Ok ( ( _, _ , r) ) => {
278
296
resp = Some ( r) ;
279
297
break ' outer;
280
298
}
@@ -290,10 +308,10 @@ impl Connection {
290
308
if let Some ( resp) = resp {
291
309
let leader = resp. leader . as_ref ( ) . unwrap ( ) ;
292
310
for ep in & leader. client_urls {
293
- if let Ok ( ( client, members) ) =
311
+ if let Ok ( ( client, keyspace_client , members) ) =
294
312
self . try_connect ( ep. as_str ( ) , cluster_id, timeout) . await
295
313
{
296
- return Ok ( ( client, ep . to_string ( ) , members) ) ;
314
+ return Ok ( ( client, keyspace_client , members) ) ;
297
315
}
298
316
}
299
317
}
@@ -306,18 +324,12 @@ type GrpcResult<T> = std::result::Result<T, tonic::Status>;
306
324
307
325
#[ async_trait]
308
326
trait PdMessage : Sized {
327
+ type Client : Send ;
309
328
type Response : PdResponse ;
310
329
311
- async fn rpc (
312
- req : Request < Self > ,
313
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
314
- ) -> GrpcResult < Self :: Response > ;
330
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > ;
315
331
316
- async fn send (
317
- self ,
318
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
319
- timeout : Duration ,
320
- ) -> Result < Self :: Response > {
332
+ async fn send ( self , client : & mut Self :: Client , timeout : Duration ) -> Result < Self :: Response > {
321
333
let mut req = self . into_request ( ) ;
322
334
req. set_timeout ( timeout) ;
323
335
let response = Self :: rpc ( req, client) . await ?;
@@ -332,64 +344,64 @@ trait PdMessage: Sized {
332
344
333
345
#[ async_trait]
334
346
impl PdMessage for pdpb:: GetRegionRequest {
347
+ type Client = pdpb:: pd_client:: PdClient < Channel > ;
335
348
type Response = pdpb:: GetRegionResponse ;
336
349
337
- async fn rpc (
338
- req : Request < Self > ,
339
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
340
- ) -> GrpcResult < Self :: Response > {
350
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
341
351
Ok ( client. get_region ( req) . await ?. into_inner ( ) )
342
352
}
343
353
}
344
354
345
355
#[ async_trait]
346
356
impl PdMessage for pdpb:: GetRegionByIdRequest {
357
+ type Client = pdpb:: pd_client:: PdClient < Channel > ;
347
358
type Response = pdpb:: GetRegionResponse ;
348
359
349
- async fn rpc (
350
- req : Request < Self > ,
351
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
352
- ) -> GrpcResult < Self :: Response > {
360
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
353
361
Ok ( client. get_region_by_id ( req) . await ?. into_inner ( ) )
354
362
}
355
363
}
356
364
357
365
#[ async_trait]
358
366
impl PdMessage for pdpb:: GetStoreRequest {
367
+ type Client = pdpb:: pd_client:: PdClient < Channel > ;
359
368
type Response = pdpb:: GetStoreResponse ;
360
369
361
- async fn rpc (
362
- req : Request < Self > ,
363
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
364
- ) -> GrpcResult < Self :: Response > {
370
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
365
371
Ok ( client. get_store ( req) . await ?. into_inner ( ) )
366
372
}
367
373
}
368
374
369
375
#[ async_trait]
370
376
impl PdMessage for pdpb:: GetAllStoresRequest {
377
+ type Client = pdpb:: pd_client:: PdClient < Channel > ;
371
378
type Response = pdpb:: GetAllStoresResponse ;
372
379
373
- async fn rpc (
374
- req : Request < Self > ,
375
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
376
- ) -> GrpcResult < Self :: Response > {
380
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
377
381
Ok ( client. get_all_stores ( req) . await ?. into_inner ( ) )
378
382
}
379
383
}
380
384
381
385
#[ async_trait]
382
386
impl PdMessage for pdpb:: UpdateGcSafePointRequest {
387
+ type Client = pdpb:: pd_client:: PdClient < Channel > ;
383
388
type Response = pdpb:: UpdateGcSafePointResponse ;
384
389
385
- async fn rpc (
386
- req : Request < Self > ,
387
- client : & mut pdpb:: pd_client:: PdClient < Channel > ,
388
- ) -> GrpcResult < Self :: Response > {
390
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
389
391
Ok ( client. update_gc_safe_point ( req) . await ?. into_inner ( ) )
390
392
}
391
393
}
392
394
395
+ #[ async_trait]
396
+ impl PdMessage for keyspacepb:: LoadKeyspaceRequest {
397
+ type Client = keyspacepb:: keyspace_client:: KeyspaceClient < Channel > ;
398
+ type Response = keyspacepb:: LoadKeyspaceResponse ;
399
+
400
+ async fn rpc ( req : Request < Self > , client : & mut Self :: Client ) -> GrpcResult < Self :: Response > {
401
+ Ok ( client. load_keyspace ( req) . await ?. into_inner ( ) )
402
+ }
403
+ }
404
+
393
405
trait PdResponse {
394
406
fn header ( & self ) -> & pdpb:: ResponseHeader ;
395
407
}
@@ -417,3 +429,9 @@ impl PdResponse for pdpb::UpdateGcSafePointResponse {
417
429
self . header . as_ref ( ) . unwrap ( )
418
430
}
419
431
}
432
+
433
+ impl PdResponse for keyspacepb:: LoadKeyspaceResponse {
434
+ fn header ( & self ) -> & pdpb:: ResponseHeader {
435
+ self . header . as_ref ( ) . unwrap ( )
436
+ }
437
+ }
0 commit comments