|
| 1 | +// Copyright 2024 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 operations |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/operation" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option" |
| 21 | + "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry" |
| 22 | + "github.com/cockroachdb/cockroach/pkg/cmd/roachtest/roachtestflags" |
| 23 | + "github.com/cockroachdb/cockroach/pkg/util/randutil" |
| 24 | +) |
| 25 | + |
| 26 | +type resumePausedJob struct { |
| 27 | + jobId string |
| 28 | +} |
| 29 | + |
| 30 | +func (r *resumePausedJob) Cleanup(ctx context.Context, o operation.Operation, c cluster.Cluster) { |
| 31 | + conn := c.Conn(ctx, o.L(), 1, option.VirtualClusterName(roachtestflags.VirtualCluster)) |
| 32 | + defer conn.Close() |
| 33 | + |
| 34 | + resumeJobStmt := fmt.Sprintf("RESUME JOB %s", r.jobId) |
| 35 | + _, err := conn.ExecContext(ctx, resumeJobStmt) |
| 36 | + if err != nil { |
| 37 | + o.Fatal(err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func pauseLDRJob( |
| 42 | + ctx context.Context, o operation.Operation, c cluster.Cluster, |
| 43 | +) registry.OperationCleanup { |
| 44 | + conn := c.Conn(ctx, o.L(), 1, option.VirtualClusterName(roachtestflags.VirtualCluster)) |
| 45 | + defer conn.Close() |
| 46 | + |
| 47 | + //fetch running ldr jobs |
| 48 | + jobs, err := conn.QueryContext(ctx, "(WITH x AS (SHOW JOBS) SELECT job_id FROM x WHERE job_type = 'LOGICAL REPLICATION' AND status = 'running')") |
| 49 | + if err != nil { |
| 50 | + o.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + var jobIds []string |
| 54 | + for jobs.Next() { |
| 55 | + var jobId string |
| 56 | + if err := jobs.Scan(&jobId); err != nil { |
| 57 | + o.Fatal(err) |
| 58 | + } |
| 59 | + jobIds = append(jobIds, jobId) |
| 60 | + } |
| 61 | + |
| 62 | + //pick a random ldr job |
| 63 | + rng, _ := randutil.NewPseudoRand() |
| 64 | + jobId := jobIds[rng.Intn(len(jobIds))] |
| 65 | + |
| 66 | + o.Status(fmt.Sprintf("pausing LDR job %s", jobId)) |
| 67 | + pauseJobStmt := fmt.Sprintf("PAUSE JOB %s WITH REASON = 'roachtest operation'", jobId) |
| 68 | + _, err = conn.ExecContext(ctx, pauseJobStmt) |
| 69 | + if err != nil { |
| 70 | + o.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + o.Status(fmt.Sprintf("paused LDR job %s", jobId)) |
| 74 | + return &resumePausedJob{ |
| 75 | + jobId: jobId, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func registerPauseLDRJob(r registry.Registry) { |
| 80 | + r.AddOperation(registry.OperationSpec{ |
| 81 | + Name: "pause-ldr", |
| 82 | + Owner: registry.OwnerDisasterRecovery, |
| 83 | + Timeout: 15 * time.Minute, |
| 84 | + CompatibleClouds: registry.AllClouds, |
| 85 | + Dependencies: []registry.OperationDependency{registry.OperationRequiresLDRJobRunning}, |
| 86 | + Run: pauseLDRJob, |
| 87 | + }) |
| 88 | +} |
0 commit comments