2021-04-06 10:56:06 +00:00
|
|
|
package writecache
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2023-03-21 10:38:44 +00:00
|
|
|
"os"
|
2021-04-06 10:56:06 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-06-16 15:21:10 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-02-15 14:53:42 +00:00
|
|
|
"go.uber.org/atomic"
|
2021-04-06 10:56:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
// Info groups the information about write-cache.
|
|
|
|
type Info struct {
|
|
|
|
// Full path to the write-cache.
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// Cache represents write-cache for objects.
|
|
|
|
type Cache interface {
|
2023-03-13 11:37:35 +00:00
|
|
|
Get(ctx context.Context, address oid.Address) (*object.Object, error)
|
|
|
|
Head(context.Context, oid.Address) (*object.Object, error)
|
2022-10-28 10:55:21 +00:00
|
|
|
// Delete removes object referenced by the given oid.Address from the
|
|
|
|
// Cache. Returns any error encountered that prevented the object to be
|
|
|
|
// removed.
|
|
|
|
//
|
|
|
|
// Returns apistatus.ObjectNotFound if object is missing in the Cache.
|
|
|
|
// Returns ErrReadOnly if the Cache is currently in the read-only mode.
|
2023-04-12 14:01:29 +00:00
|
|
|
Delete(context.Context, oid.Address) error
|
2022-05-20 15:43:05 +00:00
|
|
|
Iterate(IterationPrm) error
|
2023-04-12 14:01:29 +00:00
|
|
|
Put(context.Context, common.PutPrm) (common.PutRes, error)
|
2022-07-05 04:55:46 +00:00
|
|
|
SetMode(mode.Mode) error
|
2022-09-28 07:41:01 +00:00
|
|
|
SetLogger(*logger.Logger)
|
2021-12-20 10:03:06 +00:00
|
|
|
DumpInfo() Info
|
2023-04-12 14:01:29 +00:00
|
|
|
Flush(context.Context, bool) error
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
Init() error
|
2022-06-28 13:42:50 +00:00
|
|
|
Open(readOnly bool) error
|
2021-04-06 10:56:06 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
options
|
|
|
|
|
2022-07-07 12:52:40 +00:00
|
|
|
// mtx protects statistics, counters and compressFlags.
|
2021-04-06 10:56:06 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
mode mode.Mode
|
|
|
|
initialized atomic.Bool
|
|
|
|
stopInitCh chan struct{} // used to sync initWG initialisation routines and _only_ them
|
|
|
|
initWG sync.WaitGroup // for initialisation routines only
|
|
|
|
modeMtx sync.RWMutex
|
2022-01-18 12:47:16 +00:00
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
// compressFlags maps address of a big object to boolean value indicating
|
|
|
|
// whether object should be compressed.
|
|
|
|
compressFlags map[string]struct{}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
// flushCh is a channel with objects to flush.
|
|
|
|
flushCh chan *object.Object
|
2023-02-15 14:53:42 +00:00
|
|
|
// closeCh is close channel, protected by modeMtx.
|
2021-04-06 10:56:06 +00:00
|
|
|
closeCh chan struct{}
|
2022-07-07 12:52:40 +00:00
|
|
|
// wg is a wait group for flush workers.
|
|
|
|
wg sync.WaitGroup
|
2021-04-06 10:56:06 +00:00
|
|
|
// store contains underlying database.
|
|
|
|
store
|
|
|
|
// fsTree contains big files stored directly on file-system.
|
|
|
|
fsTree *fstree.FSTree
|
|
|
|
}
|
|
|
|
|
2022-12-20 12:29:05 +00:00
|
|
|
// wcStorageType is used for write-cache operations logging.
|
|
|
|
const wcStorageType = "write-cache"
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
type objectInfo struct {
|
|
|
|
addr string
|
|
|
|
data []byte
|
|
|
|
obj *object.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-09-01 06:08:28 +00:00
|
|
|
defaultMaxObjectSize = 64 * 1024 * 1024 // 64 MiB
|
|
|
|
defaultSmallObjectSize = 32 * 1024 // 32 KiB
|
|
|
|
defaultMaxCacheSize = 1 << 30 // 1 GiB
|
2021-04-06 10:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultBucket = []byte{0}
|
|
|
|
)
|
|
|
|
|
|
|
|
// New creates new writecache instance.
|
|
|
|
func New(opts ...Option) Cache {
|
|
|
|
c := &cache{
|
2023-02-15 14:53:42 +00:00
|
|
|
flushCh: make(chan *object.Object),
|
|
|
|
mode: mode.ReadWrite,
|
|
|
|
stopInitCh: make(chan struct{}),
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2022-01-11 11:33:04 +00:00
|
|
|
compressFlags: make(map[string]struct{}),
|
2021-04-06 10:56:06 +00:00
|
|
|
options: options{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zap.NewNop()},
|
2022-09-01 06:08:28 +00:00
|
|
|
maxObjectSize: defaultMaxObjectSize,
|
|
|
|
smallObjectSize: defaultSmallObjectSize,
|
2022-05-31 11:15:31 +00:00
|
|
|
workersCount: defaultFlushWorkersCount,
|
2022-09-01 06:08:28 +00:00
|
|
|
maxCacheSize: defaultMaxCacheSize,
|
2022-06-16 15:21:10 +00:00
|
|
|
maxBatchSize: bbolt.DefaultMaxBatchSize,
|
|
|
|
maxBatchDelay: bbolt.DefaultMaxBatchDelay,
|
2023-03-21 10:38:44 +00:00
|
|
|
openFile: os.OpenFile,
|
2021-04-06 10:56:06 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c.options)
|
|
|
|
}
|
|
|
|
|
2022-09-01 06:33:56 +00:00
|
|
|
// Make the LRU cache contain which take approximately 3/4 of the maximum space.
|
|
|
|
// Assume small and big objects are stored in 50-50 proportion.
|
|
|
|
c.maxFlushedMarksCount = int(c.maxCacheSize/c.maxObjectSize+c.maxCacheSize/c.smallObjectSize) / 2 * 3 / 4
|
|
|
|
// Trigger the removal when the cache is 7/8 full, so that new items can still arrive.
|
|
|
|
c.maxRemoveBatchSize = c.maxFlushedMarksCount / 8
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-07-19 11:27:12 +00:00
|
|
|
// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
|
2022-09-28 07:41:01 +00:00
|
|
|
func (c *cache) SetLogger(l *logger.Logger) {
|
2022-07-19 11:27:12 +00:00
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
func (c *cache) DumpInfo() Info {
|
|
|
|
return Info{
|
|
|
|
Path: c.path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
// Open opens and initializes database. Reads object counters from the ObjectCounters instance.
|
2022-06-28 13:42:50 +00:00
|
|
|
func (c *cache) Open(readOnly bool) error {
|
|
|
|
err := c.openStore(readOnly)
|
2021-09-08 09:32:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-22 11:40:03 +00:00
|
|
|
// Opening after Close is done during maintenance mode,
|
|
|
|
// thus we need to create a channel here.
|
|
|
|
c.closeCh = make(chan struct{})
|
|
|
|
|
2022-09-01 05:56:21 +00:00
|
|
|
return c.initCounters()
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init runs necessary services.
|
|
|
|
func (c *cache) Init() error {
|
2023-04-12 14:01:29 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(context.TODO(), "writecache.Init")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
c.initFlushMarks(ctx)
|
2022-07-07 12:52:40 +00:00
|
|
|
c.runFlushLoop()
|
2021-04-06 10:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:32:20 +00:00
|
|
|
// Close closes db connection and stops services. Executes ObjectCounters.FlushAndClose op.
|
2021-04-06 10:56:06 +00:00
|
|
|
func (c *cache) Close() error {
|
2023-02-15 14:53:42 +00:00
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
|
|
|
|
2022-01-18 14:27:07 +00:00
|
|
|
// Finish all in-progress operations.
|
2023-04-12 14:01:29 +00:00
|
|
|
if err := c.setMode(context.TODO(), mode.ReadOnly); err != nil {
|
2022-07-05 04:55:46 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-18 14:27:07 +00:00
|
|
|
|
2022-08-22 11:40:03 +00:00
|
|
|
if c.closeCh != nil {
|
|
|
|
close(c.closeCh)
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
c.wg.Wait()
|
2022-08-22 11:40:03 +00:00
|
|
|
if c.closeCh != nil {
|
|
|
|
c.closeCh = nil
|
|
|
|
}
|
2022-07-07 12:52:40 +00:00
|
|
|
|
2023-02-15 14:53:42 +00:00
|
|
|
c.initialized.Store(false)
|
|
|
|
|
2022-08-22 11:40:03 +00:00
|
|
|
var err error
|
2022-07-05 04:55:46 +00:00
|
|
|
if c.db != nil {
|
2022-08-22 11:40:03 +00:00
|
|
|
err = c.db.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.db = nil
|
|
|
|
}
|
2022-07-05 04:55:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|