Skip to content

Commit

Permalink
db: add Errorf to Logger interface
Browse files Browse the repository at this point in the history
Add Errorf to the Logger interface. This allows us to log errors at the error
level in Cockroach.

Informs cockroach/cockroach#112730.
  • Loading branch information
jbowens committed Oct 25, 2023
1 parent 3819121 commit 422dce9
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 14 deletions.
4 changes: 2 additions & 2 deletions compaction_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1812,7 +1812,7 @@ func pickL0(env compactionEnv, opts *Options, vers *version, baseLevel int) (pc
// has been shown to not cause a performance regression.
lcf, err := vers.L0Sublevels.PickBaseCompaction(1, vers.Levels[baseLevel].Slice())
if err != nil {
opts.Logger.Infof("error when picking base compaction: %s", err)
opts.Logger.Errorf("error when picking base compaction: %s", err)
return
}
if lcf != nil {
Expand All @@ -1830,7 +1830,7 @@ func pickL0(env compactionEnv, opts *Options, vers *version, baseLevel int) (pc
// counterproductive.
lcf, err = vers.L0Sublevels.PickIntraL0Compaction(env.earliestUnflushedSeqNum, minIntraL0Count)
if err != nil {
opts.Logger.Infof("error when picking intra-L0 compaction: %s", err)
opts.Logger.Errorf("error when picking intra-L0 compaction: %s", err)
return
}
if lcf != nil {
Expand Down
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,7 @@ func (d *DB) Metrics() *Metrics {

metrics.LogWriter.FsyncLatency = d.mu.log.metrics.fsyncLatency
if err := metrics.LogWriter.Merge(&d.mu.log.metrics.LogWriterMetrics); err != nil {
d.opts.Logger.Infof("metrics error: %s", err)
d.opts.Logger.Errorf("metrics error: %s", err)
}
metrics.Flush.WriteThroughput = d.mu.compact.flushWriteThroughput
if d.mu.compact.flushing {
Expand Down Expand Up @@ -2582,7 +2582,7 @@ func (d *DB) recycleWAL() (newLogNum base.DiskFileNum, prevLogSize uint64) {
metrics := d.mu.log.LogWriter.Metrics()
d.mu.Lock()
if err := d.mu.log.metrics.Merge(metrics); err != nil {
d.opts.Logger.Infof("metrics error: %s", err)
d.opts.Logger.Errorf("metrics error: %s", err)
}
d.mu.Unlock()

Expand Down
1 change: 1 addition & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ type testTracer struct {
}

func (t *testTracer) Infof(format string, args ...interface{}) {}
func (t *testTracer) Errorf(format string, args ...interface{}) {}
func (t *testTracer) Fatalf(format string, args ...interface{}) {}

func (t *testTracer) Eventf(ctx context.Context, format string, args ...interface{}) {
Expand Down
4 changes: 2 additions & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

type panicLogger struct{}

func (l panicLogger) Infof(format string, args ...interface{}) {
}
func (l panicLogger) Infof(format string, args ...interface{}) {}
func (l panicLogger) Errorf(format string, args ...interface{}) {}

func (l panicLogger) Fatalf(format string, args ...interface{}) {
panic(errors.Errorf("fatal: "+format, args...))
Expand Down
4 changes: 2 additions & 2 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ func (l *EventListener) EnsureDefaults(logger Logger) {
if l.BackgroundError == nil {
if logger != nil {
l.BackgroundError = func(err error) {
logger.Infof("background error: %s", err)
logger.Errorf("background error: %s", err)
}
} else {
l.BackgroundError = func(error) {}
Expand Down Expand Up @@ -630,7 +630,7 @@ func MakeLoggingEventListener(logger Logger) EventListener {

return EventListener{
BackgroundError: func(err error) {
logger.Infof("background error: %s", err)
logger.Errorf("background error: %s", err)
},
CompactionBegin: func(info CompactionInfo) {
logger.Infof("%s", info)
Expand Down
5 changes: 5 additions & 0 deletions event_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ func (l redactLogger) Infof(format string, args ...interface{}) {
l.logger.Infof("%s", redact.Sprintf(format, args...).Redact())
}

// Errorf implements the Logger.Errorf interface.
func (l redactLogger) Errorf(format string, args ...interface{}) {
l.logger.Errorf("%s", redact.Sprintf(format, args...).Redact())
}

// Fatalf implements the Logger.Fatalf interface.
func (l redactLogger) Fatalf(format string, args ...interface{}) {
l.logger.Fatalf("%s", redact.Sprintf(format, args...).Redact())
Expand Down
4 changes: 4 additions & 0 deletions filenames_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (l noFatalLogger) Infof(format string, args ...interface{}) {
l.t.Logf(format, args...)
}

func (l noFatalLogger) Errorf(format string, args ...interface{}) {
l.t.Logf(format, args...)
}

func (l noFatalLogger) Fatalf(format string, args ...interface{}) {
l.t.Logf(format, args...)
}
8 changes: 4 additions & 4 deletions ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func ingestLink(
)
if err != nil {
if err2 := ingestCleanup(objProvider, lr.localMeta[:i]); err2 != nil {
opts.Logger.Infof("ingest cleanup failed: %v", err2)
opts.Logger.Errorf("ingest cleanup failed: %v", err2)
}
return err
}
Expand Down Expand Up @@ -1399,7 +1399,7 @@ func (d *DB) ingest(
err = firstError(err, rkeyIter.Close())
}
if err != nil {
d.opts.Logger.Infof("ingest error reading flushable for log %s: %s", m.logNum, err)
d.opts.Logger.Errorf("ingest error reading flushable for log %s: %s", m.logNum, err)
}
}

Expand Down Expand Up @@ -1506,14 +1506,14 @@ func (d *DB) ingest(

if err != nil {
if err2 := ingestCleanup(d.objProvider, loadResult.localMeta); err2 != nil {
d.opts.Logger.Infof("ingest cleanup failed: %v", err2)
d.opts.Logger.Errorf("ingest cleanup failed: %v", err2)
}
} else {
// Since we either created a hard link to the ingesting files, or copied
// them over, it is safe to remove the originals paths.
for _, path := range loadResult.localPaths {
if err2 := d.opts.FS.Remove(path); err2 != nil {
d.opts.Logger.Infof("ingest failed to remove original file: %s", err2)
d.opts.Logger.Errorf("ingest failed to remove original file: %s", err2)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,10 @@ func (l testLogger) Infof(format string, args ...interface{}) {
l.t.Logf(format, args...)
}

func (l testLogger) Errorf(format string, args ...interface{}) {
l.t.Logf(format, args...)
}

func (l testLogger) Fatalf(format string, args ...interface{}) {
l.t.Fatalf(format, args...)
}
Expand Down Expand Up @@ -3138,6 +3142,11 @@ func (l *fatalCapturingLogger) Infof(fmt string, args ...interface{}) {
l.t.Logf(fmt, args...)
}

// Errorf implements the Logger interface.
func (l *fatalCapturingLogger) Errorf(fmt string, args ...interface{}) {
l.t.Logf(fmt, args...)
}

// Fatalf implements the Logger interface.
func (l *fatalCapturingLogger) Fatalf(_ string, args ...interface{}) {
l.err = args[0].(error)
Expand Down
14 changes: 14 additions & 0 deletions internal/base/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// Logger defines an interface for writing log messages.
type Logger interface {
Infof(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
type defaultLogger struct{}
Expand All @@ -33,6 +34,11 @@ func (defaultLogger) Infof(format string, args ...interface{}) {
_ = log.Output(2, fmt.Sprintf(format, args...))
}

// Errorf implements the Logger.Errorf interface.
func (defaultLogger) Errorf(format string, args ...interface{}) {
_ = log.Output(2, fmt.Sprintf(format, args...))
}

// Fatalf implements the Logger.Fatalf interface.
func (defaultLogger) Fatalf(format string, args ...interface{}) {
_ = log.Output(2, fmt.Sprintf(format, args...))
Expand Down Expand Up @@ -75,6 +81,11 @@ func (b *InMemLogger) Infof(format string, args ...interface{}) {
}
}

// Errorf is part of the Logger interface.
func (b *InMemLogger) Errorf(format string, args ...interface{}) {
b.Infof(format, args...)
}

// Fatalf is part of the Logger interface.
func (b *InMemLogger) Fatalf(format string, args ...interface{}) {
b.Infof(format, args...)
Expand Down Expand Up @@ -125,6 +136,9 @@ var _ LoggerAndTracer = NoopLoggerAndTracer{}
// Infof implements LoggerAndTracer.
func (l NoopLoggerAndTracer) Infof(format string, args ...interface{}) {}

// Errorf implements LoggerAndTracer.
func (l NoopLoggerAndTracer) Errorf(format string, args ...interface{}) {}

// Fatalf implements LoggerAndTracer.
func (l NoopLoggerAndTracer) Fatalf(format string, args ...interface{}) {}

Expand Down
6 changes: 6 additions & 0 deletions metamorphic/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func (h *history) Infof(format string, args ...interface{}) {
_ = h.log.Output(2, h.format("// INFO: ", format, args...))
}

// Errorf implements the pebble.Logger interface. Note that the output is
// commented.
func (h *history) Errorf(format string, args ...interface{}) {
_ = h.log.Output(2, h.format("// ERROR: ", format, args...))
}

// Fatalf implements the pebble.Logger interface. Note that the output is
// commented.
func (h *history) Fatalf(format string, args ...interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion objstorage/objstorageprovider/sharedcache/shared_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func (w *writeWorkers) Start(c *Cache, numWorkers int) {
if err != nil {
c.metrics.writeBackFailures.Add(1)
// TODO(radu): throttle logs.
c.logger.Infof("writing back to cache after miss failed: %v", err)
c.logger.Errorf("writing back to cache after miss failed: %v", err)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion table_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,8 @@ type catchFatalLogger struct {

var _ Logger = (*catchFatalLogger)(nil)

func (tl *catchFatalLogger) Infof(format string, args ...interface{}) {}
func (tl *catchFatalLogger) Infof(format string, args ...interface{}) {}
func (tl *catchFatalLogger) Errorf(format string, args ...interface{}) {}

func (tl *catchFatalLogger) Fatalf(format string, args ...interface{}) {
tl.fatalMsgs = append(tl.fatalMsgs, fmt.Sprintf(format, args...))
Expand Down

0 comments on commit 422dce9

Please sign in to comment.