Skip to content

Commit 7aff918

Browse files
authored
meta: clone table info with new foreign keys default to nil (#60103) (#60129)
close #60044
1 parent fc0ed72 commit 7aff918

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

pkg/ddl/placement_policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func getPlacementPolicyDependedObjectsIDs(t *meta.Mutator, policy *model.PolicyI
424424
}
425425
tables, err := meta.GetTableInfoWithAttributes(
426426
t, dbInfo.ID,
427-
`"partition":null"`,
427+
`"partition":null`,
428428
`"policy_ref_info":null`)
429429
if err != nil {
430430
return nil, nil, nil, err

pkg/meta/meta.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,10 @@ func (m *Mutator) GetMetasByDBID(dbID int64) ([]structure.HashPair, error) {
10671067
return res, nil
10681068
}
10691069

1070+
// foreign key info contain null and [] situations
1071+
var checkForeignKeyAttributesNil = `"fk_info":null`
1072+
var checkForeignKeyAttributesZero = `"fk_info":[]`
10701073
var checkAttributesInOrder = []string{
1071-
`"fk_info":null`,
10721074
`"partition":null`,
10731075
`"Lock":null`,
10741076
`"tiflash_replica":null`,
@@ -1082,8 +1084,19 @@ var checkAttributesInOrder = []string{
10821084
// then it does not need to be loaded and this function will return false.
10831085
// Otherwise, it will return true, indicating that the table info should be loaded.
10841086
// Since attributes are checked in sequence, it's important to choose the order carefully.
1085-
func isTableInfoMustLoad(json []byte, filterAttrs ...string) bool {
1087+
// isCheckForeignKeyAttrsInOrder check foreign key or not, since fk_info contains two null situations.
1088+
func isTableInfoMustLoad(json []byte, isCheckForeignKeyAttrsInOrder bool, filterAttrs ...string) bool {
10861089
idx := 0
1090+
if isCheckForeignKeyAttrsInOrder {
1091+
idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesNil))
1092+
if idx == -1 {
1093+
idx = bytes.Index(json, hack.Slice(checkForeignKeyAttributesZero))
1094+
if idx == -1 {
1095+
return true
1096+
}
1097+
}
1098+
json = json[idx:]
1099+
}
10871100
for _, substr := range filterAttrs {
10881101
idx = bytes.Index(json, hack.Slice(substr))
10891102
if idx == -1 {
@@ -1097,7 +1110,7 @@ func isTableInfoMustLoad(json []byte, filterAttrs ...string) bool {
10971110
// IsTableInfoMustLoad checks whether the table info needs to be loaded.
10981111
// Exported for testing.
10991112
func IsTableInfoMustLoad(json []byte) bool {
1100-
return isTableInfoMustLoad(json, checkAttributesInOrder...)
1113+
return isTableInfoMustLoad(json, true, checkAttributesInOrder...)
11011114
}
11021115

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

11431156
key := Unescape(nameLMatch[1])
11441157
res[strings.Clone(key)] = int64(id)
1145-
if isTableInfoMustLoad(value, checkAttributesInOrder...) {
1158+
if isTableInfoMustLoad(value, true, checkAttributesInOrder...) {
11461159
tbInfo := &model.TableInfo{}
11471160
err = json.Unmarshal(value, tbInfo)
11481161
if err != nil {
@@ -1171,7 +1184,7 @@ func GetTableInfoWithAttributes(m *Mutator, dbID int64, filterAttrs ...string) (
11711184
return nil
11721185
}
11731186

1174-
if isTableInfoMustLoad(value, filterAttrs...) {
1187+
if isTableInfoMustLoad(value, false, filterAttrs...) {
11751188
tbInfo := &model.TableInfo{}
11761189
err := json.Unmarshal(value, tbInfo)
11771190
if err != nil {

pkg/meta/meta_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,28 @@ func TestIsTableInfoMustLoad(t *testing.T) {
706706
require.NoError(t, err)
707707
require.True(t, meta.IsTableInfoMustLoad(b))
708708

709+
tableInfo = tableInfo.Clone()
710+
b, err = json.Marshal(tableInfo)
711+
require.NoError(t, err)
712+
require.True(t, meta.IsTableInfoMustLoad(b))
713+
714+
tableInfo.ForeignKeys = nil
715+
tableInfo = tableInfo.Clone()
716+
b, err = json.Marshal(tableInfo)
717+
require.NoError(t, err)
718+
require.False(t, meta.IsTableInfoMustLoad(b))
719+
720+
tableInfo.ForeignKeys = make([]*model.FKInfo, 0)
721+
tableInfo = tableInfo.Clone()
722+
b, err = json.Marshal(tableInfo)
723+
require.NoError(t, err)
724+
require.False(t, meta.IsTableInfoMustLoad(b))
725+
726+
tableInfo.ForeignKeys = make([]*model.FKInfo, 0)
727+
b, err = json.Marshal(tableInfo)
728+
require.NoError(t, err)
729+
require.False(t, meta.IsTableInfoMustLoad(b))
730+
709731
tableInfo = &model.TableInfo{
710732
TempTableType: model.TempTableGlobal,
711733
State: model.StatePublic,

pkg/meta/model/table.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (t *TableInfo) Clone() *TableInfo {
242242
nt := *t
243243
nt.Columns = make([]*ColumnInfo, len(t.Columns))
244244
nt.Indices = make([]*IndexInfo, len(t.Indices))
245-
nt.ForeignKeys = make([]*FKInfo, len(t.ForeignKeys))
245+
nt.ForeignKeys = nil
246246

247247
for i := range t.Columns {
248248
nt.Columns[i] = t.Columns[i].Clone()
@@ -252,8 +252,11 @@ func (t *TableInfo) Clone() *TableInfo {
252252
nt.Indices[i] = t.Indices[i].Clone()
253253
}
254254

255-
for i := range t.ForeignKeys {
256-
nt.ForeignKeys[i] = t.ForeignKeys[i].Clone()
255+
if len(t.ForeignKeys) > 0 {
256+
nt.ForeignKeys = make([]*FKInfo, len(t.ForeignKeys))
257+
for i := range t.ForeignKeys {
258+
nt.ForeignKeys[i] = t.ForeignKeys[i].Clone()
259+
}
257260
}
258261

259262
if t.Partition != nil {

0 commit comments

Comments
 (0)