-
Notifications
You must be signed in to change notification settings - Fork 253
txn: support shared lock #1833
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
base: master
Are you sure you want to change the base?
txn: support shared lock #1833
Conversation
* *: add basic support for shared lock Signed-off-by: zyguan <zhongyangguan@gmail.com> * update kvproto Signed-off-by: zyguan <zhongyangguan@gmail.com> * make some minor updates Signed-off-by: zyguan <zhongyangguan@gmail.com> * add basic tests for LockKeys Signed-off-by: zyguan <zhongyangguan@gmail.com> * bump kvproto Signed-off-by: zyguan <zhongyangguan@gmail.com> * shared lock: support rollback/resolve, add tests (tikv#2) * implement resolve lock Signed-off-by: you06 <you1474600@gmail.com> * remote comment code Signed-off-by: you06 <you1474600@gmail.com> * add license Signed-off-by: you06 <you1474600@gmail.com> * add test for gc shared lock Signed-off-by: you06 <you1474600@gmail.com> * address comment Signed-off-by: you06 <you1474600@gmail.com> --------- Signed-off-by: you06 <you1474600@gmail.com> * fix test (tikv#3) Signed-off-by: you06 <you1474600@gmail.com> * txnkv: prevent shared locks from being resolved by accidently Signed-off-by: zyguan <zhongyangguan@gmail.com> --------- Signed-off-by: zyguan <zhongyangguan@gmail.com> Signed-off-by: you06 <you1474600@gmail.com> Co-authored-by: you06 <you1474600@gmail.com>
* add metrics for shared lock Signed-off-by: you06 <you1474600@gmail.com> * remove SharedLockKeysDuration Signed-off-by: you06 <you1474600@gmail.com> * add span in tracing Signed-off-by: you06 <you1474600@gmail.com> --------- Signed-off-by: you06 <you1474600@gmail.com>
…tikv#1820) Signed-off-by: zyguan <zhongyangguan@gmail.com>
* update tests Signed-off-by: you06 <you1474600@gmail.com> * stabilize test Signed-off-by: you06 <you1474600@gmail.com> --------- Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
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.
It seems txn_test.go is removed by 36a8441 , may I known the reason?
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.
Good catch, I thought it was a conflict and removed it by mistake.
36a8441 reverted.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
📝 WalkthroughWalkthroughAdds shared-lock support across transaction, pessimistic lock, and lock-resolution paths; introduces key flags and metrics, expands GC/scan behavior, updates 2PC and aggressive-locking logic, and adds extensive unit and integration tests plus test stubs and a small exported test helper. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as KVTxn
participant Pess as PessimisticHandler
participant Store as KVStore
participant Resolver as LockResolver
Client->>Client: lockKeys(ctx, lockCtx)
alt lockCtx.InShareMode == true
Client->>Pess: build mutation with Op_SharedPessimisticLock
else
Client->>Pess: build mutation with Op_PessimisticLock
end
Pess->>Store: send pessimistic lock request
Store-->>Pess: reply (acquired or lock error)
alt reply contains shared-lock infos / conflict
Pess->>Resolver: handleKeyErrorForResolve(keyError)
Resolver->>Resolver: expand shared-lock infos → individual locks
Resolver->>Resolver: apply TTL/threshold filtering
Resolver-->>Pess: resolved locks or skip wrapper
end
Pess-->>Client: result
Client->>Client: UpdateFlags(key, SetKeyLockedInShareMode / SetKeyLockedInExclusiveMode)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
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.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@kv/keyflags.go`:
- Around line 81-86: The code allows flagKeyLockedInShareMode to be set without
flagKeyLocked; update ApplyFlagsOps to enforce the invariant by rejecting or
asserting when SetKeyLockedInShareMode or SetKeyLockedInExclusiveMode is applied
unless SetKeyLocked is also present, and modify DelKeyLocked to clear both
flagKeyLocked and flagKeyLockedInShareMode (i.e., ensure origin &=
^(flagKeyLocked | flagKeyLockedInShareMode)) so the share flag cannot persist
after unlocking; reference ApplyFlagsOps, DelKeyLocked, SetKeyLocked,
SetKeyLockedInShareMode, SetKeyLockedInExclusiveMode, flagKeyLocked,
flagKeyLockedInShareMode, and note persistentFlags/initKeysAndMutations remain
as-is.
In `@txnkv/transaction/txn_test.go`:
- Around line 110-128: Update the typo in the test failure message: in
TestLockKeys change the require.FailNow call inside the requireNoRequest closure
(used by txn.store.client.onSend) to say "locking keys in optimistic mode do not
invoke SendRequest" (fix "optismistic" → "optimistic") so the error string is
spelled correctly when txn.lockKeys is expected not to call SendRequest; no
logic changes needed (references: TestLockKeys, requireNoRequest, txn.lockKeys,
newTestTxn, GetMemBuffer().GetFlags).
🧹 Nitpick comments (6)
tikv/gc.go (1)
290-301: Guard against emptySharedLockInfosto avoid dropping locks.If
GetSharedLockInfos()can be non-nil but empty, the wrapper lock will be skipped and no lock will be returned for that entry. Consider guarding withlen(...) > 0and falling back to the wrapper otherwise. Please confirm kvproto’s guarantee here.♻️ Proposed defensive tweak
- for _, lockInfo := range locksInfo { - if sharedLockInfos := lockInfo.GetSharedLockInfos(); sharedLockInfos != nil { + for _, lockInfo := range locksInfo { + if sharedLockInfos := lockInfo.GetSharedLockInfos(); len(sharedLockInfos) > 0 { // expand shared lock into multiple locks, and drop the dummy wrapper lock. for _, sharedLockInfo := range sharedLockInfos { locks = append(locks, txnlock.NewLock(sharedLockInfo)) } - continue - } - locks = append(locks, txnlock.NewLock(lockInfo)) + } else { + locks = append(locks, txnlock.NewLock(lockInfo)) + } }txnkv/transaction/txn_test.go (1)
59-86: Consider:GetOracle()returns nil which could cause NPE if accessed.The
mockStore.GetOracle()returnsnil. While this works for the current tests that don't require the oracle, it could cause unexpected panics if future tests or the code under test attempts to use it.Consider returning a mock oracle
+type mockOracle struct{} + +func (o *mockOracle) GetTimestamp(ctx context.Context, opt *oracle.Option) (uint64, error) { + return 1, nil +} + +// ... other required interface methods + func (m *mockStore) GetOracle() oracle.Oracle { - return nil + return &mockOracle{} }txnkv/transaction/txn.go (1)
660-660: Verify: TODO for pipelined DML shared lock handling.This TODO indicates that pipelined DML does not prewrite shared locks even when
flags.HasLockedInShareMode()is true. Ensure this is tracked and addressed if pipelined DML with shared locks becomes a requirement.Do you want me to open a new issue to track this task?
integration_tests/shared_lock_test.go (3)
43-60: Consider documenting the rationale for the specific TTL/backoff values.The setup modifies global state (
ManagedLockTTLto 3s,CommitMaxBackoffto 1s). While this is necessary for testing, a brief comment explaining why these specific values were chosen would improve maintainability.Add clarifying comments
func (s *testSharedLockSuite) SetupSuite() { + // Set shorter TTL for faster test execution while still allowing lock heartbeat validation atomic.StoreUint64(&transaction.ManagedLockTTL, 3000) // 3s + // Reduce backoff to speed up conflict resolution tests atomic.StoreUint64(&transaction.CommitMaxBackoff, 1000) s.Nil(failpoint.Enable("tikvclient/injectLiveness", `return("reachable")`)) }
84-134: Consider argument order ins.Equalfor clearer error messages.The testify convention is
s.Equal(expected, actual). Several assertions have the arguments reversed, which affects error message clarity when tests fail.Proposed fixes for argument order
- s.Equal(txn1.GetCommitter().GetPrimaryKey(), pk1) + s.Equal(pk1, txn1.GetCommitter().GetPrimaryKey())Apply similar changes to lines 102, 112, etc.
113-133: Test relies on timing which may cause flakiness.The test uses
time.Sleep(500 * time.Millisecond)to wait for the lock to be blocked, and then verifies timing withafterRelease.After(beforeRelease). In high-load CI environments, this could potentially be flaky.Consider using a synchronization mechanism to ensure txn3 has started waiting before checking the timing.
…master-shared-lock
Signed-off-by: you06 <you1474600@gmail.com>
ref tikv/tikv#19087
Support shared lock for client-go with some limitations:
Summary by CodeRabbit
New Features
Behavior Changes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.