Skip to content

Commit 1e665ef

Browse files
committed
don't return table in non-Public state in infoSchema
Signed-off-by: Yang Keao <[email protected]>
1 parent 91702c1 commit 1e665ef

File tree

10 files changed

+247
-70
lines changed

10 files changed

+247
-70
lines changed

pkg/ddl/schematracker/info_store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ func (i *InfoStore) TableByName(_ context.Context, schema, table pmodel.CIStr) (
8888
if !ok {
8989
return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table)
9090
}
91+
if tbl.State != model.StatePublic {
92+
return nil, infoschema.ErrTableNotExists.GenWithStackByArgs(schema, table)
93+
}
9194
return tbl, nil
9295
}
9396

pkg/ddl/table_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -735,31 +735,3 @@ func TestCreateSameTableOrDBOnOwnerChange(t *testing.T) {
735735
finished.Store(true)
736736
ownerWg.Wait()
737737
}
738-
739-
func TestDropTableAccessibleInInfoSchema(t *testing.T) {
740-
// The dropped table should always be accessible until the state reaches `StateNone`.
741-
store, dom := testkit.CreateMockStoreAndDomain(t)
742-
tk := testkit.NewTestKit(t, store)
743-
tk.MustExec("use test")
744-
745-
tkDDL := testkit.NewTestKit(t, store)
746-
tkDDL.MustExec("use test")
747-
tkDDL.MustExec("create table t (id int key)")
748-
749-
var errs []error
750-
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) {
751-
if job.Type == model.ActionDropTable && job.TableName == "t" {
752-
if job.SchemaState == model.StateDeleteOnly || job.SchemaState == model.StateWriteOnly {
753-
_, err := dom.InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
754-
errs = append(errs, err)
755-
}
756-
}
757-
})
758-
tkDDL.MustExec("drop table t")
759-
760-
testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore")
761-
for _, err := range errs {
762-
require.NoError(t, err)
763-
}
764-
require.True(t, len(errs) > 0)
765-
}

