-
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
txn: fix the resolved txn status cache for pessimistic txn #21689
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1151,6 +1151,10 @@ func (mvcc *MVCCLevelDB) CheckTxnStatus(primaryKey []byte, lockTS, callerStartTS | |
|
||
// If the lock has already outdated, clean up it. | ||
if uint64(oracle.ExtractPhysical(lock.startTS))+lock.ttl < uint64(oracle.ExtractPhysical(currentTS)) { | ||
logutil.BgLogger().Info("rollback expired lock and write rollback record", | ||
zap.Stringer("primary key", kv.Key(primaryKey)), | ||
zap.Uint64("lock startTS", dec.lock.startTS), | ||
zap.Stringer("lock op", dec.lock.op)) | ||
if err = rollbackLock(batch, primaryKey, lockTS); err != nil { | ||
err = errors.Trace(err) | ||
return | ||
|
@@ -1333,6 +1337,12 @@ func (mvcc *MVCCLevelDB) ResolveLock(startKey, endKey []byte, startTS, commitTS | |
mvcc.mu.Lock() | ||
defer mvcc.mu.Unlock() | ||
|
||
if len(startKey) > 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Temproraly solving some mvcc leveldb problem without which resolve lock will not work for splitted regions. |
||
startKey = []byte{} | ||
} | ||
if len(endKey) > 0 { | ||
endKey = []byte{} | ||
} | ||
iter, currKey, err := newScanIterator(mvcc.db, startKey, endKey) | ||
defer iter.Release() | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -652,6 +652,16 @@ func (c *twoPhaseCommitter) doActionOnGroupMutations(bo *Backoffer, action twoPh | |
// by test suites. | ||
secondaryBo := NewBackofferWithVars(context.Background(), CommitMaxBackoff, c.txn.vars) | ||
go func() { | ||
failpoint.Inject("beforeCommitSecondaries", func(v failpoint.Value) { | ||
if s, ok := v.(string); !ok { | ||
logutil.Logger(bo.ctx).Info("[failpoint] sleep 2s before commit secondary keys", | ||
zap.Uint64("connID", c.connID), zap.Uint64("startTS", c.startTS)) | ||
time.Sleep(2 * time.Second) | ||
} else if s == "skip" { | ||
failpoint.Return() | ||
} | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about to make this failpoint more general (eg. like the following) so that we can either disable it by failpoint.Inject("beforeCommitSecondaries", func(v failpoint.Value) {
if s, ok := v.(string); !ok {
} else if s == "skip" {
failpoint.Return()
}
}) |
||
|
||
e := c.doActionOnBatches(secondaryBo, action, batches) | ||
if e != nil { | ||
logutil.BgLogger().Debug("2PC async doActionOnBatches", | ||
|
@@ -1218,8 +1228,10 @@ func (actionCommit) handleSingleBatch(c *twoPhaseCommitter, bo *Backoffer, batch | |
} | ||
logutil.Logger(bo.ctx).Error("2PC failed commit key after primary key committed", | ||
zap.Error(err), | ||
zap.Stringer("primaryKey", kv.Key(c.primaryKey)), | ||
zap.Uint64("txnStartTS", c.startTS), | ||
zap.Uint64("commitTS", c.commitTS), | ||
zap.Uint64("forUpdateTS", c.forUpdateTS), | ||
zap.Strings("keys", hexBatchKeys(batch.mutations.keys))) | ||
return errors.Trace(err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,6 +229,9 @@ func (lr *LockResolver) BatchResolveLocks(bo *Backoffer, locks []*Lock, loc Regi | |
if err != nil { | ||
return false, err | ||
} | ||
if l.LockType != kvrpcpb.Op_PessimisticLock && status.ttl == 0 { | ||
lr.saveResolved(l.TxnID, status) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you combine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The impact now is resolving pessimistic locks whose primary lock is prewrite lock type, will need more |
||
} | ||
|
||
if status.ttl > 0 { | ||
logutil.BgLogger().Error("BatchResolveLocks fail to clean locks, this result is not expected!") | ||
|
@@ -457,6 +460,15 @@ func (lr *LockResolver) getTxnStatusFromLock(bo *Backoffer, l *Lock, callerStart | |
for { | ||
status, err = lr.getTxnStatus(bo, l.TxnID, l.Primary, callerStartTS, currentTS, rollbackIfNotExist) | ||
if err == nil { | ||
// If l.LockType is pessimistic lock type: | ||
// - if its primary lock is pessimistic too, the check txn status result should not be cached. | ||
// - if its primary lock is prewrite lock type, the check txn status could be cached. | ||
// If l.lockType is prewrite lock type: | ||
// - always cache the check txn status result. | ||
// For prewrite locks, their primary keys should ALWAYS be the correct one. | ||
if l.LockType != kvrpcpb.Op_PessimisticLock && status.ttl == 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems in the current code, we don't cache when the primary lock is prewrite lock type. Can we move this logic to where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to cache all results, we need to change kvproto.
It seldom happens... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could not get the lock type for the lock we met in the original place, which is needed for the cache check. We could add back the saving logic for commited transaction in the original place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay i think it's optional as youjiali1995 says this case seldom happens. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @youjiali1995 @sticnarf |
||
lr.saveResolved(l.TxnID, status) | ||
} | ||
return status, nil | ||
} | ||
// If the error is something other than txnNotFoundErr, throw the error (network | ||
|
@@ -576,7 +588,6 @@ func (lr *LockResolver) getTxnStatus(bo *Backoffer, txnID uint64, primary []byte | |
} | ||
|
||
status.commitTS = cmdResp.CommitVersion | ||
lr.saveResolved(txnID, status) | ||
lysu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return status, nil | ||
} | ||
|
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's complex. I want to confirm Does it fail with the always cached version?
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 tested without cache change, it will fail with error reported from
admin check
statement. Could be treated as a reproducing case in unit-test, we need also add cases in ticase I think.