From 5404e2eaae27b9ce31264a4eff0a41aa92ffe328 Mon Sep 17 00:00:00 2001 From: crazycs Date: Tue, 14 Aug 2018 21:44:57 +0800 Subject: [PATCH] ddl: add admin check before drop table using building flag. (#7343) --- Makefile | 15 ++++++++++++++- config/config.go | 10 ++++++++++ executor/admin_test.go | 5 +++++ executor/ddl.go | 13 +++++++++++++ executor/executor.go | 2 +- executor/executor_test.go | 12 ++++++++++-- util/printer/printer.go | 1 + 7 files changed, 54 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 9f47f35540f38..64c93de20d7b3 100644 --- a/Makefile +++ b/Makefile @@ -34,12 +34,18 @@ LDFLAGS += -X "github.com/pingcap/tidb/util/printer.TiDBGitHash=$(shell git rev- LDFLAGS += -X "github.com/pingcap/tidb/util/printer.TiDBGitBranch=$(shell git rev-parse --abbrev-ref HEAD)" LDFLAGS += -X "github.com/pingcap/tidb/util/printer.GoVersion=$(shell go version)" +TEST_LDFLAGS = -X "github.com/pingcap/tidb/config.checkBeforeDropLDFlag=1" + +CHECK_LDFLAGS += $(LDFLAGS) ${TEST_LDFLAGS} + TARGET = "" .PHONY: all build update parser clean todo test gotest interpreter server dev benchkv benchraw check parserlib checklist default: server buildsucc +server-admin-check: server_check buildsucc + buildsucc: @echo Build TiDB Server successfully! @@ -141,7 +147,7 @@ ifeq ("$(TRAVIS_COVERAGE)", "1") else @echo "Running in native mode." @export log_level=error; \ - $(GOTEST) -cover $(PACKAGES) || { $(GOFAIL_DISABLE); exit 1; } + $(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover $(PACKAGES) || { $(GOFAIL_DISABLE); exit 1; } endif @$(GOFAIL_DISABLE) @@ -178,6 +184,13 @@ else $(GOBUILD) $(RACE_FLAG) -ldflags '$(LDFLAGS)' -o '$(TARGET)' tidb-server/main.go endif +server_check: parserlib +ifeq ($(TARGET), "") + $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o bin/tidb-server tidb-server/main.go +else + $(GOBUILD) $(RACE_FLAG) -ldflags '$(CHECK_LDFLAGS)' -o '$(TARGET)' tidb-server/main.go +endif + benchkv: $(GOBUILD) -ldflags '$(LDFLAGS)' -o bin/benchkv cmd/benchkv/main.go diff --git a/config/config.go b/config/config.go index 036ca3977700a..7f2ce64441a9d 100644 --- a/config/config.go +++ b/config/config.go @@ -36,6 +36,10 @@ var ( "mocktikv": true, "tikv": true, } + // checkTableBeforeDrop enable to execute `admin check table` before `drop table`. + CheckTableBeforeDrop = false + // checkBeforeDropLDFlag is a go build flag. + checkBeforeDropLDFlag = "None" ) // Config contains configuration options. @@ -374,6 +378,12 @@ func (t *OpenTracing) ToTracingConfig() *tracing.Configuration { return ret } +func init() { + if checkBeforeDropLDFlag == "1" { + CheckTableBeforeDrop = true + } +} + // The following constants represents the valid action configurations for OOMAction. // NOTE: Althrough the values is case insensitiv, we should use lower-case // strings because the configuration value will be transformed to lower-case diff --git a/executor/admin_test.go b/executor/admin_test.go index 2b09b202966e5..87f62784a3676 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -471,6 +471,11 @@ func (s *testSuite) TestAdminCheckTable(c *C) { INDEX indexIDname (ID(8),name(8)));`) tk.MustExec(`INSERT INTO t VALUES ('keyword','urlprefix','text/ /text');`) tk.MustExec(`admin check table t;`) + + tk.MustExec("use mysql") + tk.MustExec(`admin check table test.t;`) + _, err := tk.Exec("admin check table t") + c.Assert(err, NotNil) } func (s *testSuite) TestAdminCheckPrimaryIndex(c *C) { diff --git a/executor/ddl.go b/executor/ddl.go index ab0a1055f496d..ced59b52fb302 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -14,10 +14,12 @@ package executor import ( + "fmt" "strings" "github.com/juju/errors" "github.com/pingcap/tidb/ast" + "github.com/pingcap/tidb/config" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/model" @@ -25,6 +27,8 @@ import ( "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/types" "github.com/pingcap/tidb/util/chunk" + "github.com/pingcap/tidb/util/sqlexec" + log "github.com/sirupsen/logrus" "golang.org/x/net/context" ) @@ -194,6 +198,15 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error { return errors.Trace(err) } + if config.CheckTableBeforeDrop { + log.Warnf("admin check table `%s`.`%s` before drop.", fullti.Schema.O, fullti.Name.O) + sql := fmt.Sprintf("admin check table `%s`.`%s`", fullti.Schema.O, fullti.Name.O) + _, _, err = e.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(e.ctx, sql) + if err != nil { + return errors.Trace(err) + } + } + err = domain.GetDomain(e.ctx).DDL().DropTable(e.ctx, fullti) if infoschema.ErrDatabaseNotExists.Equal(err) || infoschema.ErrTableNotExists.Equal(err) { notExistTables = append(notExistTables, fullti.String()) diff --git a/executor/executor.go b/executor/executor.go index 51a126c0d27b6..552e7a3a7ba7d 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -369,8 +369,8 @@ func (e *CheckTableExec) Next(ctx context.Context, chk *chunk.Chunk) error { return nil } defer func() { e.done = true }() - dbName := model.NewCIStr(e.ctx.GetSessionVars().CurrentDB) for _, t := range e.tables { + dbName := t.DBInfo.Name tb, err := e.is.TableByName(dbName, t.Name) if err != nil { return errors.Trace(err) diff --git a/executor/executor_test.go b/executor/executor_test.go index ae0156ea61570..cc97198beebef 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -233,9 +233,17 @@ func (s *testSuite) TestAdmin(c *C) { c.Assert(err, IsNil) err = txn.Commit(context.Background()) c.Assert(err, IsNil) - r, err = tk.Exec("admin check table admin_test") - c.Assert(err, NotNil) + r, err_admin := tk.Exec("admin check table admin_test") + c.Assert(err_admin, NotNil) + + if config.CheckTableBeforeDrop { + r, err = tk.Exec("drop table admin_test") + c.Assert(err.Error(), Equals, err_admin.Error()) + // Drop inconsistency index. + tk.MustExec("alter table admin_test drop index c1") + tk.MustExec("admin check table admin_test") + } // checksum table test tk.MustExec("create table checksum_with_index (id int, count int, PRIMARY KEY(id), KEY(count))") tk.MustExec("create table checksum_without_index (id int, count int, PRIMARY KEY(id))") diff --git a/util/printer/printer.go b/util/printer/printer.go index 28b01998843a6..b3f57cc020d8a 100644 --- a/util/printer/printer.go +++ b/util/printer/printer.go @@ -43,6 +43,7 @@ func PrintTiDBInfo() { log.Infof("UTC Build Time: %s", TiDBBuildTS) log.Infof("GoVersion: %s", GoVersion) log.Infof("Race Enabled: %v", israce.RaceEnabled) + log.Infof("Check Table Before Drop: %v", config.CheckTableBeforeDrop) log.Infof("TiKV Min Version: %s", TiKVMinVersion) configJSON, err := json.Marshal(config.GetGlobalConfig()) if err != nil {