11// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22
3- use crate :: {
4- pd:: { RetryClient , RetryClientTrait } ,
5- region:: { RegionId , RegionVerId , RegionWithLeader , StoreId } ,
6- Key , Result ,
7- } ;
83use std:: {
94 collections:: { BTreeMap , HashMap , HashSet } ,
105 sync:: Arc ,
116} ;
7+
8+ use tokio:: sync:: { Notify , RwLock } ;
9+
1210use tikv_client_common:: Error ;
1311use tikv_client_pd:: Cluster ;
1412use tikv_client_proto:: metapb:: { self , Store } ;
15- use tokio:: sync:: { Notify , RwLock } ;
13+
14+ use crate :: {
15+ pd:: { RetryClient , RetryClientTrait } ,
16+ region:: { RegionId , RegionVerId , RegionWithLeader , StoreId } ,
17+ request:: request_codec:: RequestCodec ,
18+ Key , Result ,
19+ } ;
1620
1721const MAX_RETRY_WAITING_CONCURRENT_REQUEST : usize = 4 ;
1822
@@ -44,23 +48,25 @@ impl RegionCacheMap {
4448 }
4549}
4650
47- pub struct RegionCache < Client = RetryClient < Cluster > > {
51+ pub struct RegionCache < C , Client = RetryClient < Cluster > > {
4852 region_cache : RwLock < RegionCacheMap > ,
4953 store_cache : RwLock < HashMap < StoreId , Store > > ,
5054 inner_client : Arc < Client > ,
55+ codec : C ,
5156}
5257
53- impl < Client > RegionCache < Client > {
54- pub fn new ( inner_client : Arc < Client > ) -> RegionCache < Client > {
58+ impl < C , Client > RegionCache < C , Client > {
59+ pub fn new ( codec : C , inner_client : Arc < Client > ) -> Self {
5560 RegionCache {
5661 region_cache : RwLock :: new ( RegionCacheMap :: new ( ) ) ,
5762 store_cache : RwLock :: new ( HashMap :: new ( ) ) ,
5863 inner_client,
64+ codec,
5965 }
6066 }
6167}
6268
63- impl < C : RetryClientTrait > RegionCache < C > {
69+ impl < C : RequestCodec , R : RetryClientTrait > RegionCache < C , R > {
6470 // Retrieve cache entry by key. If there's no entry, query PD and update cache.
6571 pub async fn get_region_by_key ( & self , key : & Key ) -> Result < RegionWithLeader > {
6672 let region_cache_guard = self . region_cache . read ( ) . await ;
@@ -126,9 +132,14 @@ impl<C: RetryClientTrait> RegionCache<C> {
126132
127133 /// Force read through (query from PD) and update cache
128134 pub async fn read_through_region_by_key ( & self , key : Key ) -> Result < RegionWithLeader > {
129- let region = self . inner_client . clone ( ) . get_region ( key. into ( ) ) . await ?;
130- self . add_region ( region. clone ( ) ) . await ;
131- Ok ( region)
135+ let mut r = self
136+ . inner_client
137+ . clone ( )
138+ . get_region ( self . codec . encode_pd_query ( key) . into ( ) )
139+ . await ?;
140+ r. region = self . codec . decode_region ( r. region ) ?;
141+ self . add_region ( r. clone ( ) ) . await ;
142+ Ok ( r)
132143 }
133144
134145 /// Force read through (query from PD) and update cache
@@ -140,7 +151,8 @@ impl<C: RetryClientTrait> RegionCache<C> {
140151 region_cache_guard. on_my_way_id . insert ( id, notify. clone ( ) ) ;
141152 }
142153
143- let region = self . inner_client . clone ( ) . get_region_by_id ( id) . await ?;
154+ let mut region = self . inner_client . clone ( ) . get_region_by_id ( id) . await ?;
155+ region. region = self . codec . decode_region ( region. region ) ?;
144156 self . add_region ( region. clone ( ) ) . await ;
145157
146158 // notify others
@@ -226,27 +238,35 @@ impl<C: RetryClientTrait> RegionCache<C> {
226238 cache. key_to_ver_id . remove ( & start_key) ;
227239 }
228240 }
241+
242+ pub fn get_request_codec ( & self ) -> C {
243+ self . codec . clone ( )
244+ }
229245}
230246
231247#[ cfg( test) ]
232248mod test {
233- use super :: RegionCache ;
234- use crate :: {
235- pd:: RetryClientTrait ,
236- region:: { RegionId , RegionWithLeader } ,
237- Key , Result ,
238- } ;
239- use async_trait:: async_trait;
240249 use std:: {
241250 collections:: { BTreeMap , HashMap , HashSet } ,
242251 sync:: {
243252 atomic:: { AtomicU64 , Ordering :: SeqCst } ,
244253 Arc ,
245254 } ,
246255 } ;
256+
257+ use async_trait:: async_trait;
258+ use tokio:: sync:: Mutex ;
259+
247260 use tikv_client_common:: Error ;
248261 use tikv_client_proto:: metapb;
249- use tokio:: sync:: Mutex ;
262+
263+ use crate :: {
264+ pd:: RetryClientTrait ,
265+ region:: { RegionId , RegionWithLeader } ,
266+ Key , Result ,
267+ } ;
268+
269+ use super :: RegionCache ;
250270
251271 #[ derive( Default ) ]
252272 struct MockRetryClient {
0 commit comments