From 541c14754affc5cbc604fb4f6b6c96332c18f179 Mon Sep 17 00:00:00 2001 From: disksing Date: Mon, 29 Mar 2021 20:00:33 +0800 Subject: [PATCH 1/2] store/tikv: move safepoint test to /tests Signed-off-by: disksing --- store/tikv/{ => tests}/safepoint_test.go | 34 ++++++++++++------------ 1 file changed, 17 insertions(+), 17 deletions(-) rename store/tikv/{ => tests}/safepoint_test.go (68%) diff --git a/store/tikv/safepoint_test.go b/store/tikv/tests/safepoint_test.go similarity index 68% rename from store/tikv/safepoint_test.go rename to store/tikv/tests/safepoint_test.go index b1cea4bf6e5bb..bfce973042c04 100644 --- a/store/tikv/safepoint_test.go +++ b/store/tikv/tests/safepoint_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package tikv +package tikv_test import ( "context" @@ -22,11 +22,12 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/parser/terror" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/store/tikv" ) type testSafePointSuite struct { OneByOneSuite - store *KVStore + store tikv.StoreProbe prefix string } @@ -34,7 +35,7 @@ var _ = Suite(&testSafePointSuite{}) func (s *testSafePointSuite) SetUpSuite(c *C) { s.OneByOneSuite.SetUpSuite(c) - s.store = NewTestStore(c) + s.store = tikv.StoreProbe{KVStore: NewTestStore(c)} s.prefix = fmt.Sprintf("seek_%d", time.Now().Unix()) } @@ -44,7 +45,7 @@ func (s *testSafePointSuite) TearDownSuite(c *C) { s.OneByOneSuite.TearDownSuite(c) } -func (s *testSafePointSuite) beginTxn(c *C) *KVTxn { +func (s *testSafePointSuite) beginTxn(c *C) tikv.TxnProbe { txn, err := s.store.Begin() c.Assert(err, IsNil) return txn @@ -61,9 +62,9 @@ func mymakeKeys(rowNum int, prefix string) []kv.Key { func (s *testSafePointSuite) waitUntilErrorPlugIn(t uint64) { for { - saveSafePoint(s.store.GetSafePointKV(), t+10) + s.store.SaveSafePoint(t + 10) cachedTime := time.Now() - newSafePoint, err := loadSafePoint(s.store.GetSafePointKV()) + newSafePoint, err := s.store.LoadSafePoint() if err == nil { s.store.UpdateSPCache(newSafePoint, cachedTime) break @@ -86,24 +87,24 @@ func (s *testSafePointSuite) TestSafePoint(c *C) { _, err = txn2.Get(context.TODO(), encodeKey(s.prefix, s08d("key", 0))) c.Assert(err, IsNil) - s.waitUntilErrorPlugIn(txn2.startTS) + s.waitUntilErrorPlugIn(txn2.StartTS()) _, geterr2 := txn2.Get(context.TODO(), encodeKey(s.prefix, s08d("key", 0))) c.Assert(geterr2, NotNil) - isFallBehind := terror.ErrorEqual(errors.Cause(geterr2), ErrGCTooEarly) - isMayFallBehind := terror.ErrorEqual(errors.Cause(geterr2), ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) + isFallBehind := terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrGCTooEarly) + isMayFallBehind := terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) isBehind := isFallBehind || isMayFallBehind c.Assert(isBehind, IsTrue) // for txn seek txn3 := s.beginTxn(c) - s.waitUntilErrorPlugIn(txn3.startTS) + s.waitUntilErrorPlugIn(txn3.StartTS()) _, seekerr := txn3.Iter(encodeKey(s.prefix, ""), nil) c.Assert(seekerr, NotNil) - isFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrGCTooEarly) - isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) + isFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrGCTooEarly) + isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) isBehind = isFallBehind || isMayFallBehind c.Assert(isBehind, IsTrue) @@ -111,13 +112,12 @@ func (s *testSafePointSuite) TestSafePoint(c *C) { keys := mymakeKeys(10, s.prefix) txn4 := s.beginTxn(c) - s.waitUntilErrorPlugIn(txn4.startTS) + s.waitUntilErrorPlugIn(txn4.StartTS()) - snapshot := newTiKVSnapshot(s.store, txn4.StartTS(), 0) - _, batchgeterr := snapshot.BatchGet(context.Background(), keys) + _, batchgeterr := txn4.BatchGet(context.Background(), keys) c.Assert(batchgeterr, NotNil) - isFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrGCTooEarly) - isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) + isFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrGCTooEarly) + isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), tikv.ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point")) isBehind = isFallBehind || isMayFallBehind c.Assert(isBehind, IsTrue) } From f01535ff5ceb9e12f0eb52536c63c96ed5541723 Mon Sep 17 00:00:00 2001 From: disksing Date: Mon, 29 Mar 2021 20:09:09 +0800 Subject: [PATCH 2/2] fix build Signed-off-by: disksing --- store/tikv/tests/util_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/store/tikv/tests/util_test.go b/store/tikv/tests/util_test.go index 4189934b1d7c1..e02b84b041a3e 100644 --- a/store/tikv/tests/util_test.go +++ b/store/tikv/tests/util_test.go @@ -16,6 +16,7 @@ package tikv_test import ( "context" "flag" + "fmt" "strings" "sync" @@ -24,6 +25,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/util/codec" pd "github.com/tikv/pd/client" ) @@ -98,3 +100,17 @@ func (s *OneByOneSuite) TearDownSuite(c *C) { withTiKVGlobalLock.RUnlock() } } + +func encodeKey(prefix, s string) []byte { + return codec.EncodeBytes(nil, []byte(fmt.Sprintf("%s_%s", prefix, s))) +} + +func valueBytes(n int) []byte { + return []byte(fmt.Sprintf("value%d", n)) +} + +// s08d is for returning format string "%s%08d" to keep string sorted. +// e.g.: "0002" < "0011", otherwise "2" > "11" +func s08d(prefix string, n int) string { + return fmt.Sprintf("%s%08d", prefix, n) +}