2023-06-22 11:55:30 +00:00
|
|
|
package writecachebbolt
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-05-19 15:06:20 +00:00
|
|
|
"sync/atomic"
|
2023-08-09 14:14:41 +00:00
|
|
|
"time"
|
2021-09-08 09:32:20 +00:00
|
|
|
|
2023-08-09 14:14:41 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2021-09-08 09:32:20 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-08-09 14:14:41 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2021-09-08 09:32:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (c *cache) estimateCacheSize() uint64 {
|
2023-05-18 14:19:41 +00:00
|
|
|
db := c.objCounters.DB() * c.smallObjectSize
|
|
|
|
fstree := c.objCounters.FS() * c.maxObjectSize
|
2023-05-19 08:17:19 +00:00
|
|
|
c.metrics.SetEstimateSize(db, fstree)
|
2023-05-18 14:19:41 +00:00
|
|
|
return db + fstree
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) incSizeDB(sz uint64) uint64 {
|
|
|
|
return sz + c.smallObjectSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) incSizeFS(sz uint64) uint64 {
|
|
|
|
return sz + c.maxObjectSize
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:14:41 +00:00
|
|
|
func (c *cache) setCounters() error {
|
2021-09-08 09:32:20 +00:00
|
|
|
var inDB uint64
|
2023-08-09 14:14:41 +00:00
|
|
|
var inFS uint64
|
|
|
|
|
|
|
|
var eg errgroup.Group
|
|
|
|
|
|
|
|
eg.Go(func() 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)
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
2023-08-09 14:14:41 +00:00
|
|
|
c.objCounters.cDB.Store(inDB)
|
2021-09-08 09:32:20 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-08-09 14:14:41 +00:00
|
|
|
eg.Go(func() error {
|
|
|
|
var err error
|
|
|
|
inFS, err = c.fsTree.NumberOfObjects()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read write-cache FS counter: %w", err)
|
|
|
|
}
|
|
|
|
if inFS > 0 {
|
|
|
|
inFS-- //small.bolt DB file
|
|
|
|
}
|
|
|
|
c.objCounters.cFS.Store(inFS)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return err
|
2023-08-07 10:31:56 +00:00
|
|
|
}
|
2023-05-19 08:17:19 +00:00
|
|
|
c.metrics.SetActualCounters(inDB, inFS)
|
2021-09-08 09:32:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-08-09 14:14:41 +00:00
|
|
|
|
|
|
|
func (c *cache) runDBCounterLoop() {
|
|
|
|
go func() {
|
|
|
|
t := time.NewTicker(time.Second * 30)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
err := c.setCounters()
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn(logs.FailedToCountWritecacheItems, zap.Error(err))
|
|
|
|
}
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|