From bda084f3310f2ec421a7e3b42aba39b6a206c215 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 1 Sep 2022 08:56:21 +0300 Subject: [PATCH] [#1745] writecache: Simplify object counters Remove unused option and additional pointers to db/fstree. Signed-off-by: Evgenii Stratonikov --- .../writecache/options.go | 11 +---- pkg/local_object_storage/writecache/state.go | 44 +++---------------- .../writecache/writecache.go | 12 +---- 3 files changed, 8 insertions(+), 59 deletions(-) diff --git a/pkg/local_object_storage/writecache/options.go b/pkg/local_object_storage/writecache/options.go index 64e1feba..55020d49 100644 --- a/pkg/local_object_storage/writecache/options.go +++ b/pkg/local_object_storage/writecache/options.go @@ -31,8 +31,8 @@ type options struct { // maxCacheSize is the maximum total size of all objects saved in cache (DB + FS). // 1 GiB by default. maxCacheSize uint64 - // objCounters is an ObjectCounters instance needed for cache size estimation. - objCounters ObjectCounters + // objCounters contains atomic counters for the number of objects stored in cache. + objCounters counters // maxBatchSize is the maximum batch size for the small object database. maxBatchSize int // maxBatchDelay is the maximum batch wait time for the small object database. @@ -100,13 +100,6 @@ func WithFlushWorkersCount(c int) Option { } } -// WithObjectCounters sets ObjectCounters instance needed for cache write-cache size estimation. -func WithObjectCounters(v ObjectCounters) Option { - return func(o *options) { - o.objCounters = v - } -} - // WithMaxCacheSize sets maximum write-cache size in bytes. func WithMaxCacheSize(sz uint64) Option { return func(o *options) { diff --git a/pkg/local_object_storage/writecache/state.go b/pkg/local_object_storage/writecache/state.go index 2885c433..1ba5a4bd 100644 --- a/pkg/local_object_storage/writecache/state.go +++ b/pkg/local_object_storage/writecache/state.go @@ -3,33 +3,10 @@ package writecache import ( "fmt" - "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree" "go.etcd.io/bbolt" "go.uber.org/atomic" ) -// ObjectCounters is an interface of the storage of cached object amount. -type ObjectCounters interface { - // Increments number of objects saved in DB. - IncDB() - // Decrements number of objects saved in DB. - DecDB() - // Returns number of objects saved in DB. - DB() uint64 - - // Increments number of objects saved in FSTree. - IncFS() - // Decrements number of objects saved in FSTree. - DecFS() - // Returns number of objects saved in FSTree. - FS() uint64 - - // Reads number of objects saved in write-cache. It is called on write-cache initialization step. - Read() error - // Flushes the values and closes the storage. It is called on write-cache shutdown. - FlushAndClose() -} - func (c *cache) estimateCacheSize() uint64 { return c.objCounters.DB()*c.smallObjectSize + c.objCounters.FS()*c.maxObjectSize } @@ -44,10 +21,6 @@ func (c *cache) incSizeFS(sz uint64) uint64 { type counters struct { cDB, cFS atomic.Uint64 - - db *bbolt.DB - - fs *fstree.FSTree } func (x *counters) IncDB() { @@ -74,33 +47,26 @@ func (x *counters) FS() uint64 { return x.cFS.Load() } -func (x *counters) Read() error { +func (c *cache) initCounters() error { var inDB uint64 - - err := x.db.View(func(tx *bbolt.Tx) error { + err := c.db.View(func(tx *bbolt.Tx) error { b := tx.Bucket(defaultBucket) if b != nil { inDB = uint64(b.Stats().KeyN) } - return nil }) if err != nil { return fmt.Errorf("could not read write-cache DB counter: %w", err) } - x.cDB.Store(inDB) - - inFS, err := x.fs.NumberOfObjects() + inFS, err := c.fsTree.NumberOfObjects() if err != nil { return fmt.Errorf("could not read write-cache FS counter: %w", err) } - x.cFS.Store(inFS) + c.objCounters.cDB.Store(inDB) + c.objCounters.cFS.Store(inFS) return nil } - -func (x *counters) FlushAndClose() { - // values aren't stored -} diff --git a/pkg/local_object_storage/writecache/writecache.go b/pkg/local_object_storage/writecache/writecache.go index 7bd7c583..a4b4fe2e 100644 --- a/pkg/local_object_storage/writecache/writecache.go +++ b/pkg/local_object_storage/writecache/writecache.go @@ -124,12 +124,7 @@ func (c *cache) Open(readOnly bool) error { // thus we need to create a channel here. c.closeCh = make(chan struct{}) - c.objCounters = &counters{ - db: c.db, - fs: c.fsTree, - } - - return c.objCounters.Read() + return c.initCounters() } // Init runs necessary services. @@ -154,11 +149,6 @@ func (c *cache) Close() error { c.closeCh = nil } - if c.objCounters != nil { - c.objCounters.FlushAndClose() - c.objCounters = nil - } - var err error if c.db != nil { err = c.db.Close()