Skip to content

Reserve key space id encoding in backup convert#12759

Merged
ti-chi-bot merged 14 commits intotikv:masterfrom
haojinming:br_convert_new
Jun 7, 2022
Merged

Reserve key space id encoding in backup convert#12759
ti-chi-bot merged 14 commits intotikv:masterfrom
haojinming:br_convert_new

Conversation

@haojinming
Copy link
Contributor

@haojinming haojinming commented Jun 6, 2022

Signed-off-by: haojinming jinming.hao@pingcap.com

What is changed and how it works?

Issue Number: Close #12758

What's Changed:

Add encoding of key space id in apiv2 key when backup convert.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test

Side effects

Release note


Add key space id encoding in apiv2 key when backup convert.

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Jun 6, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • pingyu
  • sunxiaoguang

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Details

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/needs-triage-completed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 6, 2022
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
let (mut user_key, _) = ApiV2::decode_raw_key(&Key::from_encoded_slice(key), true)?;
user_key.remove(0); // remove first byte `RAW_KEY_PREFIX`
Ok(Self::encode_raw_key_owned(user_key, None))
Err(EngineError::Other(box_err!("unsupported conversion")))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Err(EngineError::Other(box_err!("unsupported conversion")))
Err(box_err!("unsupported conversion"))

The same to other three similar.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe just panic ? The conversion from V2 to V1/V1TTL should be rejected by TiKV-BR, and when receive backup request. Other than the moment to convert key version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, mark apiv2->apiv1 as unreachable

let mut apiv2_key = Vec::with_capacity(ApiV2::get_encode_len(key.len() + 2));
apiv2_key.push(RAW_KEY_PREFIX);
let mut tmp_buf = [0; MAX_VARINT64_LENGTH];
let written = NumberCodec::encode_var_u64(&mut tmp_buf, DEFAULT_KEY_SPACE_ID);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggest to try making written be constant. Or just write a 0 and verify it's always equal to var_u64 in unit test.

Copy link
Member

Choose a reason for hiding this comment

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

Let's make key space fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

