Skip to content

Commit afc46e9

Browse files
committed
db: don't report compaction cancelled errors
Small change to not report `ErrCancelledCompaction` as a background error. We still log it as an `Infof`, but we do it in Pebble and not in the event listener.
1 parent 269db0a commit afc46e9

File tree

5 files changed

+28
-33
lines changed

5 files changed

+28
-33
lines changed

compaction.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,8 +2312,7 @@ func (d *DB) compact(c *compaction, errChannel chan error) {
23122312
d.mu.Lock()
23132313
c.grantHandle.Started()
23142314
if err := d.compact1(c, errChannel); err != nil {
2315-
// TODO(peter): count consecutive compaction errors and backoff.
2316-
d.opts.EventListener.BackgroundError(err)
2315+
d.handleCompactFailure(err)
23172316
}
23182317
if c.isDownload {
23192318
d.mu.compact.downloadingCount--
@@ -2351,6 +2350,17 @@ func (d *DB) compact(c *compaction, errChannel chan error) {
23512350
})
23522351
}
23532352

2353+
func (d *DB) handleCompactFailure(err error) {
2354+
if errors.Is(err, ErrCancelledCompaction) {
2355+
// ErrCancelledCompaction is expected during normal operation, so we don't
2356+
// want to report it as a background error.
2357+
d.opts.Logger.Infof("%v", err)
2358+
return
2359+
}
2360+
// TODO(peter): count consecutive compaction errors and backoff.
2361+
d.opts.EventListener.BackgroundError(err)
2362+
}
2363+
23542364
// cleanupVersionEdit cleans up any on-disk artifacts that were created
23552365
// for the application of a versionEdit that is no longer going to be applied.
23562366
//

event.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ type CompactionInfo struct {
115115
// always ≥ Duration.
116116
TotalDuration time.Duration
117117
Done bool
118-
Err error
118+
// Err is set only if Done is true. If non-nil, indicates that the compaction
119+
// failed. Note that err can be ErrCancelledCompaction, which can happen
120+
// during normal operation.
121+
Err error
119122

120123
SingleLevelOverlappingRatio float64
121124
MultiLevelOverlappingRatio float64
@@ -809,15 +812,6 @@ type EventListener struct {
809812
PossibleAPIMisuse func(PossibleAPIMisuseInfo)
810813
}
811814

812-
func backgroundError(logger Logger, err error) {
813-
if errors.Is(err, ErrCancelledCompaction) {
814-
// ErrCancelledCompaction is not an unexpected error, hence severity INFO.
815-
logger.Infof("background error: %s", err)
816-
} else {
817-
logger.Errorf("background error: %s", err)
818-
}
819-
}
820-
821815
// EnsureDefaults ensures that background error events are logged to the
822816
// specified logger if a handler for those events hasn't been otherwise
823817
// specified. Ensure all handlers are non-nil so that we don't have to check
@@ -826,7 +820,7 @@ func (l *EventListener) EnsureDefaults(logger Logger) {
826820
if l.BackgroundError == nil {
827821
if logger != nil {
828822
l.BackgroundError = func(err error) {
829-
backgroundError(logger, err)
823+
logger.Errorf("background error: %s", err)
830824
}
831825
} else {
832826
l.BackgroundError = func(error) {}
@@ -915,7 +909,7 @@ func MakeLoggingEventListener(logger Logger) EventListener {
915909

916910
return EventListener{
917911
BackgroundError: func(err error) {
918-
backgroundError(logger, err)
912+
logger.Errorf("background error: %s", err)
919913
},
920914
DataCorruption: func(info DataCorruptionInfo) {
921915
logger.Errorf("%s", info)

event_listener_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,6 @@ func newCountingMockLogger(t *testing.T) (*mockLogger, *int, *int) {
402402
}, &infoCount, &errorCount
403403
}
404404

405-
func TestMakeLoggingEventListenerBackgroundErrorCancelledCompaction(t *testing.T) {
406-
mockLogger, infoCount, errorCount := newCountingMockLogger(t)
407-
e := MakeLoggingEventListener(mockLogger)
408-
409-
e.BackgroundError(ErrCancelledCompaction)
410-
require.Equal(t, 1, *infoCount)
411-
require.Equal(t, 0, *errorCount)
412-
413-
testAllCallbacksSetInEventListener(t, e)
414-
}
415-
416405
func TestMakeLoggingEventListenerBackgroundErrorOtherError(t *testing.T) {
417406
mockLogger, infoCount, errorCount := newCountingMockLogger(t)
418407
e := MakeLoggingEventListener(mockLogger)

ingest_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ type blockedCompaction struct {
14111411
func TestConcurrentExcise(t *testing.T) {
14121412
var d, d1, d2 *DB
14131413
var efos map[string]*EventuallyFileOnlySnapshot
1414-
backgroundErrs := make(chan error, 5)
1414+
compactionErrs := make(chan error, 5)
14151415
var compactions map[string]*blockedCompaction
14161416
defer func() {
14171417
for _, e := range efos {
@@ -1447,16 +1447,13 @@ func TestConcurrentExcise(t *testing.T) {
14471447
}
14481448
efos = make(map[string]*EventuallyFileOnlySnapshot)
14491449
compactions = make(map[string]*blockedCompaction)
1450-
backgroundErrs = make(chan error, 5)
1450+
compactionErrs = make(chan error, 5)
14511451

14521452
var el EventListener
14531453
el.EnsureDefaults(testLogger{t: t})
14541454
el.FlushBegin = func(info FlushInfo) {
14551455
// Don't block flushes
14561456
}
1457-
el.BackgroundError = func(err error) {
1458-
backgroundErrs <- err
1459-
}
14601457
el.CompactionBegin = func(info CompactionInfo) {
14611458
if info.Reason == "move" {
14621459
return
@@ -1468,6 +1465,11 @@ func TestConcurrentExcise(t *testing.T) {
14681465
blockedJobID = info.JobID
14691466
}
14701467
}
1468+
el.CompactionEnd = func(info CompactionInfo) {
1469+
if info.Err != nil {
1470+
compactionErrs <- info.Err
1471+
}
1472+
}
14711473
el.TableCreated = func(info TableCreateInfo) {
14721474
blockedCompactionsMu.Lock()
14731475
if info.JobID != blockedJobID {
@@ -1819,8 +1821,8 @@ func TestConcurrentExcise(t *testing.T) {
18191821
return "spun off in separate goroutine"
18201822
}
18211823
return "ok"
1822-
case "wait-for-background-error":
1823-
err := <-backgroundErrs
1824+
case "wait-for-compaction-error":
1825+
err := <-compactionErrs
18241826
return err.Error()
18251827
default:
18261828
return fmt.Sprintf("unknown command: %s", td.Cmd)

testdata/concurrent_excise

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ compact a-z
9696
----
9797
ok
9898

99-
wait-for-background-error
99+
wait-for-compaction-error
100100
----
101101
pebble: compaction cancelled by a concurrent operation, will retry compaction
102102

0 commit comments

Comments
 (0)