diff --git a/pkg/jobs/adopt.go b/pkg/jobs/adopt.go index c47b171355cc..d581e4213312 100644 --- a/pkg/jobs/adopt.go +++ b/pkg/jobs/adopt.go @@ -25,6 +25,14 @@ import ( "github.com/cockroachdb/errors" ) +const claimableStatusTupleString = `(` + + `'` + string(StatusRunning) + `', ` + + `'` + string(StatusPending) + `', ` + + `'` + string(StatusCancelRequested) + `', ` + + `'` + string(StatusPauseRequested) + `', ` + + `'` + string(StatusReverting) + `'` + + `)` + // claimJobs places a claim with the given SessionID to job rows that are // available. func (r *Registry) claimJobs(ctx context.Context, s sqlliveness.Session) error { @@ -34,14 +42,11 @@ func (r *Registry) claimJobs(ctx context.Context, s sqlliveness.Session) error { UPDATE system.jobs SET claim_session_id = $1, claim_instance_id = $2 WHERE claim_session_id IS NULL - AND status NOT IN ($3, $4, $5) + AND status IN `+claimableStatusTupleString+` ORDER BY created DESC - LIMIT $6 + LIMIT $3 RETURNING id;`, - s.ID().UnsafeBytes(), r.ID(), - // Don't touch terminal jobs. - StatusSucceeded, StatusCanceled, StatusFailed, - maxAdoptionsPerLoop, + s.ID().UnsafeBytes(), r.ID(), maxAdoptionsPerLoop, ) if err != nil { return errors.Wrap(err, "could not query jobs table") diff --git a/pkg/jobs/registry.go b/pkg/jobs/registry.go index 85167cd390ec..e4deea5c2dce 100644 --- a/pkg/jobs/registry.go +++ b/pkg/jobs/registry.go @@ -630,11 +630,9 @@ func (r *Registry) Start( UPDATE system.jobs SET claim_session_id = NULL WHERE claim_session_id <> $1 - AND status NOT IN ($2, $3, $4) + AND status IN `+claimableStatusTupleString+` AND NOT crdb_internal.sql_liveness_is_alive(claim_session_id)`, s.ID().UnsafeBytes(), - // Don't touch terminal jobs. - StatusSucceeded, StatusCanceled, StatusFailed, ); err != nil { log.Errorf(ctx, "error expiring job sessions: %s", err) }