forked from TrueCloudLab/frostfs-node
a1696a81b6
There is a need to limit disk space used by write-cache. It is almost impossible to calculate the value exactly. It is proposed to estimate the size of the cache by the number of objects stored in it. Track amounts of objects saved in DB and FSTree separately. To do this, `ObjectCounters` interface is defined. It is generalized to a store of numbers that can be made persistent (new option `WithObjectCounters`). By default DB number is calculated as key number in default bucket, and FS number is set same to DB since it is currently hard to read the actual value from `FSTree` instance. Each PUT/DELETE operation to DB or FS increases/decreases corresponding counter. Before each PUT op an overflow check is performed with the following formula for evaluating the occupied space: `NumDB * MaxDBSize + NumFS * MaxFSSize`. If next PUT can cause write-cache overflow, object is written to the main storage. By default maximum write-cache size is set to 1GB. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package writecache
|
|
|
|
import (
|
|
"errors"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
|
"go.etcd.io/bbolt"
|
|
)
|
|
|
|
// Delete removes object from write-cache.
|
|
func (c *cache) Delete(addr *objectSDK.Address) error {
|
|
saddr := addr.String()
|
|
|
|
// Check memory cache.
|
|
c.mtx.Lock()
|
|
for i := range c.mem {
|
|
if saddr == c.mem[i].addr {
|
|
copy(c.mem[i:], c.mem[i+1:])
|
|
c.mem = c.mem[:len(c.mem)-1]
|
|
c.curMemSize -= uint64(len(c.mem[i].data))
|
|
c.mtx.Unlock()
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("in-mem DELETE"))
|
|
return nil
|
|
}
|
|
}
|
|
c.mtx.Unlock()
|
|
|
|
// Check disk cache.
|
|
var has int
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(defaultBucket)
|
|
has = len(b.Get([]byte(saddr)))
|
|
return nil
|
|
})
|
|
|
|
if 0 < has {
|
|
err := c.db.Update(func(tx *bbolt.Tx) error {
|
|
b := tx.Bucket(defaultBucket)
|
|
err := b.Delete([]byte(saddr))
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
c.dbSize.Sub(uint64(has))
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("db DELETE"))
|
|
c.objCounters.DecDB()
|
|
return nil
|
|
}
|
|
|
|
err := c.fsTree.Delete(addr)
|
|
if errors.Is(err, fstree.ErrFileNotFound) {
|
|
err = object.ErrNotFound
|
|
}
|
|
|
|
if err == nil {
|
|
storagelog.Write(c.log, storagelog.AddressField(saddr), storagelog.OpField("fstree DELETE"))
|
|
c.objCounters.DecFS()
|
|
}
|
|
|
|
return err
|
|
}
|