Skip to content

Commit fff7d3a

Browse files
committed
db: fix MarkedForCompaction flake
We were accidentally using `opts` instead of `d.opts` when turning off automatic compactions, which leads to test flakiness (specifically: a marked table gets compacted before we get a chance to close and reopen). Fixes #5284
1 parent d86f6da commit fff7d3a

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

compaction_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,21 +2774,25 @@ func TestMarkedForCompaction(t *testing.T) {
27742774
var d *DB
27752775

27762776
var buf bytes.Buffer
2777+
eventListener := EventListener{
2778+
CompactionEnd: func(info CompactionInfo) {
2779+
// Fix the job ID and durations for determinism.
2780+
info.JobID = 100
2781+
info.Duration = time.Second
2782+
info.TotalDuration = 2 * time.Second
2783+
fmt.Fprintln(&buf, info)
2784+
},
2785+
}
2786+
if testing.Verbose() {
2787+
eventListener = TeeEventListener(eventListener, MakeLoggingEventListener(base.DefaultLogger))
2788+
}
27772789
opts := &Options{
27782790
FS: mem,
27792791
DebugCheck: DebugCheckLevels,
27802792
DisableAutomaticCompactions: true,
27812793
FormatMajorVersion: internalFormatNewest,
2782-
EventListener: &EventListener{
2783-
CompactionEnd: func(info CompactionInfo) {
2784-
// Fix the job ID and durations for determinism.
2785-
info.JobID = 100
2786-
info.Duration = time.Second
2787-
info.TotalDuration = 2 * time.Second
2788-
fmt.Fprintln(&buf, info)
2789-
},
2790-
},
2791-
Logger: testutils.Logger{T: t},
2794+
EventListener: &eventListener,
2795+
Logger: testutils.Logger{T: t},
27922796
}
27932797
opts.Experimental.CompactionScheduler = func() CompactionScheduler {
27942798
return NewConcurrencyLimitSchedulerWithNoPeriodicGrantingForTest()
@@ -2869,6 +2873,9 @@ func TestMarkedForCompaction(t *testing.T) {
28692873
d.mu.Lock()
28702874
defer d.mu.Unlock()
28712875
d.opts.DisableAutomaticCompactions = false
2876+
defer func() {
2877+
d.opts.DisableAutomaticCompactions = true
2878+
}()
28722879
for {
28732880
d.maybeScheduleCompaction()
28742881
if d.mu.compact.compactingCount == 0 {
@@ -2881,7 +2888,6 @@ func TestMarkedForCompaction(t *testing.T) {
28812888

28822889
fmt.Fprintln(&buf, d.mu.versions.currentVersion().DebugString())
28832890
s := strings.TrimSpace(buf.String())
2884-
opts.DisableAutomaticCompactions = true
28852891
return s
28862892

28872893
default:

options.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,9 @@ type Options struct {
987987
// DisableAutomaticCompactions dictates whether automatic compactions are
988988
// scheduled or not. The default is false (enabled). This option is only used
989989
// externally when running a manual compaction, and internally for tests.
990+
//
991+
// Note that this field is only consulted while holding DB.mu, so it it safe
992+
// for a test to modify it while holding DB.mu.
990993
DisableAutomaticCompactions bool
991994

992995
// DisableConsistencyCheck disables the consistency check that is performed on

0 commit comments

Comments
 (0)