2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2021-09-08 09:32:20 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-11 08:32:43 +00:00
|
|
|
"math"
|
2023-05-19 15:06:20 +00:00
|
|
|
"sync/atomic"
|
2021-09-08 09:32:20 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
2021-09-08 09:32:20 +00:00
|
|
|
)
|
|
|
|
|
2024-08-06 12:09:12 +00:00
|
|
|
func (c *cache) estimateCacheSize() (uint64, uint64) {
|
2023-08-11 08:32:43 +00:00
|
|
|
fsCount := c.objCounters.FS()
|
|
|
|
fsSize := fsCount * c.maxObjectSize
|
2024-09-09 15:37:06 +00:00
|
|
|
c.metrics.SetEstimateSize(0, fsSize)
|
|
|
|
c.metrics.SetActualCounters(0, fsCount)
|
|
|
|
return fsCount, fsSize
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
2024-08-06 12:09:12 +00:00
|
|
|
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
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
var _ fstree.FileCounter = &counters{}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
type counters struct {
|
2024-09-09 15:37:06 +00:00
|
|
|
cFS atomic.Uint64
|
2021-09-08 09:32:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (x *counters) FS() uint64 {
|
|
|
|
return x.cFS.Load()
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
// Set implements fstree.ObjectCounter.
|
|
|
|
func (x *counters) Set(v uint64) {
|
|
|
|
x.cFS.Store(v)
|
|
|
|
}
|
2023-08-09 14:14:41 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
// Inc implements fstree.ObjectCounter.
|
|
|
|
func (x *counters) Inc() {
|
|
|
|
x.cFS.Add(1)
|
|
|
|
}
|
2023-08-09 14:14:41 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
// Dec implements fstree.ObjectCounter.
|
|
|
|
func (x *counters) Dec() {
|
|
|
|
x.cFS.Add(math.MaxUint64)
|
|
|
|
}
|
2021-09-08 09:32:20 +00:00
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
func (c *cache) initCounters() error {
|
2023-10-27 06:53:12 +00:00
|
|
|
c.estimateCacheSize()
|
2021-09-08 09:32:20 +00:00
|
|
|
return nil
|
|
|
|
}
|