Skip to content
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

Allow configuring the policy when prewrite encounters lock #1501

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion txnkv/transaction/prewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ func (action actionPrewrite) handleSingleBatch(
zap.Uint64("session", c.sessionID),
zap.Uint64("txnID", c.startTS),
zap.Stringer("lock", lock),
zap.Stringer("policy", c.txn.prewriteEncounterLockPolicy),
)
logged[lock.TxnID] = struct{}{}
}
Expand All @@ -457,7 +458,8 @@ func (action actionPrewrite) handleSingleBatch(
// Pessimistic transactions don't need such an optimization. If this key needs a pessimistic lock,
// TiKV will return a PessimisticLockNotFound error directly if it encounters a different lock. Otherwise,
// TiKV returns lock.TTL = 0, and we still need to resolve the lock.
if lock.TxnID > c.startTS && !c.isPessimistic {
if (lock.TxnID > c.startTS && !c.isPessimistic) ||
c.txn.prewriteEncounterLockPolicy == NoResolvePolicy {
return tikverr.NewErrWriteConflictWithArgs(
c.startTS,
lock.TxnID,
Expand Down
29 changes: 29 additions & 0 deletions txnkv/transaction/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ type TxnOptions struct {
PipelinedMemDB bool
}

// PrewriteEncounterLockPolicy specifies the policy when prewrite encounters locks.
type PrewriteEncounterLockPolicy int

const (
// TryResolvePolicy is the default one: try to resolve those locks with smaller startTS.
TryResolvePolicy PrewriteEncounterLockPolicy = iota
// NoResolvePolicy means do not resolve, but return write conflict errors directly.
// This can be used to let the upper layer choose to retry in pessimistic mode.
NoResolvePolicy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the specific design for using the policy at the upper layer? Is it that autocommit optimistic transactions use NoResolvePolicy while everything else remains unchanged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autocommit optimistic transactions use NoResolvePolicy while everything else remains unchanged

Yes

)

func (p PrewriteEncounterLockPolicy) String() string {
switch p {
case TryResolvePolicy:
return "TryResolvePolicy"
case NoResolvePolicy:
return "NoResolvePolicy"
default:
return "Unknown"
}
}

// KVTxn contains methods to interact with a TiKV transaction.
type KVTxn struct {
snapshot *txnsnapshot.KVSnapshot
Expand Down Expand Up @@ -172,6 +194,8 @@ type KVTxn struct {

isPipelined bool
pipelinedCancel context.CancelFunc

prewriteEncounterLockPolicy PrewriteEncounterLockPolicy
}

// NewTiKVTxn creates a new KVTxn.
Expand Down Expand Up @@ -472,6 +496,11 @@ func (txn *KVTxn) SetAssertionLevel(assertionLevel kvrpcpb.AssertionLevel) {
txn.assertionLevel = assertionLevel
}

// SetPrewriteEncounterLockPolicy specifies the behavior when prewrite encounters locks.
func (txn *KVTxn) SetPrewriteEncounterLockPolicy(policy PrewriteEncounterLockPolicy) {
txn.prewriteEncounterLockPolicy = policy
}

// IsPessimistic returns true if it is pessimistic.
func (txn *KVTxn) IsPessimistic() bool {
return txn.isPessimistic
Expand Down
Loading