Skip to content

Commit

Permalink
use failpoints to totally prevent the testcase be affected by time
Browse files Browse the repository at this point in the history
  • Loading branch information
longfangsong committed May 12, 2021
1 parent df28d50 commit afd9044
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
33 changes: 13 additions & 20 deletions store/tikv/extract_start_ts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
package tikv

import (
"context"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore/unistore"
Expand All @@ -28,7 +27,7 @@ type extractStartTsSuite struct {
store *KVStore
}

var _ = Suite(&extractStartTsSuite{})
var _ = SerialSuites(&extractStartTsSuite{})

func (s *extractStartTsSuite) SetUpTest(c *C) {
client, pdClient, cluster, err := unistore.New("")
Expand Down Expand Up @@ -63,8 +62,10 @@ func (s *extractStartTsSuite) SetUpTest(c *C) {

func (s *extractStartTsSuite) TestExtractStartTs(c *C) {
i := uint64(100)
bo := NewBackofferWithVars(context.Background(), tsoMaxBackoff, nil)
stalenessTimestamp, _ := s.store.getStalenessTimestamp(bo, oracle.GlobalTxnScope, 100)
// to prevent time change during test case execution
// we use failpoint to make it "fixed"
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/MockStalenessTimestamp", "return(200)"), IsNil)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/MockCurrentTimestamp", `return(300)`), IsNil)

cases := []struct {
expectedTS uint64
Expand All @@ -73,34 +74,26 @@ func (s *extractStartTsSuite) TestExtractStartTs(c *C) {
// StartTS setted
{100, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: &i, PrevSec: nil, MinStartTS: nil, MaxPrevSec: nil}},
// PrevSec setted
{stalenessTimestamp, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: &i, MinStartTS: nil, MaxPrevSec: nil}},
{200, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: &i, MinStartTS: nil, MaxPrevSec: nil}},
// MinStartTS setted, global
{101, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: &i, MaxPrevSec: nil}},
// MinStartTS setted, local
{102, kv.TransactionOption{TxnScope: oracle.LocalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: &i, MaxPrevSec: nil}},
// MaxPrevSec setted
// however we need to add more cases to check the behavior when it fall backs to MinStartTS setted
// see `TestMaxPrevSecFallback`
{stalenessTimestamp, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: nil, MaxPrevSec: &i}},
{200, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: nil, MaxPrevSec: &i}},
// nothing setted
{0, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: nil, MaxPrevSec: nil}},
{300, kv.TransactionOption{TxnScope: oracle.GlobalTxnScope, StartTS: nil, PrevSec: nil, MinStartTS: nil, MaxPrevSec: nil}},
}
for _, cs := range cases {
expected := cs.expectedTS
result, _ := extractStartTs(s.store, cs.option)
if expected == 0 {
c.Assert(result, Greater, stalenessTimestamp)
} else if expected == stalenessTimestamp {
// "stalenessTimestamp" fetched by extractStartTs can be later than stalenessTimestamp fetched in this function
// because it *is* created in later physical time
c.Assert(result, GreaterEqual, expected)
// but it should not be late too much
maxStalenessTimestamp := oracle.ComposeTS(oracle.ExtractPhysical(stalenessTimestamp)+50, oracle.ExtractLogical(stalenessTimestamp))
c.Assert(result, Less, maxStalenessTimestamp)
} else {
c.Assert(result, Equals, expected)
}
c.Assert(result, Equals, expected)
}

c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/MockStalenessTimestamp"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/MockCurrentTimestamp"), IsNil)
}

func (s *extractStartTsSuite) TestMaxPrevSecFallback(c *C) {
Expand Down
14 changes: 14 additions & 0 deletions store/tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ func (s *KVStore) CurrentTimestamp(txnScope string) (uint64, error) {
}

func (s *KVStore) getTimestampWithRetry(bo *Backoffer, txnScope string) (uint64, error) {
failpoint.Inject("MockCurrentTimestamp", func(val failpoint.Value) {
if v, ok := val.(int); ok {
failpoint.Return(uint64(v), nil)
} else {
panic("MockCurrentTimestamp should be a number, try use this failpoint with \"return(ts)\"")
}
})
if span := opentracing.SpanFromContext(bo.ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("TiKVStore.getTimestampWithRetry", opentracing.ChildOf(span.Context()))
defer span1.Finish()
Expand Down Expand Up @@ -264,6 +271,13 @@ func (s *KVStore) getTimestampWithRetry(bo *Backoffer, txnScope string) (uint64,
}

func (s *KVStore) getStalenessTimestamp(bo *Backoffer, txnScope string, prevSec uint64) (uint64, error) {
failpoint.Inject("MockStalenessTimestamp", func(val failpoint.Value) {
if v, ok := val.(int); ok {
failpoint.Return(uint64(v), nil)
} else {
panic("MockStalenessTimestamp should be a number, try use this failpoint with \"return(ts)\"")
}
})
for {
startTS, err := s.oracle.GetStaleTimestamp(bo.ctx, txnScope, prevSec)
if err == nil {
Expand Down

0 comments on commit afd9044

Please sign in to comment.