Skip to content
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
41 changes: 41 additions & 0 deletions integration_tests/async_commit_fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"bytes"
"context"
"fmt"
"math"
"sort"
"sync/atomic"
"testing"
Expand All @@ -48,9 +49,11 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/suite"
tikverr "github.com/tikv/client-go/v2/error"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
"github.com/tikv/client-go/v2/txnkv/transaction"
"github.com/tikv/client-go/v2/util"
)

Expand Down Expand Up @@ -342,3 +345,41 @@ func (s *testAsyncCommitFailSuite) TestConfirmedFallbackWithRPCError() {
s.False(txn.GetCommitter().IsAsyncCommit())
s.Nil(txn.GetCommitter().GetUndeterminedErr())
}

type maxTxnTimeExpiredOracle struct {
oracle.Oracle
}

func (o maxTxnTimeExpiredOracle) IsExpired(lockTS, ttl uint64, opt *oracle.Option) bool {
if ttl == transaction.MaxTxnTimeUse {
return true
}
return o.Oracle.IsExpired(lockTS, ttl, opt)
}

func (s *testAsyncCommitFailSuite) TestAsyncCommitTooOldTxnNotPrewritten() {
baseOracle := s.store.GetOracle()
s.store.SetOracle(maxTxnTimeExpiredOracle{Oracle: baseOracle})
defer s.store.SetOracle(baseOracle)

txn := s.beginAsyncCommit()
s.Nil(txn.Set(s.key("a"), []byte("a")))
s.Nil(txn.Set(s.key("b"), []byte("b")))
var prewrites atomic.Int32
txn.SetRPCInterceptor(interceptor.NewRPCInterceptor("count-prewrite", func(next interceptor.RPCInterceptorFunc) interceptor.RPCInterceptorFunc {
return func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
if req.Type == tikvrpc.CmdPrewrite {
prewrites.Add(1)
}
return next(target, req)
}
}))
ctx := context.WithValue(context.Background(), util.SessionID, uint64(1))
err := txn.Commit(ctx)
s.ErrorContains(err, "txn takes too much time")
s.Equal(int32(0), prewrites.Load())

s.store.SetOracle(baseOracle)
s.mustGetNoneFromSnapshot(math.MaxUint64, s.key("a"))
s.mustGetNoneFromSnapshot(math.MaxUint64, s.key("b"))
}
7 changes: 6 additions & 1 deletion txnkv/transaction/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,11 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
}
}

// An async commit transaction is committed once its prewrite succeeds, so its age must be checked before that.
if c.isAsyncCommit() && c.store.GetOracle().IsExpired(c.startTS, MaxTxnTimeUse, &oracle.Option{TxnScope: oracle.GlobalTxnScope}) {
return errors.Errorf("session %d txn takes too much time, txnStartTS: %d", c.sessionID, c.startTS)
}

if c.sessionID > 0 {
util.EvalFailpoint("beforePrewrite")
}
Expand Down Expand Up @@ -1994,7 +1999,7 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
}
atomic.StoreUint64(&c.commitTS, commitTS)

if c.store.GetOracle().IsExpired(c.startTS, MaxTxnTimeUse, &oracle.Option{TxnScope: oracle.GlobalTxnScope}) {
if !c.isAsyncCommit() && c.store.GetOracle().IsExpired(c.startTS, MaxTxnTimeUse, &oracle.Option{TxnScope: oracle.GlobalTxnScope}) {
err = errors.Errorf("session %d txn takes too much time, txnStartTS: %d, comm: %d",
c.sessionID, c.startTS, c.commitTS)
return err
Expand Down