Skip to content

Commit 07693f4

Browse files
committed
add some comments to PdClient to make the encoding part clear
Signed-off-by: ekexium <ekexium@gmail.com>
1 parent 27dcee8 commit 07693f4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/pd/client.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,42 @@ use tikv_client_store::{KvClient, KvConnect, Store, TikvConnect};
2424
const CQ_COUNT: usize = 1;
2525
const CLIENT_PREFIX: &str = "tikv-client";
2626

27+
// The PdClient handles all the encoding stuff.
28+
//
29+
// Raw APIs does not require encoding/decoding at all.
30+
// All keys in all places (client, PD, TiKV) are in the same encoding (here called "raw format").
31+
//
32+
// Transactional APIs are a bit complicated.
33+
// We need encode and decode keys when we communicate with PD, but not with TiKV.
34+
// We encode keys before sending requests to PD, and decode keys in the response from PD.
35+
// That's all we need to do with encoding.
36+
//
37+
// client -encoded-> PD, PD -encoded-> client
38+
// client -raw-> TiKV, TiKV -raw-> client
39+
//
40+
// The reason for the behavior is that in transaction mode, TiKV encode keys for MVCC.
41+
// In raw mode, TiKV doesn't encode them.
42+
// TiKV tells PD using its internal representation, whatever the encoding is.
43+
// So if we use transactional APIs, keys in PD are encoded and PD does not know about the encoding stuff.
44+
//
2745
pub trait PdClient: Send + Sync + 'static {
2846
type KvClient: KvClient + Send + Sync + 'static;
2947

30-
// raw region
48+
// In transactional API, `region` is decoded (keys in raw format).
3149
fn map_region_to_store(
3250
self: Arc<Self>,
3351
region: Region,
3452
) -> BoxFuture<'static, Result<Store<Self::KvClient>>>;
3553

36-
// raw region for raw key
54+
// In transactional API, the key and returned region are both decoded (keys in raw format).
3755
fn region_for_key(&self, key: &Key) -> BoxFuture<'static, Result<Region>>;
3856

39-
// raw region for id
57+
// In transactional API, the returned region is decoded (keys in raw format)
4058
fn region_for_id(&self, id: RegionId) -> BoxFuture<'static, Result<Region>>;
4159

4260
fn get_timestamp(self: Arc<Self>) -> BoxFuture<'static, Result<Timestamp>>;
4361

44-
// store for raw key
62+
// In transactional API, `key` is in raw format
4563
fn store_for_key(
4664
self: Arc<Self>,
4765
key: &Key,

0 commit comments

Comments
 (0)