Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: Remove logger nil checks #19077

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions server/storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"go.uber.org/zap"

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

var (
Expand Down Expand Up @@ -187,6 +188,11 @@
if boltOpenOptions != nil {
*bopts = *boltOpenOptions
}

if bcfg.Logger == nil {
bcfg.Logger = zap.NewNop()

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

View check run for this annotation

Codecov / codecov/patch

server/storage/backend/backend.go#L193

Added line #L193 was not covered by tests
}

bopts.InitialMmapSize = bcfg.mmapSize()
bopts.FreelistType = bcfg.BackendFreelistType
bopts.NoSync = bcfg.UnsafeNoFsync
Expand Down Expand Up @@ -458,6 +464,7 @@
}

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 +506,7 @@
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 509 in server/storage/backend/backend.go

View check run for this annotation

Codecov / codecov/patch

server/storage/backend/backend.go#L509

Added line #L509 was not covered by tests
b.lg.Error(
"failed to remove temporary file",
zap.String("path", temp.Name()),
Expand All @@ -512,16 +519,14 @@

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 +589,17 @@
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
Loading