diff --git a/server/storage/backend/backend.go b/server/storage/backend/backend.go index 7f5862c8861c..c43542dd6771 100644 --- a/server/storage/backend/backend.go +++ b/server/storage/backend/backend.go @@ -199,6 +199,10 @@ func newBackend(bcfg BackendConfig) *backend { bcfg.Logger.Panic("failed to open database", zap.String("path", bcfg.Path), zap.Error(err)) } + if bcfg.Logger == nil { + bcfg.Logger = zap.NewNop() + } + // In future, may want to make buffering optional for low-concurrency systems // or dynamically swap between buffered/non-buffered depending on workload. b := &backend{ @@ -499,7 +503,7 @@ func (b *backend) defrag() error { tmpdb, err := bolt.Open(tdbp, 0o600, &options) if err != nil { temp.Close() - if rmErr := os.Remove(temp.Name()); rmErr != nil && b.lg != nil { + if rmErr := os.Remove(temp.Name()); rmErr != nil { b.lg.Error( "failed to remove temporary file", zap.String("path", temp.Name()), @@ -512,16 +516,14 @@ func (b *backend) defrag() error { dbp := b.db.Path() size1, sizeInUse1 := b.Size(), b.SizeInUse() - if b.lg != nil { - b.lg.Info( - "defragmenting", - zap.String("path", dbp), - zap.Int64("current-db-size-bytes", size1), - zap.String("current-db-size", humanize.Bytes(uint64(size1))), - zap.Int64("current-db-size-in-use-bytes", sizeInUse1), - zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse1))), - ) - } + b.lg.Info( + "defragmenting", + zap.String("path", dbp), + zap.Int64("current-db-size-bytes", size1), + zap.String("current-db-size", humanize.Bytes(uint64(size1))), + zap.Int64("current-db-size-in-use-bytes", sizeInUse1), + zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse1))), + ) defer func() { // NOTE: We should exit as soon as possible because that tx @@ -584,19 +586,17 @@ func (b *backend) defrag() error { defragSec.Observe(took.Seconds()) size2, sizeInUse2 := b.Size(), b.SizeInUse() - if b.lg != nil { - b.lg.Info( - "finished defragmenting directory", - zap.String("path", dbp), - zap.Int64("current-db-size-bytes-diff", size2-size1), - zap.Int64("current-db-size-bytes", size2), - zap.String("current-db-size", humanize.Bytes(uint64(size2))), - zap.Int64("current-db-size-in-use-bytes-diff", sizeInUse2-sizeInUse1), - zap.Int64("current-db-size-in-use-bytes", sizeInUse2), - zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse2))), - zap.Duration("took", took), - ) - } + b.lg.Info( + "finished defragmenting directory", + zap.String("path", dbp), + zap.Int64("current-db-size-bytes-diff", size2-size1), + zap.Int64("current-db-size-bytes", size2), + zap.String("current-db-size", humanize.Bytes(uint64(size2))), + zap.Int64("current-db-size-in-use-bytes-diff", sizeInUse2-sizeInUse1), + zap.Int64("current-db-size-in-use-bytes", sizeInUse2), + zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse2))), + zap.Duration("took", took), + ) return nil }