[#1745] writecache: Simplify object counters

Remove unused option and additional pointers to db/fstree.

Signed-off-by: Evgenii Stratonikov <evgeniy@morphbits.ru>
This commit is contained in:
Evgenii Stratonikov 2022-09-01 08:56:21 +03:00 committed by fyrchik
parent 20abdaeed4
commit bda084f331
3 changed files with 8 additions and 59 deletions

View file

@ -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) {

View file

@ -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
}

View file

@ -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()