Skip to content

Commit

Permalink
*: support admin show ddl (#7274)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala authored Aug 7, 2018
1 parent 61b4ff2 commit 3a3845f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 29 deletions.
37 changes: 20 additions & 17 deletions ddl/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 8 additions & 4 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
18 changes: 17 additions & 1 deletion util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3a3845f

Please sign in to comment.