github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/restore/log_client_test.go (about) 1 // Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. 2 3 package restore_test 4 5 import ( 6 "context" 7 "math" 8 9 . "github.com/pingcap/check" 10 filter "github.com/pingcap/tidb-tools/pkg/table-filter" 11 "github.com/pingcap/tidb/util/testleak" 12 13 "github.com/pingcap/br/pkg/gluetidb" 14 "github.com/pingcap/br/pkg/mock" 15 "github.com/pingcap/br/pkg/restore" 16 ) 17 18 type testLogRestoreSuite struct { 19 mock *mock.Cluster 20 21 client *restore.LogClient 22 } 23 24 var _ = Suite(&testLogRestoreSuite{}) 25 26 func (s *testLogRestoreSuite) SetUpSuite(c *C) { 27 var err error 28 s.mock, err = mock.NewCluster() 29 c.Assert(err, IsNil) 30 restoreClient, err := restore.NewRestoreClient( 31 gluetidb.New(), s.mock.PDClient, s.mock.Storage, nil, defaultKeepaliveCfg) 32 c.Assert(err, IsNil) 33 34 s.client, err = restore.NewLogRestoreClient( 35 context.Background(), 36 restoreClient, 37 0, 38 math.MaxInt64, 39 filter.NewSchemasFilter("test"), 40 8, 41 16, 42 5<<20, 43 16, 44 ) 45 c.Assert(err, IsNil) 46 } 47 48 func (s *testLogRestoreSuite) TearDownSuite(c *C) { 49 testleak.AfterTest(c)() 50 } 51 52 func (s *testLogRestoreSuite) TestTsInRange(c *C) { 53 fileName1 := "cdclog.1" 54 s.client.ResetTSRange(1, 2) 55 collected, err := s.client.NeedRestoreRowChange(fileName1) 56 c.Assert(err, IsNil) 57 c.Assert(collected, IsTrue) 58 59 // cdclog.3 may have events in [1, 2] 60 // so we should collect it. 61 fileName2 := "cdclog.3" 62 s.client.ResetTSRange(1, 2) 63 collected, err = s.client.NeedRestoreRowChange(fileName2) 64 c.Assert(err, IsNil) 65 c.Assert(collected, IsTrue) 66 67 fileName3 := "cdclog.3" 68 s.client.ResetTSRange(4, 5) 69 collected, err = s.client.NeedRestoreRowChange(fileName3) 70 c.Assert(err, IsNil) 71 c.Assert(collected, IsFalse) 72 73 // format cdclog will collect, because file sink will generate cdclog for streaming write. 74 fileName4 := "cdclog" 75 collected, err = s.client.NeedRestoreRowChange(fileName4) 76 c.Assert(err, IsNil) 77 c.Assert(collected, IsTrue) 78 79 for _, fileName := range []string{"cdclog.3.1", "cdclo.3"} { 80 // wrong format won't collect 81 collected, err = s.client.NeedRestoreRowChange(fileName) 82 c.Assert(err, IsNil) 83 c.Assert(collected, IsFalse) 84 } 85 86 // format cdclog will collect, because file sink will generate cdclog for streaming write. 87 ddlFile := "ddl.18446744073709551615" 88 collected, err = s.client.NeedRestoreDDL(ddlFile) 89 c.Assert(err, IsNil) 90 c.Assert(collected, IsTrue) 91 92 for _, fileName := range []string{"ddl", "dld.1"} { 93 // wrong format won't collect 94 collected, err = s.client.NeedRestoreDDL(fileName) 95 c.Assert(err, IsNil) 96 c.Assert(collected, IsFalse) 97 } 98 99 s.client.ResetTSRange(424839867765096449, 424839886560821249) 100 // ddl suffix records the first event's commit ts 101 102 // the file name include the end ts, collect it.(maxUint64 - 424839886560821249) 103 ddlFile = "ddl.18021904187148730366" 104 collected, err = s.client.NeedRestoreDDL(ddlFile) 105 c.Assert(err, IsNil) 106 c.Assert(collected, IsTrue) 107 108 // the file name include the start ts, collect it.(maxUint64 - 424839867765096449) 109 ddlFile = "ddl.18021904205944455166" 110 collected, err = s.client.NeedRestoreDDL(ddlFile) 111 c.Assert(err, IsNil) 112 c.Assert(collected, IsTrue) 113 114 // the file first event's ts is smaller than the start ts, collect it. 115 // because we only know this file's first event not in TSRange. 116 // FIXME find a unified logic for collection. 117 ddlFile = "ddl.18021904205944455167" 118 collected, err = s.client.NeedRestoreDDL(ddlFile) 119 c.Assert(err, IsNil) 120 c.Assert(collected, IsTrue) 121 122 // the file first event's ts is large than end ts, skip it. 123 ddlFile = "ddl.18021904187148730365" 124 collected, err = s.client.NeedRestoreDDL(ddlFile) 125 c.Assert(err, IsNil) 126 c.Assert(collected, IsFalse) 127 }