2023-12-22 09:58:20 +00:00
|
|
|
package writecache
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-31 16:26:47 +00:00
|
|
|
"context"
|
2023-06-22 11:55:30 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cache struct {
|
|
|
|
options
|
|
|
|
|
|
|
|
// mtx protects statistics, counters and compressFlags.
|
|
|
|
mtx sync.RWMutex
|
|
|
|
|
|
|
|
mode mode.Mode
|
|
|
|
modeMtx sync.RWMutex
|
|
|
|
|
|
|
|
// compressFlags maps address of a big object to boolean value indicating
|
|
|
|
// whether object should be compressed.
|
|
|
|
compressFlags map[string]struct{}
|
|
|
|
|
|
|
|
// flushCh is a channel with objects to flush.
|
2023-10-06 07:22:35 +00:00
|
|
|
flushCh chan objectInfo
|
2023-09-19 05:46:19 +00:00
|
|
|
// cancel is cancel function, protected by modeMtx in Close.
|
|
|
|
cancel func()
|
2023-06-22 11:55:30 +00:00
|
|
|
// wg is a wait group for flush workers.
|
|
|
|
wg sync.WaitGroup
|
|
|
|
// store contains underlying database.
|
|
|
|
store
|
|
|
|
// fsTree contains big files stored directly on file-system.
|
|
|
|
fsTree *fstree.FSTree
|
|
|
|
}
|
|
|
|
|
|
|
|
// wcStorageType is used for write-cache operations logging.
|
|
|
|
const wcStorageType = "write-cache"
|
|
|
|
|
|
|
|
type objectInfo struct {
|
|
|
|
addr string
|
|
|
|
data []byte
|
|
|
|
obj *objectSDK.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultMaxObjectSize = 64 * 1024 * 1024 // 64 MiB
|
|
|
|
defaultSmallObjectSize = 32 * 1024 // 32 KiB
|
|
|
|
defaultMaxCacheSize = 1 << 30 // 1 GiB
|
|
|
|
)
|
|
|
|
|
2023-10-31 11:56:55 +00:00
|
|
|
var defaultBucket = []byte{0}
|
2023-06-22 11:55:30 +00:00
|
|
|
|
|
|
|
// New creates new writecache instance.
|
2023-12-22 09:58:20 +00:00
|
|
|
func New(opts ...Option) Cache {
|
2023-06-22 11:55:30 +00:00
|
|
|
c := &cache{
|
2023-10-06 07:22:35 +00:00
|
|
|
flushCh: make(chan objectInfo),
|
2023-06-22 11:55:30 +00:00
|
|
|
mode: mode.ReadWrite,
|
|
|
|
|
|
|
|
compressFlags: make(map[string]struct{}),
|
|
|
|
options: options{
|
|
|
|
log: &logger.Logger{Logger: zap.NewNop()},
|
|
|
|
maxObjectSize: defaultMaxObjectSize,
|
|
|
|
smallObjectSize: defaultSmallObjectSize,
|
|
|
|
workersCount: defaultFlushWorkersCount,
|
|
|
|
maxCacheSize: defaultMaxCacheSize,
|
|
|
|
maxBatchSize: bbolt.DefaultMaxBatchSize,
|
|
|
|
maxBatchDelay: bbolt.DefaultMaxBatchDelay,
|
|
|
|
openFile: os.OpenFile,
|
2023-12-22 09:58:20 +00:00
|
|
|
metrics: DefaultMetrics(),
|
2023-06-22 11:55:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](&c.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger sets logger. It is used after the shard ID was generated to use it in logs.
|
|
|
|
func (c *cache) SetLogger(l *logger.Logger) {
|
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
|
2023-12-22 09:58:20 +00:00
|
|
|
func (c *cache) DumpInfo() Info {
|
|
|
|
return Info{
|
2023-06-22 11:55:30 +00:00
|
|
|
Path: c.path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens and initializes database. Reads object counters from the ObjectCounters instance.
|
2024-02-09 06:17:17 +00:00
|
|
|
func (c *cache) Open(_ context.Context, mode mode.Mode) error {
|
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
|
|
|
c.mode = mode
|
|
|
|
if mode.NoMetabase() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := c.openStore(mode.ReadOnly())
|
2023-06-22 11:55:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return metaerr.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2023-08-11 08:32:43 +00:00
|
|
|
return metaerr.Wrap(c.initCounters())
|
2023-06-22 11:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init runs necessary services.
|
|
|
|
func (c *cache) Init() error {
|
|
|
|
c.metrics.SetMode(c.mode)
|
2023-09-19 05:46:19 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
c.cancel = cancel
|
|
|
|
c.runFlushLoop(ctx)
|
2023-06-22 11:55:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes db connection and stops services. Executes ObjectCounters.FlushAndClose op.
|
|
|
|
func (c *cache) Close() error {
|
|
|
|
// We cannot lock mutex for the whole operation duration
|
|
|
|
// because it is taken by some background workers, so `wg.Wait()` is done without modeMtx.
|
|
|
|
c.modeMtx.Lock()
|
2023-09-19 05:46:19 +00:00
|
|
|
if c.cancel != nil {
|
|
|
|
c.cancel()
|
|
|
|
c.cancel = nil
|
2023-06-22 11:55:30 +00:00
|
|
|
}
|
|
|
|
c.mode = mode.DegradedReadOnly // prevent new operations from being processed
|
|
|
|
c.modeMtx.Unlock()
|
|
|
|
|
|
|
|
c.wg.Wait()
|
|
|
|
|
|
|
|
c.modeMtx.Lock()
|
|
|
|
defer c.modeMtx.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if c.db != nil {
|
|
|
|
err = c.db.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.db = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.metrics.Close()
|
|
|
|
return nil
|
|
|
|
}
|