Skip to content

Commit f2a76e8

Browse files
committed
db: deflake TestCompactionCorruption
This change attempts to deflake this test by removing unrelated external files and allowing the test to control the range of keys in the workload. Fixes #5039
1 parent bf6aaa5 commit f2a76e8

File tree

2 files changed

+31
-50
lines changed

2 files changed

+31
-50
lines changed

compaction_test.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,15 +3156,13 @@ func TestCompactionCorruption(t *testing.T) {
31563156
now.Store(1)
31573157
d.problemSpans.InitForTesting(manifest.NumLevels, d.cmp, func() crtime.Mono { return now.Load() })
31583158

3159-
var workloadWG sync.WaitGroup
3160-
var stopWorkload atomic.Bool
3161-
defer stopWorkload.Store(true)
3162-
startWorkload := func() {
3163-
stopWorkload.Store(false)
3164-
workloadWG.Add(1)
3159+
startWorkload := func(minKey, maxKey byte) (stop func()) {
3160+
var shouldStop atomic.Bool
3161+
var wg sync.WaitGroup
3162+
wg.Add(1)
31653163
go func() {
3166-
defer workloadWG.Done()
3167-
for !stopWorkload.Load() {
3164+
defer wg.Done()
3165+
for !shouldStop.Load() {
31683166
b := d.NewBatch()
31693167
// Write a random key of the form a012345 and flush it. This will result
31703168
// in (mostly) non-overlapping tables in L0.
@@ -3174,7 +3172,7 @@ func TestCompactionCorruption(t *testing.T) {
31743172
}
31753173
v := make([]byte, 1024+rand.IntN(10240))
31763174
_, _ = rand.NewChaCha8(valSeed).Read(v)
3177-
key := fmt.Sprintf("%c%06d", 'a'+byte(rand.IntN(int('z'-'a'+1))), rand.IntN(1000000))
3175+
key := fmt.Sprintf("%c%06d", minKey+byte(rand.IntN(int(maxKey-minKey+1))), rand.IntN(1000000))
31783176
if err := b.Set([]byte(key), v, nil); err != nil {
31793177
panic(err)
31803178
}
@@ -3187,9 +3185,19 @@ func TestCompactionCorruption(t *testing.T) {
31873185
time.Sleep(10 * time.Millisecond)
31883186
}
31893187
}()
3188+
return func() {
3189+
shouldStop.Store(true)
3190+
wg.Wait()
3191+
}
31903192
}
31913193

31923194
datadriven.RunTest(t, "testdata/compaction_corruption", func(t *testing.T, td *datadriven.TestData) string {
3195+
if arg, ok := td.Arg("workload"); ok {
3196+
if len(arg.Vals) != 2 || len(arg.Vals[0]) != 1 || len(arg.Vals[1]) != 1 {
3197+
td.Fatalf(t, "workload argument must be of the form (a,z)")
3198+
}
3199+
defer startWorkload(arg.Vals[0][0], arg.Vals[1][0])()
3200+
}
31933201
// wait until fn() returns true.
31943202
wait := func(what string, fn func() bool) {
31953203
const timeout = 2 * time.Minute
@@ -3230,13 +3238,6 @@ func TestCompactionCorruption(t *testing.T) {
32303238
require.NoError(t, writer.Close())
32313239
return fmt.Sprintf("%s -> %s", before, after)
32323240

3233-
case "start-workload":
3234-
startWorkload()
3235-
3236-
case "stop-workload":
3237-
stopWorkload.Store(true)
3238-
workloadWG.Wait()
3239-
32403241
case "wait-for-problem-span":
32413242
wait("problem span", func() bool {
32423243
return !d.problemSpans.IsEmpty()

testdata/compaction_corruption

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,53 @@
1-
build-remote file1
2-
a#0,SET = avalue
3-
b#0,SET = bvalue
4-
c#0,SET = cvalue
5-
----
6-
7-
build-remote file2-not-there
1+
build-remote file-not-there
82
d#0,SET = dvalue
93
q#0,SET = qvalue
104
w#0,SET = wvalue
115
----
126

13-
build-remote file3
14-
x#0,SET = xvalue
15-
y#0,SET = yvalue
16-
z#0,SET = zvalue
17-
----
18-
197
ingest-external
20-
file1 bounds=(a,c0)
21-
file2 bounds=(d,w0)
22-
file3 bounds=(x,z0)
23-
----
24-
25-
start-workload
8+
file bounds=(d,w0)
269
----
2710

2811
# Verify that a problem span is set.
29-
wait-for-problem-span
12+
wait-for-problem-span workload=(d,w)
3013
----
3114

3215
# Verify that compactions still go through.
33-
wait-for-compactions
16+
wait-for-compactions workload=(a,z)
3417
----
3518

36-
# Make file2 appear.
37-
move-remote-object file2-not-there file2
19+
# Make file appear.
20+
move-remote-object file-not-there file
3821
----
39-
file2-not-there -> file2
22+
file-not-there -> file
4023

4124
# Expire spans.
4225
expire-spans
4326
----
4427

4528
# Compactions should now go through and eventually there should be no external
4629
# files.
47-
wait-for-no-external-files
30+
wait-for-no-external-files workload=(d,w)
4831
----
4932

50-
build-remote file4-not-there
33+
build-remote file2-not-there
5134
a#0,SET = avalue
5235
u#0,SET = uvalue
5336
z#0,SET = zvalue
5437
----
5538

5639
ingest-external
57-
file4 bounds=(a,z0)
40+
file2 bounds=(a,z0)
5841
----
5942

6043
# Verify that a problem span is set.
61-
wait-for-problem-span
44+
wait-for-problem-span workload=(a,z)
6245
----
6346

64-
stop-workload
65-
----
66-
67-
# Make file4 appear.
68-
move-remote-object file4-not-there file4
47+
# Make file2 appear.
48+
move-remote-object file2-not-there file2
6949
----
70-
file4-not-there -> file4
50+
file2-not-there -> file2
7151

7252
# Verify that a manual compaction goes through despite the problem span.
7353
manual-compaction

0 commit comments

Comments
 (0)