Skip to content

meta: clone table info with new foreign keys default to nil (#60103) #60129

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

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/ddl/placement_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func getPlacementPolicyDependedObjectsIDs(t *meta.Mutator, policy *model.PolicyI
}
tables, err := meta.GetTableInfoWithAttributes(
t, dbInfo.ID,
`"partition":null"`,
`"partition":null`,
`"policy_ref_info":null`)
if err != nil {
return nil, nil, nil, err
Expand Down
23 changes: 18 additions & 5 deletions pkg/meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,8 +1067,10 @@ func (m *Mutator) GetMetasByDBID(dbID int64) ([]structure.HashPair, error) {
return res, nil
}

// foreign key info contain null and [] situations
var checkForeignKeyAttributesNil = `"fk_info":null`
var checkForeignKeyAttributesZero = `"fk_info":[]`
var checkAttributesInOrder = []string{
`"fk_info":null`,
`"partition":null`,
`"Lock":null`,
`"tiflash_replica":null`,
Expand All @@ -1082,8 +1084,19 @@ var checkAttributesInOrder = []string{
// then it does not need to be loaded and this function will return false.
// Otherwise, it will return true, indicating that the table info should be loaded.
// Since attributes are checked in sequence, it's important to choose the order carefully.
func isTableInfoMustLoad(json []byte, filterAttrs ...string) bool {
// isCheckForeignKeyAttrsInOrder check foreign key or not, since fk_info contains two null situations.
func isTableInfoMustLoad(json []byte, isCheckForeignKeyAttrsInOrder bool, filterAttrs ...string) bool {
idx := 0
if isCheckForeignKeyAttrsInOrder {
idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesNil))
if idx == -1 {
idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesZero))
if idx == -1 {
return true
}
}
json = json[idx:]
}
for _, substr := range filterAttrs {
idx = bytes.Index(json, hack.Slice(substr))
if idx == -1 {
Expand All @@ -1097,7 +1110,7 @@ func isTableInfoMustLoad(json []byte, filterAttrs ...string) bool {
// IsTableInfoMustLoad checks whether the table info needs to be loaded.
// Exported for testing.
func IsTableInfoMustLoad(json []byte) bool {
return isTableInfoMustLoad(json, checkAttributesInOrder...)
return isTableInfoMustLoad(json, true, checkAttributesInOrder...)
}

// NameExtractRegexp is exported for testing.
Expand Down Expand Up @@ -1142,7 +1155,7 @@ func (m *Mutator) GetAllNameToIDAndTheMustLoadedTableInfo(dbID int64) (map[strin

key := Unescape(nameLMatch[1])
res[strings.Clone(key)] = int64(id)
if isTableInfoMustLoad(value, checkAttributesInOrder...) {
if isTableInfoMustLoad(value, true, checkAttributesInOrder...) {
tbInfo := &model.TableInfo{}
err = json.Unmarshal(value, tbInfo)
if err != nil {
Expand Down Expand Up @@ -1171,7 +1184,7 @@ func GetTableInfoWithAttributes(m *Mutator, dbID int64, filterAttrs ...string) (
return nil
}

if isTableInfoMustLoad(value, filterAttrs...) {
if isTableInfoMustLoad(value, false, filterAttrs...) {
tbInfo := &model.TableInfo{}
err := json.Unmarshal(value, tbInfo)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/meta/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,28 @@ func TestIsTableInfoMustLoad(t *testing.T) {
require.NoError(t, err)
require.True(t, meta.IsTableInfoMustLoad(b))

tableInfo = tableInfo.Clone()
b, err = json.Marshal(tableInfo)
require.NoError(t, err)
require.True(t, meta.IsTableInfoMustLoad(b))

tableInfo.ForeignKeys = nil
tableInfo = tableInfo.Clone()
b, err = json.Marshal(tableInfo)
require.NoError(t, err)
require.False(t, meta.IsTableInfoMustLoad(b))

tableInfo.ForeignKeys = make([]*model.FKInfo, 0)
tableInfo = tableInfo.Clone()
b, err = json.Marshal(tableInfo)
require.NoError(t, err)
require.False(t, meta.IsTableInfoMustLoad(b))

tableInfo.ForeignKeys = make([]*model.FKInfo, 0)
b, err = json.Marshal(tableInfo)
require.NoError(t, err)
require.False(t, meta.IsTableInfoMustLoad(b))

tableInfo = &model.TableInfo{
TempTableType: model.TempTableGlobal,
State: model.StatePublic,
Expand Down
9 changes: 6 additions & 3 deletions pkg/meta/model/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (t *TableInfo) Clone() *TableInfo {
nt := *t
nt.Columns = make([]*ColumnInfo, len(t.Columns))
nt.Indices = make([]*IndexInfo, len(t.Indices))
nt.ForeignKeys = make([]*FKInfo, len(t.ForeignKeys))
nt.ForeignKeys = nil

for i := range t.Columns {
nt.Columns[i] = t.Columns[i].Clone()
Expand All @@ -252,8 +252,11 @@ func (t *TableInfo) Clone() *TableInfo {
nt.Indices[i] = t.Indices[i].Clone()
}

for i := range t.ForeignKeys {
nt.ForeignKeys[i] = t.ForeignKeys[i].Clone()
if len(t.ForeignKeys) > 0 {
nt.ForeignKeys = make([]*FKInfo, len(t.ForeignKeys))
for i := range t.ForeignKeys {
nt.ForeignKeys[i] = t.ForeignKeys[i].Clone()
}
}

if t.Partition != nil {
Expand Down