Skip to content

Commit

Permalink
kv: remove errSavepointInvalidAfterTxnRestart
Browse files Browse the repository at this point in the history
This commit simplifies logic in `checkSavepointLocked`.

Epic: None
Release note: None
  • Loading branch information
nvanbenschoten committed Aug 9, 2023
1 parent c5b6392 commit 7f6f9c3
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions pkg/kv/kvclient/kvcoord/txn_coord_sender_savepoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)

// savepoint captures the state in the TxnCoordSender necessary to restore that
Expand Down Expand Up @@ -121,15 +122,8 @@ func (tc *TxnCoordSender) RollbackToSavepoint(ctx context.Context, s kv.Savepoin
}

sp := s.(*savepoint)
err := tc.checkSavepointLocked(sp)
err := tc.checkSavepointLocked(sp, "rollback to")
if err != nil {
if errors.Is(err, errSavepointInvalidAfterTxnRestart) {
err = kvpb.NewTransactionRetryWithProtoRefreshError(
"cannot rollback to savepoint after a transaction restart",
tc.mu.txn.ID,
tc.mu.txn,
)
}
return err
}

Expand Down Expand Up @@ -165,15 +159,7 @@ func (tc *TxnCoordSender) ReleaseSavepoint(ctx context.Context, s kv.SavepointTo
}

sp := s.(*savepoint)
err := tc.checkSavepointLocked(sp)
if errors.Is(err, errSavepointInvalidAfterTxnRestart) {
err = kvpb.NewTransactionRetryWithProtoRefreshError(
"cannot release savepoint after a transaction restart",
tc.mu.txn.ID,
tc.mu.txn,
)
}
return err
return tc.checkSavepointLocked(sp, "release")
}

type errSavepointOperationInErrorTxn struct{}
Expand All @@ -193,23 +179,22 @@ func (tc *TxnCoordSender) assertNotFinalized() error {
return nil
}

var errSavepointInvalidAfterTxnRestart = errors.New("savepoint invalid after transaction restart")

// checkSavepointLocked checks whether the provided savepoint is still valid.
// Returns errSavepointInvalidAfterTxnRestart if the savepoint is not an
// Returns a TransactionRetryWithProtoRefreshError if the savepoint is not an
// "initial" one and the transaction has restarted since the savepoint was
// created.
func (tc *TxnCoordSender) checkSavepointLocked(s *savepoint) error {
func (tc *TxnCoordSender) checkSavepointLocked(s *savepoint, op redact.SafeString) error {
// Only savepoints taken before any activity are allowed to be used after a
// transaction restart.
if s.Initial() {
return nil
}
if s.txnID != tc.mu.txn.ID {
return errSavepointInvalidAfterTxnRestart
}
if s.epoch != tc.mu.txn.Epoch {
return errSavepointInvalidAfterTxnRestart
if s.txnID != tc.mu.txn.ID || s.epoch != tc.mu.txn.Epoch {
return kvpb.NewTransactionRetryWithProtoRefreshError(
redact.Sprintf("cannot %s savepoint after a transaction restart", op),
s.txnID,
tc.mu.txn,
)
}

if s.seqNum < 0 || s.seqNum > tc.interceptorAlloc.txnSeqNumAllocator.writeSeq {
Expand Down

0 comments on commit 7f6f9c3

Please sign in to comment.