github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/dbs_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package dbs 15 16 import ( 17 "context" 18 "os" 19 "testing" 20 "time" 21 22 "github.com/whtcorpsinc/BerolinaSQL/ast" 23 "github.com/whtcorpsinc/BerolinaSQL/perceptron" 24 . "github.com/whtcorpsinc/check" 25 "github.com/whtcorpsinc/milevadb/causetstore/mockstore" 26 "github.com/whtcorpsinc/milevadb/config" 27 "github.com/whtcorpsinc/milevadb/ekv" 28 "github.com/whtcorpsinc/milevadb/petri/infosync" 29 "github.com/whtcorpsinc/milevadb/soliton/logutil" 30 "github.com/whtcorpsinc/milevadb/soliton/mock" 31 "github.com/whtcorpsinc/milevadb/soliton/testleak" 32 "github.com/whtcorpsinc/milevadb/spacetime" 33 "github.com/whtcorpsinc/milevadb/spacetime/autoid" 34 "github.com/whtcorpsinc/milevadb/stochastikctx" 35 "github.com/whtcorpsinc/milevadb/types" 36 ) 37 38 type DBSForTest interface { 39 // SetHook sets the hook. 40 SetHook(h Callback) 41 // SetInterceptor sets the interceptor. 42 SetInterceptor(h Interceptor) 43 } 44 45 // SetHook implements DBS.SetHook interface. 46 func (d *dbs) SetHook(h Callback) { 47 d.mu.Lock() 48 defer d.mu.Unlock() 49 50 d.mu.hook = h 51 } 52 53 // SetInterceptor implements DBS.SetInterceptor interface. 54 func (d *dbs) SetInterceptor(i Interceptor) { 55 d.mu.Lock() 56 defer d.mu.Unlock() 57 58 d.mu.interceptor = i 59 } 60 61 // generalWorker returns the general worker. 62 func (d *dbs) generalWorker() *worker { 63 return d.workers[generalWorker] 64 } 65 66 func TestT(t *testing.T) { 67 CustomVerboseFlag = true 68 *CustomParallelSuiteFlag = true 69 logLevel := os.Getenv("log_level") 70 logutil.InitLogger(logutil.NewLogConfig(logLevel, "", "", logutil.EmptyFileLogConfig, false)) 71 autoid.SetStep(5000) 72 ReorgWaitTimeout = 30 * time.Millisecond 73 batchInsertDeleteRangeSize = 2 74 75 config.UFIDelateGlobal(func(conf *config.Config) { 76 // Test for causet dagger. 77 conf.EnableBlockLock = true 78 conf.Log.SlowThreshold = 10000 79 // Test for add/drop primary key. 80 conf.AlterPrimaryKey = true 81 }) 82 83 _, err := infosync.GlobalInfoSyncerInit(context.Background(), "t", nil, true) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 testleak.BeforeTest() 89 TestingT(t) 90 testleak.AfterTestT(t)() 91 } 92 93 func testNewDBSAndStart(ctx context.Context, c *C, options ...Option) *dbs { 94 d := newDBS(ctx, options...) 95 err := d.Start(nil) 96 c.Assert(err, IsNil) 97 98 return d 99 } 100 101 func testCreateStore(c *C, name string) ekv.CausetStorage { 102 causetstore, err := mockstore.NewMockStore() 103 c.Assert(err, IsNil) 104 return causetstore 105 } 106 107 func testNewContext(d *dbs) stochastikctx.Context { 108 ctx := mock.NewContext() 109 ctx.CausetStore = d.causetstore 110 return ctx 111 } 112 113 func getSchemaVer(c *C, ctx stochastikctx.Context) int64 { 114 err := ctx.NewTxn(context.Background()) 115 c.Assert(err, IsNil) 116 txn, err := ctx.Txn(true) 117 c.Assert(err, IsNil) 118 m := spacetime.NewMeta(txn) 119 ver, err := m.GetSchemaVersion() 120 c.Assert(err, IsNil) 121 return ver 122 } 123 124 type historyJobArgs struct { 125 ver int64 126 EDB *perceptron.DBInfo 127 tbl *perceptron.BlockInfo 128 tblIDs map[int64]struct{} 129 } 130 131 func checkEqualBlock(c *C, t1, t2 *perceptron.BlockInfo) { 132 c.Assert(t1.ID, Equals, t2.ID) 133 c.Assert(t1.Name, Equals, t2.Name) 134 c.Assert(t1.Charset, Equals, t2.Charset) 135 c.Assert(t1.DefCauslate, Equals, t2.DefCauslate) 136 c.Assert(t1.PKIsHandle, DeepEquals, t2.PKIsHandle) 137 c.Assert(t1.Comment, DeepEquals, t2.Comment) 138 c.Assert(t1.AutoIncID, DeepEquals, t2.AutoIncID) 139 } 140 141 func checkHistoryJob(c *C, job *perceptron.Job) { 142 c.Assert(job.State, Equals, perceptron.JobStateSynced) 143 } 144 145 func checkHistoryJobArgs(c *C, ctx stochastikctx.Context, id int64, args *historyJobArgs) { 146 txn, err := ctx.Txn(true) 147 c.Assert(err, IsNil) 148 t := spacetime.NewMeta(txn) 149 historyJob, err := t.GetHistoryDBSJob(id) 150 c.Assert(err, IsNil) 151 c.Assert(historyJob.BinlogInfo.FinishedTS, Greater, uint64(0)) 152 153 if args.tbl != nil { 154 c.Assert(historyJob.BinlogInfo.SchemaVersion, Equals, args.ver) 155 checkEqualBlock(c, historyJob.BinlogInfo.BlockInfo, args.tbl) 156 return 157 } 158 159 // for handling schemaReplicant job 160 c.Assert(historyJob.BinlogInfo.SchemaVersion, Equals, args.ver) 161 c.Assert(historyJob.BinlogInfo.DBInfo, DeepEquals, args.EDB) 162 // only for creating schemaReplicant job 163 if args.EDB != nil && len(args.tblIDs) == 0 { 164 return 165 } 166 } 167 168 func buildCreateIdxJob(dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, unique bool, indexName string, defCausName string) *perceptron.Job { 169 return &perceptron.Job{ 170 SchemaID: dbInfo.ID, 171 BlockID: tblInfo.ID, 172 Type: perceptron.CausetActionAddIndex, 173 BinlogInfo: &perceptron.HistoryInfo{}, 174 Args: []interface{}{unique, perceptron.NewCIStr(indexName), 175 []*ast.IndexPartSpecification{{ 176 DeferredCauset: &ast.DeferredCausetName{Name: perceptron.NewCIStr(defCausName)}, 177 Length: types.UnspecifiedLength}}}, 178 } 179 } 180 181 func testCreatePrimaryKey(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, defCausName string) *perceptron.Job { 182 job := buildCreateIdxJob(dbInfo, tblInfo, true, "primary", defCausName) 183 job.Type = perceptron.CausetActionAddPrimaryKey 184 err := d.doDBSJob(ctx, job) 185 c.Assert(err, IsNil) 186 v := getSchemaVer(c, ctx) 187 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 188 return job 189 } 190 191 func testCreateIndex(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, unique bool, indexName string, defCausName string) *perceptron.Job { 192 job := buildCreateIdxJob(dbInfo, tblInfo, unique, indexName, defCausName) 193 err := d.doDBSJob(ctx, job) 194 c.Assert(err, IsNil) 195 v := getSchemaVer(c, ctx) 196 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 197 return job 198 } 199 200 func testAddDeferredCauset(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, args []interface{}) *perceptron.Job { 201 job := &perceptron.Job{ 202 SchemaID: dbInfo.ID, 203 BlockID: tblInfo.ID, 204 Type: perceptron.CausetActionAddDeferredCauset, 205 Args: args, 206 BinlogInfo: &perceptron.HistoryInfo{}, 207 } 208 err := d.doDBSJob(ctx, job) 209 c.Assert(err, IsNil) 210 v := getSchemaVer(c, ctx) 211 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 212 return job 213 } 214 215 func testAddDeferredCausets(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, args []interface{}) *perceptron.Job { 216 job := &perceptron.Job{ 217 SchemaID: dbInfo.ID, 218 BlockID: tblInfo.ID, 219 Type: perceptron.CausetActionAddDeferredCausets, 220 Args: args, 221 BinlogInfo: &perceptron.HistoryInfo{}, 222 } 223 err := d.doDBSJob(ctx, job) 224 c.Assert(err, IsNil) 225 v := getSchemaVer(c, ctx) 226 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 227 return job 228 } 229 230 func buildDropIdxJob(dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, indexName string) *perceptron.Job { 231 tp := perceptron.CausetActionDropIndex 232 if indexName == "primary" { 233 tp = perceptron.CausetActionDropPrimaryKey 234 } 235 return &perceptron.Job{ 236 SchemaID: dbInfo.ID, 237 BlockID: tblInfo.ID, 238 Type: tp, 239 BinlogInfo: &perceptron.HistoryInfo{}, 240 Args: []interface{}{perceptron.NewCIStr(indexName)}, 241 } 242 } 243 244 func testDropIndex(c *C, ctx stochastikctx.Context, d *dbs, dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, indexName string) *perceptron.Job { 245 job := buildDropIdxJob(dbInfo, tblInfo, indexName) 246 err := d.doDBSJob(ctx, job) 247 c.Assert(err, IsNil) 248 v := getSchemaVer(c, ctx) 249 checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo}) 250 return job 251 } 252 253 func buildRebaseAutoIDJobJob(dbInfo *perceptron.DBInfo, tblInfo *perceptron.BlockInfo, newBaseID int64) *perceptron.Job { 254 return &perceptron.Job{ 255 SchemaID: dbInfo.ID, 256 BlockID: tblInfo.ID, 257 Type: perceptron.CausetActionRebaseAutoID, 258 BinlogInfo: &perceptron.HistoryInfo{}, 259 Args: []interface{}{newBaseID}, 260 } 261 }