Skip to content

Commit

Permalink
txn: fix the resolved txn status cache for pessimistic txn (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsishandsome authored Dec 16, 2020
1 parent 4c22724 commit afe381d
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,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 @@ -270,7 +276,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 @@ -346,9 +353,26 @@ private TxnStatus getTxnStatus(
status = new TxnStatus(resp.getLockTtl(), 0L, resp.getAction());
} else {
status = new TxnStatus(0L, resp.getCommitVersion(), resp.getAction());
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 afe381d

Please sign in to comment.