github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/test/replay_test.go (about) 1 // Copyright 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "context" 19 "math/rand" 20 "sync" 21 "testing" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/common/moerr" 25 "github.com/matrixorigin/matrixone/pkg/container/types" 26 "github.com/matrixorigin/matrixone/pkg/logutil" 27 "github.com/matrixorigin/matrixone/pkg/objectio" 28 "github.com/matrixorigin/matrixone/pkg/pb/plan" 29 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 30 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 31 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db" 32 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/testutil" 33 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle" 34 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 35 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config" 36 "github.com/panjf2000/ants/v2" 37 "github.com/stretchr/testify/assert" 38 ) 39 40 func TestReplayCatalog1(t *testing.T) { 41 defer testutils.AfterTest(t)() 42 ctx := context.Background() 43 44 testutils.EnsureNoLeak(t) 45 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 46 schemas := make([]*catalog.Schema, 4) 47 for i := range schemas { 48 schemas[i] = catalog.MockSchema(2, 0) 49 } 50 51 txn, _ := tae.StartTxn(nil) 52 _, err := txn.CreateDatabase("db", "", "") 53 assert.Nil(t, err) 54 assert.Nil(t, txn.Commit(context.Background())) 55 createTable := func(schema *catalog.Schema, wg *sync.WaitGroup, forceCkp bool) func() { 56 return func() { 57 defer wg.Done() 58 txn, _ := tae.StartTxn(nil) 59 db, err := txn.GetDatabase("db") 60 assert.Nil(t, err) 61 _, err = db.CreateRelation(schema) 62 assert.Nil(t, err) 63 assert.Nil(t, txn.Commit(context.Background())) 64 txn, _ = tae.StartTxn(nil) 65 db, err = txn.GetDatabase("db") 66 assert.Nil(t, err) 67 rel, err := db.GetRelationByName(schema.Name) 68 assert.Nil(t, err) 69 objCnt := rand.Intn(5) + 1 70 for i := 0; i < objCnt; i++ { 71 obj, err := rel.CreateNonAppendableObject(false, nil) 72 assert.Nil(t, err) 73 objMeta := obj.GetMeta().(*catalog.ObjectEntry) 74 objMeta.Lock() 75 baseNode := objMeta.GetLatestNodeLocked().BaseNode 76 err = objectio.SetObjectStatsSize(&baseNode.ObjectStats, 1) 77 assert.Nil(t, err) 78 err = objectio.SetObjectStatsRowCnt(&baseNode.ObjectStats, 1) 79 assert.Nil(t, err) 80 assert.False(t, baseNode.IsEmpty()) 81 objMeta.Unlock() 82 assert.Nil(t, err) 83 } 84 assert.Nil(t, txn.Commit(context.Background())) 85 if forceCkp || rand.Intn(100) > 80 { 86 err := tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 87 assert.NoError(t, err) 88 } 89 } 90 } 91 92 var wg sync.WaitGroup 93 pool, err := ants.NewPool(1) 94 assert.Nil(t, err) 95 defer pool.Release() 96 for i, schema := range schemas { 97 wg.Add(1) 98 ckp := false 99 if i == len(schemas)/2 { 100 ckp = true 101 } 102 err := pool.Submit(createTable(schema, &wg, ckp)) 103 assert.Nil(t, err) 104 } 105 wg.Wait() 106 logutil.Info(tae.Catalog.SimplePPString(common.PPL1)) 107 t.Logf("GetPenddingLSNCnt: %d", tae.Runtime.Scheduler.GetPenddingLSNCnt()) 108 t.Logf("GetCheckpointed: %d", tae.Runtime.Scheduler.GetCheckpointedLSN()) 109 tae.Close() 110 111 tae2, err := db.Open(ctx, tae.Dir, nil) 112 assert.Nil(t, err) 113 defer tae2.Close() 114 115 c := tae2.Catalog 116 defer c.Close() 117 118 // logutil.Info(c.SimplePPString(common.PPL1)) 119 // t.Logf("GetCatalogCheckpointed: %v", tae.Catalog.GetCheckpointed()) 120 // t.Logf("GetCatalogCheckpointed2: %v", c.GetCheckpointed()) 121 // assert.Equal(t, tae.Catalog.GetCheckpointed(), c.GetCheckpointed()) 122 } 123 124 func TestReplayCatalog2(t *testing.T) { 125 defer testutils.AfterTest(t)() 126 testutils.EnsureNoLeak(t) 127 ctx := context.Background() 128 129 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 130 schema := catalog.MockSchema(2, 0) 131 schema2 := catalog.MockSchema(2, 0) 132 txn, _ := tae.StartTxn(nil) 133 _, err := txn.CreateDatabase("db2", "", "") 134 assert.Nil(t, err) 135 assert.Nil(t, txn.Commit(context.Background())) 136 137 txn, _ = tae.StartTxn(nil) 138 e, err := txn.CreateDatabase("db", "", "") 139 assert.Nil(t, err) 140 rel, err := e.CreateRelation(schema) 141 assert.Nil(t, err) 142 obj, err := rel.CreateNonAppendableObject(false, nil) 143 assert.Nil(t, err) 144 objMeta := obj.GetMeta().(*catalog.ObjectEntry) 145 objMeta.Lock() 146 baseNode := objMeta.GetLatestNodeLocked().BaseNode 147 err = objectio.SetObjectStatsSize(&baseNode.ObjectStats, 1) 148 assert.Nil(t, err) 149 err = objectio.SetObjectStatsRowCnt(&baseNode.ObjectStats, 1) 150 assert.Nil(t, err) 151 assert.False(t, baseNode.IsEmpty()) 152 objMeta.Unlock() 153 assert.Nil(t, err) 154 _, err = e.CreateRelation(schema2) 155 assert.Nil(t, err) 156 assert.Nil(t, txn.Commit(context.Background())) 157 158 txn, _ = tae.StartTxn(nil) 159 _, err = txn.DropDatabase("db2") 160 assert.Nil(t, err) 161 assert.Nil(t, txn.Commit(context.Background())) 162 163 txn, _ = tae.StartTxn(nil) 164 e, err = txn.GetDatabase("db") 165 assert.Nil(t, err) 166 _, err = e.DropRelationByName(schema2.Name) 167 assert.Nil(t, err) 168 assert.Nil(t, txn.Commit(context.Background())) 169 170 txn, _ = tae.StartTxn(nil) 171 e, err = txn.GetDatabase("db") 172 assert.Nil(t, err) 173 _, err = e.GetRelationByName(schema.Name) 174 assert.Nil(t, err) 175 assert.Nil(t, txn.Commit(context.Background())) 176 177 txn, _ = tae.StartTxn(nil) 178 e, err = txn.GetDatabase("db") 179 assert.Nil(t, err) 180 rel, err = e.GetRelationByName(schema.Name) 181 assert.Nil(t, err) 182 _, err = rel.CreateObject(false) 183 assert.Nil(t, err) 184 assert.Nil(t, txn.Commit(context.Background())) 185 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 186 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 187 assert.NoError(t, err) 188 tae.Close() 189 190 tae2, err := db.Open(ctx, tae.Dir, nil) 191 assert.Nil(t, err) 192 defer tae2.Close() 193 194 c := tae2.Catalog 195 defer c.Close() 196 197 // t.Log(c.SimplePPString(common.PPL1)) 198 // t.Logf("GetCatalogCheckpointed: %v", tae.Catalog.GetCheckpointed()) 199 // t.Logf("GetCatalogCheckpointed2: %v", c.GetCheckpointed()) 200 // assert.Equal(t, tae.Catalog.GetCheckpointed(), c.GetCheckpointed()) 201 } 202 203 func TestReplayCatalog3(t *testing.T) { 204 defer testutils.AfterTest(t)() 205 testutils.EnsureNoLeak(t) 206 ctx := context.Background() 207 208 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 209 schema := catalog.MockSchema(2, 0) 210 schema2 := catalog.MockSchema(2, 0) 211 txn, _ := tae.StartTxn(nil) 212 _, err := txn.CreateDatabase("db2", "", "") 213 assert.Nil(t, err) 214 assert.Nil(t, txn.Commit(context.Background())) 215 216 txn, _ = tae.StartTxn(nil) 217 e, err := txn.CreateDatabase("db", "", "") 218 assert.Nil(t, err) 219 rel, err := e.CreateRelation(schema) 220 assert.Nil(t, err) 221 _, err = rel.CreateNonAppendableObject(false, nil) 222 assert.Nil(t, err) 223 _, err = e.CreateRelation(schema2) 224 assert.Nil(t, err) 225 assert.Nil(t, txn.Commit(context.Background())) 226 227 txn, _ = tae.StartTxn(nil) 228 _, err = txn.DropDatabase("db2") 229 assert.Nil(t, err) 230 assert.Nil(t, txn.Commit(context.Background())) 231 232 txn, _ = tae.StartTxn(nil) 233 e, err = txn.GetDatabase("db") 234 assert.Nil(t, err) 235 _, err = e.DropRelationByName(schema2.Name) 236 assert.Nil(t, err) 237 assert.Nil(t, txn.Commit(context.Background())) 238 239 txn, _ = tae.StartTxn(nil) 240 e, err = txn.GetDatabase("db") 241 assert.Nil(t, err) 242 _, err = e.GetRelationByName(schema.Name) 243 assert.Nil(t, err) 244 assert.Nil(t, txn.Commit(context.Background())) 245 246 txn, _ = tae.StartTxn(nil) 247 e, err = txn.GetDatabase("db") 248 assert.Nil(t, err) 249 rel, err = e.GetRelationByName(schema.Name) 250 assert.Nil(t, err) 251 obj, err := rel.CreateObject(false) 252 assert.Nil(t, err) 253 assert.Nil(t, txn.Commit(context.Background())) 254 255 txn, _ = tae.StartTxn(nil) 256 e, err = txn.GetDatabase("db") 257 assert.Nil(t, err) 258 rel, err = e.GetRelationByName(schema.Name) 259 assert.Nil(t, err) 260 err = rel.SoftDeleteObject(obj.GetID()) 261 assert.Nil(t, err) 262 assert.Nil(t, txn.Commit(context.Background())) 263 264 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 265 tae.Close() 266 267 tae2, err := db.Open(ctx, tae.Dir, nil) 268 assert.Nil(t, err) 269 defer tae2.Close() 270 271 c := tae2.Catalog 272 defer c.Close() 273 274 // t.Log(c.SimplePPString(common.PPL1)) 275 // t.Logf("GetCatalogCheckpointed: %v", tae.Catalog.GetCheckpointed()) 276 // t.Logf("GetCatalogCheckpointed2: %v", c.GetCheckpointed()) 277 // assert.Equal(t, tae.Catalog.GetCheckpointed(), c.GetCheckpointed()) 278 } 279 280 // catalog and data not checkpoint 281 // catalog not softdelete 282 func TestReplay1(t *testing.T) { 283 defer testutils.AfterTest(t)() 284 testutils.EnsureNoLeak(t) 285 ctx := context.Background() 286 287 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 288 schema := catalog.MockSchema(2, 1) 289 schema.BlockMaxRows = 1000 290 schema.ObjectMaxBlocks = 2 291 txn, _ := tae.StartTxn(nil) 292 assert.Nil(t, txn.Commit(context.Background())) 293 294 txn, _ = tae.StartTxn(nil) 295 e, err := txn.CreateDatabase("db", "", "") 296 assert.Nil(t, err) 297 rel, err := e.CreateRelation(schema) 298 assert.Nil(t, err) 299 _, err = rel.CreateObject(false) 300 assert.Nil(t, err) 301 assert.Nil(t, txn.Commit(context.Background())) 302 logutil.Infof("%d,%d", txn.GetStartTS(), txn.GetCommitTS()) 303 304 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 305 tae.Close() 306 307 tae2, err := db.Open(ctx, tae.Dir, nil) 308 assert.Nil(t, err) 309 c := tae2.Catalog 310 t.Log(c.SimplePPString(common.PPL1)) 311 312 bat := catalog.MockBatch(schema, 10000) 313 defer bat.Close() 314 txn, _ = tae2.StartTxn(nil) 315 e, err = txn.GetDatabase("db") 316 assert.Nil(t, err) 317 rel, err = e.GetRelationByName(schema.Name) 318 assert.Nil(t, err) 319 err = rel.Append(context.Background(), bat) 320 assert.Nil(t, err) 321 assert.Nil(t, txn.Commit(context.Background())) 322 323 txn, _ = tae2.StartTxn(nil) 324 e, err = txn.GetDatabase("db") 325 assert.Nil(t, err) 326 rel, err = e.GetRelationByName(schema.Name) 327 assert.Nil(t, err) 328 assert.Equal(t, uint64(10000), rel.GetMeta().(*catalog.TableEntry).GetRows()) 329 filter := handle.NewEQFilter(int32(5)) 330 err = rel.UpdateByFilter(context.Background(), filter, uint16(0), int32(33), false) 331 assert.Nil(t, err) 332 assert.Nil(t, txn.Commit(context.Background())) 333 334 txn, _ = tae2.StartTxn(nil) 335 e, err = txn.GetDatabase("db") 336 assert.Nil(t, err) 337 rel, err = e.GetRelationByName(schema.Name) 338 assert.Nil(t, err) 339 assert.Equal(t, uint64(10000), rel.GetMeta().(*catalog.TableEntry).GetRows()) 340 filter.Val = int32(6) 341 err = rel.DeleteByFilter(context.Background(), filter) 342 assert.Nil(t, err) 343 assert.Nil(t, txn.Commit(context.Background())) 344 345 t.Log(c.SimplePPString(common.PPL1)) 346 c.Close() 347 tae2.Close() 348 349 tae3, err := db.Open(ctx, tae.Dir, nil) 350 assert.Nil(t, err) 351 c3 := tae3.Catalog 352 t.Log(c3.SimplePPString(common.PPL1)) 353 354 txn, _ = tae3.StartTxn(nil) 355 e, err = txn.GetDatabase("db") 356 assert.Nil(t, err) 357 rel, err = e.GetRelationByName(schema.Name) 358 assert.Nil(t, err) 359 assert.Equal(t, uint64(9999), rel.GetMeta().(*catalog.TableEntry).GetRows()) 360 filter.Val = int32(5) 361 val, _, err := rel.GetValueByFilter(context.Background(), filter, 0) 362 assert.Nil(t, err) 363 assert.Equal(t, int32(33), val) 364 365 filter.Val = int32(6) 366 _, _, err = rel.GetValueByFilter(context.Background(), filter, 0) 367 assert.NotNil(t, err) 368 assert.Nil(t, txn.Commit(context.Background())) 369 370 c3.Close() 371 tae3.Close() 372 } 373 374 // 1. Create db and tbl, append data [1] 375 // 2. Update and delete. [2] 376 // 3. Delete first blk [3] 377 // replay (catalog and data not ckp, catalog softdelete) 378 // check 1. blk not exist, 2. id and row of data 379 // 1. Checkpoint catalog [ckp 3, partial 1] 380 // 2. Append [4] 381 // replay (catalog ckp, data not ckp) 382 // check id and row of data 383 // 1. Checkpoint data and catalog [ckp all] 384 // replay 385 // TODO check id and row of data 386 func TestReplay2(t *testing.T) { 387 defer testutils.AfterTest(t)() 388 testutils.EnsureNoLeak(t) 389 ctx := context.Background() 390 391 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 392 schema := catalog.MockSchema(2, 1) 393 schema.BlockMaxRows = 1000 394 schema.ObjectMaxBlocks = 2 395 bat := catalog.MockBatch(schema, 10000) 396 defer bat.Close() 397 bats := bat.Split(2) 398 399 txn, err := tae.StartTxn(nil) 400 assert.Nil(t, err) 401 e, err := txn.CreateDatabase("db", "", "") 402 assert.Nil(t, err) 403 rel, err := e.CreateRelation(schema) 404 assert.Nil(t, err) 405 err = rel.Append(context.Background(), bats[0]) 406 assert.Nil(t, err) 407 assert.Nil(t, txn.Commit(context.Background())) 408 409 txn, err = tae.StartTxn(nil) 410 assert.Nil(t, err) 411 e, err = txn.GetDatabase("db") 412 assert.Nil(t, err) 413 rel, err = e.GetRelationByName(schema.Name) 414 assert.Nil(t, err) 415 416 filter := handle.NewEQFilter(int32(1500)) 417 id, row, err := rel.GetByFilter(context.Background(), filter) 418 assert.Nil(t, err) 419 err = rel.UpdateByFilter(context.Background(), filter, uint16(0), int32(33), false) 420 assert.Nil(t, err) 421 422 err = rel.RangeDelete(id, row+1, row+100, handle.DT_Normal) 423 assert.Nil(t, err) 424 assert.Nil(t, txn.Commit(context.Background())) 425 426 txn, err = tae.StartTxn(nil) 427 assert.Nil(t, err) 428 e, err = txn.GetDatabase("db") 429 assert.Nil(t, err) 430 rel, err = e.GetRelationByName(schema.Name) 431 assert.Nil(t, err) 432 blkIterator := rel.MakeObjectIt() 433 blk := blkIterator.GetObject().GetMeta().(*catalog.ObjectEntry) 434 obj, err := rel.GetObject(&blk.ID) 435 assert.Nil(t, err) 436 err = rel.SoftDeleteObject(obj.GetID()) 437 assert.Nil(t, err) 438 objMeta := obj.GetMeta().(*catalog.ObjectEntry) 439 objMeta.Lock() 440 baseNode := objMeta.GetLatestNodeLocked().BaseNode 441 err = objectio.SetObjectStatsSize(&baseNode.ObjectStats, 1) 442 assert.Nil(t, err) 443 err = objectio.SetObjectStatsRowCnt(&baseNode.ObjectStats, 1) 444 assert.Nil(t, err) 445 assert.False(t, baseNode.IsEmpty()) 446 objMeta.Unlock() 447 assert.Nil(t, txn.Commit(context.Background())) 448 449 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 450 tae.Close() 451 //prevTs := tae.TxnMgr.TsAlloc.Get() 452 453 tae2, err := db.Open(ctx, tae.Dir, nil) 454 assert.Nil(t, err) 455 t.Log(tae2.Catalog.SimplePPString(common.PPL1)) 456 457 //currTs := tae2.TxnMgr.TsAlloc.Get() 458 //assert.True(t, currTs.GreaterEq(prevTs)) 459 460 txn, err = tae2.StartTxn(nil) 461 assert.Nil(t, err) 462 e, err = txn.GetDatabase("db") 463 assert.Nil(t, err) 464 rel, err = e.GetRelationByName(schema.Name) 465 assert.Nil(t, err) 466 objEntry, err := rel.GetMeta().(*catalog.TableEntry).GetObjectByID(obj.GetID()) 467 assert.Nil(t, err) 468 assert.True(t, objEntry.HasDropCommittedLocked()) 469 470 val, _, err := rel.GetValueByFilter(context.Background(), filter, 0) 471 assert.Nil(t, err) 472 assert.Equal(t, int32(33), val) 473 _, _, err = rel.GetValue(id, row, 0) 474 assert.NotNil(t, err) 475 assert.Nil(t, txn.Commit(context.Background())) 476 477 err = tae2.BGCheckpointRunner.ForceFlush(tae2.TxnMgr.Now(), context.Background(), time.Second*10) 478 assert.NoError(t, err) 479 err = tae2.BGCheckpointRunner.ForceIncrementalCheckpoint(tae2.TxnMgr.Now(), false) 480 assert.NoError(t, err) 481 482 txn, err = tae2.StartTxn(nil) 483 assert.Nil(t, err) 484 e, err = txn.GetDatabase("db") 485 assert.Nil(t, err) 486 rel, err = e.GetRelationByName(schema.Name) 487 assert.Nil(t, err) 488 err = rel.Append(context.Background(), bats[1]) 489 assert.Nil(t, err) 490 assert.Nil(t, txn.Commit(context.Background())) 491 492 t.Log(tae2.Catalog.SimplePPString(common.PPL1)) 493 tae2.Close() 494 495 tae3, err := db.Open(ctx, tae.Dir, nil) 496 assert.Nil(t, err) 497 t.Log(tae3.Catalog.SimplePPString(common.PPL1)) 498 499 txn, err = tae3.StartTxn(nil) 500 assert.Nil(t, err) 501 e, err = txn.GetDatabase("db") 502 assert.Nil(t, err) 503 rel, err = e.GetRelationByName(schema.Name) 504 assert.Nil(t, err) 505 _, err = rel.GetMeta().(*catalog.TableEntry).GetObjectByID(obj.GetID()) 506 assert.Nil(t, err) 507 val, _, err = rel.GetValueByFilter(context.Background(), filter, 0) 508 assert.Nil(t, err) 509 assert.Equal(t, int32(33), val) 510 _, _, err = rel.GetValue(id, row, 0) 511 assert.NotNil(t, err) 512 assert.Nil(t, txn.Commit(context.Background())) 513 514 tae3.Close() 515 516 tae4, err := db.Open(ctx, tae.Dir, nil) 517 assert.NoError(t, err) 518 tae4.Close() 519 } 520 521 // update, delete and append in one txn 522 // 1. Create db and tbl and {Append, Update, Delete} for 100 times in a same txn. 523 // 2. Append, Update, Delete each in one txn 524 // replay 525 // check rows 526 // 1. Ckp 527 // TODO check rows 528 func TestReplay3(t *testing.T) { 529 defer testutils.AfterTest(t)() 530 testutils.EnsureNoLeak(t) 531 ctx := context.Background() 532 533 tae := testutil.NewTestEngine(ctx, ModuleName, t, nil) 534 defer tae.Close() 535 schema := catalog.MockSchema(2, 1) 536 schema.BlockMaxRows = 1000 537 schema.ObjectMaxBlocks = 2 538 tae.BindSchema(schema) 539 bat := catalog.MockBatch(schema, 1) 540 defer bat.Close() 541 v := bat.Vecs[schema.GetSingleSortKeyIdx()].Get(0) 542 filter := handle.NewEQFilter(v) 543 544 txn, err := tae.StartTxn(nil) 545 assert.Nil(t, err) 546 db, err := txn.CreateDatabase("db", "", "") 547 assert.Nil(t, err) 548 tbl, err := db.CreateRelation(schema) 549 assert.Nil(t, err) 550 err = tbl.Append(context.Background(), bat) 551 assert.Nil(t, err) 552 for i := 0; i < 100; i++ { 553 err = tbl.UpdateByFilter(context.Background(), filter, 0, int32(33), false) 554 assert.NoError(t, err) 555 err = tbl.Append(context.Background(), bat) 556 assert.Error(t, err) 557 err = tbl.DeleteByFilter(context.Background(), filter) 558 assert.NoError(t, err) 559 err = tbl.Append(context.Background(), bat) 560 assert.NoError(t, err) 561 } 562 assert.NoError(t, txn.Commit(context.Background())) 563 564 for i := 0; i < 10; i++ { 565 txn, rel := tae.GetRelation() 566 err = rel.UpdateByFilter(context.Background(), filter, 0, int32(33), false) 567 assert.NoError(t, err) 568 assert.NoError(t, txn.Commit(context.Background())) 569 570 txn, rel = tae.GetRelation() 571 blkID, row, err := rel.GetByFilter(context.Background(), filter) 572 assert.NoError(t, err) 573 err = rel.RangeDelete(blkID, row, row, handle.DT_Normal) 574 assert.NoError(t, err) 575 assert.NoError(t, txn.Commit(context.Background())) 576 577 txn, rel = tae.GetRelation() 578 err = rel.Append(context.Background(), bat) 579 assert.Nil(t, err) 580 assert.Nil(t, txn.Commit(context.Background())) 581 } 582 583 tae.Restart(ctx) 584 585 txn, rel := tae.GetRelation() 586 assert.Equal(t, uint64(1), rel.GetMeta().(*catalog.TableEntry).GetRows()) 587 assert.NoError(t, txn.Commit(context.Background())) 588 589 txn, _ = tae.GetRelation() 590 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 591 assert.NoError(t, err) 592 assert.NoError(t, txn.Commit(context.Background())) 593 } 594 595 // append, delete, compact, mergeblocks, ckp 596 // 1. Create db and tbl and Append and Delete 597 // 2. Append and Delete 598 // replay 599 // check rows 600 /* TODO 601 1. Ckp 602 replay and check rows 603 1. compact 604 replay and check rows */ 605 func TestReplayTableRows(t *testing.T) { 606 defer testutils.AfterTest(t)() 607 testutils.EnsureNoLeak(t) 608 ctx := context.Background() 609 610 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 611 schema := catalog.MockSchema(2, 1) 612 schema.BlockMaxRows = 1000 613 schema.ObjectMaxBlocks = 2 614 bat := catalog.MockBatch(schema, 4800) 615 defer bat.Close() 616 bats := bat.Split(3) 617 rows := uint64(0) 618 619 txn, err := tae.StartTxn(nil) 620 assert.Nil(t, err) 621 e, err := txn.CreateDatabase("db", "", "") 622 assert.Nil(t, err) 623 tbl, err := e.CreateRelation(schema) 624 assert.Nil(t, err) 625 err = tbl.Append(context.Background(), bats[0]) 626 assert.Nil(t, err) 627 rows += 1600 628 blkIterator := tbl.MakeObjectIt() 629 blkID := blkIterator.GetObject().Fingerprint() 630 err = tbl.RangeDelete(blkID, 0, 99, handle.DT_Normal) 631 assert.Nil(t, err) 632 rows -= 100 633 assert.Nil(t, txn.Commit(context.Background())) 634 635 txn, err = tae.StartTxn(nil) 636 assert.Nil(t, err) 637 e, err = txn.GetDatabase("db") 638 assert.Nil(t, err) 639 tbl, err = e.GetRelationByName(schema.Name) 640 assert.Nil(t, err) 641 err = tbl.Append(context.Background(), bats[1]) 642 assert.Nil(t, err) 643 rows += 1600 644 blkIterator = tbl.MakeObjectIt() 645 blkID = blkIterator.GetObject().Fingerprint() 646 err = tbl.RangeDelete(blkID, 0, 99, handle.DT_Normal) 647 assert.Nil(t, err) 648 rows -= 100 649 assert.Nil(t, txn.Commit(context.Background())) 650 651 tae.Close() 652 653 tae2, err := db.Open(ctx, tae.Dir, nil) 654 assert.Nil(t, err) 655 656 txn, err = tae2.StartTxn(nil) 657 assert.Nil(t, err) 658 e, err = txn.GetDatabase("db") 659 assert.Nil(t, err) 660 tbl, err = e.GetRelationByName(schema.Name) 661 assert.Nil(t, err) 662 assert.Equal(t, rows, tbl.GetMeta().(*catalog.TableEntry).GetRows()) 663 assert.Nil(t, txn.Commit(context.Background())) 664 665 err = tae2.Close() 666 assert.Nil(t, err) 667 668 tae3, err := db.Open(ctx, tae.Dir, nil) 669 assert.Nil(t, err) 670 671 txn, err = tae3.StartTxn(nil) 672 assert.Nil(t, err) 673 assert.Nil(t, err) 674 e, err = txn.GetDatabase("db") 675 assert.Nil(t, err) 676 tbl, err = e.GetRelationByName(schema.Name) 677 assert.Nil(t, err) 678 assert.Equal(t, rows, tbl.GetMeta().(*catalog.TableEntry).GetRows()) 679 assert.Nil(t, txn.Commit(context.Background())) 680 681 // worker := ops.NewOpWorker("xx") 682 // worker.Start() 683 // txn, err = tae3.StartTxn(nil) 684 // assert.Nil(t, err) 685 // db, err = txn.GetDatabase("db") 686 // assert.Nil(t, err) 687 // tbl, err = db.GetRelationByName(schema.Name) 688 // assert.Nil(t, err) 689 // blkIterator := tbl.MakeBlockIt() 690 // blks := make([]*catalog.BlockEntry, 0) 691 // for blkIterator.Valid() { 692 // blk := blkIterator.GetBlock().GetMeta().(*catalog.BlockEntry) 693 // blks = append(blks, blk) 694 // blkIterator.Next() 695 // } 696 // for _, blk := range blks { 697 // ctx := &tasks.Context{Waitable: true} 698 // task, err := jobs.NewCompactBlockTask(ctx, txn, blk, tae3.Scheduler) 699 // assert.Nil(t, err) 700 // worker.SendOp(task) 701 // err = task.WaitDone() 702 // assert.Nil(t, err) 703 // } 704 // assert.Nil(t, txn.Commit(context.Background())) 705 // worker.Stop() 706 707 err = tae3.Close() 708 assert.Nil(t, err) 709 710 // tae4, err := Open(tae.Dir, nil) 711 // assert.Nil(t, err) 712 713 // txn, err = tae4.StartTxn(nil) 714 // assert.Nil(t, err) 715 // assert.Nil(t, err) 716 // db, err = txn.GetDatabase("db") 717 // assert.Nil(t, err) 718 // tbl, err = db.GetRelationByName(schema.Name) 719 // assert.Nil(t, err) 720 // assert.Equal(t, rows, tbl.GetMeta().(*catalog.TableEntry).GetRows()) 721 // assert.Nil(t, txn.Commit(context.Background())) 722 723 // err = tae4.Close() 724 // assert.Nil(t, err) 725 } 726 727 // Testing Steps 728 func TestReplay4(t *testing.T) { 729 defer testutils.AfterTest(t)() 730 testutils.EnsureNoLeak(t) 731 ctx := context.Background() 732 733 opts := config.WithLongScanAndCKPOpts(nil) 734 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 735 736 schema := catalog.MockSchemaAll(18, 16) 737 schema.BlockMaxRows = 10 738 schema.ObjectMaxBlocks = 2 739 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*uint32(schema.ObjectMaxBlocks+1)+1)) 740 defer bat.Close() 741 bats := bat.Split(4) 742 743 testutil.CreateRelationAndAppend(t, 0, tae, testutil.DefaultTestDB, schema, bats[0], true) 744 txn, rel := testutil.GetDefaultRelation(t, tae, schema.Name) 745 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), false) 746 assert.NoError(t, txn.Commit(context.Background())) 747 748 _ = tae.Close() 749 750 tae2, err := db.Open(ctx, tae.Dir, nil) 751 assert.NoError(t, err) 752 753 txn, rel = testutil.GetDefaultRelation(t, tae2, schema.Name) 754 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), false) 755 err = rel.Append(context.Background(), bats[1]) 756 assert.NoError(t, err) 757 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length()+bats[1].Length(), false) 758 assert.NoError(t, txn.Commit(context.Background())) 759 760 testutil.CompactBlocks(t, 0, tae2, testutil.DefaultTestDB, schema, false) 761 txn, rel = testutil.GetDefaultRelation(t, tae2, schema.Name) 762 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length()+bats[1].Length(), false) 763 err = rel.Append(context.Background(), bats[2]) 764 assert.NoError(t, err) 765 testutil.CheckAllColRowsByScan(t, rel, 766 bats[0].Length()+bats[1].Length()+bats[2].Length(), false) 767 assert.NoError(t, txn.Commit(context.Background())) 768 769 testutil.CompactBlocks(t, 0, tae2, testutil.DefaultTestDB, schema, false) 770 771 txn, rel = testutil.GetDefaultRelation(t, tae2, schema.Name) 772 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:3]), false) 773 assert.NoError(t, txn.Commit(context.Background())) 774 775 testutil.MergeBlocks(t, 0, tae2, testutil.DefaultTestDB, schema, false) 776 777 txn, rel = testutil.GetDefaultRelation(t, tae2, schema.Name) 778 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:3]), false) 779 err = rel.Append(context.Background(), bats[3]) 780 assert.NoError(t, err) 781 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), false) 782 assert.NoError(t, txn.Commit(context.Background())) 783 t.Log(tae2.Catalog.SimplePPString(common.PPL1)) 784 785 tae2.Close() 786 787 tae3, err := db.Open(ctx, tae.Dir, nil) 788 assert.NoError(t, err) 789 defer tae3.Close() 790 } 791 792 // Testing Steps 793 func TestReplay5(t *testing.T) { 794 defer testutils.AfterTest(t)() 795 testutils.EnsureNoLeak(t) 796 ctx := context.Background() 797 798 opts := config.WithLongScanAndCKPOpts(nil) 799 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 800 801 schema := catalog.MockSchemaAll(18, 16) 802 schema.BlockMaxRows = 10 803 schema.ObjectMaxBlocks = 2 804 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*uint32(schema.ObjectMaxBlocks+1)+1)) 805 defer bat.Close() 806 bats := bat.Split(8) 807 808 testutil.CreateRelationAndAppend(t, 0, tae, testutil.DefaultTestDB, schema, bats[0], true) 809 txn, rel := testutil.GetDefaultRelation(t, tae, schema.Name) 810 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), false) 811 assert.NoError(t, txn.Commit(context.Background())) 812 813 _ = tae.Close() 814 tae, err := db.Open(ctx, tae.Dir, nil) 815 assert.NoError(t, err) 816 817 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 818 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), false) 819 err = rel.Append(context.Background(), bats[0]) 820 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 821 err = rel.Append(context.Background(), bats[1]) 822 assert.NoError(t, err) 823 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:2]), false) 824 assert.NoError(t, txn.Commit(context.Background())) 825 t.Logf("LSN=%d", txn.GetLSN()) 826 827 _ = tae.Close() 828 tae, err = db.Open(ctx, tae.Dir, nil) 829 assert.NoError(t, err) 830 831 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 832 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:2]), false) 833 err = rel.Append(context.Background(), bats[0]) 834 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 835 err = rel.Append(context.Background(), bats[1]) 836 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 837 err = rel.Append(context.Background(), bats[2]) 838 assert.NoError(t, err) 839 err = rel.Append(context.Background(), bats[3]) 840 assert.NoError(t, err) 841 assert.NoError(t, txn.Commit(context.Background())) 842 843 testutil.CompactBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 844 err = tae.BGCheckpointRunner.ForceFlushWithInterval(tae.TxnMgr.Now(), context.Background(), time.Second*2, time.Millisecond*10) 845 assert.NoError(t, err) 846 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 847 assert.NoError(t, err) 848 lsn := tae.BGCheckpointRunner.MaxLSNInRange(tae.TxnMgr.Now()) 849 entry, err := tae.Wal.RangeCheckpoint(1, lsn) 850 assert.NoError(t, err) 851 err = entry.WaitDone() 852 assert.NoError(t, err) 853 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 854 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[:4]), false) 855 assert.NoError(t, txn.Commit(context.Background())) 856 857 _ = tae.Close() 858 tae, err = db.Open(ctx, tae.Dir, nil) 859 assert.NoError(t, err) 860 861 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 862 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[:4]), false) 863 err = rel.Append(context.Background(), bats[3]) 864 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 865 for _, b := range bats[4:8] { 866 err = rel.Append(context.Background(), b) 867 assert.NoError(t, err) 868 } 869 assert.NoError(t, txn.Commit(context.Background())) 870 testutil.CompactBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 871 err = tae.BGCheckpointRunner.ForceFlushWithInterval(tae.TxnMgr.Now(), context.Background(), time.Second*2, time.Millisecond*10) 872 assert.NoError(t, err) 873 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 874 assert.NoError(t, err) 875 lsn = tae.BGCheckpointRunner.MaxLSNInRange(tae.TxnMgr.Now()) 876 entry, err = tae.Wal.RangeCheckpoint(1, lsn) 877 assert.NoError(t, err) 878 assert.NoError(t, entry.WaitDone()) 879 880 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 881 testutil.PrintCheckpointStats(t, tae) 882 _ = tae.Close() 883 tae, err = db.Open(ctx, tae.Dir, nil) 884 assert.NoError(t, err) 885 886 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 887 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[:8]), false) 888 err = rel.Append(context.Background(), bats[0]) 889 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 890 assert.NoError(t, txn.Commit(context.Background())) 891 // testutils.WaitExpect(3000, func() bool { 892 // return tae.Wal.GetCheckpointed() == tae.Wal.GetCurrSeqNum()/2 893 // }) 894 // testutil.PrintCheckpointStats(t, tae) 895 // assert.Equal(t, tae.Wal.GetCurrSeqNum()/2, tae.Wal.GetCheckpointed()) 896 testutil.MergeBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 897 898 _ = tae.Close() 899 tae, err = db.Open(ctx, tae.Dir, nil) 900 assert.NoError(t, err) 901 defer tae.Close() 902 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 903 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[:8]), false) 904 err = rel.Append(context.Background(), bats[0]) 905 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 906 assert.NoError(t, txn.Commit(context.Background())) 907 908 err = tae.BGCheckpointRunner.ForceFlushWithInterval(tae.TxnMgr.Now(), context.Background(), time.Second*2, time.Millisecond*10) 909 assert.NoError(t, err) 910 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 911 assert.NoError(t, err) 912 lsn = tae.BGCheckpointRunner.MaxLSNInRange(tae.TxnMgr.Now()) 913 entry, err = tae.Wal.RangeCheckpoint(1, lsn) 914 assert.NoError(t, err) 915 assert.NoError(t, entry.WaitDone()) 916 testutils.WaitExpect(1000, func() bool { 917 return tae.Runtime.Scheduler.GetPenddingLSNCnt() == 0 918 }) 919 testutil.PrintCheckpointStats(t, tae) 920 assert.Equal(t, tae.Wal.GetCurrSeqNum(), tae.Wal.GetCheckpointed()) 921 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 922 } 923 924 func TestReplay6(t *testing.T) { 925 defer testutils.AfterTest(t)() 926 testutils.EnsureNoLeak(t) 927 ctx := context.Background() 928 929 opts := config.WithLongScanAndCKPOpts(nil) 930 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 931 schema := catalog.MockSchemaAll(18, 15) 932 schema.BlockMaxRows = 10 933 schema.ObjectMaxBlocks = 2 934 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*10-1)) 935 defer bat.Close() 936 bats := bat.Split(4) 937 938 testutil.CreateRelationAndAppend(t, 0, tae, testutil.DefaultTestDB, schema, bats[0], true) 939 940 _ = tae.Close() 941 tae, err := db.Open(ctx, tae.Dir, opts) 942 assert.NoError(t, err) 943 944 txn, rel := testutil.GetDefaultRelation(t, tae, schema.Name) 945 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:1]), false) 946 err = rel.Append(context.Background(), bats[0]) 947 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 948 err = rel.Append(context.Background(), bats[1]) 949 assert.NoError(t, err) 950 assert.NoError(t, txn.Commit(context.Background())) 951 952 testutil.CompactBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 953 954 _ = tae.Close() 955 tae, err = db.Open(ctx, tae.Dir, opts) 956 assert.NoError(t, err) 957 958 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 959 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:2]), false) 960 err = rel.Append(context.Background(), bats[0]) 961 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 962 err = rel.Append(context.Background(), bats[2]) 963 assert.NoError(t, err) 964 err = rel.Append(context.Background(), bats[3]) 965 assert.NoError(t, err) 966 assert.NoError(t, txn.Commit(context.Background())) 967 testutil.CompactBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 968 testutil.MergeBlocks(t, 0, tae, testutil.DefaultTestDB, schema, false) 969 err = tae.BGCheckpointRunner.ForceFlushWithInterval(tae.TxnMgr.Now(), context.Background(), time.Second*2, time.Millisecond*10) 970 assert.NoError(t, err) 971 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 972 assert.NoError(t, err) 973 974 _ = tae.Close() 975 tae, err = db.Open(ctx, tae.Dir, opts) 976 assert.NoError(t, err) 977 978 txn, rel = testutil.GetDefaultRelation(t, tae, schema.Name) 979 testutil.CheckAllColRowsByScan(t, rel, testutil.LenOfBats(bats[0:4]), false) 980 assert.NoError(t, txn.Commit(context.Background())) 981 testutil.PrintCheckpointStats(t, tae) 982 _ = tae.Close() 983 } 984 985 func TestReplay7(t *testing.T) { 986 defer testutils.AfterTest(t)() 987 t.Skip(any("This case crashes occasionally, is being fixed, skip it for now")) 988 testutils.EnsureNoLeak(t) 989 ctx := context.Background() 990 991 opts := config.WithQuickScanAndCKPOpts(nil) 992 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 993 schema := catalog.MockSchemaAll(18, 14) 994 schema.BlockMaxRows = 10 995 schema.ObjectMaxBlocks = 5 996 997 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*15+1)) 998 defer bat.Close() 999 testutil.CreateRelationAndAppend(t, 0, tae, testutil.DefaultTestDB, schema, bat, true) 1000 testutil.CompactBlocks(t, 0, tae, testutil.DefaultTestDB, schema, true) 1001 testutil.MergeBlocks(t, 0, tae, testutil.DefaultTestDB, schema, true) 1002 time.Sleep(time.Millisecond * 100) 1003 1004 _ = tae.Close() 1005 tae, err := db.Open(ctx, tae.Dir, opts) 1006 assert.NoError(t, err) 1007 defer tae.Close() 1008 // t.Log(tae.Catalog.SimplePPString(common.PPL1)) 1009 txn, rel := testutil.GetDefaultRelation(t, tae, schema.Name) 1010 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), false) 1011 assert.NoError(t, txn.Commit(context.Background())) 1012 } 1013 1014 func TestReplay8(t *testing.T) { 1015 defer testutils.AfterTest(t)() 1016 testutils.EnsureNoLeak(t) 1017 ctx := context.Background() 1018 1019 opts := config.WithLongScanAndCKPOpts(nil) 1020 tae := testutil.NewTestEngine(ctx, ModuleName, t, opts) 1021 defer tae.Close() 1022 schema := catalog.MockSchemaAll(18, 13) 1023 schema.BlockMaxRows = 10 1024 schema.ObjectMaxBlocks = 2 1025 tae.BindSchema(schema) 1026 1027 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*3+1)) 1028 defer bat.Close() 1029 bats := bat.Split(4) 1030 1031 tae.CreateRelAndAppend(bats[0], true) 1032 txn, rel := tae.GetRelation() 1033 v := testutil.GetSingleSortKeyValue(bats[0], schema, 2) 1034 filter := handle.NewEQFilter(v) 1035 err := rel.DeleteByFilter(context.Background(), filter) 1036 assert.NoError(t, err) 1037 assert.NoError(t, txn.Commit(context.Background())) 1038 1039 txn, rel = tae.GetRelation() 1040 window := bat.CloneWindow(2, 1) 1041 defer window.Close() 1042 err = rel.Append(context.Background(), window) 1043 assert.NoError(t, err) 1044 _ = txn.Rollback(context.Background()) 1045 1046 tae.Restart(ctx) 1047 1048 // Check the total rows by scan 1049 txn, rel = tae.GetRelation() 1050 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length()-1, true) 1051 err = rel.Append(context.Background(), bats[0]) 1052 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 1053 assert.NoError(t, txn.Commit(context.Background())) 1054 1055 // Try to append the delete row and then rollback 1056 txn, rel = tae.GetRelation() 1057 err = rel.Append(context.Background(), window) 1058 assert.NoError(t, err) 1059 _ = txn.Rollback(context.Background()) 1060 1061 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1062 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length()-1, true) 1063 assert.NoError(t, txn.Commit(context.Background())) 1064 1065 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1066 err = rel.Append(context.Background(), window) 1067 assert.NoError(t, err) 1068 _ = txn.Rollback(context.Background()) 1069 1070 tae.Restart(ctx) 1071 1072 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1073 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length()-1, true) 1074 assert.NoError(t, txn.Commit(context.Background())) 1075 1076 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1077 err = rel.Append(context.Background(), window) 1078 assert.NoError(t, err) 1079 tuple3 := bat.Window(3, 1) 1080 err = rel.Append(context.Background(), tuple3) 1081 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 1082 assert.NoError(t, txn.Commit(context.Background())) 1083 1084 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1085 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), true) 1086 assert.NoError(t, txn.Commit(context.Background())) 1087 1088 tae.Restart(ctx) 1089 1090 txn, rel = testutil.GetDefaultRelation(t, tae.DB, schema.Name) 1091 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), true) 1092 err = rel.Append(context.Background(), window) 1093 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 1094 err = rel.Append(context.Background(), bats[1]) 1095 assert.NoError(t, err) 1096 err = rel.Append(context.Background(), bats[2]) 1097 assert.NoError(t, err) 1098 err = rel.Append(context.Background(), bats[3]) 1099 assert.NoError(t, err) 1100 assert.NoError(t, txn.Commit(context.Background())) 1101 1102 tae.CompactBlocks(false) 1103 tae.Restart(ctx) 1104 1105 txn, rel = tae.GetRelation() 1106 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), true) 1107 err = rel.Append(context.Background(), window) 1108 assert.True(t, moerr.IsMoErrCode(err, moerr.ErrDuplicateEntry)) 1109 1110 v0_5 := testutil.GetSingleSortKeyValue(bats[0], schema, 5) 1111 filter = handle.NewEQFilter(v0_5) 1112 err = rel.DeleteByFilter(context.Background(), filter) 1113 assert.NoError(t, err) 1114 v1_5 := testutil.GetSingleSortKeyValue(bats[1], schema, 5) 1115 filter = handle.NewEQFilter(v1_5) 1116 err = rel.DeleteByFilter(context.Background(), filter) 1117 assert.NoError(t, err) 1118 v2_5 := testutil.GetSingleSortKeyValue(bats[2], schema, 5) 1119 filter = handle.NewEQFilter(v2_5) 1120 err = rel.DeleteByFilter(context.Background(), filter) 1121 assert.NoError(t, err) 1122 v3_2 := testutil.GetSingleSortKeyValue(bats[3], schema, 2) 1123 filter = handle.NewEQFilter(v3_2) 1124 err = rel.DeleteByFilter(context.Background(), filter) 1125 assert.NoError(t, err) 1126 assert.NoError(t, txn.Commit(context.Background())) 1127 // t.Log(tae.Catalog.SimplePPString(common.PPL1)) 1128 err = tae.BGCheckpointRunner.ForceFlushWithInterval(tae.TxnMgr.Now(), context.Background(), time.Second*2, time.Millisecond*10) 1129 assert.NoError(t, err) 1130 1131 tae.Restart(ctx) 1132 1133 txn, rel = tae.GetRelation() 1134 testutil.CheckAllColRowsByScan(t, rel, bat.Length()-4, true) 1135 tuple0_5 := bat.Window(5, 1) 1136 err = rel.Append(context.Background(), tuple0_5) 1137 assert.NoError(t, err) 1138 tuple1_5 := bats[1].Window(5, 1) 1139 err = rel.Append(context.Background(), tuple1_5) 1140 assert.NoError(t, err) 1141 tuple2_5 := bats[2].Window(5, 1) 1142 err = rel.Append(context.Background(), tuple2_5) 1143 assert.NoError(t, err) 1144 tuple3_2 := bats[3].Window(2, 1) 1145 err = rel.Append(context.Background(), tuple3_2) 1146 assert.NoError(t, err) 1147 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), true) 1148 _ = txn.Rollback(context.Background()) 1149 1150 tae.CompactBlocks(false) 1151 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 1152 assert.NoError(t, err) 1153 tae.Restart(ctx) 1154 1155 txn, rel = tae.GetRelation() 1156 testutil.CheckAllColRowsByScan(t, rel, bat.Length()-4, true) 1157 err = rel.Append(context.Background(), tuple0_5) 1158 assert.NoError(t, err) 1159 err = rel.Append(context.Background(), tuple1_5) 1160 assert.NoError(t, err) 1161 err = rel.Append(context.Background(), tuple2_5) 1162 assert.NoError(t, err) 1163 err = rel.Append(context.Background(), tuple3_2) 1164 assert.NoError(t, err) 1165 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), true) 1166 _ = txn.Rollback(context.Background()) 1167 } 1168 1169 func TestReplay9(t *testing.T) { 1170 defer testutils.AfterTest(t)() 1171 testutils.EnsureNoLeak(t) 1172 ctx := context.Background() 1173 1174 opts := config.WithLongScanAndCKPOpts(nil) 1175 tae := testutil.NewTestEngine(ctx, ModuleName, t, opts) 1176 defer tae.Close() 1177 schema := catalog.MockSchemaAll(18, 3) 1178 schema.BlockMaxRows = 10 1179 schema.ObjectMaxBlocks = 2 1180 tae.BindSchema(schema) 1181 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows*3+2)) 1182 defer bat.Close() 1183 bats := bat.Split(4) 1184 1185 tae.CreateRelAndAppend(bats[0], true) 1186 txn, rel := tae.GetRelation() 1187 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), false) 1188 v := testutil.GetSingleSortKeyValue(bats[0], schema, 2) 1189 filter := handle.NewEQFilter(v) 1190 err := rel.UpdateByFilter(context.Background(), filter, 2, int32(999), false) 1191 assert.NoError(t, err) 1192 assert.NoError(t, txn.Commit(context.Background())) 1193 1194 tae.Restart(ctx) 1195 txn, rel = tae.GetRelation() 1196 actv, _, err := rel.GetValueByFilter(context.Background(), filter, 2) 1197 assert.NoError(t, err) 1198 assert.Equal(t, int32(999), actv) 1199 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), true) 1200 assert.NoError(t, txn.Commit(context.Background())) 1201 1202 tae.CompactBlocks(false) 1203 1204 tae.Restart(ctx) 1205 txn, rel = tae.GetRelation() 1206 _, _, err = rel.GetByFilter(context.Background(), filter) 1207 assert.NoError(t, err) 1208 actv, _, err = rel.GetValueByFilter(context.Background(), filter, 2) 1209 assert.NoError(t, err) 1210 assert.Equal(t, int32(999), actv) 1211 testutil.CheckAllColRowsByScan(t, rel, bats[0].Length(), true) 1212 err = rel.Append(context.Background(), bats[1]) 1213 assert.NoError(t, err) 1214 assert.NoError(t, txn.Commit(context.Background())) 1215 tae.CompactBlocks(false) 1216 1217 txn, rel = tae.GetRelation() 1218 v2 := testutil.GetSingleSortKeyValue(bats[0], schema, 4) 1219 filter2 := handle.NewEQFilter(v2) 1220 err = rel.UpdateByFilter(context.Background(), filter2, 1, int16(199), false) 1221 assert.NoError(t, err) 1222 actv, _, err = rel.GetValueByFilter(context.Background(), filter2, 1) 1223 assert.NoError(t, err) 1224 assert.Equal(t, int16(199), actv) 1225 assert.NoError(t, txn.Commit(context.Background())) 1226 1227 tae.Restart(ctx) 1228 1229 txn, rel = tae.GetRelation() 1230 actv, _, err = rel.GetValueByFilter(context.Background(), filter2, 1) 1231 assert.NoError(t, err) 1232 assert.Equal(t, int16(199), actv) 1233 err = rel.Append(context.Background(), bats[2]) 1234 assert.NoError(t, err) 1235 err = rel.Append(context.Background(), bats[3]) 1236 assert.NoError(t, err) 1237 assert.NoError(t, txn.Commit(context.Background())) 1238 1239 tae.CompactBlocks(false) 1240 tae.MergeBlocks(false) 1241 1242 txn, rel = tae.GetRelation() 1243 v3 := testutil.GetSingleSortKeyValue(bats[1], schema, 3) 1244 filter3 := handle.NewEQFilter(v3) 1245 err = rel.UpdateByFilter(context.Background(), filter3, 5, uint16(88), false) 1246 assert.NoError(t, err) 1247 actv, _, err = rel.GetValueByFilter(context.Background(), filter3, 5) 1248 assert.NoError(t, err) 1249 assert.Equal(t, uint16(88), actv) 1250 assert.NoError(t, txn.Commit(context.Background())) 1251 1252 tae.Restart(ctx) 1253 1254 txn, rel = tae.GetRelation() 1255 actv, _, err = rel.GetValueByFilter(context.Background(), filter, 2) 1256 assert.NoError(t, err) 1257 assert.Equal(t, int32(999), actv) 1258 actv, _, err = rel.GetValueByFilter(context.Background(), filter2, 1) 1259 assert.NoError(t, err) 1260 assert.Equal(t, int16(199), actv) 1261 actv, _, err = rel.GetValueByFilter(context.Background(), filter3, 5) 1262 assert.NoError(t, err) 1263 assert.Equal(t, uint16(88), actv) 1264 assert.NoError(t, txn.Commit(context.Background())) 1265 } 1266 1267 func TestReplay10(t *testing.T) { 1268 defer testutils.AfterTest(t)() 1269 testutils.EnsureNoLeak(t) 1270 ctx := context.Background() 1271 1272 opts := config.WithQuickScanAndCKPOpts(nil) 1273 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 1274 schema := catalog.MockSchemaAll(3, 2) 1275 schema.BlockMaxRows = 10 1276 schema.ObjectMaxBlocks = 5 1277 expr := &plan.Expr{} 1278 exprbuf, err := expr.Marshal() 1279 assert.NoError(t, err) 1280 1281 schema.ColDefs[1].Default, _ = types.Encode(&plan.Default{ 1282 NullAbility: false, 1283 Expr: &plan.Expr{}, 1284 }) 1285 schema.ColDefs[1].OnUpdate, _ = types.Encode(&plan.OnUpdate{ 1286 Expr: &plan.Expr{}, 1287 }) 1288 schema.ColDefs[2].Default, _ = types.Encode(&plan.Default{ 1289 NullAbility: true, 1290 Expr: nil, 1291 }) 1292 schema.ColDefs[2].OnUpdate, _ = types.Encode(&plan.OnUpdate{ 1293 Expr: nil, 1294 }) 1295 1296 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows)) 1297 defer bat.Close() 1298 testutil.CreateRelationAndAppend(t, 0, tae, testutil.DefaultTestDB, schema, bat, true) 1299 time.Sleep(time.Millisecond * 100) 1300 1301 _ = tae.Close() 1302 tae, err = db.Open(ctx, tae.Dir, opts) 1303 assert.NoError(t, err) 1304 defer tae.Close() 1305 // t.Log(tae.Catalog.SimplePPString(common.PPL1)) 1306 txn, rel := testutil.GetDefaultRelation(t, tae, schema.Name) 1307 testutil.CheckAllColRowsByScan(t, rel, bat.Length(), false) 1308 assert.NoError(t, txn.Commit(context.Background())) 1309 schema1 := rel.Schema().(*catalog.Schema) 1310 1311 d1 := &plan.Default{} 1312 assert.NoError(t, types.Decode(schema1.ColDefs[1].Default, d1)) 1313 buf, _ := d1.Expr.Marshal() 1314 assert.Equal(t, exprbuf, buf) 1315 u1 := &plan.OnUpdate{} 1316 assert.NoError(t, types.Decode(schema1.ColDefs[1].OnUpdate, u1)) 1317 buf, _ = u1.Expr.Marshal() 1318 assert.Equal(t, exprbuf, buf) 1319 1320 d2 := &plan.Default{} 1321 assert.NoError(t, types.Decode(schema1.ColDefs[2].Default, d2)) 1322 assert.Nil(t, d2.Expr) 1323 u2 := &plan.OnUpdate{} 1324 assert.NoError(t, types.Decode(schema1.ColDefs[2].OnUpdate, u2)) 1325 assert.Nil(t, u2.Expr) 1326 assert.True(t, d2.NullAbility) 1327 } 1328 1329 // create db,tbl,obj,blk 1330 // checkpoint 1331 // softdelete obj 1332 // checkpoint 1333 // Restart 1334 func TestReplaySnapshots(t *testing.T) { 1335 defer testutils.AfterTest(t)() 1336 ctx := context.Background() 1337 1338 opts := config.WithLongScanAndCKPOpts(nil) 1339 tae := testutil.NewTestEngine(ctx, ModuleName, t, opts) 1340 schema := catalog.MockSchemaAll(1, -1) 1341 1342 txn, err := tae.StartTxn(nil) 1343 assert.NoError(t, err) 1344 db, err := txn.CreateDatabase("db", "", "") 1345 assert.NoError(t, err) 1346 rel, err := db.CreateRelation(schema) 1347 assert.NoError(t, err) 1348 obj, err := rel.CreateObject(false) 1349 assert.NoError(t, err) 1350 assert.NoError(t, txn.Commit(context.Background())) 1351 1352 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 1353 assert.NoError(t, err) 1354 1355 txn, err = tae.StartTxn(nil) 1356 assert.NoError(t, err) 1357 db, err = txn.GetDatabase("db") 1358 assert.NoError(t, err) 1359 rel, err = db.GetRelationByName(schema.Name) 1360 assert.NoError(t, err) 1361 err = rel.SoftDeleteObject(obj.GetID()) 1362 assert.NoError(t, err) 1363 objMeta := obj.GetMeta().(*catalog.ObjectEntry) 1364 objMeta.Lock() 1365 baseNode := objMeta.GetLatestNodeLocked().BaseNode 1366 err = objectio.SetObjectStatsSize(&baseNode.ObjectStats, 1) 1367 assert.Nil(t, err) 1368 err = objectio.SetObjectStatsRowCnt(&baseNode.ObjectStats, 1) 1369 assert.Nil(t, err) 1370 assert.False(t, baseNode.IsEmpty()) 1371 objMeta.Unlock() 1372 assert.NoError(t, txn.Commit(context.Background())) 1373 1374 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 1375 assert.NoError(t, err) 1376 t.Log(tae.Catalog.SimplePPString(3)) 1377 1378 tae.Restart(ctx) 1379 t.Log(tae.Catalog.SimplePPString(3)) 1380 1381 tae.Close() 1382 } 1383 1384 func TestReplayDatabaseEntry(t *testing.T) { 1385 defer testutils.AfterTest(t)() 1386 ctx := context.Background() 1387 1388 opts := config.WithLongScanAndCKPOpts(nil) 1389 tae := testutil.NewTestEngine(ctx, ModuleName, t, opts) 1390 defer tae.Close() 1391 1392 datypStr := "datyp" 1393 createSqlStr := "createSql" 1394 1395 txn, err := tae.StartTxn(nil) 1396 assert.NoError(t, err) 1397 db, err := txn.CreateDatabase("db", createSqlStr, datypStr) 1398 assert.NoError(t, err) 1399 dbID := db.GetID() 1400 assert.NoError(t, txn.Commit(context.Background())) 1401 1402 tae.Restart(ctx) 1403 t.Log(tae.Catalog.SimplePPString(3)) 1404 dbEntry, err := tae.Catalog.GetDatabaseByID(dbID) 1405 assert.NoError(t, err) 1406 assert.Equal(t, datypStr, dbEntry.GetDatType()) 1407 assert.Equal(t, createSqlStr, dbEntry.GetCreateSql()) 1408 1409 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(tae.TxnMgr.Now(), false) 1410 assert.NoError(t, err) 1411 1412 tae.Restart(ctx) 1413 dbEntry, err = tae.Catalog.GetDatabaseByID(dbID) 1414 assert.NoError(t, err) 1415 assert.Equal(t, datypStr, dbEntry.GetDatType()) 1416 assert.Equal(t, createSqlStr, dbEntry.GetCreateSql()) 1417 }