github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/disttae/cache/catalog_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 cache 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/catalog" 21 "github.com/matrixorigin/matrixone/pkg/common/mpool" 22 "github.com/matrixorigin/matrixone/pkg/container/batch" 23 "github.com/matrixorigin/matrixone/pkg/container/types" 24 "github.com/matrixorigin/matrixone/pkg/container/vector" 25 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 26 "github.com/matrixorigin/matrixone/pkg/testutil" 27 "github.com/stretchr/testify/require" 28 ) 29 30 const ( 31 Rows = 10 32 ) 33 34 func TestGC(t *testing.T) { 35 mp := mpool.MustNewZero() 36 cc := NewCatalog() 37 // insert databases 38 dbBat := newTestDatabaseBatch(mp) 39 cc.InsertDatabase(dbBat) 40 // insert tables 41 tblBat := newTestTableBatch(mp) 42 colBat := newTestColumnBatch(t, tblBat, mp) 43 cc.InsertTable(tblBat) 44 cc.InsertColumns(colBat) 45 cc.GC(timestamp.Timestamp{ 46 PhysicalTime: 100, 47 }) 48 { 49 timestamps := vector.MustFixedCol[types.TS](dbBat.GetVector(MO_TIMESTAMP_IDX)) 50 names := vector.MustStrCol(dbBat.GetVector(catalog.MO_DATABASE_DAT_NAME_IDX + MO_OFF)) 51 accounts := vector.MustFixedCol[uint32](dbBat.GetVector(catalog.MO_DATABASE_ACCOUNT_ID_IDX + MO_OFF)) 52 key := new(DatabaseItem) 53 for i, account := range accounts { 54 key.Name = names[i] 55 key.AccountId = account 56 key.Ts = timestamps[i].ToTimestamp() 57 ok := cc.GetDatabase(key) 58 require.Equal(t, false, ok) 59 } 60 } 61 { 62 63 timestamps := vector.MustFixedCol[types.TS](tblBat.GetVector(MO_TIMESTAMP_IDX)) 64 accounts := vector.MustFixedCol[uint32](tblBat.GetVector(catalog.MO_TABLES_ACCOUNT_ID_IDX + MO_OFF)) 65 names := vector.MustStrCol(tblBat.GetVector(catalog.MO_TABLES_REL_NAME_IDX + MO_OFF)) 66 databaseIds := vector.MustFixedCol[uint64](tblBat.GetVector(catalog.MO_TABLES_RELDATABASE_ID_IDX + MO_OFF)) 67 key := new(TableItem) 68 for i, account := range accounts { 69 key.Name = names[i] 70 key.AccountId = account 71 key.DatabaseId = databaseIds[i] 72 key.Ts = timestamps[i].ToTimestamp() 73 ok := cc.GetTable(key) 74 require.Equal(t, false, ok) 75 } 76 } 77 dbBat.Clean(mp) 78 tblBat.Clean(mp) 79 colBat.Clean(mp) 80 require.Equal(t, int64(0), mp.CurrNB()) 81 } 82 83 func TestTables(t *testing.T) { 84 mp := mpool.MustNewZero() 85 cc := NewCatalog() 86 bat := newTestTableBatch(mp) 87 accounts := vector.MustFixedCol[uint32](bat.GetVector(catalog.MO_TABLES_ACCOUNT_ID_IDX + MO_OFF)) 88 databaseIds := vector.MustFixedCol[uint64](bat.GetVector(catalog.MO_TABLES_RELDATABASE_ID_IDX + MO_OFF)) 89 { // reset account id 90 for i := range accounts { 91 accounts[i] = 1 92 } 93 } 94 { // reset database id 95 for i := range databaseIds { 96 databaseIds[i] = 12 97 } 98 } 99 cc.InsertTable(bat) 100 tblList, tblIdList := cc.Tables(1, 12, timestamp.Timestamp{ 101 PhysicalTime: 100, 102 }) 103 require.Equal(t, 10, len(tblList)) 104 require.Equal(t, 10, len(tblIdList)) 105 bat.Clean(mp) 106 require.Equal(t, int64(0), mp.CurrNB()) 107 } 108 109 func TestDatabases(t *testing.T) { 110 mp := mpool.MustNewZero() 111 cc := NewCatalog() 112 bat := newTestDatabaseBatch(mp) 113 accounts := vector.MustFixedCol[uint32](bat.GetVector(catalog.MO_DATABASE_ACCOUNT_ID_IDX + MO_OFF)) 114 { // reset account id 115 for i := range accounts { 116 accounts[i] = 0 117 } 118 } 119 cc.InsertDatabase(bat) 120 // test get 121 dbList := cc.Databases(0, timestamp.Timestamp{ 122 PhysicalTime: 100, 123 }) 124 require.Equal(t, 10, len(dbList)) 125 bat.Clean(mp) 126 require.Equal(t, int64(0), mp.CurrNB()) 127 } 128 129 func TestDatabasesWithMultiVersion(t *testing.T) { 130 mp := mpool.MustNewZero() 131 cc := NewCatalog() 132 bat := newTestDatabaseBatch(mp) 133 names := vector.MustFixedCol[types.Varlena](bat.GetVector(catalog.MO_DATABASE_DAT_NAME_IDX + MO_OFF)) 134 accounts := vector.MustFixedCol[uint32](bat.GetVector(catalog.MO_DATABASE_ACCOUNT_ID_IDX + MO_OFF)) 135 { // reset account id 136 for i := range accounts { 137 accounts[i] = 0 138 } 139 } 140 { // reset names 141 name := []byte{'0'} 142 for i := range names { 143 names[i], _, _ = types.BuildVarlena(name, nil, nil) 144 } 145 } 146 cc.InsertDatabase(bat) 147 // test get 148 dbList := cc.Databases(0, timestamp.Timestamp{ 149 PhysicalTime: 100, 150 }) 151 require.Equal(t, 1, len(dbList)) // only one version can be see 152 bat.Clean(mp) 153 require.Equal(t, int64(0), mp.CurrNB()) 154 } 155 156 func TestDatabaseCache(t *testing.T) { 157 mp := mpool.MustNewZero() 158 cc := NewCatalog() 159 bat := newTestDatabaseBatch(mp) 160 cc.InsertDatabase(bat) 161 timestamps := vector.MustFixedCol[types.TS](bat.GetVector(MO_TIMESTAMP_IDX)) 162 names := vector.MustStrCol(bat.GetVector(catalog.MO_DATABASE_DAT_NAME_IDX + MO_OFF)) 163 accounts := vector.MustFixedCol[uint32](bat.GetVector(catalog.MO_DATABASE_ACCOUNT_ID_IDX + MO_OFF)) 164 key := new(DatabaseItem) 165 // test get 166 for i, account := range accounts { 167 key.Name = names[i] 168 key.AccountId = account 169 key.Ts = timestamps[i].ToTimestamp() 170 ok := cc.GetDatabase(key) 171 require.Equal(t, true, ok) 172 } 173 { // set the deletion time 174 for i := range timestamps { 175 timestamps[i] = types.BuildTS(timestamps[i].Physical()+10, timestamps[i].Logical()) 176 } 177 } 178 cc.DeleteDatabase(bat) 179 // test delete 180 for i, account := range accounts { 181 key.Name = names[i] 182 key.AccountId = account 183 key.Ts = timestamps[i].ToTimestamp() 184 ok := cc.GetDatabase(key) 185 require.Equal(t, false, ok) 186 } 187 bat.Clean(mp) 188 require.Equal(t, int64(0), mp.CurrNB()) 189 } 190 191 func TestTableInsert(t *testing.T) { 192 mp := mpool.MustNewZero() 193 cc := NewCatalog() 194 bat := newTestTableBatch(mp) 195 colBat := newTestColumnBatch(t, bat, mp) 196 cc.InsertTable(bat) 197 cc.InsertColumns(colBat) 198 timestamps := vector.MustFixedCol[types.TS](bat.GetVector(MO_TIMESTAMP_IDX)) 199 accounts := vector.MustFixedCol[uint32](bat.GetVector(catalog.MO_TABLES_ACCOUNT_ID_IDX + MO_OFF)) 200 names := vector.MustStrCol(bat.GetVector(catalog.MO_TABLES_REL_NAME_IDX + MO_OFF)) 201 databaseIds := vector.MustFixedCol[uint64](bat.GetVector(catalog.MO_TABLES_RELDATABASE_ID_IDX + MO_OFF)) 202 key := new(TableItem) 203 // test get 204 for i, account := range accounts { 205 key.Name = names[i] 206 key.AccountId = account 207 key.DatabaseId = databaseIds[i] 208 key.Ts = timestamps[i].ToTimestamp() 209 ok := cc.GetTable(key) 210 require.Equal(t, true, ok) 211 require.Equal(t, 11, len(key.Defs)) 212 } 213 { // set the deletion time 214 for i := range timestamps { 215 timestamps[i] = types.BuildTS(timestamps[i].Physical()+10, timestamps[i].Logical()) 216 } 217 } 218 cc.DeleteTable(bat) 219 { // set the query time 220 for i := range timestamps { 221 timestamps[i] = types.BuildTS(timestamps[i].Physical()+10, timestamps[i].Logical()) 222 } 223 } 224 // test delete 225 for i, account := range accounts { 226 key.Name = names[i] 227 key.AccountId = account 228 key.DatabaseId = databaseIds[i] 229 key.Ts = timestamps[i].ToTimestamp() 230 ok := cc.GetTable(key) 231 require.Equal(t, false, ok) 232 } 233 bat.Clean(mp) 234 colBat.Clean(mp) 235 require.Equal(t, int64(0), mp.CurrNB()) 236 } 237 238 func newTestTableBatch(mp *mpool.MPool) *batch.Batch { 239 var typs []types.Type 240 241 typs = append(typs, types.New(types.T_Rowid, 0, 0)) 242 typs = append(typs, types.New(types.T_TS, 0, 0)) 243 typs = append(typs, catalog.MoTablesTypes...) 244 return testutil.NewBatch(typs, false, Rows, mp) 245 } 246 247 func newTestColumnBatch(t *testing.T, ibat *batch.Batch, mp *mpool.MPool) *batch.Batch { 248 var typs []types.Type 249 var vec *vector.Vector 250 251 typs = append(typs, types.New(types.T_Rowid, 0, 0)) 252 typs = append(typs, types.New(types.T_TS, 0, 0)) 253 typs = append(typs, catalog.MoColumnsTypes...) 254 timestamps := vector.MustFixedCol[types.TS](ibat.GetVector(MO_TIMESTAMP_IDX)) 255 accounts := vector.MustFixedCol[uint32](ibat.GetVector(catalog.MO_TABLES_ACCOUNT_ID_IDX + MO_OFF)) 256 names := vector.MustBytesCol(ibat.GetVector(catalog.MO_TABLES_REL_NAME_IDX + MO_OFF)) 257 ids := vector.MustFixedCol[uint64](ibat.GetVector(catalog.MO_TABLES_REL_ID_IDX + MO_OFF)) 258 databaseIds := vector.MustFixedCol[uint64](ibat.GetVector(catalog.MO_TABLES_RELDATABASE_ID_IDX + MO_OFF)) 259 bat := batch.NewWithSize(len(typs)) 260 bat.SetRowCount(Rows) 261 for i := range bat.Vecs { 262 bat.Vecs[i] = vector.NewVec(typs[i]) 263 } 264 for i, account := range accounts { 265 for j, typ := range typs { 266 switch j { 267 case MO_TIMESTAMP_IDX: 268 vec = vector.NewVec(typ) 269 for k := 0; k < Rows; k++ { 270 err := vector.AppendFixed(vec, timestamps[i], false, mp) 271 require.NoError(t, err) 272 } 273 case catalog.MO_COLUMNS_ACCOUNT_ID_IDX + MO_OFF: 274 vec = vector.NewVec(typ) 275 for k := 0; k < Rows; k++ { 276 err := vector.AppendFixed(vec, account, false, mp) 277 require.NoError(t, err) 278 } 279 case catalog.MO_COLUMNS_ATT_DATABASE_ID_IDX + MO_OFF: 280 vec = vector.NewVec(typ) 281 for k := 0; k < Rows; k++ { 282 err := vector.AppendFixed(vec, databaseIds[i], false, mp) 283 require.NoError(t, err) 284 } 285 case catalog.MO_COLUMNS_ATT_RELNAME_ID_IDX + MO_OFF: 286 vec = vector.NewVec(typ) 287 for k := 0; k < Rows; k++ { 288 err := vector.AppendFixed(vec, ids[i], false, mp) 289 require.NoError(t, err) 290 } 291 case catalog.MO_COLUMNS_ATT_RELNAME_IDX + MO_OFF: 292 vec = vector.NewVec(typ) 293 for k := 0; k < Rows; k++ { 294 err := vector.AppendBytes(vec, names[i], false, mp) 295 require.NoError(t, err) 296 } 297 case catalog.MO_COLUMNS_ATTTYP_IDX + MO_OFF: 298 data, err := types.Encode(&typ) // reuse the type for test 299 require.NoError(t, err) 300 vec = vector.NewVec(typ) 301 for k := 0; k < Rows; k++ { 302 err := vector.AppendBytes(vec, data, false, mp) 303 require.NoError(t, err) 304 } 305 case catalog.MO_COLUMNS_ATTHASDEF_IDX + MO_OFF: 306 vec = vector.NewVec(typ) 307 for k := 0; k < Rows; k++ { 308 err := vector.AppendFixed(vec, int8(0), false, mp) 309 require.NoError(t, err) 310 } 311 case catalog.MO_COLUMNS_ATT_HAS_UPDATE_IDX + MO_OFF: 312 vec = vector.NewVec(typ) 313 for k := 0; k < Rows; k++ { 314 err := vector.AppendFixed(vec, int8(0), false, mp) 315 require.NoError(t, err) 316 } 317 default: 318 vec = testutil.NewVector(Rows, typ, mp, false, nil) 319 } 320 for k := 0; k < Rows; k++ { 321 err := bat.Vecs[j].UnionOne(vec, int64(k), mp) 322 require.NoError(t, err) 323 } 324 vec.Free(mp) 325 } 326 } 327 return bat 328 } 329 330 func newTestDatabaseBatch(mp *mpool.MPool) *batch.Batch { 331 var typs []types.Type 332 333 typs = append(typs, types.New(types.T_Rowid, 0, 0)) 334 typs = append(typs, types.New(types.T_TS, 0, 0)) 335 typs = append(typs, catalog.MoDatabaseTypes...) 336 return testutil.NewBatch(typs, false, Rows, mp) 337 }