Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema_v2: add ut test Data struct fields' correctness #55709

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/infoschema/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ go_test(
],
embed = [":infoschema"],
flaky = True,
shard_count = 23,
shard_count = 24,
deps = [
"//pkg/ddl/placement",
"//pkg/domain",
Expand Down
106 changes: 106 additions & 0 deletions pkg/infoschema/infoschema_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,109 @@ func TestSpecialAttributeCorrectnessInSchemaChange(t *testing.T) {
tblInfo.ForeignKeys = nil
updateTableSpecialAttribute(t, dbInfo, tblInfo, builder, r, model.ActionDropForeignKey, 12, ForeignKeysAttribute, false)
}

func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) {
r := internal.CreateAutoIDRequirement(t)
defer func() {
r.Store().Close()
}()

schemaName := model.NewCIStr("testDB")
tableName := model.NewCIStr("testTable")
builder := NewBuilder(r, nil, NewData(), variable.SchemaCacheSize.Load() > 0)
err := builder.InitWithDBInfos(nil, nil, nil, 1)
require.NoError(t, err)
is := builder.Build(math.MaxUint64)
v2, ok := is.(*infoschemaV2)
require.True(t, ok)

// verify schema related fields after create database
dbInfo := internal.MockDBInfo(t, r.Store(), schemaName.O)
internal.AddDB(t, r.Store(), dbInfo)
txn, err := r.Store().Begin()
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionCreateSchema, Version: 1, SchemaID: dbInfo.ID})
require.NoError(t, err)
dbIDName, ok := v2.Data.schemaID2Name.Get(schemaIDName{id: dbInfo.ID, schemaVersion: 1})
require.True(t, ok)
require.Equal(t, dbIDName.name, dbInfo.Name)
dbItem, ok := v2.Data.schemaMap.Get(schemaItem{schemaVersion: 1, dbInfo: &model.DBInfo{Name: dbInfo.Name}})
require.True(t, ok)
require.Equal(t, dbItem.dbInfo.ID, dbInfo.ID)

// verify table related fields after create table
tblInfo := internal.MockTableInfo(t, r.Store(), tableName.O)
internal.AddTable(t, r.Store(), dbInfo, tblInfo)
txn, err = r.Store().Begin()
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionCreateTable, Version: 2, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
require.NoError(t, err)
tblItem, ok := v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2})
require.True(t, ok)
require.Equal(t, tblItem.tableID, tblInfo.ID)
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 2})
require.True(t, ok)
require.Equal(t, tblItem.dbID, dbInfo.ID)
tbl, ok := v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 2})
require.True(t, ok)
require.Equal(t, tbl.Meta().Name, tblInfo.Name)

// verify partition related fields after add partition
require.Equal(t, v2.Data.pid2tid.Len(), 0)
tblInfo.Partition = &model.PartitionInfo{
Definitions: []model.PartitionDefinition{
{ID: 1, Name: model.NewCIStr("p1")},
{ID: 2, Name: model.NewCIStr("p2")},
},
Enable: true,
DDLState: model.StatePublic,
}
tblInfo1 := updateTableSpecialAttribute(t, dbInfo, tblInfo, builder, r, model.ActionAddTablePartition, 3, PartitionAttribute, true)
require.Equal(t, tblInfo.Partition, tblInfo1.Partition)
require.Equal(t, v2.Data.pid2tid.Len(), 2)
tblInfoItem, ok := v2.Data.pid2tid.Get(partitionItem{partitionID: 2, schemaVersion: 3})
require.True(t, ok)
require.Equal(t, tblInfoItem.tableID, tblInfo.ID)

// verify partition related fields drop partition
tblInfo.Partition.Definitions = tblInfo.Partition.Definitions[:1]
tblInfo1 = updateTableSpecialAttribute(t, dbInfo, tblInfo, builder, r, model.ActionDropTablePartition, 4, PartitionAttribute, true)
require.Equal(t, tblInfo.Partition, tblInfo1.Partition)
require.Equal(t, v2.Data.pid2tid.Len(), 4)
tblInfoItem, ok = v2.Data.pid2tid.Get(partitionItem{partitionID: 1, schemaVersion: 4})
require.True(t, ok)
require.False(t, tblInfoItem.tomb)
tblInfoItem, ok = v2.Data.pid2tid.Get(partitionItem{partitionID: 2, schemaVersion: 4})
require.True(t, ok)
require.True(t, tblInfoItem.tomb)

// verify table and partition related fields after drop table
txn, err = r.Store().Begin()
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID})
require.NoError(t, err)
tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5})
require.True(t, ok)
require.True(t, tblItem.tomb)
tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5})
require.True(t, ok)
require.True(t, tblItem.tomb)
tbl, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})
require.False(t, ok)
require.Equal(t, v2.Data.pid2tid.Len(), 5) // tomb partition info
tblInfoItem, ok = v2.Data.pid2tid.Get(partitionItem{partitionID: 1, schemaVersion: 5})
require.True(t, ok)
require.True(t, tblInfoItem.tomb)

