2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2023-05-18 14:19:41 +00:00
|
|
|
"time"
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2022-07-07 12:52:40 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Put puts object to write-cache.
|
2023-02-15 14:53:42 +00:00
|
|
|
//
|
|
|
|
// Returns ErrReadOnly if write-cache is in R/O mode.
|
|
|
|
// Returns ErrNotInitialized if write-cache has not been initialized yet.
|
|
|
|
// Returns ErrOutOfSpace if saving an object leads to WC's size overflow.
|
|
|
|
// Returns ErrBigObject if an objects exceeds maximum object size.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (c *cache) Put(ctx context.Context, prm common.PutPrm) (common.PutRes, error) {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Put",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.Bool("dont_compress", prm.DontCompress),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-05-18 14:19:41 +00:00
|
|
|
startedAt := time.Now()
|
|
|
|
added := false
|
2023-12-22 09:58:20 +00:00
|
|
|
storageType := StorageTypeUndefined
|
2023-05-18 14:19:41 +00:00
|
|
|
defer func() {
|
|
|
|
c.metrics.Put(time.Since(startedAt), added, storageType)
|
|
|
|
}()
|
|
|
|
|
2023-12-27 08:40:55 +00:00
|
|
|
if !c.modeMtx.TryRLock() {
|
|
|
|
return common.PutRes{}, ErrNotInitialized
|
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
defer c.modeMtx.RUnlock()
|
2022-03-17 11:55:25 +00:00
|
|
|
if c.readOnly() {
|
2023-12-22 09:58:20 +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 {
|
2023-12-22 09:58:20 +00:00
|
|
|
storageType = StorageTypeDB
|
2023-05-18 14:19:41 +00:00
|
|
|
err := c.putSmall(oi)
|
|
|
|
if err == nil {
|
|
|
|
added = true
|
|
|
|
}
|
|
|
|
return common.PutRes{}, err
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
storageType = StorageTypeFSTree
|
2023-05-18 14:19:41 +00:00
|
|
|
err := c.putBig(ctx, oi.addr, prm)
|
|
|
|
if err == nil {
|
|
|
|
added = true
|
2022-07-07 12:52:40 +00:00
|
|
|
}
|
2023-06-15 10:19:36 +00:00
|
|
|
return common.PutRes{}, metaerr.Wrap(err)
|
2022-07-07 12:52:40 +00:00
|
|
|
}
|
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
|
|
|
|
2023-08-11 09:51:41 +00:00
|
|
|
var newRecord bool
|
2022-07-07 12:52:40 +00:00
|
|
|
err := c.db.Batch(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
2023-08-11 09:51:41 +00:00
|
|
|
key := []byte(obj.addr)
|
|
|
|
newRecord = b.Get(key) == nil
|
|
|
|
if newRecord {
|
|
|
|
return b.Put(key, obj.data)
|
|
|
|
}
|
|
|
|
return nil
|
2022-07-07 12:52:40 +00:00
|
|
|
})
|
|
|
|
if err == nil {
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(obj.addr),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("db PUT"),
|
|
|
|
)
|
2023-08-11 09:51:41 +00:00
|
|
|
if newRecord {
|
|
|
|
c.objCounters.cDB.Add(1)
|
|
|
|
c.estimateCacheSize()
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
}
|
2023-05-05 15:59:49 +00:00
|
|
|
return err
|
2022-07-07 12:52:40 +00:00
|
|
|
}
|
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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (c *cache) putBig(ctx context.Context, addr string, prm common.PutPrm) error {
|
2022-07-07 12:52:40 +00:00
|
|
|
cacheSz := c.estimateCacheSize()
|
|
|
|
if c.maxCacheSize < c.incSizeFS(cacheSz) {
|
|
|
|
return ErrOutOfSpace
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := c.fsTree.Put(ctx, prm)
|
2022-07-07 12:52:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-08-09 12:54:08 +00:00
|
|
|
if compressor := c.blobstor.Compressor(); compressor != nil && compressor.NeedsCompression(prm.Object) {
|
2022-07-07 12:52:40 +00:00
|
|
|
c.mtx.Lock()
|
|
|
|
c.compressFlags[addr] = struct{}{}
|
|
|
|
c.mtx.Unlock()
|
2021-10-27 11:48:33 +00:00
|
|
|
}
|
2022-12-20 12:29:05 +00:00
|
|
|
storagelog.Write(c.log,
|
|
|
|
storagelog.AddressField(addr),
|
|
|
|
storagelog.StorageTypeField(wcStorageType),
|
|
|
|
storagelog.OpField("fstree PUT"),
|
|
|
|
)
|
2023-10-27 06:53:12 +00:00
|
|
|
// counter changed by fstree
|
2023-08-11 09:51:41 +00:00
|
|
|
c.estimateCacheSize()
|
2022-12-20 12:29:05 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|