pub fn add_prefix(key: &[u8]) -> Vec<u8> {
let mut apiv2_key = Vec::with_capacity(ApiV2::get_encode_len(key.len() + 2));
Copy link
Contributor

Choose a reason for hiding this comment

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

Reserve capacity after encoded keyspace id is got and avoid hard coding the length.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

// the file range need be encoded as v2 format.
// And range in response keep in v1 format.
let (start, end) = codec.convert_key_range_to_dst_version(
let ret = codec.convert_key_range_to_dst_version(
Copy link
Contributor

Choose a reason for hiding this comment

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

Reject V2 -> V1/V1TTL earlier, e.g. KeyValueCodec::check_backup_api_version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Signed-off-by: haojinming <jinming.hao@pingcap.com>
@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed do-not-merge/needs-triage-completed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 6, 2022
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
@haojinming haojinming changed the title Add key space id encoding in backup convert Reserve key space id encoding in backup convert Jun 6, 2022
Signed-off-by: haojinming <jinming.hao@pingcap.com>
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jun 6, 2022
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 6, 2022
@haojinming
Copy link
Contributor Author

/run-build

@haojinming
Copy link
Contributor Author

/run-test

user_key.remove(0); // remove first byte `RAW_KEY_PREFIX`
Ok(Self::encode_raw_key_owned(user_key, None))
}
ApiVersion::V2 => unreachable!(), // reject apiv2 -> apiv1 conversion
Copy link
Member

Choose a reason for hiding this comment

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

It will panic TiKV if the arm matches. What about returning an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

backup process do check the apiversion in

pub fn check_backup_api_version(&self, start_key: &[u8], end_key: &[u8]) -> bool {
if self.is_raw_kv
&& self.cur_api_ver != self.dst_api_ver
&& self.dst_api_ver != ApiVersion::V2
{
return false;
}
to ensure that this branch is unreachable. Handle error will make backup process a little complicated here
let (start, end) = codec.convert_key_range_to_dst_version(
msg.start_key.clone(),
msg.end_key.clone(),
);
. And we want take this pr in v6.1.0 release, so we need make min changes.

Copy link
Member

Choose a reason for hiding this comment

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

Agree should return error instead of just panicking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@overvenus @BusyJay error handling is added, PTAL~ thanks.

start_key.insert(0, RAW_KEY_PREFIX);
start_key = ApiV2::add_prefix(&start_key);
if end_key.is_empty() {
end_key.insert(0, RAW_KEY_PREFIX_END);
Copy link
Member

@iosmanthus iosmanthus Jun 6, 2022

Choose a reason for hiding this comment

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

The EMPTY end key should be mapped to r001?.

Copy link
Member

Choose a reason for hiding this comment

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

Another question, when do we need this function while executing backup and restore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a conversion scenario, no key space exists in apiv1, so I think we need set 'r' + 1 as the whole end. Anyway, 'r' + 1 and r001 both works here. @pingyu How do you think?

Copy link
Contributor Author

@haojinming haojinming Jun 6, 2022

Choose a reason for hiding this comment

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

This function is needed when return backup response, range in BackupResponse.File need to be the apiv2 format if convert from apiv1/v1ttl to apiv2.

Copy link
Contributor

Choose a reason for hiding this comment

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

En... I suggest r001 . It is a reasonble range start & end for default key space. Although both are ok here.

Copy link
Contributor Author

@haojinming haojinming Jun 6, 2022

Choose a reason for hiding this comment

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

After consideration, i think use r001 as the end is better. r001 as end can avoid some useless download and ingest during restore if target tikv cluster is not empty. I have changed the end as r001. PTAL~ @iosmanthus @pingyu

Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Copy link
Contributor

@pingyu pingyu left a comment

Choose a reason for hiding this comment

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

LGTM~

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 6, 2022
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: haojinming <jinming.hao@pingcap.com>
pub const TXN_KEY_PREFIX: u8 = b'x';
pub const TIDB_META_KEY_PREFIX: u8 = b'm';
pub const TIDB_TABLE_KEY_PREFIX: u8 = b't';
pub const DEFAULT_KEY_SPACE_ID: [u8; 3] = [0, 0, 0]; // reserve 3 bytes for key space id.
Copy link
Member

Choose a reason for hiding this comment

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

Why? I thought it should be just one byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We plan to use 3 bytes fixed length to encode key space id, support more than 16 million key space. And we does not use var int encoding incase there are some issues we may miss.

@pingyu pingyu added the needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. label Jun 7, 2022
Copy link
Member

@Connor1996 Connor1996 left a comment

Choose a reason for hiding this comment

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

LGTM

@iosmanthus
Copy link
Member

/merge

@ti-chi-bot
Copy link
Member

@iosmanthus: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests

You only need to trigger /merge once, and if the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

If you have any questions about the PR merge process, please refer to pr process.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

DetailsCommit hash: 16796b8

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 7, 2022
@ti-chi-bot ti-chi-bot merged commit 62545b0 into tikv:master Jun 7, 2022
ti-srebot pushed a commit to ti-srebot/tikv that referenced this pull request Jun 7, 2022
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-6.1 in PR #12776

@haojinming haojinming deleted the br_convert_new branch June 7, 2022 06:54
ti-chi-bot pushed a commit that referenced this pull request Jun 7, 2022
close #12758, ref #12759

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

Co-authored-by: haojinming <jinming.hao@pingcap.com>
joccau pushed a commit to joccau/tikv that referenced this pull request Jun 23, 2022
)

close tikv#12758, ref tikv#12759

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

Co-authored-by: haojinming <jinming.hao@pingcap.com>
Signed-off-by: joccau <zak.zhao@pingcap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RawKV API V2 supports "key space" encoded in key

9 participants