Skip to content
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

tidb_decode_key doesn't work for clustered index #23180

Open
kolbe opened this issue Mar 9, 2021 · 6 comments
Open

tidb_decode_key doesn't work for clustered index #23180

kolbe opened this issue Mar 9, 2021 · 6 comments
Labels
severity/minor sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.

Comments

@kolbe
Copy link
Contributor

kolbe commented Mar 9, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

set tidb_enable_clustered_index=ON;
create table t1 ( `a` int(10) unsigned NOT NULL, b int unsigned not null, PRIMARY KEY (a,b) /*T![clustered_index] CLUSTERED */);
split table t1 between (0,0) and (3000,0) regions 10;
select start_key, tidb_decode_key(start_key) from information_schema.tikv_region_status where table_name='t1';

2. What did you expect to see? (Required)

3. What did you see instead (Required)

5.7.25-TiDB-v4.0.0-beta.2-2292-g4218f2836-dirty (root) [test]> set tidb_enable_clustered_index=ON;
Query OK, 0 rows affected (0.000 sec)

create table t1 ( `a` int(10) unsigned NOT NULL, b int unsigned not null, PRIMARY KEY (a,b) /*T![clustered_index] CLUSTERED */);
split table t1 between (0,0) and (3000,0) regions 10;
select start_key, tidb_decode_key(start_key) from information_schema.tikv_region_status where table_name='t1';
5.7.25-TiDB-v4.0.0-beta.2-2292-g4218f2836-dirty (root) [test]> create table t1 ( `a` int(10) unsigned NOT NULL, b int unsigned not null, PRIMARY KEY (a,b) /*T![clustered_index] CLUSTERED */);
Query OK, 0 rows affected (0.078 sec)

5.7.25-TiDB-v4.0.0-beta.2-2292-g4218f2836-dirty (root) [test]> split table t1 between (0,0) and (3000,0) regions 10;
+--------------------+----------------------+
| TOTAL_SPLIT_REGION | SCATTER_FINISH_RATIO |
+--------------------+----------------------+
|                  9 |                    1 |
+--------------------+----------------------+
1 row in set (1.367 sec)

5.7.25-TiDB-v4.0.0-beta.2-2292-g4218f2836-dirty (root) [test]> select start_key, tidb_decode_key(start_key) from information_schema.tikv_region_status where table_name='t1';
+--------------------------------------------------------------------------+--------------------------------------------------------------------------+
| start_key                                                                | tidb_decode_key(start_key)                                               |
+--------------------------------------------------------------------------+--------------------------------------------------------------------------+
| 7480000000000000FF665F720400000000FF000004B004000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF000004B004000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF0000012C04000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000012C04000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF0000025804000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000025804000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF000005DC04000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF000005DC04000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF0000083404000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000083404000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF0000070804000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000070804000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF0000038404000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000038404000000FF0000000000000000F9 |
| 7480000000000000FF665F720400000000FF00000A8C04000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF00000A8C04000000FF0000000000000000F9 |
| 7480000000000000FF6600000000000000F8                                     | 7480000000000000FF6600000000000000F8                                     |
| 7480000000000000FF6600000000000000F8                                     | 7480000000000000FF6600000000000000F8                                     |
| 7480000000000000FF665F720400000000FF0000096004000000FF0000000000000000F9 | 7480000000000000FF665F720400000000FF0000096004000000FF0000000000000000F9 |
+--------------------------------------------------------------------------+--------------------------------------------------------------------------+
11 rows in set, 11 warnings (0.023 sec)

4. What is your TiDB version? (Required)

tidb_version(): Release Version: v4.0.0-beta.2-2292-g4218f2836-dirty
Edition: Community
Git Commit Hash: 4218f2836bb38ec79fd080fa88d09d3fe3766c3a
Git Branch: HEAD
UTC Build Time: 2021-03-07 19:53:36
GoVersion: go1.13.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@kolbe kolbe added the type/bug The issue is confirmed as a bug. label Mar 9, 2021
@kolbe kolbe changed the title tidb_decode_key doesn't work tidb_decode_key doesn't work for clustered index Mar 9, 2021
@kolbe
Copy link
Contributor Author

kolbe commented Mar 9, 2021

5.7.25-TiDB-v4.0.0-beta.2-2292-g4218f2836-dirty (root) [test]> show warnings;
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                               |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 8045 | invalid record key - "7480000000000000665f720400000000000004b0040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f7204000000000000012c040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000258040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f720400000000000005dc040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000834040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000708040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000384040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000a8c040000000000" invalid encoded key, expected length: 9, actual length: 6 |
| Warning | 1105 | invalid record/index key: 748000000000000066                                                                                          |
| Warning | 1105 | invalid record/index key: 748000000000000066                                                                                          |
| Warning | 8045 | invalid record key - "7480000000000000665f72040000000000000960040000000000" invalid encoded key, expected length: 9, actual length: 6 |
+---------+------+---------------------------------------------------------------------------------------------------------------------------------------+
11 rows in set (0.000 sec)

@tangenta
Copy link
Contributor

tangenta commented Mar 9, 2021

This is expected because of the implementation of SPLIT TABLE(on a clustered index table):

The clustered primary key's upper bound & lower bound values are converted to []byte first, then it calculates the cut points without the knowledge of "column data".

tidb/executor/split.go

Lines 256 to 271 in 602bcd2

func getValuesList(lower, upper []byte, num int, valuesList [][]byte) [][]byte {
commonPrefixIdx := longestCommonPrefixLen(lower, upper)
step := getStepValue(lower[commonPrefixIdx:], upper[commonPrefixIdx:], num)
startV := getUint64FromBytes(lower[commonPrefixIdx:], 0)
// To get `num` regions, only need to split `num-1` idx keys.
buf := make([]byte, 8)
for i := 0; i < num-1; i++ {
value := make([]byte, 0, commonPrefixIdx+8)
value = append(value, lower[:commonPrefixIdx]...)
startV += step
binary.BigEndian.PutUint64(buf, startV)
value = append(value, buf...)
valuesList = append(valuesList, value)
}
return valuesList
}

So the cut points are probably not decodable ;(

@wjhuang2016
Copy link
Member

It's the expected behavior and the error should be more understandable.

@tangenta
Copy link
Contributor

tangenta commented Mar 9, 2021

@lilinghai Could we remove the critical tag now?

There are two possible solutions for this issue:

  • Leave the cut points undecodable. Refine the error message.
  • Design a new SPLIT TABLE algorithm for the clustered index tables so that the cut points can be decoded.

WDYT @coocood @bb7133

@jebter jebter added sig/transaction SIG:Transaction and removed sig/execution SIG execution labels Mar 9, 2021
@bb7133
Copy link
Member

bb7133 commented Mar 9, 2021

Basically I agree with @tangenta , and is there any possibility that we can decode the cut points? If possible, we can turn it into an 'enhancement' or 'feature request'.

I just found that we don't have any user doc for TIDB_DECODE_KEY :(

@coocood
Copy link
Member

coocood commented Mar 9, 2021

The issue not only relates to the clustered index, when you split an index, the region's bound key can not be decoded too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity/minor sig/transaction SIG:Transaction type/bug The issue is confirmed as a bug.
Projects
None yet
Development

No branches or pull requests

8 participants