2021-09-08 09:32:20 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
)
|
|
|
|
|
2023-05-05 15:36:12 +00:00
|
|
|
func (c *Cache) estimateCacheSize() uint64 {
|
2021-09-08 09:32:20 +00:00
|
|
|
return c.objCounters.DB()*c.smallObjectSize + c.objCounters.FS()*c.maxObjectSize
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:36:12 +00:00
|
|
|
func (c *Cache) incSizeDB(sz uint64) uint64 {
|
2021-09-08 09:32:20 +00:00
|
|
|
return sz + c.smallObjectSize
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:36:12 +00:00
|
|
|
func (c *Cache) incSizeFS(sz uint64) uint64 {
|
2021-09-08 09:32:20 +00:00
|
|
|
return sz + c.maxObjectSize
|
|
|
|
}
|
|
|
|
|
|
|
|
type counters struct {
|
|
|
|
cDB, cFS atomic.Uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) IncDB() {
|
|
|
|
x.cDB.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DecDB() {
|
|
|
|
x.cDB.Dec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DB() uint64 {
|
|
|
|
return x.cDB.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) IncFS() {
|
|
|
|
x.cFS.Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DecFS() {
|
|
|
|
x.cFS.Dec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) FS() uint64 {
|
|
|
|
return x.cFS.Load()
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:36:12 +00:00
|
|
|
func (c *Cache) initCounters() error {
|
2021-09-08 09:32:20 +00:00
|
|
|
var inDB uint64
|
2022-09-01 05:56:21 +00:00
|
|
|
err := c.db.View(func(tx *bbolt.Tx) error {
|
2021-09-08 09:32:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:56:21 +00:00
|
|
|
inFS, err := c.fsTree.NumberOfObjects()
|
2021-10-19 16:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not read write-cache FS counter: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:56:21 +00:00
|
|
|
c.objCounters.cDB.Store(inDB)
|
|
|
|
c.objCounters.cFS.Store(inFS)
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|