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

*: Add support for GET_LOCK() / RELEASE_LOCK() / RELEASE_ALL_LOCKS() #33947

Merged
merged 10 commits into from
Apr 29, 2022

Conversation

morgo
Copy link
Contributor

@morgo morgo commented Apr 13, 2022

What problem does this PR solve?

Issue Number: close #14994

Problem Summary:

This supports the GET_LOCK() / RELEASE_LOCK() / RELEASE_ALL_LOCKS() functions previously proposed in the design document @ #33406

What is changed and how it works?

Functions GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS are no longer noops. The functionality is now implemented.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
    Deployed 2x tidb, 1x tidb/pd. Confirmed functionality works correctly.
  • 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
    Changes the behavior of tidb_enable_noop_funcs, so documentation needs updating.
  • Contains experimental features
  • Changes MySQL compatibility

Release note

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

TiDB now supports the GET_LOCK() / RELEASE_LOCK() / RELEASE_ALL_LOCKS() advisory lock functions.

@morgo morgo requested a review from a team as a code owner April 13, 2022 15:37
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Apr 13, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • bb7133
  • mjonss

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 release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Apr 13, 2022
@morgo morgo requested a review from mjonss April 13, 2022 18:00
@morgo
Copy link
Contributor Author

morgo commented Apr 13, 2022

/run-unit-test

@sre-bot
Copy link
Contributor

sre-bot commented Apr 13, 2022

@morgo morgo changed the title *: Add support for GET_LOCK() / RELEASE_LOCK() *: Add support for GET_LOCK() / RELEASE_LOCK() / RELEASE_ALL_LOCKS() Apr 14, 2022
add similar validation for release_lock lockname as get_lock
@yahonda
Copy link
Member

yahonda commented Apr 14, 2022

Let me confirm if my understanding is correct that based on this implementation, the maximum value of advisory lock timeout only depends on innodb_lock_wait_timeout whose max value is 1073741824 sec, about 34 years.
And it should not be limited by the max value of max-txn-ttl timeout 1 hour.

@yahonda
Copy link
Member

yahonda commented Apr 14, 2022

It looks like the get_lock timeout is limited by 1 hour.

  1. Get lock with second argument as 3800 sec.
mysql> select get_lock('foo', 3800);
+-----------------------+
| get_lock('foo', 3800) |
+-----------------------+
|                     1 |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
  1. Attempt to get the same lock, which results this failure
mysql> select get_lock('FOO', -1);
+---------------------+
| get_lock('FOO', -1) |
+---------------------+
|                   0 |
+---------------------+
1 row in set, 1 warning (1 hour 0.01 sec)
mysql> show warnings;
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                            |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect get_lock value: '%!s(int64=-00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001)' |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
  • ./bin/tidb-server STDOUT
[2022/04/14 16:37:56.632 +09:00] [INFO] [2pc.go:1092] ["ttlManager live up to its lifetime"] [txnStartTS=432516176655155200] [uptime=3600207] [maxTxnTTL=3600000]
[2022/04/14 16:38:01.229 +09:00] [WARN] [session.go:1946] ["run statement failed"] [schemaVersion=34] [error="[tikv:1205]Lock wait timeout exceeded; try restarting transaction"] [session="{\n  \"currDBName\": \"\",\n  \"id\": 0,\n  \"status\": 3,\n  \"strictMode\": true,\n  \"txn\": \"432516177913446400\",\n  \"user\": null\n}"]

@morgo
Copy link
Contributor Author

morgo commented Apr 14, 2022

Let me confirm if my understanding is correct that based on this implementation, the maximum value of advisory lock timeout only depends on innodb_lock_wait_timeout whose max value is 1073741824 sec, about 34 years. And it should not be limited by the max value of max-txn-ttl timeout 1 hour.

The maximum for innodb_lock_wait_timeout was changed to 1hr to match the default of max-txn-ttl. See: #33473

For this:

mysql> show warnings;
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                                                            |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1292 | Truncated incorrect get_lock value: '%!s(int64=-00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001)' |
+---------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

I'll fix the message. But the warning is intentional - it's not a MySQL behavior, but since we can't do unlimited I thought this was important.

Copy link
Contributor

@mjonss mjonss left a comment

Choose a reason for hiding this comment

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

When a TiDB server with advisory locks (or any pessimistic transaction) dies (kill -9 for example) how are those locks handled? I did not find any documentation on this...

Mostly questions and still looks good to me, so approved, feel free to consider my suggestions.

expression/builtin_miscellaneous.go Show resolved Hide resolved
expression/function_traits.go Show resolved Hide resolved
expression/integration_test.go Outdated Show resolved Hide resolved
Comment on lines +362 to +363
tk.MustQuery("SELECT get_lock('mygloballock', 0)").Check(testkit.Rows("0")) // someone else has the lock
tk.MustQuery("SELECT release_lock('mygloballock')").Check(testkit.Rows("0")) // never had the lock
Copy link
Contributor

Choose a reason for hiding this comment

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

Could not this be used for implementing IS_FREE_LOCK()?
I.e. first check if the session already have the lock, if not do a get_lock with 0 timeout and if we got the lock immediately release it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I guess that's possible.

But it's not the most elegant. If I learn the transaction code, I assume I could implement this correctly by checking in TiKV if the lock is held. So I will try and keep out of scope for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this a little more. I think you are right: some implementation is better than no implementation. We know IS_FREE_LOCK() is used in the rails test suite (but not in rails itself).

I will plan to add it for this sprint, but in a separate PR as this one is already getting large.

expression/integration_test.go Show resolved Hide resolved
session/session.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 14, 2022
@morgo
Copy link
Contributor Author

morgo commented Apr 14, 2022

When a TiDB server with advisory locks (or any pessimistic transaction) dies (kill -9 for example) how are those locks handled? I did not find any documentation on this...

For clean shutdown and kill, I've verified it works. For unsafe shutdown it could take up to the 1hr timeout to clear, I'm not sure if there are mechanisms in place which could shorten it.

@morgo
Copy link
Contributor Author

morgo commented Apr 14, 2022

@mjonss thanks for the review. PTAL again :-)

@yahonda
Copy link
Member

yahonda commented Apr 15, 2022

The maximum for innodb_lock_wait_timeout was changed to 1hr to match the default of max-txn-ttl. See: #33473

Understood the limitation and show warning is helpful when users specified lock timeout value longer than 1 hour.

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 28, 2022
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 28, 2022
Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot ti-chi-bot removed the status/LGT1 Indicates that a PR has LGTM 1. label Apr 29, 2022
@ti-chi-bot ti-chi-bot added the status/LGT2 Indicates that a PR has LGTM 2. label Apr 29, 2022
@bb7133
Copy link
Member

bb7133 commented Apr 29, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 3f7646accea4fc737634eea39c47094a1b982206

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

morgo commented Apr 29, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 1911dcf

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 29, 2022
@ti-chi-bot ti-chi-bot merged commit 6d8669e into pingcap:master Apr 29, 2022
@morgo morgo mentioned this pull request Apr 29, 2022
16 tasks
@sre-bot
Copy link
Contributor

sre-bot commented Apr 29, 2022

TiDB MergeCI notify

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci/integration-cdc-test 🟢 all 34 tests passed 24 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 11 min Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 6 min 38 sec Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 12 tests passed 5 min 54 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 5 min 33 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 11 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 4 min 32 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 32 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 3 min 12 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
release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 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.

UCP: Support get_lock and release_lock functions
6 participants