github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/checksum/executor_test.go (about)

     1  // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
     2  
     3  package checksum_test
     4  
     5  import (
     6  	"context"
     7  	"math"
     8  	"testing"
     9  
    10  	"github.com/pingcap/br/pkg/metautil"
    11  
    12  	. "github.com/pingcap/check"
    13  	"github.com/pingcap/parser/model"
    14  	"github.com/pingcap/tidb/kv"
    15  	"github.com/pingcap/tidb/sessionctx/variable"
    16  	"github.com/pingcap/tidb/util/testkit"
    17  	"github.com/pingcap/tidb/util/testleak"
    18  
    19  	"github.com/pingcap/br/pkg/backup"
    20  	"github.com/pingcap/br/pkg/checksum"
    21  	"github.com/pingcap/br/pkg/mock"
    22  )
    23  
    24  func TestT(t *testing.T) {
    25  	TestingT(t)
    26  }
    27  
    28  var _ = Suite(&testChecksumSuite{})
    29  
    30  type testChecksumSuite struct {
    31  	mock *mock.Cluster
    32  }
    33  
    34  func (s *testChecksumSuite) SetUpSuite(c *C) {
    35  	var err error
    36  	s.mock, err = mock.NewCluster()
    37  	c.Assert(err, IsNil)
    38  }
    39  
    40  func (s *testChecksumSuite) TearDownSuite(c *C) {
    41  	testleak.AfterTest(c)()
    42  }
    43  
    44  func (s *testChecksumSuite) getTableInfo(c *C, db, table string) *model.TableInfo {
    45  	info, err := s.mock.Domain.GetSnapshotInfoSchema(math.MaxUint64)
    46  	c.Assert(err, IsNil)
    47  	cDBName := model.NewCIStr(db)
    48  	cTableName := model.NewCIStr(table)
    49  	tableInfo, err := info.TableByName(cDBName, cTableName)
    50  	c.Assert(err, IsNil)
    51  	return tableInfo.Meta()
    52  }
    53  
    54  func (s *testChecksumSuite) TestChecksum(c *C) {
    55  	c.Assert(s.mock.Start(), IsNil)
    56  	defer s.mock.Stop()
    57  
    58  	tk := testkit.NewTestKit(c, s.mock.Storage)
    59  	tk.MustExec("use test")
    60  
    61  	tk.MustExec("drop table if exists t1;")
    62  	tk.MustExec("create table t1 (a int);")
    63  	tk.MustExec("insert into t1 values (10);")
    64  	tableInfo1 := s.getTableInfo(c, "test", "t1")
    65  	exe1, err := checksum.NewExecutorBuilder(tableInfo1, math.MaxUint64).
    66  		SetConcurrency(variable.DefChecksumTableConcurrency).
    67  		Build()
    68  	c.Assert(err, IsNil)
    69  	c.Assert(exe1.Each(func(r *kv.Request) error {
    70  		c.Assert(r.NotFillCache, IsTrue)
    71  		c.Assert(r.Concurrency, Equals, variable.DefChecksumTableConcurrency)
    72  		return nil
    73  	}), IsNil)
    74  	c.Assert(exe1.Len(), Equals, 1)
    75  	resp, err := exe1.Execute(context.TODO(), s.mock.Storage.GetClient(), func() {})
    76  	c.Assert(err, IsNil)
    77  	// Cluster returns a dummy checksum (all fields are 1).
    78  	c.Assert(resp.Checksum, Equals, uint64(1), Commentf("%v", resp))
    79  	c.Assert(resp.TotalKvs, Equals, uint64(1), Commentf("%v", resp))
    80  	c.Assert(resp.TotalBytes, Equals, uint64(1), Commentf("%v", resp))
    81  
    82  	tk.MustExec("drop table if exists t2;")
    83  	tk.MustExec("create table t2 (a int);")
    84  	tk.MustExec("alter table t2 add index i2(a);")
    85  	tk.MustExec("insert into t2 values (10);")
    86  	tableInfo2 := s.getTableInfo(c, "test", "t2")
    87  	exe2, err := checksum.NewExecutorBuilder(tableInfo2, math.MaxUint64).Build()
    88  	c.Assert(err, IsNil)
    89  	c.Assert(exe2.Len(), Equals, 2, Commentf("%v", tableInfo2))
    90  	resp2, err := exe2.Execute(context.TODO(), s.mock.Storage.GetClient(), func() {})
    91  	c.Assert(err, IsNil)
    92  	c.Assert(resp2.Checksum, Equals, uint64(0), Commentf("%v", resp2))
    93  	c.Assert(resp2.TotalKvs, Equals, uint64(2), Commentf("%v", resp2))
    94  	c.Assert(resp2.TotalBytes, Equals, uint64(2), Commentf("%v", resp2))
    95  
    96  	// Test rewrite rules
    97  	tk.MustExec("alter table t1 add index i2(a);")
    98  	tableInfo1 = s.getTableInfo(c, "test", "t1")
    99  	oldTable := metautil.Table{Info: tableInfo1}
   100  	exe2, err = checksum.NewExecutorBuilder(tableInfo2, math.MaxUint64).
   101  		SetOldTable(&oldTable).Build()
   102  	c.Assert(err, IsNil)
   103  	c.Assert(exe2.Len(), Equals, 2)
   104  	rawReqs, err := exe2.RawRequests()
   105  	c.Assert(err, IsNil)
   106  	c.Assert(rawReqs, HasLen, 2)
   107  	for _, rawReq := range rawReqs {
   108  		c.Assert(rawReq.Rule, NotNil)
   109  	}
   110  	resp2, err = exe2.Execute(context.TODO(), s.mock.Storage.GetClient(), func() {})
   111  	c.Assert(err, IsNil)
   112  	c.Assert(resp2, NotNil)
   113  
   114  	// Test commonHandle ranges
   115  
   116  	tk.MustExec("drop table if exists t3;")
   117  	tk.MustExec("create table t3 (a char(255), b int, primary key(a) CLUSTERED);")
   118  	tk.MustExec("insert into t3 values ('fffffffff', 1), ('010101010', 2), ('394393fj39efefe', 3);")
   119  	tableInfo3 := s.getTableInfo(c, "test", "t3")
   120  	exe3, err := checksum.NewExecutorBuilder(tableInfo3, math.MaxUint64).Build()
   121  	c.Assert(err, IsNil)
   122  	first := true
   123  	c.Assert(exe3.Each(func(req *kv.Request) error {
   124  		if first {
   125  			first = false
   126  			ranges, err := backup.BuildTableRanges(tableInfo3)
   127  			c.Assert(err, IsNil)
   128  			c.Assert(req.KeyRanges, DeepEquals, ranges[:1], Commentf("%v", req.KeyRanges))
   129  		}
   130  		return nil
   131  	}), IsNil)
   132  }