// verify schema related fields after drop database
txn, err = r.Store().Begin()
require.NoError(t, err)
_, err = builder.ApplyDiff(meta.NewMeta(txn), &model.SchemaDiff{Type: model.ActionDropSchema, Version: 6, SchemaID: dbInfo.ID})
require.NoError(t, err)
dbIDName, ok = v2.Data.schemaID2Name.Get(schemaIDName{id: dbInfo.ID, schemaVersion: 6})
require.True(t, ok)
require.True(t, dbIDName.tomb)
dbItem, ok = v2.Data.schemaMap.Get(schemaItem{schemaVersion: 6, dbInfo: &model.DBInfo{Name: dbInfo.Name}})
require.True(t, ok)
require.True(t, dbItem.tomb)
}
2 changes: 1 addition & 1 deletion pkg/meta/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ go_test(
],
embed = [":meta"],
flaky = True,
shard_count = 15,
shard_count = 16,
deps = [
"//pkg/ddl",
"//pkg/infoschema",
Expand Down
85 changes: 84 additions & 1 deletion pkg/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ func benchFastJSONTableNameInfo(b *testing.B, sql string) {
}
}

func TestSpecialAttributeCorrectnessAfterBootstrap(t *testing.T) {
func TestInfoSchemaV2SpecialAttributeCorrectnessAfterBootstrap(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
defer func() {
Expand Down Expand Up @@ -954,3 +954,86 @@ func TestSpecialAttributeCorrectnessAfterBootstrap(t *testing.T) {
require.Equal(t, len(tblInfoRes[0].TableInfos), 1)
require.Equal(t, tblInfo.TTLInfo, tblInfoRes[0].TableInfos[0].TTLInfo)
}

func TestInfoSchemaV2DataFieldsCorrectnessAfterBootstrap(t *testing.T) {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
}()

// create database
dbInfo := &model.DBInfo{
ID: 10001,
Name: model.NewCIStr("sc"),
Charset: "utf8",
Collate: "utf8_general_ci",
State: model.StatePublic,
}

// create table with partition info
tblInfo := &model.TableInfo{
ID: 10002,
Name: model.NewCIStr("cs"),
Charset: "latin1",
Collate: "latin1_bin",
State: model.StatePublic,
Partition: &model.PartitionInfo{
Definitions: []model.PartitionDefinition{
{ID: 1, Name: model.NewCIStr("p1")},
},
Enable: true,
},
}

ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnDDL)
err = kv.RunInNewTxn(ctx, store, true, func(ctx context.Context, txn kv.Transaction) error {
err := meta.NewMeta(txn).CreateDatabase(dbInfo)
require.NoError(t, err)
err = meta.NewMeta(txn).CreateTableOrView(dbInfo.ID, tblInfo)
require.NoError(t, err)
return errors.Trace(err)
})
require.NoError(t, err)

// bootstrap
dom, err := session.BootstrapSession(store)
require.NoError(t, err)
defer dom.Close()

is := dom.InfoSchema()
//byID, traverse byID and load from store
tbl, ok := is.TableByID(context.Background(), 10002)
require.True(t, ok)
require.Equal(t, tbl.Meta().ID, tblInfo.ID)

//byName, traverse byName and load from store,
tbl, err = is.TableByName(context.Background(), model.NewCIStr("sc"), model.NewCIStr("cs"))
require.NoError(t, err)
require.Equal(t, tbl.Meta().ID, tblInfo.ID)

//tableCache, table info exists in cache now, just use id to seek
tbl, ok = is.TableByID(context.Background(), 10002)
require.True(t, ok)
require.Equal(t, tbl.Meta().ID, tblInfo.ID)

//schemaMap, traverse schemaMap find dbInfo
db, ok := is.SchemaByName(model.NewCIStr("sc"))
require.True(t, ok)
require.Equal(t, db.ID, dbInfo.ID)

//schemaID2Name, traverse schemaID2Name find dbInfo
db, ok = is.SchemaByID(dbInfo.ID)
require.True(t, ok)
require.Equal(t, db.ID, dbInfo.ID)

//pid2tid, traverse pid2tid find tblInfo, dbInfo and partition info
tbl, ok = is.TableByID(context.Background(), 10002)
require.True(t, ok)
require.Equal(t, len(tbl.Meta().GetPartitionInfo().Definitions), 1)
pid := tbl.Meta().GetPartitionInfo().Definitions[0].ID
tbl, db, pDef := is.FindTableByPartitionID(pid)
require.NotNil(t, tbl)
require.NotNil(t, db)
require.NotNil(t, pDef)
}