Skip to content

Commit c1e97f7

Browse files
craig[bot]fqazi
andcommitted
Merge #156111
156111: sql/schemachanger: add sampling for pause / rollback tests r=fqazi a=fqazi Previously, the pause and rollback tests for the declarative schema changer could timeout due to the number of stages being tested. To address this, this patch adds supports for sampling stages in plans for rollback / pause testing. Fixes: #155668 Fixes: #155502 Release note: None Co-authored-by: Faizan Qazi <faizan@cockroachlabs.com>
2 parents 258b166 + 40aea7f commit c1e97f7

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

pkg/sql/schemachanger/sctest/backup.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func BackupSuccess(t *testing.T, path string, factory TestServerFactory) {
5252
backupArgs = backupSuccessPrepare(t, factory, spec, dbName)
5353
}, func(t *testing.T, cs CumulativeTestCaseSpec) {
5454
backupSuccess(t, backupArgs, cs)
55-
})
55+
},
56+
nil /*samplingFn */)
5657
}
5758

5859
// BackupRollbacks tests that the schema changer can handle being backed up
@@ -70,7 +71,8 @@ func BackupRollbacks(t *testing.T, path string, factory TestServerFactory) {
7071
factory = factory.WithSchemaLockDisabled()
7172
cumulativeTestForEachPostCommitStage(t, path, factory, nil, func(t *testing.T, cs CumulativeTestCaseSpec) {
7273
backupRollbacks(t, factory, cs)
73-
})
74+
},
75+
nil /* samplingFn */)
7476
}
7577

7678
// BackupSuccessMixedVersion is like BackupSuccess but in a mixed-version
@@ -96,7 +98,8 @@ func BackupSuccessMixedVersion(t *testing.T, path string, factory TestServerFact
9698
backupArgs = backupSuccessPrepare(t, factory, spec, dbName)
9799
}, func(t *testing.T, cs CumulativeTestCaseSpec) {
98100
backupSuccess(t, backupArgs, cs)
99-
})
101+
},
102+
nil /* samplingFn */)
100103
}
101104

102105
// BackupRollbacksMixedVersion is like BackupRollbacks but in a mixed-version
@@ -116,7 +119,7 @@ func BackupRollbacksMixedVersion(t *testing.T, path string, factory TestServerFa
116119
factory = factory.WithSchemaLockDisabled()
117120
cumulativeTestForEachPostCommitStage(t, path, factory, nil, func(t *testing.T, cs CumulativeTestCaseSpec) {
118121
backupRollbacks(t, factory, cs)
119-
})
122+
}, nil /* samplingFn */)
120123
}
121124

122125
// runAllBackups runs all the backup tests, disabling the random skipping.

pkg/sql/schemachanger/sctest/cumulative.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func Rollback(t *testing.T, relPath string, factory TestServerFactory) {
117117
}
118118
factory.WithSchemaChangerKnobs(knobs).Run(ctx, t, runfn)
119119
}
120-
cumulativeTestForEachPostCommitStage(t, relPath, factory, nil, testRollbackCase)
120+
cumulativeTestForEachPostCommitStage(t, relPath, factory, nil, testRollbackCase, sampleAllPostCommitRevertible /* enableSampling */)
121121
}
122122

123123
// ExecuteWithDMLInjection tests that the schema changer behaviour is sane
@@ -335,7 +335,8 @@ func Pause(t *testing.T, path string, factory TestServerFactory) {
335335

336336
cumulativeTestForEachPostCommitStage(t, path, factory, nil, func(t *testing.T, cs CumulativeTestCaseSpec) {
337337
pause(t, factory, cs)
338-
})
338+
},
339+
sampleAllPostCommitStages /* enableSampling */)
339340
}
340341

341342
// PauseMixedVersion is like Pause but in a mixed-version cluster which gets
@@ -349,7 +350,8 @@ func PauseMixedVersion(t *testing.T, path string, factory TestServerFactory) {
349350
factory.WithMixedVersion()
350351
cumulativeTestForEachPostCommitStage(t, path, factory, nil, func(t *testing.T, cs CumulativeTestCaseSpec) {
351352
pause(t, factory, cs)
352-
})
353+
},
354+
sampleAllPostCommitStages /* enableSampling */)
353355
}
354356

