From cc377b34d293fa6c4df02c0cab2786c592f5c036 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 7 Oct 2021 17:19:55 +0300 Subject: [PATCH] [#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 --- cmd/neofs-node/config.go | 2 +- .../blobstor/blobovnicza.go | 16 ++++++++++++ pkg/local_object_storage/blobstor/blobstor.go | 25 +++---------------- .../blobstor/iterate_test.go | 3 +-- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index d8c14fde6..d43e02256 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -385,7 +385,7 @@ func initShardOptions(c *cfg) { shard.WithRefillMetabase(sc.RefillMetabase()), shard.WithBlobStorOptions( blobstor.WithRootPath(blobStorCfg.Path()), - blobstor.WithCompressObjects(blobStorCfg.Compress(), c.log), + blobstor.WithCompressObjects(blobStorCfg.Compress()), blobstor.WithRootPerm(blobStorCfg.Perm()), blobstor.WithShallowDepth(blobStorCfg.ShallowDepth()), blobstor.WithSmallSizeLimit(blobStorCfg.SmallSizeLimit()), diff --git a/pkg/local_object_storage/blobstor/blobovnicza.go b/pkg/local_object_storage/blobstor/blobovnicza.go index 04524d305..355445ba3 100644 --- a/pkg/local_object_storage/blobstor/blobovnicza.go +++ b/pkg/local_object_storage/blobstor/blobovnicza.go @@ -781,6 +781,22 @@ func (b *blobovniczas) updateAndGet(p string, old *uint64) (blobovniczaWithIndex func (b *blobovniczas) init() error { 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 { if err := blz.Init(); err != nil { return fmt.Errorf("could not initialize blobovnicza structure %s: %w", p, err) diff --git a/pkg/local_object_storage/blobstor/blobstor.go b/pkg/local_object_storage/blobstor/blobstor.go index 30ee25d65..395c85a82 100644 --- a/pkg/local_object_storage/blobstor/blobstor.go +++ b/pkg/local_object_storage/blobstor/blobstor.go @@ -26,6 +26,8 @@ type Option func(*cfg) type cfg struct { fsTree fstree.FSTree + compressionEnabled bool + compressor func([]byte) []byte decompressor func([]byte) ([]byte, error) @@ -65,8 +67,6 @@ func defaultCfg() *cfg { RootPath: "./", }, }, - compressor: noOpCompressor, - decompressor: noOpDecompressor, smallSizeLimit: defaultSmallSizeLimit, log: zap.L(), openedCacheSize: defaultOpenedCacheSize, @@ -111,26 +111,9 @@ func WithShallowDepth(depth int) Option { // If compressor (decompressor) creation failed, // the uncompressed option will be used, and the error // is recorded in the provided log. -func WithCompressObjects(comp bool, log *logger.Logger) Option { +func WithCompressObjects(comp bool) Option { return func(c *cfg) { - if 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 + c.compressionEnabled = comp } } diff --git a/pkg/local_object_storage/blobstor/iterate_test.go b/pkg/local_object_storage/blobstor/iterate_test.go index 4f2551826..2da38aa2a 100644 --- a/pkg/local_object_storage/blobstor/iterate_test.go +++ b/pkg/local_object_storage/blobstor/iterate_test.go @@ -8,7 +8,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/object" 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/util/logger/test" "github.com/stretchr/testify/require" ) @@ -19,7 +18,7 @@ func TestIterateObjects(t *testing.T) { // create BlobStor instance blobStor := New( - WithCompressObjects(true, test.NewLogger(false)), + WithCompressObjects(true), WithRootPath(p), WithSmallSizeLimit(smalSz), WithBlobovniczaShallowWidth(1),