From d2b5387183a591847f952aa4e3a3c4597c34dcba Mon Sep 17 00:00:00 2001 From: crazycs Date: Fri, 2 Aug 2019 15:03:24 +0800 Subject: [PATCH] infoschema: fix load drop database schema bug and refine db-table api error. (#11573) --- infoschema/builder.go | 12 ++++++------ infoschema/tables_test.go | 20 ++++++++++++++++++++ server/http_handler.go | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/infoschema/builder.go b/infoschema/builder.go index 7b90e1f23cd12..72d0d9ce29e72 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -154,22 +154,22 @@ func (b *Builder) applyDropSchema(schemaID int64) []int64 { delete(b.is.schemaMap, di.Name.L) // Copy the sortedTables that contain the table we are going to drop. + tableIDs := make([]int64, 0, len(di.Tables)) bucketIdxMap := make(map[int]struct{}) for _, tbl := range di.Tables { bucketIdxMap[tableBucketIdx(tbl.ID)] = struct{}{} + // TODO: If the table ID doesn't exist. + tableIDs = append(tableIDs, tbl.ID) } for bucketIdx := range bucketIdxMap { b.copySortedTablesBucket(bucketIdx) } - ids := make([]int64, 0, len(di.Tables)) di = di.Clone() - for _, tbl := range di.Tables { - b.applyDropTable(di, tbl.ID) - // TODO: If the table ID doesn't exist. - ids = append(ids, tbl.ID) + for _, id := range tableIDs { + b.applyDropTable(di, id) } - return ids + return tableIDs } func (b *Builder) copySortedTablesBucket(bucketIdx int) { diff --git a/infoschema/tables_test.go b/infoschema/tables_test.go index c2c7c84cb1ea5..13fa50cf8d15b 100644 --- a/infoschema/tables_test.go +++ b/infoschema/tables_test.go @@ -22,7 +22,9 @@ import ( . "github.com/pingcap/check" "github.com/pingcap/parser/auth" + "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" + "github.com/pingcap/parser/terror" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" @@ -525,3 +527,21 @@ func (s *testTableSuite) TestColumnStatistics(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustQuery("select * from information_schema.column_statistics").Check(testkit.Rows()) } + +func (s *testTableSuite) TestReloadDropDatabase(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create database test_dbs") + tk.MustExec("use test_dbs") + tk.MustExec("create table t1 (a int)") + tk.MustExec("create table t2 (a int)") + tk.MustExec("create table t3 (a int)") + is := domain.GetDomain(tk.Se).InfoSchema() + t2, err := is.TableByName(model.NewCIStr("test_dbs"), model.NewCIStr("t2")) + c.Assert(err, IsNil) + tk.MustExec("drop database test_dbs") + is = domain.GetDomain(tk.Se).InfoSchema() + _, err = is.TableByName(model.NewCIStr("test_dbs"), model.NewCIStr("t2")) + c.Assert(terror.ErrorEqual(infoschema.ErrTableNotExists, err), IsTrue) + _, ok := is.TableByID(t2.Meta().ID) + c.Assert(ok, IsFalse) +} diff --git a/server/http_handler.go b/server/http_handler.go index 446cd027b3a84..3c59ea613901e 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -1531,8 +1531,8 @@ func (h dbTableHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { dbTblInfo.TableInfo = tbl.Meta() dbInfo, ok := schema.SchemaByTable(dbTblInfo.TableInfo) if !ok { - log.Warnf("can not find the database of table id: %v, table name: %v", dbTblInfo.TableInfo.ID, dbTblInfo.TableInfo.Name) - writeData(w, dbTblInfo) + logutil.BgLogger().Error("can not find the database of the table", zap.Int64("table id", dbTblInfo.TableInfo.ID), zap.String("table name", dbTblInfo.TableInfo.Name.L)) + writeError(w, infoschema.ErrTableNotExists.GenWithStack("Table which ID = %s does not exist.", tableID)) return } dbTblInfo.DBInfo = dbInfo