355357
func pause(t *testing.T, factory TestServerFactory, cs CumulativeTestCaseSpec) {

pkg/sql/schemachanger/sctest/framework.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package sctest
88
import (
99
"context"
1010
gosql "database/sql"
11+
"flag"
1112
"fmt"
1213
"math"
14+
"math/rand"
1315
"os"
1416
"path/filepath"
1517
"regexp"
@@ -634,6 +636,31 @@ type CumulativeTestCaseSpec struct {
634636
CreateDatabaseStmt string
635637
}
636638

639+
// sampleAllPostCommitRevertible samples all post commit revertible stages, and
640+
// limits testing to maxStagesToTest if *runAllCumulative is not set.
641+
func sampleAllPostCommitRevertible(testCases []CumulativeTestCaseSpec) []CumulativeTestCaseSpec {
642+
newTestCases := make([]CumulativeTestCaseSpec, 0, len(testCases))
643+
for _, tc := range testCases {
644+
if tc.Phase != scop.PostCommitNonRevertiblePhase {
645+
newTestCases = append(newTestCases, tc)
646+
}
647+
}
648+
return sampleAllPostCommitStages(newTestCases)
649+
}
650+
651+
// sampleAllPostCommitStages samples all post-commit stages, and limits these to
652+
// maxStagesToTest if *runAllCumulative is not set.
653+
func sampleAllPostCommitStages(testCases []CumulativeTestCaseSpec) []CumulativeTestCaseSpec {
654+
if len(testCases) > maxStagesToTest && !(*runAllCumulative) {
655+
// Shuffle and pick up to maxStagesToTest.
656+
rand.Shuffle(len(testCases), func(i, j int) {
657+
testCases[i], testCases[j] = testCases[j], testCases[i]
658+
})
659+
testCases = testCases[:maxStagesToTest]
660+
}
661+
return testCases
662+
}
663+
637664
func (cs CumulativeTestCaseSpec) run(t *testing.T, fn func(t *testing.T)) bool {
638665
var prefix string
639666
switch cs.Phase {
@@ -647,6 +674,12 @@ func (cs CumulativeTestCaseSpec) run(t *testing.T, fn func(t *testing.T)) bool {
647674
return t.Run(fmt.Sprintf("%s_stage_%d_of_%d", prefix, cs.StageOrdinal, cs.StagesCount), fn)
648675
}
649676

677+
// / runAllCumulative used to disable sampling for cumulative tests.
678+
var runAllCumulative = flag.Bool(
679+
"run-all-cumulative", false,
680+
"if true, run all cumulative instead of a random subset",
681+
)
682+
650683
// cumulativeTestForEachPostCommitStage invokes `tf` once for each stage in the
651684
// PostCommitPhase.
652685
func cumulativeTestForEachPostCommitStage(
@@ -655,6 +688,7 @@ func cumulativeTestForEachPostCommitStage(
655688
factory TestServerFactory,
656689
prepFn func(t *testing.T, spec CumulativeTestSpec, dbName string),
657690
tf func(t *testing.T, spec CumulativeTestCaseSpec),
691+
samplingFn func([]CumulativeTestCaseSpec) []CumulativeTestCaseSpec,
658692
) {
659693
testFunc := func(t *testing.T, spec CumulativeTestSpec) {
660694
// Skip this test if any of the stmts is not fully supported.
@@ -720,6 +754,10 @@ func cumulativeTestForEachPostCommitStage(
720754
if prepFn != nil {
721755
prepFn(t, spec, dbName)
722756
}
757+
// If sampling is enabled limit the number of stages executed.
758+
if samplingFn != nil {
759+
testCases = samplingFn(testCases)
760+
}
723761
for _, tc := range testCases {
724762
fn := func(t *testing.T) {
725763
tf(t, tc)

0 commit comments

Comments
 (0)