frostfs-node/pkg/local_object_storage/writecache/state.go
Dmitrii Stepanov 5b9928536d
Some checks failed
Tests and linters / Tests with -race (pull_request) Failing after 12s
Tests and linters / Run gofumpt (pull_request) Successful in 1m14s
DCO action / DCO (pull_request) Successful in 1m26s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m51s
Vulncheck / Vulncheck (pull_request) Successful in 1m48s
Build / Build Components (pull_request) Successful in 2m2s
Tests and linters / Tests (pull_request) Successful in 3m7s
Tests and linters / Staticcheck (pull_request) Successful in 3m9s
Tests and linters / Lint (pull_request) Successful in 3m45s
Tests and linters / gopls check (pull_request) Successful in 3m56s
[#1335] writecache: Change DB engine to Pebble
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-09-05 13:31:16 +03:00

81 lines
1.7 KiB
Go

package writecache
import (
"fmt"
"math"
"sync/atomic"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
)
func (c *cache) estimateCacheSize() (uint64, uint64) {
dbCount := c.objCounters.DB()
fsCount := c.objCounters.FS()
dbSize := dbCount * c.smallObjectSize
fsSize := fsCount * c.maxObjectSize
c.metrics.SetEstimateSize(dbSize, fsSize)
c.metrics.SetActualCounters(dbCount, fsCount)
return dbCount + fsCount, dbSize + fsSize
}
func (c *cache) hasEnoughSpaceDB() bool {
return c.hasEnoughSpace(c.smallObjectSize)
}
func (c *cache) hasEnoughSpaceFS() bool {
return c.hasEnoughSpace(c.maxObjectSize)
}
func (c *cache) hasEnoughSpace(objectSize uint64) bool {
count, size := c.estimateCacheSize()
if c.maxCacheCount > 0 && count+1 > c.maxCacheCount {
return false
}
return c.maxCacheSize >= size+objectSize
}
var _ fstree.FileCounter = &counters{}
type counters struct {
cDB, cFS atomic.Uint64
}
func (x *counters) DB() uint64 {
return x.cDB.Load()
}
func (x *counters) FS() uint64 {
return x.cFS.Load()
}
// Set implements fstree.ObjectCounter.
func (x *counters) Set(v uint64) {
x.cFS.Store(v)
}
// Inc implements fstree.ObjectCounter.
func (x *counters) Inc() {
x.cFS.Add(1)
}
// Dec implements fstree.ObjectCounter.
func (x *counters) Dec() {
x.cFS.Add(math.MaxUint64)
}
func (c *cache) initCounters() error {
var inDB uint64
it, err := c.db.NewIter(nil)
if err != nil {
return fmt.Errorf("can't create write-cache database iterator: %w", err)
}
for v := it.First(); v; v = it.Next() {
inDB++
}
if err := it.Close(); err != nil {
return fmt.Errorf("can't close write-cache database iterator: %w", err)
}
c.objCounters.cDB.Store(inDB)
c.estimateCacheSize()
return nil
}