Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/tikv: move safepoint test to /tests #23637

Merged
merged 9 commits into from
Mar 30, 2021
Merged
22 changes: 11 additions & 11 deletions store/tikv/safepoint_test.go → store/tikv/tests/safepoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,20 +22,21 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/parser/terror"
tidbkv "github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/kv"
)

type testSafePointSuite struct {
OneByOneSuite
store *KVStore
store tikv.StoreProbe
prefix string
}

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())
}

Expand All @@ -45,7 +46,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
Expand All @@ -62,9 +63,9 @@ func mymakeKeys(rowNum int, prefix string) []tidbkv.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
Expand All @@ -87,7 +88,7 @@ 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)
Expand All @@ -99,7 +100,7 @@ func (s *testSafePointSuite) TestSafePoint(c *C) {
// 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)
Expand All @@ -112,10 +113,9 @@ 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), kv.ErrGCTooEarly)
isMayFallBehind = terror.ErrorEqual(errors.Cause(geterr2), kv.ErrPDServerTimeout.GenWithStackByArgs("start timestamp may fall behind safe point"))
Expand Down
16 changes: 16 additions & 0 deletions store/tikv/tests/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package tikv_test
import (
"context"
"flag"
"fmt"
"strings"
"sync"

Expand All @@ -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"
)

Expand Down Expand Up @@ -98,3 +100,17 @@ func (s *OneByOneSuite) TearDownSuite(c *C) {
withTiKVGlobalLock.RUnlock()
}
}

func encodeKey(prefix, s string) []byte {
Copy link
Contributor

@AndreMouche AndreMouche Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicate function makes me unhappy to give LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove one of them in the end.

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)
}