From f194c840d76573abc95ecb51af01d448b3d99a4f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 18 Nov 2020 15:27:21 +0300 Subject: [PATCH] [#176] blobstor: Handle error of zstd (de)compressor creation In previous implementation WithCompressObjects returned Option than could panic if zstd (de)compressor creation failed with error. From now errors with (de)compressor creation result in an option without using data compression. In this case, the error is written to the log passed to the option constructor. Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/blobstor/blobstor.go | 32 +++++++++++++++---- pkg/local_object_storage/blobstor/compress.go | 12 +++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/pkg/local_object_storage/blobstor/blobstor.go b/pkg/local_object_storage/blobstor/blobstor.go index ab9eeb7fd3..1edf6bf902 100644 --- a/pkg/local_object_storage/blobstor/blobstor.go +++ b/pkg/local_object_storage/blobstor/blobstor.go @@ -4,6 +4,9 @@ import ( "encoding/hex" "os" "sync" + + "github.com/nspcc-dev/neofs-node/pkg/util/logger" + "go.uber.org/zap" ) // BlobStor represents NeoFS local BLOB storage. @@ -71,15 +74,32 @@ func WithShallowDepth(depth int) Option { // WithCompressObjects returns option to toggle // compression of the stored objects. -func WithCompressObjects(comp bool) Option { +// +// If true, Zstandard algorithm is used for data compression. +// +// 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 { return func(c *cfg) { if comp { - c.compressor = zstdCompressor() - c.decompressor = zstdDecompressor() - } else { - c.compressor = noOpCompressor - c.decompressor = noOpDecompressor + 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 } } diff --git a/pkg/local_object_storage/blobstor/compress.go b/pkg/local_object_storage/blobstor/compress.go index 01eee1006d..414ae35668 100644 --- a/pkg/local_object_storage/blobstor/compress.go +++ b/pkg/local_object_storage/blobstor/compress.go @@ -12,24 +12,24 @@ func noOpDecompressor(data []byte) ([]byte, error) { return data, nil } -func zstdCompressor() func([]byte) []byte { +func zstdCompressor() (func([]byte) []byte, error) { enc, err := zstd.NewWriter(nil) if err != nil { - panic(err) + return nil, err } return func(data []byte) []byte { return enc.EncodeAll(data, make([]byte, 0, len(data))) - } + }, nil } -func zstdDecompressor() func([]byte) ([]byte, error) { +func zstdDecompressor() (func([]byte) ([]byte, error), error) { dec, err := zstd.NewReader(nil) if err != nil { - panic(err) + return nil, err } return func(data []byte) ([]byte, error) { return dec.DecodeAll(data, nil) - } + }, nil }