[#868] blobstor: initialize (de-)compressors in Init

Do not log in options constructors. Also failure to
initialize compression module (possibly due to invalid options) is
certainly an error deserving proper treatment.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-07 17:19:55 +03:00 committed by Leonard Lyubich
parent fa1dc31320
commit cc377b34d2
4 changed files with 22 additions and 24 deletions

View file

@ -385,7 +385,7 @@ func initShardOptions(c *cfg) {
shard.WithRefillMetabase(sc.RefillMetabase()), shard.WithRefillMetabase(sc.RefillMetabase()),
shard.WithBlobStorOptions( shard.WithBlobStorOptions(
blobstor.WithRootPath(blobStorCfg.Path()), blobstor.WithRootPath(blobStorCfg.Path()),
blobstor.WithCompressObjects(blobStorCfg.Compress(), c.log), blobstor.WithCompressObjects(blobStorCfg.Compress()),
blobstor.WithRootPerm(blobStorCfg.Perm()), blobstor.WithRootPerm(blobStorCfg.Perm()),
blobstor.WithShallowDepth(blobStorCfg.ShallowDepth()), blobstor.WithShallowDepth(blobStorCfg.ShallowDepth()),
blobstor.WithSmallSizeLimit(blobStorCfg.SmallSizeLimit()), blobstor.WithSmallSizeLimit(blobStorCfg.SmallSizeLimit()),

View file

@ -781,6 +781,22 @@ func (b *blobovniczas) updateAndGet(p string, old *uint64) (blobovniczaWithIndex
func (b *blobovniczas) init() error { func (b *blobovniczas) init() error {
b.log.Debug("initializing Blobovnicza's") b.log.Debug("initializing Blobovnicza's")
if b.compressionEnabled {
zstdC, err := zstdCompressor()
if err != nil {
return fmt.Errorf("could not create zstd compressor: %v", err)
}
zstdD, err := zstdDecompressor()
if err != nil {
return fmt.Errorf("could not create zstd decompressor: %v", err)
}
b.compressor = zstdC
b.decompressor = zstdD
} else {
b.compressor = noOpCompressor
b.decompressor = noOpDecompressor
}
return b.iterateBlobovniczas(func(p string, blz *blobovnicza.Blobovnicza) error { return b.iterateBlobovniczas(func(p string, blz *blobovnicza.Blobovnicza) error {
if err := blz.Init(); err != nil { if err := blz.Init(); err != nil {
return fmt.Errorf("could not initialize blobovnicza structure %s: %w", p, err) return fmt.Errorf("could not initialize blobovnicza structure %s: %w", p, err)

View file

@ -26,6 +26,8 @@ type Option func(*cfg)
type cfg struct { type cfg struct {
fsTree fstree.FSTree fsTree fstree.FSTree
compressionEnabled bool
compressor func([]byte) []byte compressor func([]byte) []byte
decompressor func([]byte) ([]byte, error) decompressor func([]byte) ([]byte, error)
@ -65,8 +67,6 @@ func defaultCfg() *cfg {
RootPath: "./", RootPath: "./",
}, },
}, },
compressor: noOpCompressor,
decompressor: noOpDecompressor,
smallSizeLimit: defaultSmallSizeLimit, smallSizeLimit: defaultSmallSizeLimit,
log: zap.L(), log: zap.L(),
openedCacheSize: defaultOpenedCacheSize, openedCacheSize: defaultOpenedCacheSize,
@ -111,26 +111,9 @@ func WithShallowDepth(depth int) Option {
// If compressor (decompressor) creation failed, // If compressor (decompressor) creation failed,
// the uncompressed option will be used, and the error // the uncompressed option will be used, and the error
// is recorded in the provided log. // is recorded in the provided log.
func WithCompressObjects(comp bool, log *logger.Logger) Option { func WithCompressObjects(comp bool) Option {
return func(c *cfg) { return func(c *cfg) {
if comp { c.compressionEnabled = comp
var err error
if c.compressor, err = zstdCompressor(); err != nil {
log.Error("could not create zstd compressor",
zap.String("error", err.Error()),
)
} else if c.decompressor, err = zstdDecompressor(); err != nil {
log.Error("could not create zstd decompressor",
zap.String("error", err.Error()),
)
} else {
return
}
}
c.compressor = noOpCompressor
c.decompressor = noOpDecompressor
} }
} }

View file

@ -8,7 +8,6 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
objecttest "github.com/nspcc-dev/neofs-api-go/pkg/object/test" objecttest "github.com/nspcc-dev/neofs-api-go/pkg/object/test"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza"
"github.com/nspcc-dev/neofs-node/pkg/util/logger/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -19,7 +18,7 @@ func TestIterateObjects(t *testing.T) {
// create BlobStor instance // create BlobStor instance
blobStor := New( blobStor := New(
WithCompressObjects(true, test.NewLogger(false)), WithCompressObjects(true),
WithRootPath(p), WithRootPath(p),
WithSmallSizeLimit(smalSz), WithSmallSizeLimit(smalSz),
WithBlobovniczaShallowWidth(1), WithBlobovniczaShallowWidth(1),