pkg/infoschema/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ go_test(
9292
],
9393
embed = [":infoschema"],
9494
flaky = True,
95-
shard_count = 25,
95+
shard_count = 27,
9696
deps = [
9797
"//pkg/ddl/placement",
9898
"//pkg/domain",
@@ -107,6 +107,7 @@ go_test(
107107
"//pkg/store/driver",
108108
"//pkg/table",
109109
"//pkg/testkit",
110+
"//pkg/testkit/testfailpoint",
110111
"//pkg/testkit/testsetup",
111112
"//pkg/testkit/testutil",
112113
"//pkg/types",

pkg/infoschema/context/infoschema.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ func (d DBInfoAsInfoSchema) AllSchemas() []*model.DBInfo {
8080
func (d DBInfoAsInfoSchema) SchemaTableInfos(ctx stdctx.Context, schema pmodel.CIStr) ([]*model.TableInfo, error) {
8181
for _, db := range d {
8282
if db.Name == schema {
83-
return db.Deprecated.Tables, nil
83+
tables := make([]*model.TableInfo, 0, len(db.Deprecated.Tables))
84+
for _, tbl := range db.Deprecated.Tables {
85+
if tbl.State == model.StatePublic {
86+
tables = append(tables, tbl)
87+
}
88+
}
89+
90+
return tables, nil
8491
}
8592
}
8693
return nil, nil

pkg/infoschema/infoschema.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (is *infoSchema) SchemaExists(schema pmodel.CIStr) bool {
227227
return ok
228228
}
229229

230-
func (is *infoSchema) TableByName(ctx stdctx.Context, schema, table pmodel.CIStr) (t table.Table, err error) {
230+
func (is *infoSchema) tableByNameWithoutConsideringState(ctx stdctx.Context, schema, table pmodel.CIStr) (t table.Table, err error) {
231231
if tbNames, ok := is.schemaMap[schema.L]; ok {
232232
if t, ok = tbNames.tables[table.L]; ok {
233233
return
@@ -236,6 +236,27 @@ func (is *infoSchema) TableByName(ctx stdctx.Context, schema, table pmodel.CIStr
236236
return nil, ErrTableNotExists.FastGenByArgs(schema, table)
237237
}
238238

239+
// TableByName implements InfoSchema.TableByName
240+
func (is *infoSchema) TableByName(ctx stdctx.Context, schema, table pmodel.CIStr) (t table.Table, err error) {
241+
t, err = is.tableByNameWithoutConsideringState(ctx, schema, table)
242+
if err != nil {
243+
return nil, err
244+
}
245+
if t.Meta().State != model.StatePublic {
246+
return nil, ErrTableNotExists.FastGenByArgs(schema, table)
247+
}
248+
return t, nil
249+
}
250+
251+
// TableByFKInfo implements InfoSchema.TableByFKInfo
252+
func (is *infoSchema) TableByFKInfo(ctx stdctx.Context, info *model.ReferredFKInfo) (t table.Table, err error) {
253+
t, err = is.tableByNameWithoutConsideringState(ctx, info.ChildSchema, info.ChildTable)
254+
if err != nil {
255+
return nil, err
256+
}
257+
return t, nil
258+
}
259+
239260
// TableInfoByName implements InfoSchema.TableInfoByName
240261
func (is *infoSchema) TableInfoByName(schema, table pmodel.CIStr) (*model.TableInfo, error) {
241262
tbl, err := is.TableByName(stdctx.Background(), schema, table)
@@ -262,7 +283,7 @@ func TableIsSequence(is InfoSchema, schema, table pmodel.CIStr) bool {
262283

263284
func (is *infoSchema) TableExists(schema, table pmodel.CIStr) bool {
264285
if tbNames, ok := is.schemaMap[schema.L]; ok {
265-
if _, ok = tbNames.tables[table.L]; ok {
286+
if tbl, ok := tbNames.tables[table.L]; ok && tbl.Meta().State == model.StatePublic {
266287
return true
267288
}
268289
}
@@ -305,7 +326,12 @@ func (is *infoSchema) TableByID(_ stdctx.Context, id int64) (val table.Table, ok
305326
if idx == -1 {
306327
return nil, false
307328
}
308-
return slice[idx], true
329+
330+
tbl := slice[idx]
331+
if tbl.Meta().State != model.StatePublic {
332+
return nil, false
333+
}
334+
return tbl, true
309335
}
310336

311337
// TableInfoByID implements InfoSchema.TableInfoByID
@@ -330,6 +356,9 @@ func (is *infoSchema) SchemaTableInfos(ctx stdctx.Context, schema pmodel.CIStr)
330356
}
331357
tables := make([]*model.TableInfo, 0, len(schemaTables.tables))
332358
for _, tbl := range schemaTables.tables {
359+
if tbl.Meta().State != model.StatePublic {
360+
continue
361+
}
333362
tables = append(tables, tbl.Meta())
334363
}
335364
return tables, nil
@@ -343,6 +372,9 @@ func (is *infoSchema) SchemaSimpleTableInfos(ctx stdctx.Context, schema pmodel.C
343372
}
344373
ret := make([]*model.TableNameInfo, 0, len(schemaTables.tables))
345374
for _, t := range schemaTables.tables {
375+
if t.Meta().State != model.StatePublic {
376+
continue
377+
}
346378
ret = append(ret, &model.TableNameInfo{
347379
ID: t.Meta().ID,
348380
Name: t.Meta().Name,
@@ -363,12 +395,17 @@ func (is *infoSchema) ListTablesWithSpecialAttribute(filter specialAttributeFilt
363395
tblInfos, err := is.SchemaTableInfos(stdctx.Background(), dbName)
364396
terror.Log(err)
365397
for _, tblInfo := range tblInfos {
398+
if tblInfo.State != model.StatePublic {
399+
continue
400+
}
366401
if !filter(tblInfo) {
367402
continue
368403
}
369404
res.TableInfos = append(res.TableInfos, tblInfo)
370405
}
371-
ret = append(ret, res)
406+
if len(res.TableInfos) > 0 {
407+
ret = append(ret, res)
408+
}
372409
}
373410
return ret
374411
}
@@ -402,6 +439,9 @@ func (is *infoSchema) AllSchemaNames() (schemas []pmodel.CIStr) {
402439
func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition) {
403440
for _, v := range is.schemaMap {
404441
for _, tbl := range v.tables {
442+
if tbl.Meta().State != model.StatePublic {
443+
continue
444+
}
405445
pi := tbl.Meta().GetPartitionInfo()
406446
if pi == nil {
407447
continue
@@ -681,6 +721,9 @@ func NewSessionTables() *SessionTables {
681721
func (is *SessionTables) TableByName(ctx stdctx.Context, schema, table pmodel.CIStr) (table.Table, bool) {
682722
if tbNames, ok := is.schemaMap[schema.L]; ok {
683723
if t, ok := tbNames.tables[table.L]; ok {
724+
if t.Meta().State != model.StatePublic {
725+
return nil, false
726+
}
684727
return t, true
685728
}
686729
}
@@ -696,6 +739,9 @@ func (is *SessionTables) TableExists(schema, table pmodel.CIStr) (ok bool) {
696739
// TableByID get table by table id
697740
func (is *SessionTables) TableByID(id int64) (tbl table.Table, ok bool) {
698741
tbl, ok = is.idx2table[id]
742+
if ok && tbl.Meta().State != model.StatePublic {
743+
return nil, false
744+
}
699745
return
700746
}
701747

@@ -790,12 +836,18 @@ type SessionExtendedInfoSchema struct {
790836
func (ts *SessionExtendedInfoSchema) TableByName(ctx stdctx.Context, schema, table pmodel.CIStr) (table.Table, error) {
791837
if ts.LocalTemporaryTables != nil {
792838
if tbl, ok := ts.LocalTemporaryTables.TableByName(ctx, schema, table); ok {
839+
if tbl.Meta().State != model.StatePublic {
840+
return nil, ErrTableNotExists.GenWithStackByArgs(schema, table)
841+
}
793842
return tbl, nil
794843
}
795844
}
796845

797846
if ts.MdlTables != nil {
798847
if tbl, ok := ts.MdlTables.TableByName(ctx, schema, table); ok {
848+
if tbl.Meta().State != model.StatePublic {
849+
return nil, ErrTableNotExists.GenWithStackByArgs(schema, table)
850+
}
799851
return tbl, nil
800852
}
801853
}
@@ -831,12 +883,18 @@ func (ts *SessionExtendedInfoSchema) TableByID(ctx stdctx.Context, id int64) (ta
831883

832884
if ts.LocalTemporaryTables != nil {
833885
if tbl, ok := ts.LocalTemporaryTables.TableByID(id); ok {
886+
if tbl.Meta().State != model.StatePublic {
887+
return nil, false
888+
}
834889
return tbl, true
835890
}
836891
}
837892

838893
if ts.MdlTables != nil {
839894
if tbl, ok := ts.MdlTables.TableByID(id); ok {
895+
if tbl.Meta().State != model.StatePublic {
896+
return nil, false
897+
}
840898
return tbl, true
841899
}
842900
}

pkg/infoschema/infoschema_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/pingcap/tidb/pkg/sessionctx/variable"
3737
"github.com/pingcap/tidb/pkg/table"
3838
"github.com/pingcap/tidb/pkg/testkit"
39+
"github.com/pingcap/tidb/pkg/testkit/testfailpoint"
3940
"github.com/pingcap/tidb/pkg/testkit/testutil"
4041
"github.com/pingcap/tidb/pkg/types"
4142
"github.com/pingcap/tidb/pkg/util"
@@ -1307,3 +1308,106 @@ func TestApplyDiff(t *testing.T) {
13071308
}
13081309
// TODO(ywqzzy): check all actions.
13091310
}
1311+
1312+
func testNonPublicTableNotAccessibilityInInfoSchema(t *testing.T, v2 bool) {
1313+
// The dropped table should always be accessible until the state reaches `StateNone`.
1314+
store, dom := testkit.CreateMockStoreAndDomain(t)
1315+
tk := testkit.NewTestKit(t, store)
1316+
tk.MustExec("use test")
1317+
if v2 {
1318+
tk.MustExec("set @@global.tidb_schema_cache_size = 512 * 1024 * 1024")
1319+
} else {
1320+
tk.MustExec("set @@global.tidb_schema_cache_size = 0")
1321+
}
1322+
1323+
tkDDL := testkit.NewTestKit(t, store)
1324+
tkDDL.MustExec("use test")
1325+
if v2 {
1326+
tkDDL.MustExec("set @@global.tidb_schema_cache_size = 512 * 1024 * 1024")
1327+
} else {
1328+
tkDDL.MustExec("set @@global.tidb_schema_cache_size = 0")
1329+
}
1330+
1331+
var shouldNotFound []bool
1332+
var shouldFound []bool
1333+
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore", func(job *model.Job) {
1334+
if job.Type == model.ActionDropTable && job.TableName == "t" {
1335+
if job.SchemaState == model.StateDeleteOnly || job.SchemaState == model.StateWriteOnly {
1336+
_, err := dom.InfoSchema().TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
1337+
shouldNotFound = append(shouldNotFound, err == nil)
1338+
_, err = dom.InfoSchema().TableInfoByName(pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
1339+
shouldNotFound = append(shouldNotFound, err == nil)
1340+
_, ok := dom.InfoSchema().TableByID(context.Background(), job.TableID)
1341+
shouldNotFound = append(shouldNotFound, ok)
1342+
_, ok = dom.InfoSchema().TableInfoByID(job.TableID)
1343+
shouldNotFound = append(shouldNotFound, ok)
1344+
1345+
res := dom.InfoSchema().ListTablesWithSpecialAttribute(func(info *model.TableInfo) bool {
1346+
return info.ID == job.TableID
1347+
})
1348+
shouldNotFound = append(shouldNotFound, len(res) > 0)
1349+
shouldNotFound = append(shouldNotFound, dom.InfoSchema().TableExists(pmodel.NewCIStr("test"), pmodel.NewCIStr("t")))
1350+
1351+
infos, err := dom.InfoSchema().SchemaTableInfos(context.Background(), pmodel.NewCIStr("test"))
1352+
if err != nil {
1353+
// It should not have an error. Append to the `shouldNotFound` to make this test failed.
1354+
shouldNotFound = append(shouldNotFound, true)
1355+
}
1356+
f := false
1357+
for _, info := range infos {
1358+
if info.ID == job.TableID {
1359+
f = true
1360+
break
1361+
}
1362+
}
1363+
shouldNotFound = append(shouldNotFound, f)
1364+
1365+
nameInfos, err := dom.InfoSchema().SchemaSimpleTableInfos(context.Background(), pmodel.NewCIStr("test"))
1366+
if err != nil {
1367+
// It should not have an error. Append to the `shouldNotFound` to make this test failed.
1368+
shouldNotFound = append(shouldNotFound, true)
1369+
}
1370+
f = false
1371+
for _, info := range nameInfos {
1372+
if info.ID == job.TableID {
1373+
f = true
1374+
break
1375+
}
1376+
}
1377+
shouldNotFound = append(shouldNotFound, f)
1378+
1379+
// The only exception is that TableByFKInfo can get the table in non-public state.
1380+
_, err = dom.InfoSchema().TableByFKInfo(context.Background(), &model.ReferredFKInfo{
1381+
ChildSchema: pmodel.NewCIStr("test"),
1382+
ChildTable: pmodel.NewCIStr("t"),
1383+
})
1384+
shouldFound = append(shouldFound, err == nil)
1385+
}
1386+
}
1387+
})
1388+
tkDDL.MustExec("create table t (id int key)")
1389+
tkDDL.MustExec("drop table t")
1390+
1391+
testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/onJobRunBefore")
1392+
if v2 {
1393+
require.Len(t, shouldNotFound, 9*2)
1394+
} else {
1395+
require.Len(t, shouldNotFound, 8*2)
1396+
}
1397+
for _, b := range shouldNotFound {
1398+
require.False(t, b)
1399+
}
1400+
1401+
require.Len(t, shouldFound, 1*2)
1402+
for _, b := range shouldFound {
1403+
require.True(t, b)
1404+
}
1405+
}
1406+
1407+
func TestNonPublicTableNotAccessibilityInInfoSchema(t *testing.T) {
1408+
testNonPublicTableNotAccessibilityInInfoSchema(t, false)
1409+
}
1410+
1411+
func TestNonPublicTableNotAccessibilityInInfoSchemaV2(t *testing.T) {
1412+
testNonPublicTableNotAccessibilityInInfoSchema(t, true)
1413+
}

0 commit comments

Comments
 (0)