2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
|
|
|
import (
|
2020-11-17 17:39:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor"
|
2020-11-30 14:30:32 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase/v2"
|
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"
|
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
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
writeCache *blobstor.BlobStor
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
type cfg struct {
|
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
|
|
|
|
|
|
|
|
log *logger.Logger
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultCfg() *cfg {
|
|
|
|
return new(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns new Shard instance.
|
|
|
|
func New(opts ...Option) *Shard {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
2020-12-01 07:35:25 +00:00
|
|
|
var writeCache *blobstor.BlobStor
|
|
|
|
|
|
|
|
if c.useWriteCache {
|
|
|
|
writeCache = blobstor.New(
|
|
|
|
blobstor.WithBlobovniczaShallowDepth(0),
|
|
|
|
blobstor.WithBlobovniczaShallowWidth(1),
|
|
|
|
blobstor.WithLogger(c.log),
|
|
|
|
// ? what about path
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
blobStor: blobstor.New(c.blobOpts...),
|
|
|
|
metaBase: meta.New(c.metaOpts...),
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns option to set Shard's logger.
|
|
|
|
func WithLogger(l *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = l
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// hasWriteCache returns bool if write cache exists on shars.
|
|
|
|
func (s Shard) hasWriteCache() bool {
|
|
|
|
return s.cfg.useWriteCache
|
|
|
|
}
|