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
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
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) IncDB() {
|
2023-05-19 15:06:20 +00:00
|
|
|
x.cDB.Add(1)
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DecDB() {
|
2023-05-19 15:06:20 +00:00
|
|
|
x.cDB.Add(math.MaxUint64)
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DB() uint64 {
|
|
|
|
return x.cDB.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) IncFS() {
|
2023-05-19 15:06:20 +00:00
|
|
|
x.cFS.Add(1)
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) DecFS() {
|
2023-05-19 15:06:20 +00:00
|
|
|
x.cFS.Add(math.MaxUint64)
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) FS() uint64 {
|
|
|
|
return x.cFS.Load()
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:56:21 +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)
|
2023-05-19 08:17:19 +00:00
|
|
|
c.metrics.SetActualCounters(inDB, inFS)
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|