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

lightning: fix auto_increment out-of-range error #34146

Merged
merged 18 commits into from
Apr 29, 2022

Conversation

buchuitoudegou
Copy link
Contributor

@buchuitoudegou buchuitoudegou commented Apr 21, 2022

What problem does this PR solve?

Issue Number: close #27937

Problem Summary:

What is changed and how it works?

  • Change AlterAutoIncrement's parameter to uint64 whose maximum is larger than BIGINT.
  • Throw warning and return immediately while incr is larger than math.MaxInt64 (i.e. maximum of BIGINT)

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Apr 21, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • D3Hunter
  • gozssky

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.

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 needs-cherry-pick-release-5.2 release-note-none Denotes a PR that doesn't merit a release note. needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-6.0 size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 21, 2022
@buchuitoudegou
Copy link
Contributor Author

/cc @gozssky @glorv

@buchuitoudegou
Copy link
Contributor Author

/component lightning

@ti-chi-bot ti-chi-bot added the component/lightning This issue is related to Lightning of TiDB. label Apr 21, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Apr 21, 2022

if incr > math.MaxInt64 {
logger.Warn("auto_increment out of the maximum value TiDB supports", zap.Uint64("auto_increment", incr))
return nil
}
Copy link
Contributor

@sleepymole sleepymole Apr 21, 2022

Choose a reason for hiding this comment

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

Do we need to set auto_increment to math.MaxInt64 when incr > math.MaxInt64?

Copy link
Contributor Author

@buchuitoudegou buchuitoudegou Apr 21, 2022

Choose a reason for hiding this comment

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

MaxInt64 is a legal value in TiDB (i.e. you can insert (9223372036854775807, ....) to a table) whereas MaxInt64+1 is illegal, which will trigger the error, ERROR 1467 (HY000): Failed to read auto-increment value from storage engine.

e.g.

mysql> create table test1(
    -> a bigint auto_increment,
    -> b int,
    -> primary key(a));
Query OK, 0 rows affected (0.11 sec)

mysql> alter table test1 auto_increment=9223372036854775807;
Query OK, 0 rows affected (0.12 sec)

mysql> insert into test1(b) values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test1;
+---------------------+------+
| a                   | b    |
+---------------------+------+
| 9223372036854775807 |    1 |
+---------------------+------+
1 row in set (0.00 sec)

Copy link
Contributor

Choose a reason for hiding this comment

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

Does incr > math.MaxInt64 means lightning has inserted rows whose id is greater than math.MaxInt64? Will this cause duplicate entry or data corruption?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mostly yes. But in terms of the case shown in the linked issue, 9223372036854775807 is a valid input, but after inserting it, Lightning would try alter table xxx auto_increment=9223372036854775807+1, get syntax error from TiDB, and fail unexpectedly.

So I'm not trying to legalize auto-incr value that exceeds math.MaxInt64 but try to skip this syntax error and let Lightning fail by other clearer issues (such as data corruption, duplicate entry, etc.). As for the case in this issue, because Lightning doesn't try to write entries that are larger than 9223372036854775807, it will succeed.

Failing by duplicate entries might be more intuitive in terms of the effectiveness of reporting the error. Syntax error is helpless...

Copy link
Contributor

Choose a reason for hiding this comment

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

Could confirm whether data corruption or any other error can be reported when using local backend and auto_inc is overflow?

Copy link
Contributor Author

@buchuitoudegou buchuitoudegou Apr 21, 2022

Choose a reason for hiding this comment

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

Yes. If the auto_incr column overflows, the checksum will be mismatched.

e.g. create a table with auto-incremental key:

CREATE TABLE `test1` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `a` int(11) NOT NULL,
  PRIMARY KEY (`a`),
  UNIQUE KEY `id` (`id`)
)

insert 256 rows (where only one column "a" in the source file), and get error:

