Skip to content

Commit

Permalink
Don't let pessimistic txn prewrite in the optimistic way (#584)
Browse files Browse the repository at this point in the history
* don't let pessimistic txn prewrite in the optimistic way even if it does not lock any key

Signed-off-by: ekexium <eke@fastmail.com>

* add a todo

Signed-off-by: ekexium <eke@fastmail.com>

Signed-off-by: ekexium <eke@fastmail.com>
  • Loading branch information
ekexium authored Sep 13, 2022
1 parent 426055b commit ffaaf71
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions txnkv/transaction/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,18 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
c.hasTriedOnePC = true
}

// if lazy uniqueness check is enabled in TiDB (@@constraint_check_in_place_pessimistic=0), for_update_ts might be
// zero for a pessimistic transaction. We set it to the start_ts to force the PrewritePessimistic path in TiKV.
// TODO: can we simply set for_update_ts = start_ts for all pessimistic transactions whose for_update_ts=0?
if c.forUpdateTS == 0 {
for i := 0; i < c.mutations.Len(); i++ {
if c.mutations.NeedConstraintCheckInPrewrite(i) {
c.forUpdateTS = c.startTS
break
}
}
}

// TODO(youjiali1995): It's better to use different maxSleep for different operations
// and distinguish permanent errors from temporary errors, for example:
// - If all PDs are down, all requests to PD will fail due to network error.
Expand Down Expand Up @@ -1442,6 +1454,7 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
}

start := time.Now()

err = c.prewriteMutations(bo, c.mutations)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions txnkv/transaction/prewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *twoPhaseCommitter) buildPrewriteRequest(batch batchMutations, txnSize u
m := batch.mutations
mutations := make([]*kvrpcpb.Mutation, m.Len())
pessimisticActions := make([]kvrpcpb.PrewriteRequest_PessimisticAction, m.Len())

for i := 0; i < m.Len(); i++ {
assertion := kvrpcpb.Assertion_None
if m.IsAssertExists(i) {
Expand Down

0 comments on commit ffaaf71

Please sign in to comment.