From 3a3845fb24a971f57fd0abab999799a9ff3562c8 Mon Sep 17 00:00:00 2001 From: Lynn Date: Tue, 7 Aug 2018 18:23:41 +0800 Subject: [PATCH] *: support admin show ddl (#7274) --- ddl/stat.go | 37 ++++++++++++++++++++----------------- executor/executor.go | 12 ++++++++---- meta/meta.go | 2 +- plan/planbuilder.go | 2 +- util/admin/admin.go | 22 +++++++++++++++++----- util/admin/admin_test.go | 18 +++++++++++++++++- 6 files changed, 64 insertions(+), 29 deletions(-) diff --git a/ddl/stat.go b/ddl/stat.go index 7b5ec8f0387e6..63917f0f88e16 100644 --- a/ddl/stat.go +++ b/ddl/stat.go @@ -65,23 +65,26 @@ func (d *ddl) Stats(vars *variable.SessionVars) (map[string]interface{}, error) m[ddlSchemaVersion] = ddlInfo.SchemaVer // TODO: Get the owner information. - if ddlInfo.Job != nil { - m[ddlJobID] = ddlInfo.Job.ID - m[ddlJobAction] = ddlInfo.Job.Type.String() - m[ddlJobStartTS] = ddlInfo.Job.StartTS / 1e9 // unit: second - m[ddlJobState] = ddlInfo.Job.State.String() - m[ddlJobRows] = ddlInfo.Job.RowCount - if ddlInfo.Job.Error == nil { - m[ddlJobError] = "" - } else { - m[ddlJobError] = ddlInfo.Job.Error.Error() - } - m[ddlJobSchemaState] = ddlInfo.Job.SchemaState.String() - m[ddlJobSchemaID] = ddlInfo.Job.SchemaID - m[ddlJobTableID] = ddlInfo.Job.TableID - m[ddlJobSnapshotVer] = ddlInfo.Job.SnapshotVer - m[ddlJobReorgHandle] = ddlInfo.ReorgHandle - m[ddlJobArgs] = ddlInfo.Job.Args + if len(ddlInfo.Jobs) == 0 { + return m, nil + } + // TODO: Add all job infromation if needed. + job := ddlInfo.Jobs[0] + m[ddlJobID] = job.ID + m[ddlJobAction] = job.Type.String() + m[ddlJobStartTS] = job.StartTS / 1e9 // unit: second + m[ddlJobState] = job.State.String() + m[ddlJobRows] = job.RowCount + if job.Error == nil { + m[ddlJobError] = "" + } else { + m[ddlJobError] = job.Error.Error() } + m[ddlJobSchemaState] = job.SchemaState.String() + m[ddlJobSchemaID] = job.SchemaID + m[ddlJobTableID] = job.TableID + m[ddlJobSnapshotVer] = job.SnapshotVer + m[ddlJobReorgHandle] = ddlInfo.ReorgHandle + m[ddlJobArgs] = job.Args return m, nil } diff --git a/executor/executor.go b/executor/executor.go index 349b0cbd085b1..38454ac9ab077 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -199,13 +199,17 @@ func (e *ShowDDLExec) Next(ctx context.Context, chk *chunk.Chunk) error { return nil } - ddlJob := "" - if e.ddlInfo.Job != nil { - ddlJob = e.ddlInfo.Job.String() + ddlJobs := "" + l := len(e.ddlInfo.Jobs) + for i, job := range e.ddlInfo.Jobs { + ddlJobs += job.String() + if i != l-1 { + ddlJobs += "\n" + } } chk.AppendInt64(0, e.ddlInfo.SchemaVer) chk.AppendString(1, e.ddlOwnerID) - chk.AppendString(2, ddlJob) + chk.AppendString(2, ddlJobs) chk.AppendString(3, e.selfID) e.done = true return nil diff --git a/meta/meta.go b/meta/meta.go index f662dbcc83ae5..0f9f7a80e3622 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -463,7 +463,7 @@ var ( type JobListKeyType []byte var ( - // DefaultJobListKey keeps all actions of DDL jobs. + // DefaultJobListKey keeps all actions of DDL jobs except "add index". DefaultJobListKey JobListKeyType = mDDLJobListKey // AddIndexJobListKey only keeps the action of adding index. AddIndexJobListKey JobListKeyType = mDDLJobAddIdxList diff --git a/plan/planbuilder.go b/plan/planbuilder.go index 30cc7e8d2524e..2f01bea731a72 100644 --- a/plan/planbuilder.go +++ b/plan/planbuilder.go @@ -633,7 +633,7 @@ func buildShowDDLFields() *expression.Schema { schema := expression.NewSchema(make([]*expression.Column, 0, 4)...) schema.Append(buildColumn("", "SCHEMA_VER", mysql.TypeLonglong, 4)) schema.Append(buildColumn("", "OWNER", mysql.TypeVarchar, 64)) - schema.Append(buildColumn("", "JOB", mysql.TypeVarchar, 128)) + schema.Append(buildColumn("", "RUNNING_JOBS", mysql.TypeVarchar, 256)) schema.Append(buildColumn("", "SELF_ID", mysql.TypeVarchar, 64)) return schema diff --git a/util/admin/admin.go b/util/admin/admin.go index efc2a60ebf1df..5a0a3f64a08ca 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -39,8 +39,8 @@ import ( // DDLInfo is for DDL information. type DDLInfo struct { SchemaVer int64 - ReorgHandle int64 // it's only used for DDL information. - Job *model.Job + ReorgHandle int64 // It's only used for DDL information. + Jobs []*model.Job // It's the currently running jobs. } // GetDDLInfo returns DDL information. @@ -49,19 +49,31 @@ func GetDDLInfo(txn kv.Transaction) (*DDLInfo, error) { info := &DDLInfo{} t := meta.NewMeta(txn) - info.Job, err = t.GetDDLJobByIdx(0) + info.Jobs = make([]*model.Job, 0, 2) + job, err := t.GetDDLJobByIdx(0) if err != nil { return nil, errors.Trace(err) } + if job != nil { + info.Jobs = append(info.Jobs, job) + } + addIdxJob, err := t.GetDDLJobByIdx(0, meta.AddIndexJobListKey) + if err != nil { + return nil, errors.Trace(err) + } + if addIdxJob != nil { + info.Jobs = append(info.Jobs, addIdxJob) + } + info.SchemaVer, err = t.GetSchemaVersion() if err != nil { return nil, errors.Trace(err) } - if info.Job == nil { + if addIdxJob == nil { return info, nil } - info.ReorgHandle, _, _, err = t.GetDDLReorgHandle(info.Job) + info.ReorgHandle, _, _, err = t.GetDDLReorgHandle(addIdxJob) if err != nil { return nil, errors.Trace(err) } diff --git a/util/admin/admin_test.go b/util/admin/admin_test.go index 7dc87d33db35f..ed58353ac894b 100644 --- a/util/admin/admin_test.go +++ b/util/admin/admin_test.go @@ -80,11 +80,27 @@ func (s *testSuite) TestGetDDLInfo(c *C) { Type: model.ActionCreateSchema, RowCount: 0, } + job1 := &model.Job{ + SchemaID: dbInfo2.ID, + Type: model.ActionAddIndex, + RowCount: 0, + } err = t.EnQueueDDLJob(job) c.Assert(err, IsNil) info, err := GetDDLInfo(txn) c.Assert(err, IsNil) - c.Assert(info.Job, DeepEquals, job) + c.Assert(info.Jobs, HasLen, 1) + c.Assert(info.Jobs[0], DeepEquals, job) + c.Assert(info.ReorgHandle, Equals, int64(0)) + // Two jobs. + t = meta.NewMeta(txn, meta.AddIndexJobListKey) + err = t.EnQueueDDLJob(job1) + c.Assert(err, IsNil) + info, err = GetDDLInfo(txn) + c.Assert(err, IsNil) + c.Assert(info.Jobs, HasLen, 2) + c.Assert(info.Jobs[0], DeepEquals, job) + c.Assert(info.Jobs[1], DeepEquals, job1) c.Assert(info.ReorgHandle, Equals, int64(0)) err = txn.Rollback() c.Assert(err, IsNil)