Error: [Lighting:Restore:ErrChecksumMismatch]checksum mismatched remote vs local => (checksum: 1088876813058384307 vs 11572238353217052498) (total_kvs: 383 vs 512) (total_bytes:11996 vs 16640)
tidb lightning encountered error: [Lighting:Restore:ErrChecksumMismatch]checksum mismatched remote vs local => (checksum: 1088876813058384307 vs 11572238353217052498) (total_kvs: 383 vs 512) (total_bytes:11996 vs 16640)

Copy link
Contributor

Choose a reason for hiding this comment

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

"checksum mismatch" is hard for user to know what happened. How about checking overflow during encoding and give a clear error to user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

"checksum mismatch" is hard for user to know what happened. How about checking overflow during encoding and give a clear error to user?

Yes. Actually I'm looking at it #28776. This PR only solves the unexpected syntax error.

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 21, 2022
@buchuitoudegou
Copy link
Contributor Author

/cc @D3Hunter

Copy link
Contributor

@glorv glorv left a comment

Choose a reason for hiding this comment

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

BTW, please also fix and add test for auto_random, auto_random should have the same issue as auto_increment.

logger := log.With(zap.String("table", tableName), zap.Int64("auto_increment", incr))
func AlterAutoIncrement(ctx context.Context, g glue.SQLExecutor, tableName string, incr uint64) error {
logger := log.With(zap.String("table", tableName), zap.Uint64("auto_increment", incr))
if incr > math.MaxInt64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since incr > math.MaxInt64 can only happen when there is one row that there is one rwo explitly set the max-value, so here you should alter the auto increment value to the max value then. So the behavior then is the same as import via sql.
Please also add a test case to ensure: Lightning local backend can successfully import data which explicitly set auto_increment row value to the max valid value according to tidb's restriction. After import, any new insert statement without explicitly set the auto_increment row will result with an error.

Copy link
Contributor Author

@buchuitoudegou buchuitoudegou Apr 21, 2022

Choose a reason for hiding this comment

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

I'll add a IT later. But it seems alter table xxx auto_increment=math.MaxInt64 will lead to some unexpected errors (I've talked to the guys from sql-infra, it might be bugs of TiDB #34142). So I won't set it until they fix this issue (leave a TODO here, or perhaps I can use alter table xxx force ...).

@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 21, 2022
@buchuitoudegou
Copy link
Contributor Author

/run-integration-br-test

@buchuitoudegou buchuitoudegou force-pushed the auto-incr branch 4 times, most recently from 67691e8 to 683c730 Compare April 22, 2022 04:24
@ti-chi-bot
Copy link
Member

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

Commit hash: 6959514

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 28, 2022
@buchuitoudegou
Copy link
Contributor Author

/run-mysql-test
/run-unit-test

@buchuitoudegou
Copy link
Contributor Author

/run-mysql-test

1 similar comment
@sleepymole
Copy link
Contributor

/run-mysql-test

@ti-chi-bot ti-chi-bot merged commit 5617941 into pingcap:master Apr 29, 2022
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 29, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-5.2 in PR #34333

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 29, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-5.3 in PR #34334

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 29, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-5.4 in PR #34335

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 29, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-6.0 in PR #34336

@sre-bot
Copy link
Contributor

sre-bot commented Apr 29, 2022

TiDB MergeCI notify

🔴 Bad News! New failing [2] after this pr merged.
These new failed integration tests seem to be caused by the current PR, please try to fix these new failed integration tests, thanks!

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci/integration-cdc-test 🟥 failed 4, success 30, total 34 28 min New failing
idc-jenkins-ci-tidb/common-test 🟥 failed 1, success 11, total 12 21 min New failing
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 22 min Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 22 min Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 18 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 15 min Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 11 min Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 7 min 16 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 34 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/lightning This issue is related to Lightning of TiDB. needs-cherry-pick-release-5.2 needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. release-note-none Denotes a PR that doesn't merit a release note. 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.

lighting: local backend import failed if data has largest number as bigint auto_increment column
7 participants