diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 1bca9bc15e82f..ecacb79a239b0 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -86,6 +86,7 @@ func (d *ddl) CreateSchema(ctx sessionctx.Context, schema model.CIStr, charsetIn job := &model.Job{ SchemaID: schemaID, + SchemaName: dbInfo.Name.L, Type: model.ActionCreateSchema, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{dbInfo}, @@ -144,6 +145,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( // Do the DDL job. job := &model.Job{ SchemaID: dbInfo.ID, + SchemaName: dbInfo.Name.L, Type: model.ActionModifySchemaCharsetAndCollate, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{toCharset, toCollate}, @@ -161,6 +163,7 @@ func (d *ddl) DropSchema(ctx sessionctx.Context, schema model.CIStr) (err error) } job := &model.Job{ SchemaID: old.ID, + SchemaName: old.Name.L, Type: model.ActionDropSchema, BinlogInfo: &model.HistoryInfo{}, } @@ -1293,6 +1296,7 @@ func (d *ddl) CreateTableWithLike(ctx sessionctx.Context, ident, referIdent ast. job := &model.Job{ SchemaID: schema.ID, TableID: tblInfo.ID, + SchemaName: schema.Name.L, Type: model.ActionCreateTable, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{tblInfo}, @@ -1476,6 +1480,7 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e job := &model.Job{ SchemaID: schema.ID, TableID: tbInfo.ID, + SchemaName: schema.Name.L, Type: model.ActionCreateTable, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{tbInfo}, @@ -1543,6 +1548,7 @@ func (d *ddl) RecoverTable(ctx sessionctx.Context, recoverInfo *RecoverInfo) (er job := &model.Job{ SchemaID: schemaID, TableID: tbInfo.ID, + SchemaName: schema.Name.L, Type: model.ActionRecoverTable, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{tbInfo, recoverInfo.CurAutoIncID, recoverInfo.DropJobID, @@ -1610,6 +1616,7 @@ func (d *ddl) CreateView(ctx sessionctx.Context, s *ast.CreateViewStmt) (err err job := &model.Job{ SchemaID: schema.ID, TableID: tbInfo.ID, + SchemaName: schema.Name.L, Type: model.ActionCreateView, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{tbInfo, s.OrReplace, oldViewTblID}, @@ -2079,6 +2086,7 @@ func (d *ddl) RebaseAutoID(ctx sessionctx.Context, ident ast.Ident, newBase int6 job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionRebaseAutoID, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{newBase}, @@ -2109,6 +2117,7 @@ func (d *ddl) ShardRowID(ctx sessionctx.Context, tableIdent ast.Ident, uVal uint Type: model.ActionShardRowID, SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{uVal}, } @@ -2225,6 +2234,7 @@ func (d *ddl) AddColumn(ctx sessionctx.Context, ti ast.Ident, spec *ast.AlterTab job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionAddColumn, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{col, spec.Position, 0}, @@ -2270,6 +2280,7 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec * job := &model.Job{ SchemaID: schema.ID, TableID: meta.ID, + SchemaName: schema.Name.L, Type: model.ActionAddTablePartition, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{partInfo}, @@ -2342,6 +2353,7 @@ func (d *ddl) TruncateTablePartition(ctx sessionctx.Context, ident ast.Ident, sp job := &model.Job{ SchemaID: schema.ID, TableID: meta.ID, + SchemaName: schema.Name.L, Type: model.ActionTruncateTablePartition, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{pid}, @@ -2384,6 +2396,7 @@ func (d *ddl) DropTablePartition(ctx sessionctx.Context, ident ast.Ident, spec * job := &model.Job{ SchemaID: schema.ID, TableID: meta.ID, + SchemaName: schema.Name.L, Type: model.ActionDropTablePartition, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{partName}, @@ -2422,6 +2435,7 @@ func (d *ddl) DropColumn(ctx sessionctx.Context, ti ast.Ident, colName model.CIS job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionDropColumn, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{colName}, @@ -2765,6 +2779,7 @@ func (d *ddl) getModifiableColumnJob(ctx sessionctx.Context, ident ast.Ident, or job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionModifyColumn, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{&newCol, originalColName, spec.Position, modifyColumnTp}, @@ -2929,6 +2944,7 @@ func (d *ddl) AlterColumn(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionSetDefaultValue, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{col}, @@ -2955,6 +2971,7 @@ func (d *ddl) AlterTableComment(ctx sessionctx.Context, ident ast.Ident, spec *a job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionModifyTableComment, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{spec.Comment}, @@ -3006,6 +3023,7 @@ func (d *ddl) AlterTableCharsetAndCollate(ctx sessionctx.Context, ident ast.Iden job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionModifyTableCharsetAndCollate, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{toCharset, toCollate}, @@ -3166,6 +3184,7 @@ func (d *ddl) RenameIndex(ctx sessionctx.Context, ident ast.Ident, spec *ast.Alt job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionRenameIndex, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{spec.FromKey, spec.ToKey}, @@ -3190,6 +3209,7 @@ func (d *ddl) DropTable(ctx sessionctx.Context, ti ast.Ident) (err error) { job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionDropTable, BinlogInfo: &model.HistoryInfo{}, } @@ -3222,6 +3242,7 @@ func (d *ddl) DropView(ctx sessionctx.Context, ti ast.Ident) (err error) { job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionDropView, BinlogInfo: &model.HistoryInfo{}, } @@ -3244,6 +3265,7 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error { job := &model.Job{ SchemaID: schema.ID, TableID: tb.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionTruncateTable, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{newTableID}, @@ -3312,6 +3334,7 @@ func (d *ddl) RenameTable(ctx sessionctx.Context, oldIdent, newIdent ast.Ident, job := &model.Job{ SchemaID: newSchema.ID, TableID: oldTbl.Meta().ID, + SchemaName: newSchema.Name.L, Type: model.ActionRenameTable, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{oldSchema.ID, newIdent.Name}, @@ -3444,6 +3467,7 @@ func (d *ddl) CreateIndex(ctx sessionctx.Context, ti ast.Ident, unique bool, ind job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionAddIndex, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{unique, indexName, idxColNames, indexOption}, @@ -3500,6 +3524,7 @@ func (d *ddl) CreateForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName mode job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionAddForeignKey, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{fkInfo}, @@ -3526,6 +3551,7 @@ func (d *ddl) DropForeignKey(ctx sessionctx.Context, ti ast.Ident, fkName model. job := &model.Job{ SchemaID: schema.ID, TableID: t.Meta().ID, + SchemaName: schema.Name.L, Type: model.ActionDropForeignKey, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{fkName}, @@ -3586,6 +3612,7 @@ func (d *ddl) DropIndex(ctx sessionctx.Context, ti ast.Ident, indexName model.CI SchemaID: schema.ID, TableID: t.Meta().ID, Type: jobTp, + SchemaName: schema.Name.L, BinlogInfo: &model.HistoryInfo{}, Args: []interface{}{indexName}, } diff --git a/executor/executor.go b/executor/executor.go index 2860d5da4ba24..e4958039e719c 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -412,15 +412,35 @@ func (e *ShowDDLJobsExec) Next(ctx context.Context, req *chunk.Chunk) error { numCurBatch := mathutil.Min(req.Capacity(), len(e.jobs)-e.cursor) for i := e.cursor; i < e.cursor+numCurBatch; i++ { req.AppendInt64(0, e.jobs[i].ID) - req.AppendString(1, getSchemaName(e.is, e.jobs[i].SchemaID)) - req.AppendString(2, getTableName(e.is, e.jobs[i].TableID)) + schemaName := e.jobs[i].SchemaName + tableName := "" + finishTS := uint64(0) + if e.jobs[i].BinlogInfo != nil { + finishTS = e.jobs[i].BinlogInfo.FinishedTS + if e.jobs[i].BinlogInfo.TableInfo != nil { + tableName = e.jobs[i].BinlogInfo.TableInfo.Name.L + } + if len(schemaName) == 0 && e.jobs[i].BinlogInfo.DBInfo != nil { + schemaName = e.jobs[i].BinlogInfo.DBInfo.Name.L + } + } + // For compatibility, the old version of DDL Job wasn't store the schema name and table name. + if len(schemaName) == 0 { + schemaName = getSchemaName(e.is, e.jobs[i].SchemaID) + } + if len(tableName) == 0 { + tableName = getTableName(e.is, e.jobs[i].TableID) + } + req.AppendString(1, schemaName) + req.AppendString(2, tableName) req.AppendString(3, e.jobs[i].Type.String()) req.AppendString(4, e.jobs[i].SchemaState.String()) req.AppendInt64(5, e.jobs[i].SchemaID) req.AppendInt64(6, e.jobs[i].TableID) req.AppendInt64(7, e.jobs[i].RowCount) req.AppendString(8, model.TSConvert2Time(e.jobs[i].StartTS).String()) - req.AppendString(9, e.jobs[i].State.String()) + req.AppendString(9, model.TSConvert2Time(finishTS).String()) + req.AppendString(10, e.jobs[i].State.String()) } e.cursor += numCurBatch return nil diff --git a/executor/executor_test.go b/executor/executor_test.go index 96e6256b757a0..d68be53420147 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -217,7 +217,7 @@ func (s *testSuite) TestAdmin(c *C) { err = r.Next(ctx, req) c.Assert(err, IsNil) row = req.GetRow(0) - c.Assert(row.Len(), Equals, 10) + c.Assert(row.Len(), Equals, 11) txn, err = s.store.Begin() c.Assert(err, IsNil) historyJobs, err := admin.GetHistoryDDLJobs(txn, admin.DefNumHistoryJobs) @@ -233,7 +233,7 @@ func (s *testSuite) TestAdmin(c *C) { err = r.Next(ctx, req) c.Assert(err, IsNil) row = req.GetRow(0) - c.Assert(row.Len(), Equals, 10) + c.Assert(row.Len(), Equals, 11) c.Assert(row.GetInt64(0), Equals, historyJobs[0].ID) c.Assert(err, IsNil) @@ -302,6 +302,13 @@ func (s *testSuite) TestAdmin(c *C) { tk.MustExec("ALTER TABLE t1 ADD INDEX idx3 (c4);") tk.MustExec("admin check table t1;") + // Test admin show ddl jobs table name after table has been droped. + tk.MustExec("drop table if exists t1;") + re := tk.MustQuery("admin show ddl jobs 1") + rows := re.Rows() + c.Assert(len(rows), Equals, 1) + c.Assert(rows[0][2], Equals, "t1") + // Test for reverse scan get history ddl jobs when ddl history jobs queue has multiple regions. txn, err = s.store.Begin() c.Assert(err, IsNil) @@ -319,6 +326,39 @@ func (s *testSuite) TestAdmin(c *C) { c.Assert(historyJobs, DeepEquals, historyJobs2) } +func (s *testSuite) TestAdminShowDDLJobs(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("create database if not exists test_admin_show_ddl_jobs") + tk.MustExec("use test_admin_show_ddl_jobs") + tk.MustExec("create table t (a int);") + + re := tk.MustQuery("admin show ddl jobs 1") + row := re.Rows()[0] + c.Assert(row[1], Equals, "test_admin_show_ddl_jobs") + jobID, err := strconv.Atoi(row[0].(string)) + c.Assert(err, IsNil) + + c.Assert(tk.Se.NewTxn(context.Background()), IsNil) + txn, err := tk.Se.Txn(true) + c.Assert(err, IsNil) + t := meta.NewMeta(txn) + job, err := t.GetHistoryDDLJob(int64(jobID)) + c.Assert(err, IsNil) + c.Assert(job, NotNil) + // Test for compatibility. Old TiDB version doesn't have SchemaName field, and the BinlogInfo maybe nil. + // See PR: 11561. + job.BinlogInfo = nil + job.SchemaName = "" + err = t.AddHistoryDDLJob(job, true) + c.Assert(err, IsNil) + err = tk.Se.CommitTxn(context.Background()) + c.Assert(err, IsNil) + + re = tk.MustQuery("admin show ddl jobs 1") + row = re.Rows()[0] + c.Assert(row[1], Equals, "test_admin_show_ddl_jobs") +} + func (s *testSuite) TestAdminChecksumOfPartitionedTable(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("USE test;") diff --git a/go.mod b/go.mod index dc423acabf2fc..7bcb2c1d7e9aa 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20200317043902-2838e21ca222 github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 - github.com/pingcap/parser v3.1.0-beta.1.0.20200317043536-9ebea32e03a6+incompatible + github.com/pingcap/parser v3.1.0-beta.1.0.20200318042458-a313c64f73b5+incompatible github.com/pingcap/pd v1.1.0-beta.0.20200213133706-fbbe75e180e6 github.com/pingcap/tidb-tools v3.0.6-0.20191119150227-ff0a3c6e5763+incompatible github.com/pingcap/tipb v0.0.0-20191126033718-169898888b24 diff --git a/go.sum b/go.sum index 47591a8a9f81d..f1c96fb2e850c 100644 --- a/go.sum +++ b/go.sum @@ -241,8 +241,8 @@ github.com/pingcap/kvproto v0.0.0-20200317043902-2838e21ca222 h1:y+qDC9hP5ZMQADk github.com/pingcap/kvproto v0.0.0-20200317043902-2838e21ca222/go.mod h1:IOdRDPLyda8GX2hE/jO7gqaCV/PNFh8BZQCQZXfIOqI= github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9 h1:AJD9pZYm72vMgPcQDww9rkZ1DnWfl0pXV3BOWlkYIjA= github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8= -github.com/pingcap/parser v3.1.0-beta.1.0.20200317043536-9ebea32e03a6+incompatible h1:LXg9RBvy+a6odBikQF0IcITVFYYDCgbVf5YPhM+AIIU= -github.com/pingcap/parser v3.1.0-beta.1.0.20200317043536-9ebea32e03a6+incompatible/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v3.1.0-beta.1.0.20200318042458-a313c64f73b5+incompatible h1:G6rKtIRbBsNpA4bBw6Gf1N44hx7dG6Zt6JL9OpenMtg= +github.com/pingcap/parser v3.1.0-beta.1.0.20200318042458-a313c64f73b5+incompatible/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v1.1.0-beta.0.20200213133706-fbbe75e180e6 h1:6Ut7/Gg6nO2tkrufRncFsI4WnYsmrLI0DN8xcGGOFL8= github.com/pingcap/pd v1.1.0-beta.0.20200213133706-fbbe75e180e6/go.mod h1:zezAKmc5aqNUREQdxxeP4WuAx22FlPQL/p7xFYKoThU= github.com/pingcap/tidb-tools v3.0.6-0.20191119150227-ff0a3c6e5763+incompatible h1:I8HirWsu1MZp6t9G/g8yKCEjJJxtHooKakEgccvdJ4M= diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index b7dd58b80ab50..b29ae057db01d 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -1251,7 +1251,7 @@ func buildCleanupIndexFields() *expression.Schema { } func buildShowDDLJobsFields() *expression.Schema { - schema := expression.NewSchema(make([]*expression.Column, 0, 10)...) + schema := expression.NewSchema(make([]*expression.Column, 0, 11)...) schema.Append(buildColumn("", "JOB_ID", mysql.TypeLonglong, 4)) schema.Append(buildColumn("", "DB_NAME", mysql.TypeVarchar, 64)) schema.Append(buildColumn("", "TABLE_NAME", mysql.TypeVarchar, 64)) @@ -1261,6 +1261,7 @@ func buildShowDDLJobsFields() *expression.Schema { schema.Append(buildColumn("", "TABLE_ID", mysql.TypeLonglong, 4)) schema.Append(buildColumn("", "ROW_COUNT", mysql.TypeLonglong, 4)) schema.Append(buildColumn("", "START_TIME", mysql.TypeVarchar, 64)) + schema.Append(buildColumn("", "END_TIME", mysql.TypeVarchar, 64)) schema.Append(buildColumn("", "STATE", mysql.TypeVarchar, 64)) return schema }