1
1
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.
2
2
3
- use crate :: {
4
- compat:: stream_fn,
5
- kv:: codec,
6
- pd:: { retry:: RetryClientTrait , RetryClient } ,
7
- region:: { RegionId , RegionVerId , RegionWithLeader } ,
8
- region_cache:: RegionCache ,
9
- store:: RegionStore ,
10
- BoundRange , Config , Key , Result , SecurityManager , Timestamp ,
11
- } ;
3
+ use std:: { collections:: HashMap , sync:: Arc , thread} ;
4
+ use std:: marker:: PhantomData ;
5
+
12
6
use async_trait:: async_trait;
13
7
use futures:: { prelude:: * , stream:: BoxStream } ;
14
8
use grpcio:: { EnvBuilder , Environment } ;
15
9
use slog:: Logger ;
16
- use std:: { collections:: HashMap , sync:: Arc , thread} ;
10
+ use tokio:: sync:: RwLock ;
11
+
17
12
use tikv_client_pd:: Cluster ;
18
13
use tikv_client_proto:: { kvrpcpb, metapb} ;
19
14
use tikv_client_store:: { KvClient , KvConnect , TikvConnect } ;
20
- use tokio:: sync:: RwLock ;
15
+
16
+ use crate :: {
17
+ BoundRange ,
18
+ compat:: stream_fn,
19
+ Config ,
20
+ Key ,
21
+ kv:: codec,
22
+ pd:: { retry:: RetryClientTrait , RetryClient } ,
23
+ region:: { RegionId , RegionVerId , RegionWithLeader } , region_cache:: RegionCache , Result , SecurityManager , store:: RegionStore , Timestamp ,
24
+ } ;
25
+ use crate :: request:: request_codec:: RequestCodec ;
21
26
22
27
const CQ_COUNT : usize = 1 ;
23
28
const CLIENT_PREFIX : & str = "tikv-client" ;
@@ -42,6 +47,7 @@ const CLIENT_PREFIX: &str = "tikv-client";
42
47
#[ async_trait]
43
48
pub trait PdClient : Send + Sync + ' static {
44
49
type KvClient : KvClient + Send + Sync + ' static ;
50
+ type RequestCodec : RequestCodec ;
45
51
46
52
/// In transactional API, `region` is decoded (keys in raw format).
47
53
async fn map_region_to_store ( self : Arc < Self > , region : RegionWithLeader ) -> Result < RegionStore > ;
@@ -69,11 +75,11 @@ pub trait PdClient: Send + Sync + 'static {
69
75
70
76
fn group_keys_by_region < K , K2 > (
71
77
self : Arc < Self > ,
72
- keys : impl Iterator < Item = K > + Send + Sync + ' static ,
78
+ keys : impl Iterator < Item = K > + Send + Sync + ' static ,
73
79
) -> BoxStream < ' static , Result < ( RegionId , Vec < K2 > ) > >
74
- where
75
- K : AsRef < Key > + Into < K2 > + Send + Sync + ' static ,
76
- K2 : Send + Sync + ' static ,
80
+ where
81
+ K : AsRef < Key > + Into < K2 > + Send + Sync + ' static ,
82
+ K2 : Send + Sync + ' static ,
77
83
{
78
84
let keys = keys. peekable ( ) ;
79
85
stream_fn ( keys, move |mut keys| {
@@ -95,7 +101,7 @@ pub trait PdClient: Send + Sync + 'static {
95
101
}
96
102
}
97
103
} )
98
- . boxed ( )
104
+ . boxed ( )
99
105
}
100
106
101
107
/// Returns a Stream which iterates over the contexts for each region covered by range.
@@ -126,7 +132,7 @@ pub trait PdClient: Send + Sync + 'static {
126
132
Ok ( Some ( ( Some ( region_end) , store) ) )
127
133
}
128
134
} )
129
- . boxed ( )
135
+ . boxed ( )
130
136
}
131
137
132
138
/// Returns a Stream which iterates over the contexts for ranges in the same region.
@@ -190,7 +196,7 @@ pub trait PdClient: Send + Sync + 'static {
190
196
}
191
197
}
192
198
} )
193
- . boxed ( )
199
+ . boxed ( )
194
200
}
195
201
196
202
fn decode_region ( mut region : RegionWithLeader , enable_codec : bool ) -> Result < RegionWithLeader > {
@@ -204,22 +210,27 @@ pub trait PdClient: Send + Sync + 'static {
204
210
async fn update_leader ( & self , ver_id : RegionVerId , leader : metapb:: Peer ) -> Result < ( ) > ;
205
211
206
212
async fn invalidate_region_cache ( & self , ver_id : RegionVerId ) ;
213
+
214
+ fn get_request_codec ( & self ) -> Self :: RequestCodec ;
207
215
}
208
216
209
217
/// This client converts requests for the logical TiKV cluster into requests
210
218
/// for a single TiKV store using PD and internal logic.
211
- pub struct PdRpcClient < KvC : KvConnect + Send + Sync + ' static = TikvConnect , Cl = Cluster > {
219
+ pub struct PdRpcClient < C , KvC : KvConnect + Send + Sync + ' static = TikvConnect , Cl = Cluster > {
212
220
pd : Arc < RetryClient < Cl > > ,
213
221
kv_connect : KvC ,
214
222
kv_client_cache : Arc < RwLock < HashMap < String , KvC :: KvClient > > > ,
215
223
enable_codec : bool ,
216
224
region_cache : RegionCache < RetryClient < Cl > > ,
217
225
logger : Logger ,
226
+ // TODO: change to a real codec.
227
+ _phantom : PhantomData < C > ,
218
228
}
219
229
220
230
#[ async_trait]
221
- impl < KvC : KvConnect + Send + Sync + ' static > PdClient for PdRpcClient < KvC > {
231
+ impl < C : RequestCodec , KvC : KvConnect + Send + Sync + ' static > PdClient for PdRpcClient < C , KvC > {
222
232
type KvClient = KvC :: KvClient ;
233
+ type RequestCodec = C ;
223
234
224
235
async fn map_region_to_store ( self : Arc < Self > , region : RegionWithLeader ) -> Result < RegionStore > {
225
236
let store_id = region. get_store_id ( ) ?;
@@ -260,15 +271,19 @@ impl<KvC: KvConnect + Send + Sync + 'static> PdClient for PdRpcClient<KvC> {
260
271
async fn invalidate_region_cache ( & self , ver_id : RegionVerId ) {
261
272
self . region_cache . invalidate_region_cache ( ver_id) . await
262
273
}
274
+
275
+ fn get_request_codec ( & self ) -> Self :: RequestCodec {
276
+ todo ! ( )
277
+ }
263
278
}
264
279
265
- impl PdRpcClient < TikvConnect , Cluster > {
280
+ impl < C > PdRpcClient < C , TikvConnect , Cluster > {
266
281
pub async fn connect (
267
282
pd_endpoints : & [ String ] ,
268
283
config : Config ,
269
284
enable_codec : bool ,
270
285
logger : Logger ,
271
- ) -> Result < PdRpcClient > {
286
+ ) -> Result < PdRpcClient < C , TikvConnect , Cluster > > {
272
287
PdRpcClient :: new (
273
288
config. clone ( ) ,
274
289
|env, security_mgr| TikvConnect :: new ( env, security_mgr, config. timeout ) ,
@@ -278,7 +293,7 @@ impl PdRpcClient<TikvConnect, Cluster> {
278
293
enable_codec,
279
294
logger,
280
295
)
281
- . await
296
+ . await
282
297
}
283
298
}
284
299
@@ -291,18 +306,18 @@ fn thread_name(prefix: &str) -> String {
291
306
. unwrap_or_else ( || prefix. to_owned ( ) )
292
307
}
293
308
294
- impl < KvC : KvConnect + Send + Sync + ' static , Cl > PdRpcClient < KvC , Cl > {
309
+ impl < C , KvC : KvConnect + Send + Sync + ' static , Cl > PdRpcClient < C , KvC , Cl > {
295
310
pub async fn new < PdFut , MakeKvC , MakePd > (
296
311
config : Config ,
297
312
kv_connect : MakeKvC ,
298
313
pd : MakePd ,
299
314
enable_codec : bool ,
300
315
logger : Logger ,
301
- ) -> Result < PdRpcClient < KvC , Cl > >
302
- where
303
- PdFut : Future < Output = Result < RetryClient < Cl > > > ,
304
- MakeKvC : FnOnce ( Arc < Environment > , Arc < SecurityManager > ) -> KvC ,
305
- MakePd : FnOnce ( Arc < Environment > , Arc < SecurityManager > ) -> PdFut ,
316
+ ) -> Result < PdRpcClient < C , KvC , Cl > >
317
+ where
318
+ PdFut : Future < Output = Result < RetryClient < Cl > > > ,
319
+ MakeKvC : FnOnce ( Arc < Environment > , Arc < SecurityManager > ) -> KvC ,
320
+ MakePd : FnOnce ( Arc < Environment > , Arc < SecurityManager > ) -> PdFut ,
306
321
{
307
322
let env = Arc :: new (
308
323
EnvBuilder :: new ( )
@@ -312,7 +327,7 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
312
327
) ;
313
328
let security_mgr = Arc :: new (
314
329
if let ( Some ( ca_path) , Some ( cert_path) , Some ( key_path) ) =
315
- ( & config. ca_path , & config. cert_path , & config. key_path )
330
+ ( & config. ca_path , & config. cert_path , & config. key_path )
316
331
{
317
332
SecurityManager :: load ( ca_path, cert_path, key_path) ?
318
333
} else {
@@ -329,6 +344,8 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
329
344
enable_codec,
330
345
region_cache : RegionCache :: new ( pd) ,
331
346
logger,
347
+ // TODO
348
+ _phantom : PhantomData ,
332
349
} )
333
350
}
334
351
@@ -352,10 +369,11 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
352
369
353
370
#[ cfg( test) ]
354
371
pub mod test {
355
- use super :: * ;
372
+ use futures:: { executor, executor:: block_on} ;
373
+
356
374
use crate :: mock:: * ;
357
375
358
- use futures :: { executor , executor :: block_on } ;
376
+ use super :: * ;
359
377
360
378
#[ tokio:: test]
361
379
async fn test_kv_client_caching ( ) {
@@ -396,7 +414,7 @@ pub mod test {
396
414
vec![ 1 ] . into( ) ,
397
415
vec![ 2 ] . into( ) ,
398
416
vec![ 3 ] . into( ) ,
399
- vec![ 5 , 2 ] . into( )
417
+ vec![ 5 , 2 ] . into( ) ,
400
418
]
401
419
) ;
402
420
assert_eq ! (
@@ -458,36 +476,36 @@ pub mod test {
458
476
vec![
459
477
kvrpcpb:: KeyRange {
460
478
start_key: k1. clone( ) ,
461
- end_key: k2. clone( )
479
+ end_key: k2. clone( ) ,
462
480
} ,
463
481
kvrpcpb:: KeyRange {
464
482
start_key: k1,
465
- end_key: k_split. clone( )
466
- }
483
+ end_key: k_split. clone( ) ,
484
+ } ,
467
485
]
468
486
) ;
469
487
assert_eq ! ( ranges2. 0 , 2 ) ;
470
488
assert_eq ! (
471
489
ranges2. 1 ,
472
490
vec![ kvrpcpb:: KeyRange {
473
491
start_key: k_split. clone( ) ,
474
- end_key: k3
492
+ end_key: k3,
475
493
} ]
476
494
) ;
477
495
assert_eq ! ( ranges3. 0 , 1 ) ;
478
496
assert_eq ! (
479
497
ranges3. 1 ,
480
498
vec![ kvrpcpb:: KeyRange {
481
499
start_key: k2,
482
- end_key: k_split. clone( )
500
+ end_key: k_split. clone( ) ,
483
501
} ]
484
502
) ;
485
503
assert_eq ! ( ranges4. 0 , 2 ) ;
486
504
assert_eq ! (
487
505
ranges4. 1 ,
488
506
vec![ kvrpcpb:: KeyRange {
489
507
start_key: k_split,
490
- end_key: k4
508
+ end_key: k4,
491
509
} ]
492
510
) ;
493
511
assert ! ( stream. next( ) . is_none( ) ) ;
0 commit comments