Skip to content

Commit 0999cc8

Browse files
craig[bot]Vidit Bhat
andcommitted
Merge #144367
144367: drt: add cancel-job operation for backup, restore and LDR r=shailendra-patel a=vidit-bhat This patch introduces cancel-job operations for Backup, Restore and LDR by issuing a `CANCEL JOB` statement. No cleanup is required as cancellation is terminal. Epic: none Fixes: #138500 #138501 Release note: None Co-authored-by: Vidit Bhat <[email protected]>
2 parents 7dc9b6f + d960301 commit 0999cc8

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

pkg/cmd/roachtest/operations/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"add_index.go",
1010
"add_rls_policy.go",
1111
"backup_restore.go",
12+
"cancel_job.go",
1213
"cluster_settings.go",
1314
"debug_zip.go",
1415
"disk_stall.go",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2024 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package operations
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"time"
12+
13+
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster"
14+
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/operation"
15+
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option"
16+
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry"
17+
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/roachtestflags"
18+
"github.com/cockroachdb/cockroach/pkg/util/randutil"
19+
)
20+
21+
func runCancelJob(
22+
jobType string,
23+
) func(ctx context.Context, o operation.Operation, c cluster.Cluster) registry.OperationCleanup {
24+
return func(ctx context.Context, o operation.Operation, c cluster.Cluster) registry.OperationCleanup {
25+
conn := c.Conn(ctx, o.L(), 1, option.VirtualClusterName(roachtestflags.VirtualCluster))
26+
defer conn.Close()
27+
28+
query := `
29+
WITH jobs AS (SHOW JOBS)
30+
SELECT job_id FROM jobs
31+
WHERE job_type = $1 AND status = 'running'
32+
`
33+
rows, err := conn.QueryContext(ctx, query, jobType)
34+
if err != nil {
35+
o.Fatal(err)
36+
}
37+
38+
var jobIds []string
39+
for rows.Next() {
40+
var jobId string
41+
if err := rows.Scan(&jobId); err != nil {
42+
o.Fatal(err)
43+
}
44+
jobIds = append(jobIds, jobId)
45+
}
46+
47+
if len(jobIds) == 0 {
48+
o.Fatal(fmt.Sprintf("no running %s jobs found", jobType))
49+
}
50+
51+
rng, _ := randutil.NewPseudoRand()
52+
jobId := jobIds[rng.Intn(len(jobIds))]
53+
54+
o.Status(fmt.Sprintf("cancelling %s job %s", jobType, jobId))
55+
stmt := fmt.Sprintf("CANCEL JOB %s", jobId)
56+
if _, err := conn.ExecContext(ctx, stmt); err != nil {
57+
o.Fatal(err)
58+
}
59+
60+
o.Status(fmt.Sprintf("cancelled %s job %s", jobType, jobId))
61+
return nil // No cleanup needed after cancel
62+
}
63+
}
64+
65+
func registerCancelJob(r registry.Registry) {
66+
jobs := []struct {
67+
JobType string
68+
OpName string
69+
Owner registry.Owner
70+
Dependency registry.OperationDependency
71+
}{
72+
{"LOGICAL REPLICATION", "cancel-job/logical-replication", registry.OwnerDisasterRecovery, registry.OperationRequiresLDRJobRunning},
73+
{"BACKUP", "cancel-job/backup", registry.OwnerDisasterRecovery, registry.OperationRequiresRunningBackupJob},
74+
{"RESTORE", "cancel-job/restore", registry.OwnerDisasterRecovery, registry.OperationRequiresRunningRestoreJob},
75+
}
76+
77+
for _, j := range jobs {
78+
r.AddOperation(registry.OperationSpec{
79+
Name: j.OpName,
80+
Owner: j.Owner,
81+
Timeout: 15 * time.Minute,
82+
CompatibleClouds: registry.AllClouds,
83+
Dependencies: []registry.OperationDependency{j.Dependency},
84+
Run: runCancelJob(j.JobType),
85+
CanRunConcurrently: registry.OperationCanRunConcurrently,
86+
})
87+
}
88+
}

pkg/cmd/roachtest/operations/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func RegisterOperations(r registry.Registry) {
2828
registerManualCompaction(r)
2929
registerResize(r)
3030
registerPauseJob(r)
31+
registerCancelJob(r)
3132
registerLicenseThrottle(r)
3233
registerSessionVariables(r)
3334
registerDebugZip(r)

0 commit comments

Comments
 (0)