github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/test/catalog_test.go (about) 1 // Copyright 2021 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 "sync" 20 "testing" 21 "time" 22 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 25 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog" 26 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 27 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/testutil" 28 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 29 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config" 30 31 "github.com/panjf2000/ants/v2" 32 "github.com/stretchr/testify/assert" 33 ) 34 35 func TestCatalog1(t *testing.T) { 36 defer testutils.AfterTest(t)() 37 testutils.EnsureNoLeak(t) 38 ctx := context.Background() 39 40 db := testutil.InitTestDB(ctx, ModuleName, t, nil) 41 defer db.Close() 42 43 schema := catalog.MockSchema(1, 0) 44 txn, _, rel := testutil.CreateRelationNoCommit(t, db, testutil.DefaultTestDB, schema, true) 45 // relMeta := rel.GetMeta().(*catalog.TableEntry) 46 obj, err := rel.CreateNonAppendableObject(false, nil) 47 assert.Nil(t, err) 48 assert.Nil(t, txn.Commit(context.Background())) 49 t.Log(db.Catalog.SimplePPString(common.PPL1)) 50 51 txn, rel = testutil.GetDefaultRelation(t, db, schema.Name) 52 sobj, err := rel.GetObject(obj.GetID()) 53 assert.Nil(t, err) 54 t.Log(sobj.String()) 55 56 t.Log(db.Catalog.SimplePPString(common.PPL1)) 57 assert.Nil(t, txn.Commit(context.Background())) 58 t.Log(db.Catalog.SimplePPString(common.PPL1)) 59 60 { 61 _, rel = testutil.GetDefaultRelation(t, db, schema.Name) 62 it := rel.MakeObjectIt() 63 cnt := 0 64 for it.Valid() { 65 object := it.GetObject() 66 cnt++ 67 t.Log(object.GetMeta().(*catalog.ObjectEntry).String()) 68 it.Next() 69 } 70 assert.Equal(t, 1, cnt) 71 } 72 } 73 74 func TestShowDatabaseNames(t *testing.T) { 75 defer testutils.AfterTest(t)() 76 testutils.EnsureNoLeak(t) 77 ctx := context.Background() 78 79 tae := testutil.InitTestDB(ctx, ModuleName, t, nil) 80 defer tae.Close() 81 82 { 83 txn, _ := tae.StartTxn(nil) 84 _, err := txn.CreateDatabase("db1", "", "") 85 assert.Nil(t, err) 86 names := txn.DatabaseNames() 87 assert.Equal(t, 2, len(names)) 88 assert.Equal(t, "db1", names[1]) 89 assert.Nil(t, txn.Commit(context.Background())) 90 } 91 { 92 txn, _ := tae.StartTxn(nil) 93 names := txn.DatabaseNames() 94 assert.Equal(t, 2, len(names)) 95 assert.Equal(t, "db1", names[1]) 96 _, err := txn.CreateDatabase("db2", "", "") 97 assert.Nil(t, err) 98 names = txn.DatabaseNames() 99 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 100 assert.Equal(t, 3, len(names)) 101 assert.Equal(t, "db1", names[1]) 102 assert.Equal(t, "db2", names[2]) 103 { 104 txn, _ := tae.StartTxn(nil) 105 names := txn.DatabaseNames() 106 assert.Equal(t, 2, len(names)) 107 assert.Equal(t, "db1", names[1]) 108 _, err := txn.CreateDatabase("db2", "", "") 109 assert.NotNil(t, err) 110 err = txn.Rollback(context.Background()) 111 assert.Nil(t, err) 112 } 113 { 114 txn, _ := tae.StartTxn(nil) 115 _, err := txn.CreateDatabase("db3", "", "") 116 assert.Nil(t, err) 117 names := txn.DatabaseNames() 118 assert.Equal(t, "db1", names[1]) 119 assert.Equal(t, "db3", names[2]) 120 assert.Nil(t, txn.Commit(context.Background())) 121 } 122 { 123 txn, _ := tae.StartTxn(nil) 124 names := txn.DatabaseNames() 125 assert.Equal(t, 3, len(names)) 126 assert.Equal(t, "db1", names[1]) 127 assert.Equal(t, "db3", names[2]) 128 _, err := txn.DropDatabase("db1") 129 assert.Nil(t, err) 130 names = txn.DatabaseNames() 131 t.Log(tae.Catalog.SimplePPString(common.PPL1)) 132 t.Log(names) 133 assert.Equal(t, 2, len(names)) 134 assert.Equal(t, "db3", names[1]) 135 assert.Nil(t, txn.Commit(context.Background())) 136 } 137 names = txn.DatabaseNames() 138 assert.Equal(t, 3, len(names)) 139 assert.Equal(t, "db1", names[1]) 140 assert.Equal(t, "db2", names[2]) 141 assert.Nil(t, txn.Commit(context.Background())) 142 } 143 } 144 145 func TestCheckpointCatalog2(t *testing.T) { 146 defer testutils.AfterTest(t)() 147 testutils.EnsureNoLeak(t) 148 ctx := context.Background() 149 150 opts := config.WithLongScanAndCKPOpts(nil) 151 tae := testutil.InitTestDB(ctx, ModuleName, t, opts) 152 defer tae.Close() 153 txn, _ := tae.StartTxn(nil) 154 schema := catalog.MockSchemaAll(13, 12) 155 db, err := txn.CreateDatabase("db", "", "") 156 assert.Nil(t, err) 157 _, err = db.CreateRelation(schema) 158 assert.Nil(t, err) 159 err = txn.Commit(context.Background()) 160 assert.Nil(t, err) 161 schema.BlockMaxRows = 10 162 batchCnt := 10 163 bat := catalog.MockBatch(schema, int(schema.BlockMaxRows)*batchCnt) 164 bats := bat.Split(batchCnt) 165 166 pool, _ := ants.NewPool(20) 167 defer pool.Release() 168 var wg sync.WaitGroup 169 mockRes := func(i int) func() { 170 return func() { 171 defer wg.Done() 172 txn, _ := tae.StartTxn(nil) 173 db, _ := txn.GetDatabase("db") 174 rel, _ := db.GetRelationByName(schema.Name) 175 err := rel.Append(context.Background(), bats[i]) 176 assert.Nil(t, err) 177 err = txn.Commit(context.Background()) 178 assert.Nil(t, err) 179 } 180 } 181 for i := 0; i < batchCnt; i++ { 182 wg.Add(1) 183 err := pool.Submit(mockRes(i)) 184 assert.Nil(t, err) 185 } 186 wg.Wait() 187 ts := types.BuildTS(time.Now().UTC().UnixNano(), 0) 188 err = tae.BGCheckpointRunner.ForceIncrementalCheckpoint(ts, false) 189 assert.NoError(t, err) 190 lsn := tae.BGCheckpointRunner.MaxLSNInRange(ts) 191 entry, err := tae.Wal.RangeCheckpoint(1, lsn) 192 assert.NoError(t, err) 193 assert.NoError(t, entry.WaitDone()) 194 testutils.WaitExpect(1000, func() bool { 195 return tae.Runtime.Scheduler.GetPenddingLSNCnt() == 0 196 }) 197 assert.Equal(t, tae.BGCheckpointRunner.MaxLSN(), tae.Runtime.Scheduler.GetCheckpointedLSN()) 198 }