Skip to content

Commit 4f3e287

Browse files
committed
pebble: deflake TestCompactionDeleteOnlyHints
maybe-compact was previously waiting on compactingCount to be 0 before reading the collected compaction info. This observation was flaky, as compactingCount is updated just before another call to `maybeScheduleCompaction`, with the mutex being dropped in between. Now, we observe both `compactingCount` and a new field, `compactProcesses`, which track the number of calls to compact which still need to perform the compaction scheduling step. Fixes: #5351
1 parent 6c6fd75 commit 4f3e287

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

compaction.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,7 @@ func (d *DB) runPickedCompaction(pc pickedCompaction, grantHandle CompactionGran
21232123
}
21242124

21252125
d.mu.compact.compactingCount++
2126+
d.mu.compact.compactProcesses++
21262127
c := pc.ConstructCompaction(d, grantHandle)
21272128
c.AddInProgressLocked(d)
21282129
go func() {
@@ -2281,6 +2282,7 @@ func (d *DB) tryScheduleDeleteOnlyCompaction() bool {
22812282
if len(inputs) > 0 {
22822283
c := newDeleteOnlyCompaction(d.opts, v, inputs, d.timeNow(), resolvedHints, exciseEnabled)
22832284
d.mu.compact.compactingCount++
2285+
d.mu.compact.compactProcesses++
22842286
c.AddInProgressLocked(d)
22852287
go d.compact(c, nil)
22862288
return true
@@ -2655,6 +2657,7 @@ func (d *DB) compact(c compaction, errChannel chan error) {
26552657
d.mu.Lock()
26562658
defer d.mu.Unlock()
26572659
d.maybeScheduleCompaction()
2660+
d.mu.compact.compactProcesses--
26582661
d.mu.compact.cond.Broadcast()
26592662
}()
26602663
})

compaction_test.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,23 +1226,14 @@ func runCompactionTest(
12261226
err := func() error {
12271227
d.mu.Lock()
12281228
defer d.mu.Unlock()
1229-
prevCount := d.mu.versions.metrics.Compact.Count
12301229
prev := d.opts.DisableAutomaticCompactions
12311230
d.opts.DisableAutomaticCompactions = false
1232-
err := try(100*time.Microsecond, 60*time.Second, func() error {
1233-
d.maybeScheduleCompaction()
1234-
for d.mu.compact.compactingCount > 0 {
1235-
d.mu.compact.cond.Wait()
1236-
}
1237-
compactions := d.mu.versions.metrics.Compact.Count - prevCount
1238-
if compactions < expectedCount {
1239-
return errors.Errorf("expectedCount at least %d automatic compaction(s), got %d, total: %d",
1240-
expectedCount, compactions, d.mu.versions.metrics.Compact.Count)
1241-
}
1242-
return nil
1243-
})
1231+
d.maybeScheduleCompaction()
1232+
for d.mu.compact.compactProcesses > 0 {
1233+
d.mu.compact.cond.Wait()
1234+
}
12441235
d.opts.DisableAutomaticCompactions = prev
1245-
return err
1236+
return nil
12461237
}()
12471238
if err != nil {
12481239
return err.Error() + "\n" + describeLSM(d, verbose)
@@ -1688,7 +1679,7 @@ func TestCompactionDeleteOnlyHints(t *testing.T) {
16881679

16891680
compactInfo = nil
16901681
compactionString := func() string {
1691-
for d.mu.compact.compactingCount > 0 {
1682+
for d.mu.compact.compactProcesses > 0 {
16921683
d.mu.compact.cond.Wait()
16931684
}
16941685
slices.SortFunc(compactInfo, func(a, b CompactionInfo) int {
@@ -1939,7 +1930,7 @@ func TestCompactionTombstones(t *testing.T) {
19391930
var compactInfo []*CompactionInfo // protected by d.mu
19401931

19411932
compactionString := func() string {
1942-
for d.mu.compact.compactingCount > 0 {
1933+
for d.mu.compact.compactProcesses > 0 {
19431934
d.mu.compact.cond.Wait()
19441935
}
19451936

db.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ type DB struct {
418418
flushing bool
419419
// The number of ongoing non-download compactions.
420420
compactingCount int
421+
// The number of calls to compact that have not yet finished. This is different
422+
// from compactingCount, as calls to compact will attempt to schedule and run
423+
// follow-up compactions after the current compaction finishes, dropping db.mu
424+
// in between updating compactingCount and the scheduling operation. This value is
425+
// used in tests in order to track when all compaction activity has finished.
426+
compactProcesses int
421427
// The number of download compactions.
422428
downloadingCount int
423429
// The list of deletion hints, suggesting ranges for delete-only

testdata/compaction/l0_to_lbase_compaction

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ L0.0:
2424
000009:[vhn@1#14977,SET-zzz@1#18287,SET]
2525

2626

27-
auto-compact count=3
27+
auto-compact
2828
----
2929
L6:
3030
000006:[a@1#10,SET-hci@1#4995,SET]

0 commit comments

Comments
 (0)