After Pre-splitting TiKV Region, Key Insertion into Region Fails

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: tikv region预拆分后,key插入region失败

| username: TiDBer_Jxly6caL

[TiDB Usage Environment] Testing
[TiDB Version] tikv client: 3.3.2
[Reproduction Path] Operations performed that led to the issue

conf = TiConfiguration.createDefault("*****");
session = TiSession.create(conf);
client = session.createRawClient();
// Pre-split region
List<byte[]> list = new ArrayList<>();
list.add(toByteArray("inodes", 500000));
list.add(toByteArray("inodes", 800000));
session.splitRegionAndScatter(list);
// Insert a record
client.put(ByteString.copyFrom(toByteArray("inodes", 611224)), ByteString.copyFromUtf8("Hello"));

[Encountered Issue: Phenomenon and Impact]
After pre-splitting the region, writing the key can find the corresponding region based on the key, but when finally writing the key to the region, an error occurs indicating key_not_in_region.
Tracking the source code reveals that when obtaining the region, the key undergoes an encoding step:

PDClient.getRegionByKey():
GetRegionRequest.newBuilder().setRegionKey(codec.encodePdQuery(key))
returns CodecUtils.encode(key);

However, when inserting the key, this process does not encode the key, causing a mismatch and resulting in an error when inserting into the corresponding region:

RegionStoreClient.rawPut():
RawPutRequest.newBuilder().setKey(codec.encodeKey(key))
directly returns the key;

Does anyone know why this encoding step is necessary for the region here, and is there any way to avoid this issue?

[Resource Configuration]
[Attachment: Screenshot/Log/Monitoring]

| username: xfworld | Original post link

The internal processing method is definitely for compatibility, to meet PD’s scheduling of regions. As for the gaps between region keys, it is best to follow the rules.

Refer to the usage given in the documentation, exploring new territories may lead to more pitfalls…
https://tikv.github.io/client-java/examples/rawkv.html

Additionally, be aware of the compatibility differences:

| username: TiDBer_Jxly6caL | Original post link

Setting KVMode to RAW solves it
conf.setKvMode(String.valueOf(TiConfiguration.KVMode.RAW))

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.