2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-17 16:44:58 +00:00
|
|
|
"bytes"
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2022-09-21 06:33:58 +00:00
|
|
|
"errors"
|
2021-04-06 10:56:06 +00:00
|
|
|
"time"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
2023-12-27 05:20:15 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-04-06 10:56:06 +00:00
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
"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
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// flushBatchSize is amount of keys which will be read from cache to be flushed
|
|
|
|
// to the main storage. It is used to reduce contention between cache put
|
|
|
|
// and cache persist.
|
|
|
|
flushBatchSize = 512
|
2022-05-31 11:15:31 +00:00
|
|
|
// defaultFlushWorkersCount is number of workers for putting objects in main storage.
|
|
|
|
defaultFlushWorkersCount = 20
|
2021-04-06 10:56:06 +00:00
|
|
|
// defaultFlushInterval is default time interval between successive flushes.
|
|
|
|
defaultFlushInterval = time.Second
|
|
|
|
)
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
var errIterationCompleted = errors.New("iteration completed")
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// runFlushLoop starts background workers which periodically flush objects to the blobstor.
|
2023-09-19 05:46:19 +00:00
|
|
|
func (c *cache) runFlushLoop(ctx context.Context) {
|
2023-11-17 14:41:13 +00:00
|
|
|
if c.disableBackgroundFlush {
|
|
|
|
return
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := 0; i < c.workersCount; i++ {
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Add(1)
|
2023-09-19 05:46:19 +00:00
|
|
|
go c.workerFlushSmall(ctx)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Add(1)
|
2023-04-20 14:59:44 +00:00
|
|
|
go func() {
|
2023-05-24 11:09:11 +00:00
|
|
|
c.workerFlushBig(ctx)
|
2023-04-20 14:59:44 +00:00
|
|
|
c.wg.Done()
|
|
|
|
}()
|
2022-07-07 12:52:40 +00:00
|
|
|
|
|
|
|
c.wg.Add(1)
|
2021-04-06 10:56:06 +00:00
|
|
|
go func() {
|
2022-07-07 12:52:40 +00:00
|
|
|
defer c.wg.Done()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
tt := time.NewTimer(defaultFlushInterval)
|
|
|
|
defer tt.Stop()
|
2022-05-31 10:56:35 +00:00
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tt.C:
|
2023-09-19 05:46:19 +00:00
|
|
|
c.flushSmallObjects(ctx)
|
2022-07-07 12:52:40 +00:00
|
|
|
tt.Reset(defaultFlushInterval)
|
2023-10-27 06:53:12 +00:00
|
|
|
c.estimateCacheSize()
|
2023-09-19 05:46:19 +00:00
|
|
|
case <-ctx.Done():
|
2022-07-07 12:52:40 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
}()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 05:46:19 +00:00
|
|
|
func (c *cache) flushSmallObjects(ctx context.Context) {
|
2022-11-17 16:44:58 +00:00
|
|
|
var lastKey []byte
|
2021-04-06 10:56:06 +00:00
|
|
|
for {
|
2022-07-07 12:52:40 +00:00
|
|
|
select {
|
2023-09-19 05:46:19 +00:00
|
|
|
case <-ctx.Done():
|
2022-07-07 12:52:40 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-10-06 07:22:35 +00:00
|
|
|
var m []objectInfo
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
2023-05-10 14:43:49 +00:00
|
|
|
if c.readOnly() {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// We put objects in batches of fixed size to not interfere with main put cycle a lot.
|
|
|
|
_ = c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
cs := b.Cursor()
|
2022-11-17 16:44:58 +00:00
|
|
|
|
|
|
|
var k, v []byte
|
|
|
|
|
|
|
|
if len(lastKey) == 0 {
|
|
|
|
k, v = cs.First()
|
|
|
|
} else {
|
|
|
|
k, v = cs.Seek(lastKey)
|
|
|
|
if bytes.Equal(k, lastKey) {
|
|
|
|
k, v = cs.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ; k != nil && len(m) < flushBatchSize; k, v = cs.Next() {
|
|
|
|
if len(lastKey) == len(k) {
|
|
|
|
copy(lastKey, k)
|
|
|
|
} else {
|
2023-11-17 07:58:04 +00:00
|
|
|
lastKey = bytes.Clone(k)
|
2022-11-17 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
m = append(m, objectInfo{
|
|
|
|
addr: string(k),
|
2023-11-17 07:58:04 +00:00
|
|
|
data: bytes.Clone(v),
|
2021-04-06 10:56:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-02-06 13:03:37 +00:00
|
|
|
var count int
|
2021-04-06 10:56:06 +00:00
|
|
|
for i := range m {
|
2023-07-06 12:36:41 +00:00
|
|
|
obj := objectSDK.New()
|
2021-04-06 10:56:06 +00:00
|
|
|
if err := obj.Unmarshal(m[i].data); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-06 07:22:35 +00:00
|
|
|
m[i].obj = obj
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-02-06 13:03:37 +00:00
|
|
|
count++
|
2021-04-06 10:56:06 +00:00
|
|
|
select {
|
2023-10-06 07:22:35 +00:00
|
|
|
case c.flushCh <- m[i]:
|
2023-09-19 05:46:19 +00:00
|
|
|
case <-ctx.Done():
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2021-04-06 10:56:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 08:38:10 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2023-02-06 13:03:37 +00:00
|
|
|
if count == 0 {
|
2022-06-02 17:39:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.WritecacheTriedToFlushItemsFromWritecache,
|
2023-02-06 13:03:37 +00:00
|
|
|
zap.Int("count", count),
|
2021-04-06 10:56:06 +00:00
|
|
|
zap.String("start", base58.Encode(lastKey)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:40:57 +00:00
|
|
|
func (c *cache) workerFlushBig(ctx context.Context) {
|
2021-04-06 10:56:06 +00:00
|
|
|
tick := time.NewTicker(defaultFlushInterval * 10)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RLock()
|
2024-02-20 14:24:57 +00:00
|
|
|
if c.readOnly() || c.noMetabase() {
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
break
|
|
|
|
}
|
2021-12-09 12:30:45 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_ = c.flushFSTree(ctx, true)
|
2021-12-09 12:29:31 +00:00
|
|
|
|
2022-10-18 14:57:50 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2023-09-19 05:46:19 +00:00
|
|
|
case <-ctx.Done():
|
2022-10-18 14:57:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 14:11:48 +00:00
|
|
|
|
2022-10-20 10:40:25 +00:00
|
|
|
func (c *cache) reportFlushError(msg string, addr string, err error) {
|
|
|
|
if c.reportError != nil {
|
|
|
|
c.reportError(msg, err)
|
|
|
|
} else {
|
|
|
|
c.log.Error(msg,
|
|
|
|
zap.String("address", addr),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (c *cache) flushFSTree(ctx context.Context, ignoreErrors bool) error {
|
2022-10-18 14:57:50 +00:00
|
|
|
var prm common.IteratePrm
|
|
|
|
prm.IgnoreErrors = ignoreErrors
|
2023-11-15 10:12:23 +00:00
|
|
|
prm.Handler = func(e common.IterationElement) error {
|
|
|
|
sAddr := e.Address.EncodeToString()
|
2021-12-09 12:29:31 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var obj objectSDK.Object
|
2023-11-15 10:12:23 +00:00
|
|
|
err := obj.Unmarshal(e.ObjectData)
|
2022-10-18 14:57:50 +00:00
|
|
|
if err != nil {
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FSTreeCantUnmarshalObject, sAddr, metaerr.Wrap(err))
|
2022-10-18 14:57:50 +00:00
|
|
|
if ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-01-11 11:33:04 +00:00
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
err = c.flushObject(ctx, &obj, e.ObjectData, StorageTypeFSTree)
|
2022-10-18 14:57:50 +00:00
|
|
|
if err != nil {
|
|
|
|
if ignoreErrors {
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
2022-07-07 12:03:45 +00:00
|
|
|
}
|
2022-10-18 14:57:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-23 14:15:16 +00:00
|
|
|
|
2023-11-17 14:41:13 +00:00
|
|
|
c.deleteFromDisk(ctx, e.Address)
|
2022-10-18 14:57:50 +00:00
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-10-18 14:57:50 +00:00
|
|
|
|
2023-05-24 11:09:11 +00:00
|
|
|
_, err := c.fsTree.Iterate(ctx, prm)
|
2022-10-18 14:57:50 +00:00
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:40:57 +00:00
|
|
|
// workerFlushSmall writes small objects to the main storage.
|
2023-09-19 05:46:19 +00:00
|
|
|
func (c *cache) workerFlushSmall(ctx context.Context) {
|
2022-07-07 12:52:40 +00:00
|
|
|
defer c.wg.Done()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-10-06 07:22:35 +00:00
|
|
|
var objInfo objectInfo
|
2021-04-06 10:56:06 +00:00
|
|
|
for {
|
|
|
|
// Give priority to direct put.
|
|
|
|
select {
|
2023-10-06 07:22:35 +00:00
|
|
|
case objInfo = <-c.flushCh:
|
2023-09-19 05:46:19 +00:00
|
|
|
case <-ctx.Done():
|
2022-07-07 12:52:40 +00:00
|
|
|
return
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
err := c.flushObject(ctx, objInfo.obj, objInfo.data, StorageTypeDB)
|
2023-05-10 14:43:49 +00:00
|
|
|
if err != nil {
|
|
|
|
// Error is handled in flushObject.
|
|
|
|
continue
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2023-05-10 14:43:49 +00:00
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
c.deleteFromDB(objInfo.addr, true)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// flushObject is used to write object directly to the main storage.
|
2023-12-22 09:58:20 +00:00
|
|
|
func (c *cache) flushObject(ctx context.Context, obj *objectSDK.Object, data []byte, st StorageType) error {
|
2023-05-18 14:19:41 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
c.metrics.Flush(err == nil, st)
|
|
|
|
}()
|
|
|
|
|
2022-10-20 10:40:25 +00:00
|
|
|
addr := objectCore.AddressOf(obj)
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
var prm common.PutPrm
|
|
|
|
prm.Object = obj
|
2022-10-20 10:40:25 +00:00
|
|
|
prm.RawData = data
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
res, err := c.blobstor.Put(ctx, prm)
|
2022-07-07 12:52:40 +00:00
|
|
|
if err != nil {
|
2022-10-20 10:40:25 +00:00
|
|
|
if !errors.Is(err, common.ErrNoSpace) && !errors.Is(err, common.ErrReadOnly) &&
|
|
|
|
!errors.Is(err, blobstor.ErrNoPlaceFound) {
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FSTreeCantFushObjectBlobstor,
|
2022-10-20 10:40:25 +00:00
|
|
|
addr.EncodeToString(), err)
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 12:15:46 +00:00
|
|
|
var updPrm meta.UpdateStorageIDPrm
|
2022-10-20 10:40:25 +00:00
|
|
|
updPrm.SetAddress(addr)
|
2022-10-20 12:15:46 +00:00
|
|
|
updPrm.SetStorageID(res.StorageID)
|
2022-07-12 14:42:55 +00:00
|
|
|
|
2023-10-13 11:01:14 +00:00
|
|
|
_, err = c.metabase.UpdateStorageID(ctx, updPrm)
|
2022-10-20 10:40:25 +00:00
|
|
|
if err != nil {
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FSTreeCantUpdateID,
|
2022-10-20 10:40:25 +00:00
|
|
|
addr.EncodeToString(), err)
|
|
|
|
}
|
2022-07-12 14:42:55 +00:00
|
|
|
return err
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
|
|
|
|
// Flush flushes all objects from the write-cache to the main storage.
|
2023-12-27 05:20:15 +00:00
|
|
|
func (c *cache) Flush(ctx context.Context, ignoreErrors, seal bool) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Flush",
|
2023-04-12 14:01:29 +00:00
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Bool("ignore_errors", ignoreErrors),
|
2023-12-27 05:20:15 +00:00
|
|
|
attribute.Bool("seal", seal),
|
2023-04-12 14:01:29 +00:00
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
c.modeMtx.Lock() // exclusive lock to not to conflict with background flush
|
|
|
|
defer c.modeMtx.Unlock()
|
2024-02-20 14:24:57 +00:00
|
|
|
if c.noMetabase() {
|
|
|
|
return ErrDegraded
|
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
if err := c.flush(ctx, ignoreErrors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if seal {
|
|
|
|
m := c.mode | mode.ReadOnly
|
|
|
|
if err := c.setMode(ctx, m, ignoreErrors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.metrics.SetMode(m)
|
|
|
|
}
|
|
|
|
return nil
|
2022-09-30 10:41:37 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (c *cache) flush(ctx context.Context, ignoreErrors bool) error {
|
|
|
|
if err := c.flushFSTree(ctx, ignoreErrors); err != nil {
|
2022-09-21 06:33:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
var last string
|
|
|
|
for {
|
|
|
|
batch, err := c.readNextDBBatch(ignoreErrors, last)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(batch) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, item := range batch {
|
|
|
|
var obj objectSDK.Object
|
|
|
|
if err := obj.Unmarshal(item.data); err != nil {
|
|
|
|
c.reportFlushError(logs.FSTreeCantDecodeDBObjectAddress, item.address, metaerr.Wrap(err))
|
2022-09-26 08:54:21 +00:00
|
|
|
if ignoreErrors {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
if err := c.flushObject(ctx, &obj, item.data, StorageTypeDB); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.deleteFromDB(item.address, false)
|
|
|
|
}
|
|
|
|
last = batch[len(batch)-1].address
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type batchItem struct {
|
|
|
|
data []byte
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) readNextDBBatch(ignoreErrors bool, last string) ([]batchItem, error) {
|
|
|
|
const batchSize = 100
|
|
|
|
var batch []batchItem
|
|
|
|
err := c.db.View(func(tx *bbolt.Tx) error {
|
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
b := tx.Bucket(defaultBucket)
|
|
|
|
cs := b.Cursor()
|
|
|
|
for k, data := cs.Seek([]byte(last)); k != nil; k, data = cs.Next() {
|
|
|
|
sa := string(k)
|
|
|
|
if sa == last {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := addr.DecodeString(sa); err != nil {
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FSTreeCantDecodeDBObjectAddress, sa, metaerr.Wrap(err))
|
2022-09-26 08:54:21 +00:00
|
|
|
if ignoreErrors {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
batch = append(batch, batchItem{data: bytes.Clone(data), address: sa})
|
|
|
|
if len(batch) == batchSize {
|
|
|
|
return errIterationCompleted
|
2022-09-21 06:33:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2023-12-27 05:20:15 +00:00
|
|
|
if err == nil || errors.Is(err, errIterationCompleted) {
|
|
|
|
return batch, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
2022-09-21 06:33:58 +00:00
|
|
|
}
|