Skip to content

Commit

Permalink
*: support 'admin show ddl jobs <number>' grammar (#7028)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and zz-jason committed Jul 12, 2018
1 parent 5f7fc80 commit 4616636
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 20 deletions.
9 changes: 5 additions & 4 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,11 @@ type HandleRange struct {
type AdminStmt struct {
stmtNode

Tp AdminStmtType
Index string
Tables []*TableName
JobIDs []int64
Tp AdminStmtType
Index string
Tables []*TableName
JobIDs []int64
JobNumber int64

HandleRanges []HandleRange
}
Expand Down
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func (b *executorBuilder) buildShowDDL(v *plan.ShowDDL) Executor {

func (b *executorBuilder) buildShowDDLJobs(v *plan.ShowDDLJobs) Executor {
e := &ShowDDLJobsExec{
jobNumber: v.JobNumber,
is: b.is,
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ExplainID()),
}
Expand Down
16 changes: 9 additions & 7 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,10 @@ func (e *ShowDDLExec) Next(ctx context.Context, chk *chunk.Chunk) error {
type ShowDDLJobsExec struct {
baseExecutor

cursor int
jobs []*model.Job
is infoschema.InfoSchema
cursor int
jobs []*model.Job
jobNumber int64
is infoschema.InfoSchema
}

// ShowDDLJobQueriesExec represents a show DDL job queries executor.
Expand All @@ -246,8 +247,7 @@ func (e *ShowDDLJobQueriesExec) Open(ctx context.Context) error {
if err != nil {
return errors.Trace(err)
}
// TODO: need to return the job that the user needs.
historyJobs, err := admin.GetHistoryDDLJobs(e.ctx.Txn())
historyJobs, err := admin.GetHistoryDDLJobs(e.ctx.Txn(), admin.DefNumHistoryJobs)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -288,8 +288,10 @@ func (e *ShowDDLJobsExec) Open(ctx context.Context) error {
if err != nil {
return errors.Trace(err)
}

historyJobs, err := admin.GetHistoryDDLJobs(e.ctx.Txn())
if e.jobNumber == 0 {
e.jobNumber = admin.DefNumHistoryJobs
}
historyJobs, err := admin.GetHistoryDDLJobs(e.ctx.Txn(), int(e.jobNumber))
if err != nil {
return errors.Trace(err)
}
Expand Down
14 changes: 12 additions & 2 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,23 @@ func (s *testSuite) TestAdmin(c *C) {
c.Assert(row.Len(), Equals, 10)
txn, err = s.store.Begin()
c.Assert(err, IsNil)
historyJobs, err := admin.GetHistoryDDLJobs(txn)
historyJobs, err := admin.GetHistoryDDLJobs(txn, admin.DefNumHistoryJobs)
c.Assert(len(historyJobs), Greater, 1)
c.Assert(len(row.GetString(1)), Greater, 0)
c.Assert(err, IsNil)
c.Assert(row.GetInt64(0), Equals, historyJobs[0].ID)
c.Assert(err, IsNil)

r, err = tk.Exec("admin show ddl jobs 20")
c.Assert(err, IsNil)
chk = r.NewChunk()
err = r.Next(ctx, chk)
c.Assert(err, IsNil)
row = chk.GetRow(0)
c.Assert(row.Len(), Equals, 10)
c.Assert(row.GetInt64(0), Equals, historyJobs[0].ID)
c.Assert(err, IsNil)

// show DDL job queries test
tk.MustExec("use test")
tk.MustExec("drop table if exists admin_test2")
Expand All @@ -196,7 +206,7 @@ func (s *testSuite) TestAdmin(c *C) {
result.Check(testkit.Rows())
result = tk.MustQuery(`admin show ddl job queries 1, 2, 3, 4`)
result.Check(testkit.Rows())
historyJob, err := admin.GetHistoryDDLJobs(txn)
historyJob, err := admin.GetHistoryDDLJobs(txn, admin.DefNumHistoryJobs)
result = tk.MustQuery(fmt.Sprintf("admin show ddl job queries %d", historyJob[0].ID))
result.Check(testkit.Rows(historyJob[0].Query))
c.Assert(err, IsNil)
Expand Down
7 changes: 7 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -5096,6 +5096,13 @@ AdminStmt:
{
$$ = &ast.AdminStmt{Tp: ast.AdminShowDDLJobs}
}
| "ADMIN" "SHOW" "DDL" "JOBS" NUM
{
$$ = &ast.AdminStmt{
Tp: ast.AdminShowDDLJobs,
JobNumber: $5.(int64),
}
}
| "ADMIN" "CHECK" "TABLE" TableNameList
{
$$ = &ast.AdminStmt{
Expand Down
2 changes: 2 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ func (s *testParserSuite) TestDMLStmt(c *C) {
// for admin
{"admin show ddl;", true},
{"admin show ddl jobs;", true},
{"admin show ddl jobs 20;", true},
{"admin show ddl jobs -1;", false},
{"admin show ddl job queries 1", true},
{"admin show ddl job queries 1, 2, 3, 4", true},
{"admin check table t1, t2;", true},
Expand Down
2 changes: 2 additions & 0 deletions plan/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type ShowDDL struct {
// ShowDDLJobs is for showing DDL job list.
type ShowDDLJobs struct {
baseSchemaProducer

JobNumber int64
}

// ShowDDLJobQueries is for showing DDL job queries sql.
Expand Down
2 changes: 1 addition & 1 deletion plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (b *planBuilder) buildAdmin(as *ast.AdminStmt) Plan {
p.SetSchema(buildShowDDLFields())
ret = p
case ast.AdminShowDDLJobs:
p := &ShowDDLJobs{}
p := &ShowDDLJobs{JobNumber: as.JobNumber}
p.SetSchema(buildShowDDLJobsFields())
ret = p
case ast.AdminCancelDDLJobs:
Expand Down
11 changes: 7 additions & 4 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,21 @@ func GetDDLJobs(txn kv.Transaction) ([]*model.Job, error) {
// MaxHistoryJobs is exported for testing.
const MaxHistoryJobs = 10

// DefNumHistoryJobs is default value of the default number of history job
const DefNumHistoryJobs = 10

// GetHistoryDDLJobs returns the DDL history jobs and an error.
// The maximum count of history jobs is MaxHistoryJobs.
func GetHistoryDDLJobs(txn kv.Transaction) ([]*model.Job, error) {
// The maximum count of history jobs is num.
func GetHistoryDDLJobs(txn kv.Transaction, maxNumJobs int) ([]*model.Job, error) {
t := meta.NewMeta(txn)
jobs, err := t.GetAllHistoryDDLJobs()
if err != nil {
return nil, errors.Trace(err)
}

jobsLen := len(jobs)
if jobsLen > MaxHistoryJobs {
start := jobsLen - MaxHistoryJobs
if jobsLen > maxNumJobs {
start := jobsLen - maxNumJobs
jobs = jobs[start:]
}
jobsLen = len(jobs)
Expand Down
4 changes: 2 additions & 2 deletions util/admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (s *testSuite) TestGetHistoryDDLJobs(c *C) {
}
err = t.AddHistoryDDLJob(jobs[i])
c.Assert(err, IsNil)
historyJobs, err1 := GetHistoryDDLJobs(txn)
historyJobs, err1 := GetHistoryDDLJobs(txn, DefNumHistoryJobs)
c.Assert(err1, IsNil)
if i+1 > MaxHistoryJobs {
c.Assert(historyJobs, HasLen, MaxHistoryJobs)
Expand All @@ -182,7 +182,7 @@ func (s *testSuite) TestGetHistoryDDLJobs(c *C) {
}

delta := cnt - MaxHistoryJobs
historyJobs, err := GetHistoryDDLJobs(txn)
historyJobs, err := GetHistoryDDLJobs(txn, DefNumHistoryJobs)
c.Assert(err, IsNil)
c.Assert(historyJobs, HasLen, MaxHistoryJobs)
l := len(historyJobs) - 1
Expand Down

0 comments on commit 4616636

Please sign in to comment.