github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/control_jobs.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/jobs"
    17  	"github.com/cockroachdb/cockroach/pkg/server/telemetry"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
    20  	"github.com/cockroachdb/errors"
    21  )
    22  
    23  type controlJobsNode struct {
    24  	rows          planNode
    25  	desiredStatus jobs.Status
    26  	numRows       int
    27  }
    28  
    29  var jobCommandToDesiredStatus = map[tree.JobCommand]jobs.Status{
    30  	tree.CancelJob: jobs.StatusCanceled,
    31  	tree.ResumeJob: jobs.StatusRunning,
    32  	tree.PauseJob:  jobs.StatusPaused,
    33  }
    34  
    35  // FastPathResults implements the planNodeFastPath inteface.
    36  func (n *controlJobsNode) FastPathResults() (int, bool) {
    37  	return n.numRows, true
    38  }
    39  
    40  func (n *controlJobsNode) startExec(params runParams) error {
    41  	reg := params.p.ExecCfg().JobRegistry
    42  	for {
    43  		ok, err := n.rows.Next(params)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		if !ok {
    48  			break
    49  		}
    50  
    51  		jobIDDatum := n.rows.Values()[0]
    52  		if jobIDDatum == tree.DNull {
    53  			continue
    54  		}
    55  
    56  		jobID, ok := tree.AsDInt(jobIDDatum)
    57  		if !ok {
    58  			return errors.AssertionFailedf("%q: expected *DInt, found %T", jobIDDatum, jobIDDatum)
    59  		}
    60  
    61  		switch n.desiredStatus {
    62  		case jobs.StatusPaused:
    63  			err = reg.PauseRequested(params.ctx, params.p.txn, int64(jobID))
    64  		case jobs.StatusRunning:
    65  			err = reg.Resume(params.ctx, params.p.txn, int64(jobID))
    66  		case jobs.StatusCanceled:
    67  			err = reg.CancelRequested(params.ctx, params.p.txn, int64(jobID))
    68  		default:
    69  			err = errors.AssertionFailedf("unhandled status %v", n.desiredStatus)
    70  		}
    71  		if err != nil {
    72  			return err
    73  		}
    74  		n.numRows++
    75  	}
    76  	switch n.desiredStatus {
    77  	case jobs.StatusPaused:
    78  		telemetry.Inc(sqltelemetry.SchemaJobControlCounter("pause"))
    79  	case jobs.StatusRunning:
    80  		telemetry.Inc(sqltelemetry.SchemaJobControlCounter("resume"))
    81  	case jobs.StatusCanceled:
    82  		telemetry.Inc(sqltelemetry.SchemaJobControlCounter("cancel"))
    83  	}
    84  	return nil
    85  }
    86  
    87  func (*controlJobsNode) Next(runParams) (bool, error) { return false, nil }
    88  
    89  func (*controlJobsNode) Values() tree.Datums { return nil }
    90  
    91  func (n *controlJobsNode) Close(ctx context.Context) {
    92  	n.rows.Close(ctx)
    93  }