diff --git a/store/driver/tikv_driver.go b/store/driver/tikv_driver.go index ad0ce7078ee50..af533a19c8f6f 100644 --- a/store/driver/tikv_driver.go +++ b/store/driver/tikv_driver.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/tidb/store/gcworker" "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/store/tikv/config" + "github.com/pingcap/tidb/store/tikv/oracle" "github.com/pingcap/tidb/util/execdetails" "github.com/pingcap/tidb/util/logutil" pd "github.com/tikv/pd/client" @@ -304,10 +305,23 @@ func (s *tikvStore) Begin() (kv.Transaction, error) { // BeginWithOption begins a transaction with given option func (s *tikvStore) BeginWithOption(option kv.TransactionOption) (kv.Transaction, error) { - txn, err := s.KVStore.BeginWithOption(option) + txnScope := option.TxnScope + if txnScope == "" { + txnScope = oracle.GlobalTxnScope + } + var txn *tikv.KVTxn + var err error + if option.StartTS != nil { + txn, err = s.BeginWithStartTS(txnScope, *option.StartTS) + } else if option.PrevSec != nil { + txn, err = s.BeginWithExactStaleness(txnScope, *option.PrevSec) + } else { + txn, err = s.BeginWithTxnScope(txnScope) + } if err != nil { return nil, errors.Trace(err) } + return txn_driver.NewTiKVTxn(txn), err } diff --git a/store/mockstore/unistore.go b/store/mockstore/unistore.go index 6842fb59f8d9f..f1b68d6600256 100644 --- a/store/mockstore/unistore.go +++ b/store/mockstore/unistore.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/store/mockstore/unistore" "github.com/pingcap/tidb/store/tikv" "github.com/pingcap/tidb/store/tikv/config" + "github.com/pingcap/tidb/store/tikv/oracle" "github.com/pingcap/tidb/util/execdetails" ) @@ -97,8 +98,16 @@ func (s *mockStorage) Begin() (kv.Transaction, error) { // BeginWithOption begins a transaction with given option func (s *mockStorage) BeginWithOption(option kv.TransactionOption) (kv.Transaction, error) { - txn, err := s.KVStore.BeginWithOption(option) - return newTiKVTxn(txn, err) + txnScope := option.TxnScope + if txnScope == "" { + txnScope = oracle.GlobalTxnScope + } + if option.StartTS != nil { + return newTiKVTxn(s.BeginWithStartTS(txnScope, *option.StartTS)) + } else if option.PrevSec != nil { + return newTiKVTxn(s.BeginWithExactStaleness(txnScope, *option.PrevSec)) + } + return newTiKVTxn(s.BeginWithTxnScope(txnScope)) } // GetSnapshot gets a snapshot that is able to read any data which data is <= ver. diff --git a/store/tikv/2pc_test.go b/store/tikv/2pc_test.go index ad89dbc48e0a7..3541c7bb3cd00 100644 --- a/store/tikv/2pc_test.go +++ b/store/tikv/2pc_test.go @@ -599,12 +599,12 @@ func (s *testCommitterSuite) TestRejectCommitTS(c *C) { // Use max.Uint64 to read the data and success. // That means the final commitTS > startTS+2, it's not the one we provide. // So we cover the rety commitTS logic. - txn1, err := s.store.BeginWithOption(kv.TransactionOption{}.SetTxnScope(oracle.GlobalTxnScope).SetStartTs(committer.startTS + 2)) + txn1, err := s.store.BeginWithStartTS(oracle.GlobalTxnScope, committer.startTS+2) c.Assert(err, IsNil) _, err = txn1.Get(bo.ctx, []byte("x")) c.Assert(kv.IsErrNotFound(err), IsTrue) - txn2, err := s.store.BeginWithOption(kv.TransactionOption{}.SetTxnScope(oracle.GlobalTxnScope).SetStartTs(math.MaxUint64)) + txn2, err := s.store.BeginWithStartTS(oracle.GlobalTxnScope, math.MaxUint64) c.Assert(err, IsNil) val, err := txn2.Get(bo.ctx, []byte("x")) c.Assert(err, IsNil) diff --git a/store/tikv/kv.go b/store/tikv/kv.go index 549b3211d4c53..6930f59d6a1ef 100644 --- a/store/tikv/kv.go +++ b/store/tikv/kv.go @@ -167,25 +167,11 @@ func (s *KVStore) runSafePointChecker() { // Begin a global transaction. func (s *KVStore) Begin() (*KVTxn, error) { - return s.beginWithTxnScope(oracle.GlobalTxnScope) + return s.BeginWithTxnScope(oracle.GlobalTxnScope) } -// BeginWithOption begins a transaction with given option -func (s *KVStore) BeginWithOption(option kv.TransactionOption) (*KVTxn, error) { - txnScope := option.TxnScope - if txnScope == "" { - txnScope = oracle.GlobalTxnScope - } - if option.StartTS != nil { - return s.beginWithStartTS(txnScope, *option.StartTS) - } else if option.PrevSec != nil { - return s.beginWithExactStaleness(txnScope, *option.PrevSec) - } - return s.beginWithTxnScope(txnScope) -} - -// beginWithTxnScope begins a transaction with the given txnScope (local or global) -func (s *KVStore) beginWithTxnScope(txnScope string) (*KVTxn, error) { +// BeginWithTxnScope begins a transaction with the given txnScope (local or global) +func (s *KVStore) BeginWithTxnScope(txnScope string) (*KVTxn, error) { txn, err := newTiKVTxn(s, txnScope) if err != nil { return nil, errors.Trace(err) @@ -193,8 +179,8 @@ func (s *KVStore) beginWithTxnScope(txnScope string) (*KVTxn, error) { return txn, nil } -// beginWithStartTS begins a transaction with startTS. -func (s *KVStore) beginWithStartTS(txnScope string, startTS uint64) (*KVTxn, error) { +// BeginWithStartTS begins a transaction with startTS. +func (s *KVStore) BeginWithStartTS(txnScope string, startTS uint64) (*KVTxn, error) { txn, err := newTiKVTxnWithStartTS(s, txnScope, startTS, s.nextReplicaReadSeed()) if err != nil { return nil, errors.Trace(err) @@ -202,8 +188,8 @@ func (s *KVStore) beginWithStartTS(txnScope string, startTS uint64) (*KVTxn, err return txn, nil } -// beginWithExactStaleness begins transaction with given staleness -func (s *KVStore) beginWithExactStaleness(txnScope string, prevSec uint64) (*KVTxn, error) { +// BeginWithExactStaleness begins transaction with given staleness +func (s *KVStore) BeginWithExactStaleness(txnScope string, prevSec uint64) (*KVTxn, error) { txn, err := newTiKVTxnWithExactStaleness(s, txnScope, prevSec) if err != nil { return nil, errors.Trace(err)