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"
|
2024-09-09 15:37:06 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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"
|
2024-09-10 08:49:17 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
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"
|
2024-09-10 09:56:29 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
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
|
|
|
"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 (
|
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.
|
2024-09-10 08:49:17 +00:00
|
|
|
defaultFlushInterval = 10 * time.Second
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
2024-09-10 09:56:29 +00:00
|
|
|
fl := newFlushLimiter(c.flushSizeLimit)
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Add(1)
|
2023-04-20 14:59:44 +00:00
|
|
|
go func() {
|
2024-09-10 08:49:17 +00:00
|
|
|
defer c.wg.Done()
|
2024-09-10 09:56:29 +00:00
|
|
|
c.pushToFlushQueue(ctx, fl)
|
2023-04-20 14:59:44 +00:00
|
|
|
}()
|
2024-09-10 08:49:17 +00:00
|
|
|
|
|
|
|
for range c.workersCount {
|
|
|
|
c.wg.Add(1)
|
2024-09-10 09:56:29 +00:00
|
|
|
go c.workerFlush(ctx, fl)
|
2024-09-10 08:49:17 +00:00
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 09:56:29 +00:00
|
|
|
func (c *cache) pushToFlushQueue(ctx context.Context, fl *flushLimiter) {
|
|
|
|
stopf := context.AfterFunc(ctx, func() {
|
|
|
|
fl.close()
|
|
|
|
})
|
|
|
|
defer stopf()
|
|
|
|
|
2024-09-10 08:49:17 +00:00
|
|
|
tick := time.NewTicker(defaultFlushInterval)
|
2021-04-06 10:56:06 +00:00
|
|
|
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()
|
2024-09-10 08:49:17 +00:00
|
|
|
continue
|
2022-01-18 12:47:16 +00:00
|
|
|
}
|
2021-12-09 12:30:45 +00:00
|
|
|
|
2024-09-10 08:49:17 +00:00
|
|
|
err := c.fsTree.IterateInfo(ctx, func(oi fstree.ObjectInfo) error {
|
2024-09-10 09:56:29 +00:00
|
|
|
if err := fl.acquire(oi.DataSize); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-10 08:49:17 +00:00
|
|
|
select {
|
|
|
|
case c.flushCh <- objectInfo{
|
|
|
|
addr: oi.Address,
|
|
|
|
size: oi.DataSize,
|
|
|
|
}:
|
|
|
|
return nil
|
|
|
|
case <-ctx.Done():
|
2024-09-10 09:56:29 +00:00
|
|
|
fl.release(oi.DataSize)
|
2024-09-10 08:49:17 +00:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.log.Warn(logs.BlobstorErrorOccurredDuringTheIteration, zap.Error(err))
|
|
|
|
}
|
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
|
|
|
|
2024-09-10 09:56:29 +00:00
|
|
|
func (c *cache) workerFlush(ctx context.Context, fl *flushLimiter) {
|
2024-09-10 08:49:17 +00:00
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
var objInfo objectInfo
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case objInfo = <-c.flushCh:
|
2024-09-10 09:56:29 +00:00
|
|
|
c.flushIfAnObjectExistsWorker(ctx, objInfo, fl)
|
2024-09-10 08:49:17 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2024-09-10 09:56:29 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 08:49:17 +00:00
|
|
|
|
2024-09-10 09:56:29 +00:00
|
|
|
func (c *cache) flushIfAnObjectExistsWorker(ctx context.Context, objInfo objectInfo, fl *flushLimiter) {
|
|
|
|
defer fl.release(objInfo.size)
|
2024-09-10 08:49:17 +00:00
|
|
|
|
2024-09-10 09:56:29 +00:00
|
|
|
res, err := c.fsTree.Get(ctx, common.GetPrm{
|
|
|
|
Address: objInfo.addr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if !client.IsErrObjectNotFound(err) {
|
|
|
|
c.reportFlushError(logs.WritecacheCantGetObject, objInfo.addr.EncodeToString(), metaerr.Wrap(err))
|
2024-09-10 08:49:17 +00:00
|
|
|
}
|
2024-09-10 09:56:29 +00:00
|
|
|
return
|
|
|
|
}
|
2024-09-10 08:49:17 +00:00
|
|
|
|
2024-09-10 09:56:29 +00:00
|
|
|
err = c.flushObject(ctx, res.Object, res.RawData, StorageTypeFSTree)
|
|
|
|
if err != nil {
|
|
|
|
// Error is handled in flushObject.
|
|
|
|
return
|
2024-09-10 08:49:17 +00:00
|
|
|
}
|
2024-09-10 09:56:29 +00:00
|
|
|
|
|
|
|
c.deleteFromDisk(ctx, objInfo.addr)
|
2024-09-10 08:49:17 +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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
2024-08-06 12:45:53 +00:00
|
|
|
if err := c.setMode(ctx, m, setModePrm{ignoreErrors: ignoreErrors}); err != nil {
|
2023-12-27 05:20:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-06-04 13:28:47 +00:00
|
|
|
c.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))
|
2023-12-27 05:20:15 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-09-09 15:37:06 +00:00
|
|
|
return c.flushFSTree(ctx, ignoreErrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
type batchItem struct {
|
|
|
|
data []byte
|
|
|
|
address string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cache) flushAndDropBBoltDB(ctx context.Context) error {
|
|
|
|
_, err := os.Stat(filepath.Join(c.path, dbName))
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return nil
|
2022-09-21 06:33:58 +00:00
|
|
|
}
|
2024-09-09 15:37:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not check write-cache database existence: %w", err)
|
|
|
|
}
|
|
|
|
db, err := OpenDB(c.path, true, os.OpenFile, c.pageSize)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open write-cache database: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = db.Close()
|
|
|
|
}()
|
2022-09-21 06:33:58 +00:00
|
|
|
|
2023-12-27 05:20:15 +00:00
|
|
|
var last string
|
|
|
|
for {
|
2024-09-09 15:37:06 +00:00
|
|
|
batch, err := c.readNextDBBatch(db, last)
|
2023-12-27 05:20:15 +00:00
|
|
|
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 {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("unmarshal object from database: %w", err)
|
2022-09-21 06:33:58 +00:00
|
|
|
}
|
2023-12-27 05:20:15 +00:00
|
|
|
if err := c.flushObject(ctx, &obj, item.data, StorageTypeDB); err != nil {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("flush object from database: %w", err)
|
2023-12-27 05:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
last = batch[len(batch)-1].address
|
|
|
|
}
|
2024-09-09 15:37:06 +00:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
return fmt.Errorf("close write-cache database: %w", err)
|
|
|
|
}
|
|
|
|
if err := os.Remove(filepath.Join(c.path, dbName)); err != nil {
|
|
|
|
return fmt.Errorf("remove write-cache database: %w", err)
|
|
|
|
}
|
2023-12-27 05:20:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-09 15:37:06 +00:00
|
|
|
func (c *cache) readNextDBBatch(db *bbolt.DB, last string) ([]batchItem, error) {
|
2023-12-27 05:20:15 +00:00
|
|
|
const batchSize = 100
|
|
|
|
var batch []batchItem
|
2024-09-09 15:37:06 +00:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2023-12-27 05:20:15 +00:00
|
|
|
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 {
|
2024-09-09 15:37:06 +00:00
|
|
|
return fmt.Errorf("decode address from database: %w", err)
|
2022-09-21 06:33:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|