Skip to content

Commit e4d1da7

Browse files
authored
Merge pull request #19077 from srujangit123/remove-nil-checks
backend: Remove logger nil checks
2 parents 21021b2 + 5398519 commit e4d1da7

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

server/storage/backend/backend.go

+27-24
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"go.uber.org/zap"
2929

3030
bolt "go.etcd.io/bbolt"
31+
"go.etcd.io/etcd/client/pkg/v3/verify"
3132
)
3233

3334
var (
@@ -187,6 +188,11 @@ func newBackend(bcfg BackendConfig) *backend {
187188
if boltOpenOptions != nil {
188189
*bopts = *boltOpenOptions
189190
}
191+
192+
if bcfg.Logger == nil {
193+
bcfg.Logger = zap.NewNop()
194+
}
195+
190196
bopts.InitialMmapSize = bcfg.mmapSize()
191197
bopts.FreelistType = bcfg.BackendFreelistType
192198
bopts.NoSync = bcfg.UnsafeNoFsync
@@ -458,6 +464,7 @@ func (b *backend) Defrag() error {
458464
}
459465

460466
func (b *backend) defrag() error {
467+
verify.Assert(b.lg != nil, "the logger should not be nil")
461468
now := time.Now()
462469
isDefragActive.Set(1)
463470
defer isDefragActive.Set(0)
@@ -499,7 +506,7 @@ func (b *backend) defrag() error {
499506
tmpdb, err := bolt.Open(tdbp, 0o600, &options)
500507
if err != nil {
501508
temp.Close()
502-
if rmErr := os.Remove(temp.Name()); rmErr != nil && b.lg != nil {
509+
if rmErr := os.Remove(temp.Name()); rmErr != nil {
503510
b.lg.Error(
504511
"failed to remove temporary file",
505512
zap.String("path", temp.Name()),
@@ -512,16 +519,14 @@ func (b *backend) defrag() error {
512519

513520
dbp := b.db.Path()
514521
size1, sizeInUse1 := b.Size(), b.SizeInUse()
515-
if b.lg != nil {
516-
b.lg.Info(
517-
"defragmenting",
518-
zap.String("path", dbp),
519-
zap.Int64("current-db-size-bytes", size1),
520-
zap.String("current-db-size", humanize.Bytes(uint64(size1))),
521-
zap.Int64("current-db-size-in-use-bytes", sizeInUse1),
522-
zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse1))),
523-
)
524-
}
522+
b.lg.Info(
523+
"defragmenting",
524+
zap.String("path", dbp),
525+
zap.Int64("current-db-size-bytes", size1),
526+
zap.String("current-db-size", humanize.Bytes(uint64(size1))),
527+
zap.Int64("current-db-size-in-use-bytes", sizeInUse1),
528+
zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse1))),
529+
)
525530

526531
defer func() {
527532
// NOTE: We should exit as soon as possible because that tx
@@ -584,19 +589,17 @@ func (b *backend) defrag() error {
584589
defragSec.Observe(took.Seconds())
585590

586591
size2, sizeInUse2 := b.Size(), b.SizeInUse()
587-
if b.lg != nil {
588-
b.lg.Info(
589-
"finished defragmenting directory",
590-
zap.String("path", dbp),
591-
zap.Int64("current-db-size-bytes-diff", size2-size1),
592-
zap.Int64("current-db-size-bytes", size2),
593-
zap.String("current-db-size", humanize.Bytes(uint64(size2))),
594-
zap.Int64("current-db-size-in-use-bytes-diff", sizeInUse2-sizeInUse1),
595-
zap.Int64("current-db-size-in-use-bytes", sizeInUse2),
596-
zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse2))),
597-
zap.Duration("took", took),
598-
)
599-
}
592+
b.lg.Info(
593+
"finished defragmenting directory",
594+
zap.String("path", dbp),
595+
zap.Int64("current-db-size-bytes-diff", size2-size1),
596+
zap.Int64("current-db-size-bytes", size2),
597+
zap.String("current-db-size", humanize.Bytes(uint64(size2))),
598+
zap.Int64("current-db-size-in-use-bytes-diff", sizeInUse2-sizeInUse1),
599+
zap.Int64("current-db-size-in-use-bytes", sizeInUse2),
600+
zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse2))),
601+
zap.Duration("took", took),
602+
)
600603
return nil
601604
}
602605

0 commit comments

Comments
 (0)