Skip to content

Commit 754a707

Browse files
ti-chi-botekexiumlcwangchao
authored
Allow configuring the policy when prewrite encounters lock (#1501) (#1946)
ref pingcap/tidb#58675 Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io> Signed-off-by: Chao Wang <cclcwangchao@hotmail.com> Co-authored-by: ekexium <eke@fastmail.com> Co-authored-by: Chao Wang <cclcwangchao@hotmail.com>
1 parent 5ad4b85 commit 754a707

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

txnkv/transaction/prewrite.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ func (action actionPrewrite) handleSingleBatch(
478478
zap.Uint64("session", c.sessionID),
479479
zap.Uint64("txnID", c.startTS),
480480
zap.Stringer("lock", lock),
481+
zap.Stringer("policy", c.txn.prewriteEncounterLockPolicy),
481482
)
482483
logged[lock.TxnID] = struct{}{}
483484
}
@@ -486,7 +487,8 @@ func (action actionPrewrite) handleSingleBatch(
486487
// Pessimistic transactions don't need such an optimization. If this key needs a pessimistic lock,
487488
// TiKV will return a PessimisticLockNotFound error directly if it encounters a different lock. Otherwise,
488489
// TiKV returns lock.TTL = 0, and we still need to resolve the lock.
489-
if lock.TxnID > c.startTS && !c.isPessimistic {
490+
if (lock.TxnID > c.startTS && !c.isPessimistic) ||
491+
c.txn.prewriteEncounterLockPolicy == NoResolvePolicy {
490492
return tikverr.NewErrWriteConflictWithArgs(
491493
c.startTS,
492494
lock.TxnID,

txnkv/transaction/txn.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,28 @@ type TxnOptions struct {
118118
PipelinedMemDB bool
119119
}
120120

121+
// PrewriteEncounterLockPolicy specifies the policy when prewrite encounters locks.
122+
type PrewriteEncounterLockPolicy int
123+
124+
const (
125+
// TryResolvePolicy is the default one: try to resolve those locks with smaller startTS.
126+
TryResolvePolicy PrewriteEncounterLockPolicy = iota
127+
// NoResolvePolicy means do not resolve, but return write conflict errors directly.
128+
// This can be used to let the upper layer choose to retry in pessimistic mode.
129+
NoResolvePolicy
130+
)
131+
132+
func (p PrewriteEncounterLockPolicy) String() string {
133+
switch p {
134+
case TryResolvePolicy:
135+
return "TryResolvePolicy"
136+
case NoResolvePolicy:
137+
return "NoResolvePolicy"
138+
default:
139+
return "Unknown"
140+
}
141+
}
142+
121143
// KVTxn contains methods to interact with a TiKV transaction.
122144
type KVTxn struct {
123145
snapshot *txnsnapshot.KVSnapshot
@@ -187,6 +209,8 @@ type KVTxn struct {
187209
firstAttemptTS uint64
188210
backoffCnt int
189211
}
212+
213+
prewriteEncounterLockPolicy PrewriteEncounterLockPolicy
190214
}
191215

192216
// NewTiKVTxn creates a new KVTxn.
@@ -513,6 +537,11 @@ func (txn *KVTxn) SetAssertionLevel(assertionLevel kvrpcpb.AssertionLevel) {
513537
txn.assertionLevel = assertionLevel
514538
}
515539

540+
// SetPrewriteEncounterLockPolicy specifies the behavior when prewrite encounters locks.
541+
func (txn *KVTxn) SetPrewriteEncounterLockPolicy(policy PrewriteEncounterLockPolicy) {
542+
txn.prewriteEncounterLockPolicy = policy
543+
}
544+
516545
// IsPessimistic returns true if it is pessimistic.
517546
func (txn *KVTxn) IsPessimistic() bool {
518547
return txn.isPessimistic

0 commit comments

Comments
 (0)