From 2920c5203b99052b6dd2a4334001bb9d4267db88 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 8 Dec 2021 13:45:23 +0300 Subject: [PATCH] [#915] blobovnicza: Re-use Bolt transaction in syncFullnessCounter Make `syncFullnessCounter` to accept `bbolt.Tx` argument of Bolt transaction within which counter should be synchronized. Pass corresponding transaction during `Init`. Signed-off-by: Leonard Lyubich --- .../blobovnicza/control.go | 2 +- pkg/local_object_storage/blobovnicza/sizes.go | 33 ++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/pkg/local_object_storage/blobovnicza/control.go b/pkg/local_object_storage/blobovnicza/control.go index 5c2141310..653808e86 100644 --- a/pkg/local_object_storage/blobovnicza/control.go +++ b/pkg/local_object_storage/blobovnicza/control.go @@ -65,7 +65,7 @@ func (b *Blobovnicza) Init() error { b.log.Debug("bucket already exists, initializing state") - return true, b.syncFullnessCounter() + return true, b.syncFullnessCounter(tx) } if err != nil { diff --git a/pkg/local_object_storage/blobovnicza/sizes.go b/pkg/local_object_storage/blobovnicza/sizes.go index 027018a50..c6e91bc0b 100644 --- a/pkg/local_object_storage/blobovnicza/sizes.go +++ b/pkg/local_object_storage/blobovnicza/sizes.go @@ -52,29 +52,24 @@ func (b *Blobovnicza) full() bool { return b.filled.Load() >= b.fullSizeLimit } -func (b *Blobovnicza) syncFullnessCounter() error { - err := b.boltDB.View(func(tx *bbolt.Tx) error { - sz := uint64(0) +func (b *Blobovnicza) syncFullnessCounter(tx *bbolt.Tx) error { + sz := uint64(0) - if err := b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) { - buck := tx.Bucket(key) - if buck == nil { - return false, fmt.Errorf("bucket not found %s", stringifyBounds(lower, upper)) - } - - sz += uint64(buck.Stats().KeyN) * (upper - lower) - - return false, nil - }); err != nil { - return err + if err := b.iterateBucketKeys(func(lower, upper uint64, key []byte) (bool, error) { + buck := tx.Bucket(key) + if buck == nil { + return false, fmt.Errorf("bucket not found %s", stringifyBounds(lower, upper)) } - b.filled.Store(sz) + sz += uint64(buck.Stats().KeyN) * (upper - lower) - return nil - }) - if err != nil { - return fmt.Errorf("could not sync fullness counter: %w", err) + return false, nil + }); err != nil { + return err } + + b.filled.Store(sz) + return nil + }