From 1970a917c175665c3510ea57a1ea1d417e34f4ee Mon Sep 17 00:00:00 2001 From: Yilin Chen Date: Wed, 24 Feb 2021 21:04:03 +0800 Subject: [PATCH] store/tikv: fix unstable tests in snapshot_fail_test.go (#22916) --- session/session_fail_test.go | 11 +++++++---- store/gcworker/gc_worker_test.go | 3 ++- store/tikv/snapshot_fail_test.go | 27 ++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/session/session_fail_test.go b/session/session_fail_test.go index dec1738547672..8bf7e277cad57 100644 --- a/session/session_fail_test.go +++ b/session/session_fail_test.go @@ -111,10 +111,13 @@ func (s *testSessionSerialSuite) TestAutoCommitNeedNotLinearizability(c *C) { tk.MustExec(`create table t1 (c int)`) c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/getMinCommitTSFromTSO", `panic`), IsNil) - defer failpoint.Disable("github.com/pingcap/tidb/store/tikv/getMinCommitTSFromTSO") + defer func() { + c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/getMinCommitTSFromTSO"), IsNil) + }() + + c.Assert(tk.Se.GetSessionVars().SetSystemVar("tidb_enable_async_commit", "1"), IsNil) + c.Assert(tk.Se.GetSessionVars().SetSystemVar("tidb_guarantee_linearizability", "1"), IsNil) - tk.Se.GetSessionVars().SetSystemVar("tidb_enable_async_commit", "1") - tk.Se.GetSessionVars().SetSystemVar("tidb_guarantee_linearizability", "1") // Auto-commit transactions don't need to get minCommitTS from TSO tk.MustExec("INSERT INTO t1 VALUES (1)") @@ -141,7 +144,7 @@ func (s *testSessionSerialSuite) TestAutoCommitNeedNotLinearizability(c *C) { // Same for 1PC tk.MustExec("set autocommit = 1") - tk.Se.GetSessionVars().SetSystemVar("tidb_enable_1pc", "1") + c.Assert(tk.Se.GetSessionVars().SetSystemVar("tidb_enable_1pc", "1"), IsNil) tk.MustExec("INSERT INTO t1 VALUES (4)") tk.MustExec("BEGIN") diff --git a/store/gcworker/gc_worker_test.go b/store/gcworker/gc_worker_test.go index 985f070909b3e..d8c07f6ebfa99 100644 --- a/store/gcworker/gc_worker_test.go +++ b/store/gcworker/gc_worker_test.go @@ -924,10 +924,11 @@ func (s *testGCWorkerSuite) TestResolveLockRangeMeetRegionEnlargeCausedByRegionM mCluster := s.cluster.(*mocktikv.Cluster) mCluster.Merge(s.initRegion.regionID, region2) regionMeta, _ := mCluster.GetRegion(s.initRegion.regionID) - s.tikvStore.GetRegionCache().OnRegionEpochNotMatch( + err := s.tikvStore.GetRegionCache().OnRegionEpochNotMatch( tikv.NewNoopBackoff(context.Background()), &tikv.RPCContext{Region: regionID, Store: &tikv.Store{}}, []*metapb.Region{regionMeta}) + c.Assert(err, IsNil) // also let region1 contains all 4 locks s.gcWorker.testingKnobs.scanLocks = func(key []byte, regionID uint64) []*tikv.Lock { if regionID == s.initRegion.regionID { diff --git a/store/tikv/snapshot_fail_test.go b/store/tikv/snapshot_fail_test.go index b76b6ccf8f11f..0ee262f6928dc 100644 --- a/store/tikv/snapshot_fail_test.go +++ b/store/tikv/snapshot_fail_test.go @@ -43,11 +43,27 @@ func (s *testSnapshotFailSuite) TearDownSuite(c *C) { s.OneByOneSuite.TearDownSuite(c) } +func (s *testSnapshotFailSuite) cleanup(c *C) { + txn, err := s.store.Begin() + c.Assert(err, IsNil) + iter, err := txn.Iter(kv.Key(""), kv.Key("")) + c.Assert(err, IsNil) + for iter.Valid() { + err = txn.Delete(iter.Key()) + c.Assert(err, IsNil) + err = iter.Next() + c.Assert(err, IsNil) + } + c.Assert(txn.Commit(context.TODO()), IsNil) +} + func (s *testSnapshotFailSuite) TestBatchGetResponseKeyError(c *C) { // Meaningless to test with tikv because it has a mock key error if *WithTiKV { return } + defer s.cleanup(c) + // Put two KV pairs txn, err := s.store.Begin() c.Assert(err, IsNil) @@ -75,6 +91,8 @@ func (s *testSnapshotFailSuite) TestScanResponseKeyError(c *C) { if *WithTiKV { return } + defer s.cleanup(c) + // Put two KV pairs txn, err := s.store.Begin() c.Assert(err, IsNil) @@ -120,6 +138,8 @@ func (s *testSnapshotFailSuite) TestScanResponseKeyError(c *C) { } func (s *testSnapshotFailSuite) TestRetryPointGetWithTS(c *C) { + defer s.cleanup(c) + snapshot := s.store.GetSnapshot(kv.MaxVersion) c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/snapshotGetTSAsync", `pause`), IsNil) ch := make(chan error) @@ -138,13 +158,14 @@ func (s *testSnapshotFailSuite) TestRetryPointGetWithTS(c *C) { c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/asyncCommitDoNothing", `return`), IsNil) committer, err := newTwoPhaseCommitterWithInit(txn.(*tikvTxn), 1) c.Assert(err, IsNil) - // Sets its minCommitTS to a large value, so the lock can be actually ignored. - committer.minCommitTS = committer.startTS + (1 << 28) + // Sets its minCommitTS to one second later, so the lock will be ignored by point get. + committer.minCommitTS = committer.startTS + (1000 << 18) err = committer.execute(context.Background()) c.Assert(err, IsNil) - c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/asyncCommitDoNothing"), IsNil) c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/snapshotGetTSAsync"), IsNil) err = <-ch c.Assert(err, ErrorMatches, ".*key not exist") + + c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/asyncCommitDoNothing"), IsNil) }