2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-02-17 12:27:40 +00:00
|
|
|
"context"
|
2021-12-27 18:07:02 +00:00
|
|
|
"sync"
|
2021-02-16 11:34:48 +00:00
|
|
|
"time"
|
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2020-12-08 07:51:34 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2021-04-06 10:56:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache"
|
2021-02-16 11:34:48 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util"
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2021-02-16 11:34:48 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-17 12:23:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Shard represents single shard of NeoFS Local Storage Engine.
|
|
|
|
type Shard struct {
|
|
|
|
*cfg
|
|
|
|
|
2021-08-04 12:04:37 +00:00
|
|
|
gc *gc
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
writeCache writecache.Cache
|
2020-12-01 07:35:25 +00:00
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
blobStor *blobstor.BlobStor
|
|
|
|
|
|
|
|
metaBase *meta.DB
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option represents Shard's constructor option.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
2021-02-17 12:27:40 +00:00
|
|
|
// ExpiredObjectsCallback is a callback handling list of expired objects.
|
2022-01-26 12:11:13 +00:00
|
|
|
type ExpiredObjectsCallback func(context.Context, []*addressSDK.Address)
|
2021-02-17 12:27:40 +00:00
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
type cfg struct {
|
2021-12-27 18:07:02 +00:00
|
|
|
m sync.RWMutex
|
2021-12-27 11:04:07 +00:00
|
|
|
|
2021-09-13 13:56:07 +00:00
|
|
|
refillMetabase bool
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
rmBatchSize int
|
|
|
|
|
2020-11-30 16:55:21 +00:00
|
|
|
useWriteCache bool
|
|
|
|
|
2020-11-19 13:53:45 +00:00
|
|
|
info Info
|
2020-11-17 17:39:43 +00:00
|
|
|
|
|
|
|
blobOpts []blobstor.Option
|
|
|
|
|
|
|
|
metaOpts []meta.Option
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
writeCacheOpts []writecache.Option
|
2020-12-01 09:26:19 +00:00
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
log *logger.Logger
|
2021-02-16 11:34:48 +00:00
|
|
|
|
|
|
|
gcCfg *gcCfg
|
2021-02-17 12:27:40 +00:00
|
|
|
|
|
|
|
expiredTombstonesCallback ExpiredObjectsCallback
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
2021-02-16 11:34:48 +00:00
|
|
|
return &cfg{
|
|
|
|
rmBatchSize: 100,
|
|
|
|
log: zap.L(),
|
|
|
|
gcCfg: defaultGCCfg(),
|
|
|
|
}
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns new Shard instance.
|
|
|
|
func New(opts ...Option) *Shard {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
bs := blobstor.New(c.blobOpts...)
|
|
|
|
mb := meta.New(c.metaOpts...)
|
2020-12-01 07:35:25 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
var writeCache writecache.Cache
|
2020-12-01 07:35:25 +00:00
|
|
|
if c.useWriteCache {
|
2021-04-06 10:56:06 +00:00
|
|
|
writeCache = writecache.New(
|
|
|
|
append(c.writeCacheOpts,
|
|
|
|
writecache.WithBlobstor(bs),
|
|
|
|
writecache.WithMetabase(mb))...)
|
2020-12-01 07:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
s := &Shard{
|
2020-12-01 07:35:25 +00:00
|
|
|
cfg: c,
|
2021-04-06 10:56:06 +00:00
|
|
|
blobStor: bs,
|
|
|
|
metaBase: mb,
|
2020-12-01 07:35:25 +00:00
|
|
|
writeCache: writeCache,
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
2021-12-20 10:03:06 +00:00
|
|
|
|
|
|
|
s.fillInfo()
|
|
|
|
|
|
|
|
return s
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 08:59:05 +00:00
|
|
|
// WithID returns option to set the default shard identifier.
|
2020-11-17 12:23:15 +00:00
|
|
|
func WithID(id *ID) Option {
|
|
|
|
return func(c *cfg) {
|
2020-11-19 13:53:45 +00:00
|
|
|
c.info.ID = id
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
|
|
|
// WithBlobStorOptions returns option to set internal BlobStor options.
|
2020-11-18 11:42:06 +00:00
|
|
|
func WithBlobStorOptions(opts ...blobstor.Option) Option {
|
2020-11-17 17:39:43 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.blobOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMetaBaseOptions returns option to set internal metabase options.
|
2020-11-18 11:42:06 +00:00
|
|
|
func WithMetaBaseOptions(opts ...meta.Option) Option {
|
2020-11-17 17:39:43 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.metaOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// WithWriteCacheOptions returns option to set internal write cache options.
|
2021-04-06 10:56:06 +00:00
|
|
|
func WithWriteCacheOptions(opts ...writecache.Option) Option {
|
2020-12-01 09:26:19 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.writeCacheOpts = opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 17:39:43 +00:00
|
|
|
// WithLogger returns option to set Shard's logger.
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l
|
2021-02-16 11:34:48 +00:00
|
|
|
c.gcCfg.log = l
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 16:55:21 +00:00
|
|
|
|
|
|
|
// WithWriteCache returns option to toggle write cache usage.
|
|
|
|
func WithWriteCache(use bool) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.useWriteCache = use
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 07:35:25 +00:00
|
|
|
|
2020-12-03 09:50:20 +00:00
|
|
|
// hasWriteCache returns bool if write cache exists on shards.
|
2020-12-01 07:35:25 +00:00
|
|
|
func (s Shard) hasWriteCache() bool {
|
|
|
|
return s.cfg.useWriteCache
|
|
|
|
}
|
2021-02-16 11:34:48 +00:00
|
|
|
|
2021-09-13 13:56:07 +00:00
|
|
|
// needRefillMetabase returns true if metabase is needed to be refilled.
|
|
|
|
func (s Shard) needRefillMetabase() bool {
|
|
|
|
return s.cfg.refillMetabase
|
|
|
|
}
|
|
|
|
|
2021-02-16 11:34:48 +00:00
|
|
|
// WithRemoverBatchSize returns option to set batch size
|
|
|
|
// of single removal operation.
|
|
|
|
func WithRemoverBatchSize(sz int) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.rmBatchSize = sz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGCWorkerPoolInitializer returns option to set initializer of
|
|
|
|
// worker pool with specified worker number.
|
|
|
|
func WithGCWorkerPoolInitializer(wpInit func(int) util.WorkerPool) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.gcCfg.workerPoolInit = wpInit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGCEventChannelInitializer returns option to set set initializer of
|
|
|
|
// GC event channel.
|
|
|
|
func WithGCEventChannelInitializer(chInit func() <-chan Event) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.gcCfg.eventChanInit = chInit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithGCRemoverSleepInterval returns option to specify sleep
|
|
|
|
// interval between object remover executions.
|
|
|
|
func WithGCRemoverSleepInterval(dur time.Duration) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.gcCfg.removerInterval = dur
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 12:27:40 +00:00
|
|
|
|
|
|
|
// WithExpiredObjectsCallback returns option to specify callback
|
|
|
|
// of the expired tombstones handler.
|
|
|
|
func WithExpiredObjectsCallback(cb ExpiredObjectsCallback) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.expiredTombstonesCallback = cb
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 13:56:07 +00:00
|
|
|
|
|
|
|
// WithRefillMetabase returns option to set flag to refill the Metabase on Shard's initialization step.
|
|
|
|
func WithRefillMetabase(v bool) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.refillMetabase = v
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 10:03:06 +00:00
|
|
|
|
2021-12-27 11:04:07 +00:00
|
|
|
// WithMode returns option to set shard's mode. Mode must be one of the predefined:
|
|
|
|
// - ModeReadWrite;
|
|
|
|
// - ModeReadOnly.
|
|
|
|
func WithMode(v Mode) Option {
|
|
|
|
return func(c *cfg) {
|
2021-12-27 18:07:02 +00:00
|
|
|
c.info.Mode = v
|
2021-12-27 11:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 10:03:06 +00:00
|
|
|
func (s *Shard) fillInfo() {
|
|
|
|
s.cfg.info.MetaBaseInfo = s.metaBase.DumpInfo()
|
|
|
|
s.cfg.info.BlobStorInfo = s.blobStor.DumpInfo()
|
2021-12-27 18:07:02 +00:00
|
|
|
s.cfg.info.Mode = s.GetMode()
|
2021-12-20 10:03:06 +00:00
|
|
|
|
|
|
|
if s.cfg.useWriteCache {
|
|
|
|
s.cfg.info.WriteCacheInfo = s.writeCache.DumpInfo()
|
|
|
|
}
|
|
|
|
}
|