Skip to content

Commit

Permalink
backend: remove ugly nil checks
Browse files Browse the repository at this point in the history
To remove the ugly nil checks by instantiating the logger with a no-op logger if it is nil.

Signed-off-by: Srujan Bharadwaj <[email protected]>
  • Loading branch information
srujangit123 committed Dec 17, 2024
1 parent e0bbea9 commit cbeccd6
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions server/storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.uber.org/zap"

bolt "go.etcd.io/bbolt"
"go.etcd.io/etcd/client/pkg/v3/verify"
)

var (
Expand Down Expand Up @@ -199,6 +200,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()

Check warning on line 204 in server/storage/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

server/storage/backend/backend.go#L204

Added line #L204 was not covered by tests
}

// In future, may want to make buffering optional for low-concurrency systems
// or dynamically swap between buffered/non-buffered depending on workload.
b := &backend{
Expand Down Expand Up @@ -458,6 +463,7 @@ func (b *backend) Defrag() error {
}

func (b *backend) defrag() error {
verify.Assert(b.lg != nil, "the logger should not be nil")
now := time.Now()
isDefragActive.Set(1)
defer isDefragActive.Set(0)
Expand Down Expand Up @@ -499,7 +505,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 {

Check warning on line 508 in server/storage/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

server/storage/backend/backend.go#L508

Added line #L508 was not covered by tests
b.lg.Error(
"failed to remove temporary file",
zap.String("path", temp.Name()),
Expand All @@ -512,16 +518,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
Expand Down Expand Up @@ -584,19 +588,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
}

Expand Down

0 comments on commit cbeccd6

Please sign in to comment.