|
| 1 | +// Copyright 2025 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 inspect_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "regexp" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/sql" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 19 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 20 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 21 | + "github.com/cockroachdb/errors" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +// TestInspectJobImplicitTxnSemantics validates how inspect jobs behave when |
| 26 | +// triggered by statements that run in implicit transactions. It verifies that the job |
| 27 | +// starts correctly, that errors or timeouts propagate to the user, and that |
| 28 | +// client-visible semantics (like statement timeout or job failure) behave as expected. |
| 29 | +// |
| 30 | +// Note: This test currently uses SCRUB to trigger a job, but is not testing SCRUB |
| 31 | +// itself. The goal is to verify general execution semantics for async job statements. |
| 32 | +func TestInspectJobImplicitTxnSemantics(t *testing.T) { |
| 33 | + defer leaktest.AfterTest(t)() |
| 34 | + defer log.Scope(t).Close(t) |
| 35 | + |
| 36 | + var onInspectErrorToReturn atomic.Pointer[error] |
| 37 | + var pauseJobStart atomic.Bool |
| 38 | + s, db, _ := serverutils.StartServer(t, base.TestServerArgs{ |
| 39 | + Knobs: base.TestingKnobs{ |
| 40 | + Inspect: &sql.InspectTestingKnobs{ |
| 41 | + OnInspectJobStart: func() error { |
| 42 | + // Use a timeout so we aren't stuck in here forever in case something bad happens. |
| 43 | + const maxPause = 30 * time.Second |
| 44 | + deadline := time.After(maxPause) |
| 45 | + for { |
| 46 | + if !pauseJobStart.Load() { |
| 47 | + break |
| 48 | + } |
| 49 | + select { |
| 50 | + case <-deadline: |
| 51 | + return errors.Newf("test timed out after %s while waiting for pause to clear", maxPause) |
| 52 | + default: |
| 53 | + time.Sleep(10 * time.Millisecond) |
| 54 | + } |
| 55 | + } |
| 56 | + if errPtr := onInspectErrorToReturn.Load(); errPtr != nil { |
| 57 | + return *errPtr |
| 58 | + } |
| 59 | + return nil |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }) |
| 64 | + defer s.Stopper().Stop(context.Background()) |
| 65 | + runner := sqlutils.MakeSQLRunner(db) |
| 66 | + |
| 67 | + runner.Exec(t, ` |
| 68 | + CREATE DATABASE db; |
| 69 | + SET enable_scrub_job = true; |
| 70 | + CREATE TABLE db.t ( |
| 71 | + id INT PRIMARY KEY, |
| 72 | + val INT |
| 73 | + ); |
| 74 | + CREATE INDEX i1 on db.t (val); |
| 75 | + INSERT INTO db.t VALUES (1, 2), (2,3); |
| 76 | + `) |
| 77 | + |
| 78 | + for _, tc := range []struct { |
| 79 | + desc string |
| 80 | + setupSQL string |
| 81 | + tearDownSQL string |
| 82 | + pauseAtStart bool |
| 83 | + onStartError error |
| 84 | + expectedErrRegex string |
| 85 | + }{ |
| 86 | + {desc: "inspect success"}, |
| 87 | + {desc: "inspect failure", onStartError: errors.Newf("inspect validation error"), expectedErrRegex: "inspect validation error"}, |
| 88 | + // Note: avoiding small statement timeouts, as this can impact the ability to reset. |
| 89 | + {desc: "statement timeout", setupSQL: "SET statement_timeout = '1s'", tearDownSQL: "RESET statement_timeout", pauseAtStart: true, expectedErrRegex: "canceled"}, |
| 90 | + } { |
| 91 | + t.Run(tc.desc, func(t *testing.T) { |
| 92 | + if tc.setupSQL != "" { |
| 93 | + runner.Exec(t, tc.setupSQL) |
| 94 | + } |
| 95 | + if tc.tearDownSQL != "" { |
| 96 | + defer func() { runner.Exec(t, tc.tearDownSQL) }() |
| 97 | + } |
| 98 | + if tc.pauseAtStart { |
| 99 | + pauseJobStart.Store(true) |
| 100 | + } |
| 101 | + if tc.onStartError != nil { |
| 102 | + onInspectErrorToReturn.Store(&tc.onStartError) |
| 103 | + defer func() { onInspectErrorToReturn.Store(nil) }() |
| 104 | + } |
| 105 | + _, err := db.Exec("EXPERIMENTAL SCRUB TABLE db.t") |
| 106 | + pauseJobStart.Store(false) |
| 107 | + if tc.expectedErrRegex == "" { |
| 108 | + require.NoError(t, err) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + require.Error(t, err) |
| 113 | + re := regexp.MustCompile(tc.expectedErrRegex) |
| 114 | + match := re.MatchString(err.Error()) |
| 115 | + require.True(t, match, "Error text %q doesn't match the expected regexp of %q", |
| 116 | + err.Error(), tc.expectedErrRegex) |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments