@@ -28,6 +28,7 @@ import (
28
28
"go.uber.org/zap"
29
29
30
30
bolt "go.etcd.io/bbolt"
31
+ "go.etcd.io/etcd/client/pkg/v3/verify"
31
32
)
32
33
33
34
var (
@@ -187,6 +188,11 @@ func newBackend(bcfg BackendConfig) *backend {
187
188
if boltOpenOptions != nil {
188
189
* bopts = * boltOpenOptions
189
190
}
191
+
192
+ if bcfg .Logger == nil {
193
+ bcfg .Logger = zap .NewNop ()
194
+ }
195
+
190
196
bopts .InitialMmapSize = bcfg .mmapSize ()
191
197
bopts .FreelistType = bcfg .BackendFreelistType
192
198
bopts .NoSync = bcfg .UnsafeNoFsync
@@ -458,6 +464,7 @@ func (b *backend) Defrag() error {
458
464
}
459
465
460
466
func (b * backend ) defrag () error {
467
+ verify .Assert (b .lg != nil , "the logger should not be nil" )
461
468
now := time .Now ()
462
469
isDefragActive .Set (1 )
463
470
defer isDefragActive .Set (0 )
@@ -499,7 +506,7 @@ func (b *backend) defrag() error {
499
506
tmpdb , err := bolt .Open (tdbp , 0o600 , & options )
500
507
if err != nil {
501
508
temp .Close ()
502
- if rmErr := os .Remove (temp .Name ()); rmErr != nil && b . lg != nil {
509
+ if rmErr := os .Remove (temp .Name ()); rmErr != nil {
503
510
b .lg .Error (
504
511
"failed to remove temporary file" ,
505
512
zap .String ("path" , temp .Name ()),
@@ -512,16 +519,14 @@ func (b *backend) defrag() error {
512
519
513
520
dbp := b .db .Path ()
514
521
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
+ )
525
530
526
531
defer func () {
527
532
// NOTE: We should exit as soon as possible because that tx
@@ -584,19 +589,17 @@ func (b *backend) defrag() error {
584
589
defragSec .Observe (took .Seconds ())
585
590
586
591
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
+ )
600
603
return nil
601
604
}
602
605
0 commit comments