-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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 LOCK/UNLOCK TABLES feature #10343
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… check in sessino
crazycs520
added
type/new-feature
require-LGT3
Indicates that the PR requires three LGTM.
labels
May 5, 2019
…ORMATION_SCHEMA, PERFORMANCE_SCHEMA, mysql
Codecov Report
@@ Coverage Diff @@
## master #10343 +/- ##
===========================================
Coverage ? 80.8257%
===========================================
Files ? 419
Lines ? 88546
Branches ? 0
===========================================
Hits ? 71568
Misses ? 11753
Partials ? 5225 |
/run-all-tests |
zimulala
reviewed
Jun 17, 2019
zimulala
previously approved these changes
Jun 18, 2019
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.
LGTM
zimulala
added
status/LGT3
The PR has already had 3 LGTM.
and removed
status/LGT2
Indicates that a PR has LGTM 2.
labels
Jun 18, 2019
/run-all-tests |
/run-all-tests |
1 similar comment
/run-all-tests |
/run-all-tests |
zimulala
approved these changes
Jun 18, 2019
marsishandsome
pushed a commit
to marsishandsome/tidb
that referenced
this pull request
Jul 24, 2019
lzmhhh123
pushed a commit
to lzmhhh123/tidb
that referenced
this pull request
Jan 19, 2020
crazycs520
added a commit
to crazycs520/tidb
that referenced
this pull request
Apr 23, 2020
sre-bot
pushed a commit
that referenced
this pull request
Apr 27, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
require-LGT3
Indicates that the PR requires three LGTM.
status/LGT3
The PR has already had 3 LGTM.
type/new-feature
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Background
Currently, we only support LOCK TABLES/UNLOCK TABLES syntax, but actually do nothing.
TiDB only supports Optimistic Lock currently. A large write transaction maybe failed caused by other transaction has 1 insert. Large transaction retries are expensive, so we want to use LOCK TABLES to avoid this expensive retry.
Proposal
LOCK TABLES
explicitly acquires table locks for the current client session. Table locks can be only acquired for base tables, not support for views. You must have the LOCK TABLES privilege, and the SELECT privilege for each table to be locked.UNLOCK TABLES
explicitly releases any table locks held by the current session.LOCK TABLES
implicitly releases any table locks held by the current session before acquiring new locks.While the locks thus obtained are held, the session can access only the locked tables. Tables in the
INFORMATION_SCHEMA
database are an exception. They can be accessed without being locked explicitly.LOCK TABLES or UNLOCK TABLES, when applied to a partitioned table, always locks or unlocks the entire table; these statements do not support partition lock pruning.
Principles
The Difference with Mysql
LOCK TABLE
You can't implement transaction with
LOCK TABLES
andUNLOCK TABLES
in TiDB. If you need transaction, you should also executebegin
afterLOCK TABLES
.Table locks of mysql is metadata locks, and user can use table locks to implement transaction when the mysql storage engine is nontransactional.
Because MySQL execute
select/insert/update
will automatically to acquire the related table locks first. If session 1 is executing aselect * from t1
, and session 2 executelock tables t1 write
, then session 2 won't aquire the t1 table lock untill the session 1 execute finish. Details as follows chart:Select * from t1
t1
table lock.Lock tables t1 write
Select * from t1
) FinishLock tables t1 write
) sucess.But in TiDB, session2 will aquire the t1 table lock soon. Because session 1 won't acquire the
t1
table lock. In TiDB, It is very expensive to acquire the related table locks when execute DML likeselect/insert
. Details in TiDB as follows chart:Select * from t1
Lock tables t1 write
Lock tables t1 write
) sucess.Select * from t1
) FinishMySQL session1 execute
LOCK TABLES t1 write
, if other session2 obtains the lock oft1
, session 1 will wait until the session2 releaset1
lock. In TiDB, session 1 will return error like:Table 't1' was locked by server xx session xx.
Lock tables t1 read
Lock tables t1 write
Lock tables t1 write
Error: Table 't1' was locked by server xx session 1
Lock tables t1 write
sucess.TiDB table lock not support
alias
name currently. TiDB will lock thetable.ID
instead oftable.Name
What is changed and how it works?
TableLockInfo
inTableInfo
.LOCK TABLES
andUNLOCK TABLES
. So add 2 DDL job type:ActionLockTable
,ActionUnlockTable
.lockedTables
map insession
.lockedTables
use to record the table locks hold by the session.CheckTableLock
will use thevisitInfo
to check table locked. This check was add after theCheckPrivilege
.Related parser PR: pingcap/parser#305
Check List
Tests
Code changes
Side effects