Skip to content

Commit

Permalink
Merge branch 'delay-before-not-null' of https://github.com/sticnarf/tidb
Browse files Browse the repository at this point in the history
 into delay-before-not-null
  • Loading branch information
sticnarf committed Mar 17, 2021
2 parents 383e6ea + 597064f commit 9bd964a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
16 changes: 15 additions & 1 deletion store/driver/tikv_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
13 changes: 11 additions & 2 deletions store/mockstore/unistore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions store/tikv/2pc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 7 additions & 21 deletions store/tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,43 +167,29 @@ 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)
}
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)
}
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)
Expand Down

0 comments on commit 9bd964a

Please sign in to comment.