-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: make PAUSE/RESUME/CANCEL JOB (->JOBS) support multiple jobs
Prior to this patch, `CANCEL/PAUSE/RESUME JOB` would accept a single expression as argument. This patch extends the syntax to accept an arbitrary number of job IDs, via a selection query. The syntax becomes `CANCEL/PAUSE/RESUME JOBS <query...>`. The syntax `CANCEL/PAUSE/RESUME JOB` with a single expression is preserved for backward compatibility, and is desugared to `CANCEL/PAUSE/RESUME JOBS VALUES (...)` during parsing. Commands are processed atomically. If a job's state cannot be updated, an error occurs and all the commands are cancelled (i.e. every job remains in its initial state). In addition, the statement now returns the number of job commands processed. Finally, the amount of boilerplate in the AST nodes is reduced, by using a single `ControlJobs` node type instead of separate `ResumeJob` / `CancelJob` / `PauseJob`. Release note (sql change): The `CANCEL/PAUSE/RESUME JOB` statements are extended with variants `CANCEL/PAUSE/RESUME JOBS` able to operate on multiple jobs at once. For example, to pause all running jobs run `PAUSE JOBS SELECT id FROM [SHOW JOBS] WHERE status = 'running'`.
- Loading branch information
Showing
18 changed files
with
315 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2017 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package sql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/jobs" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/types" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type controlJobsNode struct { | ||
rows planNode | ||
desiredStatus jobs.Status | ||
numRows int | ||
} | ||
|
||
var jobCommandToDesiredStatus = map[tree.JobCommand]jobs.Status{ | ||
tree.CancelJob: jobs.StatusCanceled, | ||
tree.ResumeJob: jobs.StatusRunning, | ||
tree.PauseJob: jobs.StatusPaused, | ||
} | ||
|
||
func (p *planner) ControlJobs(ctx context.Context, n *tree.ControlJobs) (planNode, error) { | ||
rows, err := p.newPlan(ctx, n.Jobs, []types.T{types.Int}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cols := planColumns(rows) | ||
if len(cols) != 1 { | ||
return nil, errors.Errorf("%s JOBS expects a single column source, got %d columns", | ||
tree.JobCommandToStatement[n.Command], len(cols)) | ||
} | ||
if !cols[0].Typ.Equivalent(types.Int) { | ||
return nil, errors.Errorf("%s QUERIES requires int values, not type %s", | ||
tree.JobCommandToStatement[n.Command], cols[0].Typ) | ||
} | ||
|
||
return &controlJobsNode{ | ||
rows: rows, | ||
desiredStatus: jobCommandToDesiredStatus[n.Command], | ||
}, nil | ||
} | ||
|
||
// FastPathResults implements the planNodeFastPath inteface. | ||
func (n *controlJobsNode) FastPathResults() (int, bool) { | ||
return n.numRows, true | ||
} | ||
|
||
// startExec implements the execStartable interface. | ||
func (n *controlJobsNode) startExec(params runParams) error { | ||
reg := params.p.ExecCfg().JobRegistry | ||
for { | ||
ok, err := n.rows.Next(params) | ||
if err != nil { | ||
return err | ||
} | ||
if !ok { | ||
break | ||
} | ||
|
||
datum := n.rows.Values()[0] | ||
if datum == tree.DNull { | ||
continue | ||
} | ||
|
||
jobIDDatum := datum.(*tree.DInt) | ||
jobID, ok := tree.AsDInt(jobIDDatum) | ||
if !ok { | ||
return fmt.Errorf("%s is not a valid job ID", jobIDDatum) | ||
} | ||
|
||
switch n.desiredStatus { | ||
case jobs.StatusPaused: | ||
err = reg.Pause(params.ctx, params.p.txn, int64(jobID)) | ||
case jobs.StatusRunning: | ||
err = reg.Resume(params.ctx, params.p.txn, int64(jobID)) | ||
case jobs.StatusCanceled: | ||
err = reg.Cancel(params.ctx, params.p.txn, int64(jobID)) | ||
default: | ||
err = fmt.Errorf("programmer error: unhandled status %v", n.desiredStatus) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
n.numRows++ | ||
} | ||
return nil | ||
} | ||
|
||
func (*controlJobsNode) Next(runParams) (bool, error) { return false, nil } | ||
|
||
func (*controlJobsNode) Values() tree.Datums { return nil } | ||
|
||
func (n *controlJobsNode) Close(ctx context.Context) { | ||
n.rows.Close(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.