Skip to content

[WIP] Add codec for encoding region for transaction client #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 48 commits into from
Sep 29, 2020

Conversation

georgeteo
Copy link
Contributor

@georgeteo georgeteo commented Sep 13, 2020

In this github issue, it appears that transaction client does not encode/decode keys when querying PD.

This PR:

  • duplicates bytes encoder/decoder from tikv_utils
  • Adds a method to encode Key
  • Adds PdCodecClient that wraps/decorates PdClient encodes key and decodes start_key and end_key in region meta before delegating to underlying PdClient.
  • Swap the underlying PdClient in TransactionClient with the one that encodes/decodes keys

Reference Go client.

Caveat: I'm new to Rust so general Rust tips would be appreciated!

Signed-off-by: George Teo george.c.teo@gmail.com

src/pd/client.rs Outdated
pd: Arc<PdClient>
}

impl<PdC: PdClient + Send + Sync + 'static> PdClient for PdCodecClient<PdC> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: write tests

@ekexium
Copy link
Collaborator

ekexium commented Sep 14, 2020

You can check the commands and outputs of CI. Note you need to use latest nightly channel to build.
Seems to me the only error was an unresolved import and some format issues.

error[E0432]: unresolved import `crate::kv::codec::bytes`

   --> tikv-client-common/src/kv/codec.rs:134:9

    |

134 |     use crate::kv::codec::bytes;

Removing that line and cargo fmt should fix the build.

Also you need to sign-off your commit.

@ekexium
Copy link
Collaborator

ekexium commented Sep 14, 2020

PTAL @nrc @sticnarf

Copy link
Collaborator

@nrc nrc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The rest of it looks great.

src/pd/client.rs Outdated
@@ -266,6 +267,58 @@ impl<KvC: KvConnect + Send + Sync + 'static, Cl> PdRpcClient<KvC, Cl> {
}
}

/// This client wraps a PdClient and encodes/decodes keys and regions according to
/// TiKV transaction codec.
pub struct PdCodecClient<PdClient = PdRpcClient> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a separate client for this, you can just add the encoding to the existing client.

use std::{io::Write, ptr};

const ENC_GROUP_SIZE: usize = 8;
const ENC_MARKER: u8 = b'\xff';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const ENC_MARKER: u8 = b'\xff';
const ENC_MARKER: u8 = 0xff';

@@ -101,6 +102,14 @@ impl Key {
Bound::Excluded(self)
}
}

#[inline]
pub fn as_encoded(&self) -> Key {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use the type system to differentiate between an encoded and unencoded key by adding an EncodedKey type

pub fn as_encoded(&self) -> Key {
let len = codec::max_encoded_bytes_size(self.0.len());
let mut encoded = Vec::with_capacity(len);
encoded.encode_bytes(self.into(), false).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
encoded.encode_bytes(self.into(), false).unwrap();
encoded.encode_bytes(self.0, false).unwrap();

@georgeteo
Copy link
Contributor Author

georgeteo commented Sep 14, 2020

@nrc: thanks for the quick review! Just to clarify the overarching point so I don't go down the wrong rabbit hole:

  • Have a single PdRpcClient
  • Handle RawKey and EncodedKey as two structs that impl a Key trait and wrap that concern in Key.
  • Use EncodedKey in TransactionClient and RawKey in RawClient?

Thoughts on naming EncodedKey as TransactionKey or TransactionalKey for TransactionClient to mirror RawKey <-> RawClient?

@nrc
Copy link
Collaborator

nrc commented Sep 15, 2020

Handle RawKey and EncodedKey as two structs that impl a Key trait and wrap that concern in Key.

I don't think you need a Key trait - the idea is to prevent using an encoded key where a raw key is required and vice versa, a Key trait would circumvent that.

Use EncodedKey in TransactionClient and RawKey in RawClient?

AIUI, encoded keys are only used with PD and we use raw keys with TiKV.

Thoughts on naming EncodedKey as TransactionKey or TransactionalKey for TransactionClient to mirror RawKey <-> RawClient?

I don't think it makes sense, whether a key is encoded is orthogonal to which API it is used with.

@ekexium
Copy link
Collaborator

ekexium commented Sep 27, 2020

I made some changes:

  1. Use a single PD client so that we don't need to write duplicated code. All encoding stuff is hiden in the client. Lower-level PD functions and higher-level clients do not care about encoding.
  2. Fix a few bugs related to encoding
  3. Find and fix a few bugs not related to encoding.
  4. Construct a simple integration test to test with splitted regions.

Copy link
Collaborator

@ekexium ekexium left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None and Some(empty) can both represent upper unbounded. I didn't comprehensively consider this change. Related to #174

@ekexium
Copy link
Collaborator

ekexium commented Sep 27, 2020

@sticnarf PTAL

@ekexium ekexium force-pushed the fix-codec branch 2 times, most recently from 07693f4 to 44c582c Compare September 28, 2020 09:30
Copy link
Collaborator

@sticnarf sticnarf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ekexium
Copy link
Collaborator

ekexium commented Sep 28, 2020

Hi @georgeteo, we need you to sign off your commits to merge the PR. About the DCO check: https://github.com/probot/dco#how-it-works

georgeteo and others added 13 commits September 28, 2020 18:13
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: longfangsong <longfangsong@icloud.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
ekexium and others added 26 commits September 28, 2020 18:15
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
…ehavior with TiKV

Signed-off-by: ekexium <ekexium@gmail.com>
…rors

Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: George Teo <george.c.teo@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
@ekexium ekexium merged commit ad8ef07 into tikv:master Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants