2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common"
|
2021-09-06 14:28:55 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
2022-07-07 12:52:40 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
var (
|
|
|
|
// ErrBigObject is returned when object is too big to be placed in cache.
|
|
|
|
ErrBigObject = errors.New("too big object")
|
|
|
|
// ErrOutOfSpace is returned when there is no space left to put a new object.
|
|
|
|
ErrOutOfSpace = errors.New("no space left in the write cache")
|
|
|
|
)
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
// Put puts object to write-cache.
|
2022-07-07 12:52:40 +00:00
|
|
|
func (c *cache) Put(prm common.PutPrm) (common.PutRes, error) {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
|
|
|
defer c.modeMtx.RUnlock()
|
2022-03-17 11:55:25 +00:00
|
|
|
if c.readOnly() {
|
2022-07-07 12:52:40 +00:00
|
|
|
return common.PutRes{}, ErrReadOnly
|
2022-01-18 12:47:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
sz := uint64(len(prm.RawData))
|
2021-04-06 10:56:06 +00:00
|
|
|
if sz > c.maxObjectSize {
|
2022-07-07 12:52:40 +00:00
|
|
|
return common.PutRes{}, ErrBigObject
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oi := objectInfo{
|
2022-07-07 12:52:40 +00:00
|
|
|
addr: prm.Address.EncodeToString(),
|
|
|
|
obj: prm.Object,
|
|
|
|
data: prm.RawData,
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
if sz <= c.smallObjectSize {
|
|
|
|
return common.PutRes{}, c.putSmall(oi)
|
|
|
|
}
|
|
|
|
return common.PutRes{}, c.putBig(oi.addr, prm)
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// putSmall persists small objects to the write-cache database and
|
|
|
|
// pushes the to the flush workers queue.
|
|
|
|
func (c *cache) putSmall(obj objectInfo) error {
|
|
|
|
cacheSize := c.estimateCacheSize()
|
|
|
|
if c.maxCacheSize < c.incSizeDB(cacheSize) {
|
|
|
|
return ErrOutOfSpace
|
|
|
|
}
|
2021-09-06 14:28:55 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
err := c.db.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
return b.Put([]byte(obj.addr), obj.data)
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
storagelog.Write(c.log, storagelog.AddressField(obj.addr), storagelog.OpField("db PUT"))
|
|
|
|
c.objCounters.IncDB()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-06 14:28:55 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// putBig writes object to FSTree and pushes it to the flush workers queue.
|
|
|
|
func (c *cache) putBig(addr string, prm common.PutPrm) error {
|
|
|
|
cacheSz := c.estimateCacheSize()
|
|
|
|
if c.maxCacheSize < c.incSizeFS(cacheSz) {
|
|
|
|
return ErrOutOfSpace
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
_, err := c.fsTree.Put(prm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
if c.blobstor.NeedsCompression(prm.Object) {
|
|
|
|
c.mtx.Lock()
|
|
|
|
c.compressFlags[addr] = struct{}{}
|
|
|
|
c.mtx.Unlock()
|
2021-10-27 11:48:33 +00:00
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
c.objCounters.IncFS()
|
|
|
|
storagelog.Write(c.log, storagelog.AddressField(addr), storagelog.OpField("fstree PUT"))
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|