Skip to content

Commit

Permalink
test: try to stablize scan test (#19242)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored Aug 20, 2020
1 parent 47526d3 commit d8b37f6
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions store/tikv/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,41 @@
package tikv

import (
"bytes"
"context"
"fmt"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/rowcodec"
"go.uber.org/zap"
)

type testScanSuite struct {
OneByOneSuite
store *tikvStore
prefix string
rowNums []int
store *tikvStore
recordPrefix []byte
rowNums []int
ctx context.Context
}

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

func (s *testScanSuite) SetUpSuite(c *C) {
s.OneByOneSuite.SetUpSuite(c)
s.store = NewTestStore(c).(*tikvStore)
s.prefix = fmt.Sprintf("seek_%d", time.Now().Unix())
s.recordPrefix = tablecodec.GenTableRecordPrefix(1)
s.rowNums = append(s.rowNums, 1, scanBatchSize, scanBatchSize+1, scanBatchSize*3)
// Avoid using async commit logic.
s.ctx = context.WithValue(context.Background(), sessionctx.ConnID, uint64(0))
}

func (s *testScanSuite) TearDownSuite(c *C) {
txn := s.beginTxn(c)
scanner, err := txn.Iter(encodeKey(s.prefix, ""), nil)
scanner, err := txn.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
c.Assert(scanner, NotNil)
for scanner.Valid() {
Expand All @@ -49,7 +57,7 @@ func (s *testScanSuite) TearDownSuite(c *C) {
c.Assert(err, IsNil)
scanner.Next()
}
err = txn.Commit(context.Background())
err = txn.Commit(s.ctx)
c.Assert(err, IsNil)
err = s.store.Close()
c.Assert(err, IsNil)
Expand All @@ -66,10 +74,20 @@ func (s *testScanSuite) TestScan(c *C) {
check := func(c *C, scan kv.Iterator, rowNum int, keyOnly bool) {
for i := 0; i < rowNum; i++ {
k := scan.Key()
c.Assert([]byte(k), BytesEquals, encodeKey(s.prefix, s08d("key", i)))
expectedKey := tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(i))
if ok := bytes.Equal([]byte(k), []byte(expectedKey)); !ok {
logutil.BgLogger().Error("bytes equal check fail",
zap.Int("i", i),
zap.Int("rowNum", rowNum),
zap.Stringer("obtained key", k),
zap.Stringer("obtained val", kv.Key(scan.Value())),
zap.Stringer("expected", expectedKey),
zap.Bool("keyOnly", keyOnly))
}
c.Assert([]byte(k), BytesEquals, []byte(expectedKey))
if !keyOnly {
v := scan.Value()
c.Assert(v, BytesEquals, valueBytes(i))
c.Assert(v, BytesEquals, genValueBytes(i))
}
// Because newScan return first item without calling scan.Next() just like go-hbase,
// for-loop count will decrease 1.
Expand All @@ -84,55 +102,61 @@ func (s *testScanSuite) TestScan(c *C) {
for _, rowNum := range s.rowNums {
txn := s.beginTxn(c)
for i := 0; i < rowNum; i++ {
err := txn.Set(encodeKey(s.prefix, s08d("key", i)), valueBytes(i))
err := txn.Set(tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(i)), genValueBytes(i))
c.Assert(err, IsNil)
}
err := txn.Commit(context.Background())
err := txn.Commit(s.ctx)
c.Assert(err, IsNil)

if rowNum > 123 {
_, err = s.store.SplitRegions(context.Background(), [][]byte{encodeKey(s.prefix, s08d("key", 123))}, false)
_, err = s.store.SplitRegions(s.ctx, [][]byte{tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(123))}, false)
c.Assert(err, IsNil)
}

if rowNum > 456 {
_, err = s.store.SplitRegions(context.Background(), [][]byte{encodeKey(s.prefix, s08d("key", 456))}, false)
_, err = s.store.SplitRegions(s.ctx, [][]byte{tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(456))}, false)
c.Assert(err, IsNil)
}

txn2 := s.beginTxn(c)
val, err := txn2.Get(context.TODO(), encodeKey(s.prefix, s08d("key", 0)))
val, err := txn2.Get(context.TODO(), tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(0)))
c.Assert(err, IsNil)
c.Assert(val, BytesEquals, valueBytes(0))
c.Assert(val, BytesEquals, genValueBytes(0))
// Test scan without upperBound
scan, err := txn2.Iter(encodeKey(s.prefix, ""), nil)
scan, err := txn2.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, false)
// Test scan with upperBound
upperBound := rowNum / 2
scan, err = txn2.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound)))
scan, err = txn2.Iter(s.recordPrefix, tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(upperBound)))
c.Assert(err, IsNil)
check(c, scan, upperBound, false)

txn3 := s.beginTxn(c)
txn3.SetOption(kv.KeyOnly, true)
// Test scan without upper bound
scan, err = txn3.Iter(encodeKey(s.prefix, ""), nil)
scan, err = txn3.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, true)
// test scan with upper bound
scan, err = txn3.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound)))
scan, err = txn3.Iter(s.recordPrefix, tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(upperBound)))
c.Assert(err, IsNil)
check(c, scan, upperBound, true)

// Restore KeyOnly to false
txn3.SetOption(kv.KeyOnly, false)
scan, err = txn3.Iter(encodeKey(s.prefix, ""), nil)
scan, err = txn3.Iter(s.recordPrefix, nil)
c.Assert(err, IsNil)
check(c, scan, rowNum, true)
// test scan with upper bound
scan, err = txn3.Iter(encodeKey(s.prefix, ""), encodeKey(s.prefix, s08d("key", upperBound)))
scan, err = txn3.Iter(s.recordPrefix, tablecodec.EncodeRecordKey(s.recordPrefix, kv.IntHandle(upperBound)))
c.Assert(err, IsNil)
check(c, scan, upperBound, true)
}
}

func genValueBytes(i int) []byte {
var res = []byte{rowcodec.CodecVer}
res = append(res, []byte(fmt.Sprintf("%d", i))...)
return res
}

0 comments on commit d8b37f6

Please sign in to comment.