Dmitrii Stepanov
d4b6ebe7e7
All checks were successful
DCO action / DCO (pull_request) Successful in 1m33s
Build / Build Components (1.21) (pull_request) Successful in 3m11s
Build / Build Components (1.20) (pull_request) Successful in 3m22s
Vulncheck / Vulncheck (pull_request) Successful in 3m34s
Tests and linters / Tests (1.20) (pull_request) Successful in 6m0s
Tests and linters / Tests (1.21) (pull_request) Successful in 6m1s
Tests and linters / Staticcheck (pull_request) Successful in 6m4s
Tests and linters / Tests with -race (pull_request) Successful in 3m58s
Tests and linters / Lint (pull_request) Successful in 11m43s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package writecachebadger
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"sync/atomic"
|
|
|
|
"github.com/dgraph-io/badger/v4"
|
|
)
|
|
|
|
func (c *cache) estimateCacheSize() uint64 {
|
|
onDiskSize, _ := c.db.EstimateSize(nil)
|
|
c.metrics.SetEstimateSize(onDiskSize, 0)
|
|
return onDiskSize
|
|
}
|
|
|
|
func (c *cache) incSizeDB(sz uint64) uint64 {
|
|
return sz + c.maxObjectSize
|
|
}
|
|
|
|
type counters struct {
|
|
cDB atomic.Uint64
|
|
}
|
|
|
|
func (x *counters) IncDB() {
|
|
x.cDB.Add(1)
|
|
}
|
|
|
|
func (x *counters) DecDB() {
|
|
x.cDB.Add(math.MaxUint64)
|
|
}
|
|
|
|
func (x *counters) DB() uint64 {
|
|
return x.cDB.Load()
|
|
}
|
|
|
|
func (c *cache) initCounters() error {
|
|
var inDB uint64
|
|
err := c.db.View(func(tx *badger.Txn) error {
|
|
opts := badger.DefaultIteratorOptions
|
|
opts.PrefetchValues = false
|
|
it := tx.NewIterator(opts)
|
|
defer it.Close()
|
|
for it.Rewind(); it.Valid(); it.Next() {
|
|
inDB++
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("could not read write-cache DB counter: %w", err)
|
|
}
|
|
|
|
c.objCounters.cDB.Store(inDB)
|
|
c.metrics.SetActualCounters(inDB, 0)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *cache) incDB() {
|
|
c.objCounters.IncDB()
|
|
c.metrics.SetActualCounters(c.objCounters.DB(), 0)
|
|
}
|
|
|
|
func (c *cache) decDB() {
|
|
c.objCounters.DecDB()
|
|
c.metrics.SetActualCounters(c.objCounters.DB(), 0)
|
|
}
|