-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: support MySQL compatible AUTO_INCREMENT
#38449
Conversation
[REVIEW NOTIFICATION] This pull request has not been approved. To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. Reviewer can indicate their review by submitting an approval review. |
## Compatibility | ||
|
||
The old implementation can still be kept, its performance is better and might meet some user's needs. | ||
We can rename the syntax for it, such as `AUTO_INCREMENT_FAST` or `AUTO_INCREMENT_OLD` or `DEFICIT_AUTO_INCREMENT`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would propose the name 'AUTO_INCREMENT_CACHED' for the old implementation or 'CACHED_AUTO_INCREMENT'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we name this:
- `AUTO_INCREMENT`` for the new fully MySQL/InnoDB compatible method
AUTO_INCREMENT_FAST
for the old, more distributed autoid
Then when migrating, using ORMs, tools, etc people are likely to use the new MySQL compatible autoid instead of the more scalable distributed autoid while they might not need the new behavior.
Another way of doing this would be this:
SET tidb_autoid_mysql_compatible=ON;
CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY);
By using a session variable instead of a different keyword we allow the default to use the distributed autoid while only using the new MySQL compatibility when really needed. The session variable can be set on the connection or via a global variable, this makes it possible to run third-party applications that rely on the new MySQL compatible autoid without having to change the SQL queries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've update this part.
Use AUTO_ID_CACHE 1
to make it compatible with both old TiDB and MySQL
|
||
## Proposal | ||
|
||
This proposes that we introduce a centralized auto ID allocating service, the `AUTO_INCREMENT` ID is allocated from the centralized service and there is no caching mechanism on the TiDB layer, so the behaviour can be MySQL compatible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this would be another service in a primary and a backup TiDB node? Which are the reasons for having it in TiDB vs using the primary PD node for this service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now it's embeded in TiDB ... just like the ddl owner
The old implementation can still be kept, its performance is better and might meet some user's needs. | ||
We can rename the syntax for it, such as `AUTO_INCREMENT_FAST` or `AUTO_INCREMENT_OLD` or `DEFICIT_AUTO_INCREMENT`. | ||
|
||
The new `AUTO_INCREMENT` behaviour is still a compatibility-breaker between old and new versioned TiDB, but it's worthy on behalf of MySQL compatibility. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are all the auto_increment allocations done during INSERT and UPDATE done the same ways in TiDB as in MySQL? (which may be another issue of compatibility)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
INSERT and UPDATE handling is the same between TiDB and MySQL...
|
||
When the switch happen, the current max allocated ID information is required so as to guarantee the uniqueness. The new primary process allocate IDs begin from the max allocated ID plus one. | ||
|
||
We can persist the max allocated ID every time, but that's costly. Another choice is persisting it periodically, it's safe to allocate IDs in range `[base, max persisted)`. When the primary process crash abnormally, the backup process gets the max persisted ID as its base. Etcd is also used as the storage for the max persisted ID. This optimization could still make the ID not be consecutive, but it's not so common (and I believe MySQL may also crash and facing such problem?), so this is not a big issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For MySQL it depends on version, it changed in 8.0 vs 5.7, see https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html#innodb-auto-increment-initialization
In 5.7 on (re)start of the server, it would initialise the auto_increment like SELECT MAX(auto_inc_col) FROM t
, while in 8.0 it persist each allocation in the redo log. So there should not be any 'lost' values due to caching and restart.
Also there are a lot of different details that can still differ between TiDB and MySQL/InnoDB, like the setting of sysvar_innodb_autoinc_lock_mode.
But getting the basics more compatible is good!
after reload table, the allocator is change to the basic one Lines starting
/run-build-arm64 comment=true |
download tidb binary(linux arm64) at http://fileserver.pingcap.net/download/builds/pingcap/test/tidb/cba21767e97d0afdce02ca62bef0a78de71b3888/centos7/tidb-linux-arm64.tar.gz |
/run-build-arm64 comment=true |
download tidb binary(linux arm64) at http://fileserver.pingcap.net/download/builds/pingcap/test/tidb/5d7d63c062dcfc81c144cf4e3d706cb661bef776/centos7/tidb-linux-arm64.tar.gz |
/run-build-arm64 comment=true |
download tidb binary(linux arm64) at http://fileserver.pingcap.net/download/builds/pingcap/test/tidb/1ef80ab69539db36511a5123a9f4ff310d92e6fa/centos7/tidb-linux-arm64.tar.gz |
/run-build-arm64 comment=true |
download tidb binary(linux arm64) at http://fileserver.pingcap.net/download/builds/pingcap/test/tidb/9891dc672523596ed1df8df463e8b307dc46733d/centos7/tidb-linux-arm64.tar.gz |
@tiancaiamao: PR needs rebase. 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 kubernetes/test-infra repository. |
/run-build-arm64 comment=true |
download tidb binary(linux arm64) at http://fileserver.pingcap.net/download/builds/pingcap/test/tidb/40ddeff6de71cc1331104d6a456194394060af68/centos7/tidb-linux-arm64.tar.gz |
This one had been splited into several PRs and merged. |
What problem does this PR solve?
Issue Number: close #38442
Problem Summary:
What is changed and how it works?
Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.