2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2021-02-17 12:27:40 +00:00
|
|
|
"context"
|
2021-02-16 11:34:48 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-17 12:27:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
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"
|
2020-11-19 12:56:10 +00:00
|
|
|
"go.uber.org/atomic"
|
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
|
|
|
|
|
2020-11-19 12:56:10 +00:00
|
|
|
mode *atomic.Uint32
|
|
|
|
|
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.
|
|
|
|
type ExpiredObjectsCallback func(context.Context, []*object.Address)
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
type cfg struct {
|
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
|
|
|
}
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
return &Shard{
|
2020-12-01 07:35:25 +00:00
|
|
|
cfg: c,
|
|
|
|
mode: atomic.NewUint32(0), // TODO: init with particular mode
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithID returns option to set shard identifier.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 09:26:19 +00:00
|
|
|
// WithMetaBaseOptions returns option to set internal metabase 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
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|