2023-06-22 11:55:30 +00:00
|
|
|
package writecachebadger
|
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"
|
2023-06-22 11:55:30 +00:00
|
|
|
"encoding/hex"
|
2022-09-21 06:33:58 +00:00
|
|
|
"errors"
|
2023-06-22 11:55:30 +00:00
|
|
|
"fmt"
|
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-06-22 11:55:30 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/writecache"
|
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-06-22 11:55:30 +00:00
|
|
|
"github.com/dgraph-io/badger/v4"
|
2023-08-23 07:05:40 +00:00
|
|
|
"github.com/dgraph-io/ristretto/z"
|
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-08-23 07:05:40 +00:00
|
|
|
type collector struct {
|
|
|
|
cache *cache
|
|
|
|
scheduled int
|
|
|
|
processed int
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collector) Send(buf *z.Buffer) error {
|
|
|
|
list, err := badger.BufferToKVList(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, kv := range list.Kv {
|
|
|
|
select {
|
|
|
|
case <-c.cache.closeCh:
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if kv.StreamDone {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if c.scheduled >= flushBatchSize {
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if got, want := len(kv.Key), len(internalKey{}); got != want {
|
|
|
|
c.cache.log.Debug(
|
|
|
|
fmt.Sprintf("not expected db key len: got %d, want %d", got, want))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.processed++
|
|
|
|
obj := objectSDK.New()
|
|
|
|
val := bytes.Clone(kv.Value)
|
|
|
|
if err = obj.Unmarshal(val); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addr := objectCore.AddressOf(obj)
|
|
|
|
c.cache.scheduled4FlushMtx.RLock()
|
|
|
|
_, ok := c.cache.scheduled4Flush[addr]
|
|
|
|
c.cache.scheduled4FlushMtx.RUnlock()
|
|
|
|
if ok {
|
|
|
|
c.cache.log.Debug(logs.WritecacheBadgerObjAlreadyScheduled, zap.Stringer("obj", addr))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.cache.scheduled4FlushMtx.Lock()
|
|
|
|
c.cache.scheduled4Flush[addr] = struct{}{}
|
|
|
|
c.cache.scheduled4FlushMtx.Unlock()
|
|
|
|
c.scheduled++
|
|
|
|
select {
|
|
|
|
case c.cache.flushCh <- obj:
|
|
|
|
case <-c.cache.closeCh:
|
|
|
|
c.cancel()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// runFlushLoop starts background workers which periodically flush objects to the blobstor.
|
|
|
|
func (c *cache) runFlushLoop() {
|
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-05-05 15:40:57 +00:00
|
|
|
go c.workerFlushSmall()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
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-05-05 15:40:57 +00:00
|
|
|
c.flushSmallObjects()
|
2022-07-07 12:52:40 +00:00
|
|
|
tt.Reset(defaultFlushInterval)
|
|
|
|
case <-c.closeCh:
|
|
|
|
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-05-05 15:40:57 +00:00
|
|
|
func (c *cache) flushSmallObjects() {
|
2021-04-06 10:56:06 +00:00
|
|
|
for {
|
2022-07-07 12:52:40 +00:00
|
|
|
select {
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-08-14 07:54:09 +00:00
|
|
|
// Using the db after Close will panic and badger won't wait for outstanding txs,
|
|
|
|
// so we need to check manually.
|
|
|
|
if c.db.IsClosed() {
|
|
|
|
c.modeMtx.RUnlock()
|
|
|
|
return
|
|
|
|
}
|
2023-08-23 07:05:40 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
coll := collector{
|
|
|
|
cache: c,
|
|
|
|
cancel: cancel,
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2023-08-23 07:05:40 +00:00
|
|
|
stream := c.db.NewStream()
|
|
|
|
// All calls to Send are done by a single goroutine
|
|
|
|
stream.Send = coll.Send
|
|
|
|
if err := stream.Orchestrate(ctx); err != nil {
|
|
|
|
c.log.Debug(fmt.Sprintf(
|
|
|
|
"error during flushing object from wc: %s", err))
|
2022-06-02 17:39:40 +00:00
|
|
|
}
|
2022-01-18 12:47:16 +00:00
|
|
|
c.modeMtx.RUnlock()
|
2023-08-23 07:05:40 +00:00
|
|
|
if coll.scheduled == 0 {
|
|
|
|
break
|
|
|
|
}
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.WritecacheTriedToFlushItemsFromWritecache,
|
2023-08-23 07:05:40 +00:00
|
|
|
zap.Int("scheduled", coll.scheduled), zap.Int("processed", coll.processed))
|
2022-10-18 14:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
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-05-05 15:40:57 +00:00
|
|
|
// workerFlushSmall writes small objects to the main storage.
|
|
|
|
func (c *cache) workerFlushSmall() {
|
2022-07-07 12:52:40 +00:00
|
|
|
defer c.wg.Done()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
var obj *objectSDK.Object
|
2021-04-06 10:56:06 +00:00
|
|
|
for {
|
|
|
|
// Give priority to direct put.
|
|
|
|
select {
|
2022-07-07 12:52:40 +00:00
|
|
|
case obj = <-c.flushCh:
|
|
|
|
case <-c.closeCh:
|
|
|
|
return
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 07:05:40 +00:00
|
|
|
addr := objectCore.AddressOf(obj)
|
2023-06-22 11:55:30 +00:00
|
|
|
err := c.flushObject(context.TODO(), obj, nil, writecache.StorageTypeDB)
|
2023-08-23 07:05:40 +00:00
|
|
|
if err == nil {
|
|
|
|
c.deleteFromDB([]internalKey{addr2key(addr)})
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2023-08-23 07:05:40 +00:00
|
|
|
c.scheduled4FlushMtx.Lock()
|
|
|
|
delete(c.scheduled4Flush, addr)
|
|
|
|
c.scheduled4FlushMtx.Unlock()
|
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-06-22 11:55:30 +00:00
|
|
|
func (c *cache) flushObject(ctx context.Context, obj *objectSDK.Object, data []byte, st writecache.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.FrostFSNodeCantFlushObjectToBlobstor,
|
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
|
|
|
|
2022-10-20 12:15:46 +00:00
|
|
|
_, err = c.metabase.UpdateStorageID(updPrm)
|
2022-10-20 10:40:25 +00:00
|
|
|
if err != nil {
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FrostFSNodeCantUpdateObjectStorageID,
|
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.
|
|
|
|
// Write-cache must be in readonly mode to ensure correctness of an operation and
|
|
|
|
// to prevent interference with background flush workers.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (c *cache) Flush(ctx context.Context, ignoreErrors bool) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "writecache.Flush",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Bool("ignore_errors", ignoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-09-21 06:33:58 +00:00
|
|
|
c.modeMtx.RLock()
|
|
|
|
defer c.modeMtx.RUnlock()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
return c.flush(ctx, ignoreErrors)
|
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 {
|
2023-06-22 11:55:30 +00:00
|
|
|
return c.db.View(func(tx *badger.Txn) error {
|
|
|
|
it := tx.NewIterator(badger.DefaultIteratorOptions)
|
|
|
|
defer it.Close()
|
|
|
|
var key internalKey
|
|
|
|
for it.Rewind(); it.Valid(); it.Next() {
|
|
|
|
if got, want := int(it.Item().KeySize()), len(key); got != want {
|
|
|
|
err := fmt.Errorf("invalid db key len: got %d, want %d", got, want)
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FrostFSNodeCantDecodeObjectAddressFromDB, hex.EncodeToString(it.Item().Key()), 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-06-22 11:55:30 +00:00
|
|
|
if err := it.Item().Value(func(data []byte) error {
|
|
|
|
var obj objectSDK.Object
|
|
|
|
if err := obj.Unmarshal(data); err != nil {
|
|
|
|
copy(key[:], it.Item().Key())
|
2023-08-09 12:53:13 +00:00
|
|
|
c.reportFlushError(logs.FrostFSNodeCantUnmarshalObjectFromDB, key.address().EncodeToString(), metaerr.Wrap(err))
|
2023-06-22 11:55:30 +00:00
|
|
|
if ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2022-09-26 08:54:21 +00:00
|
|
|
}
|
2022-09-21 06:33:58 +00:00
|
|
|
|
2023-06-22 11:55:30 +00:00
|
|
|
return c.flushObject(ctx, &obj, data, writecache.StorageTypeDB)
|
|
|
|
}); err != nil {
|
2022-09-21 06:33:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|