Skip to content

Commit

Permalink
txn: fix the resolved txn status cache for pessimistic txn pingcap#1868
Browse files Browse the repository at this point in the history
  • Loading branch information
marsishandsome committed Dec 16, 2020
1 parent 2219618 commit e9bd049
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ private TxnStatus getTxnStatusFromLock(BackOffer bo, Lock lock, long callerStart
while (true) {
try {
return getTxnStatus(
bo, lock.getTxnID(), lock.getPrimary(), callerStartTS, currentTS, rollbackIfNotExist);
bo,
lock.getTxnID(),
lock.getPrimary(),
callerStartTS,
currentTS,
rollbackIfNotExist,
lock);
} catch (TxnNotFoundException e) {
// If the error is something other than txnNotFoundErr, throw the error (network
// unavailable, tikv down, backoff timeout etc) to the caller.
Expand Down Expand Up @@ -294,7 +300,8 @@ private TxnStatus getTxnStatus(
ByteString primary,
Long callerStartTS,
Long currentTS,
boolean rollbackIfNotExist) {
boolean rollbackIfNotExist,
Lock lock) {
TxnStatus status = getResolved(txnID);
if (status != null) {
return status;
Expand Down Expand Up @@ -378,7 +385,25 @@ private TxnStatus getTxnStatus(
status.setTtl(resp.getLockTtl());
} else {
status.setCommitTS(resp.getCommitVersion());
saveResolved(txnID, status);

// If the transaction is still valid with ttl greater than zero, do nothing.
// If its status is certain:
// If transaction is already committed, the result could be cached.
// Otherwise:
// 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, todo.
// 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 and will NOT
// change.
if (status.isCommitted()
|| (lock != null
&& lock.getLockType() != org.tikv.kvproto.Kvrpcpb.Op.PessimisticLock)) {
saveResolved(txnID, status);
}
}

return status;
Expand Down

0 comments on commit e9bd049

Please sign in to comment.