[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-18 15:27:21 +03:00 committed by Alex Vanin
parent 6f8c45d61b
commit f194c840d7
2 changed files with 32 additions and 12 deletions

View file

@ -4,6 +4,9 @@ import (
"encoding/hex" "encoding/hex"
"os" "os"
"sync" "sync"
"github.com/nspcc-dev/neofs-node/pkg/util/logger"
"go.uber.org/zap"
) )
// BlobStor represents NeoFS local BLOB storage. // BlobStor represents NeoFS local BLOB storage.
@ -71,17 +74,34 @@ func WithShallowDepth(depth int) Option {
// WithCompressObjects returns option to toggle // WithCompressObjects returns option to toggle
// compression of the stored objects. // 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) { return func(c *cfg) {
if comp { if comp {
c.compressor = zstdCompressor() var err error
c.decompressor = zstdDecompressor()
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 { } else {
return
}
}
c.compressor = noOpCompressor c.compressor = noOpCompressor
c.decompressor = noOpDecompressor c.decompressor = noOpDecompressor
} }
} }
}
// WithTreeRootPath returns option to set path to root directory // WithTreeRootPath returns option to set path to root directory
// of the fs tree to write the objects. // of the fs tree to write the objects.

View file

@ -12,24 +12,24 @@ func noOpDecompressor(data []byte) ([]byte, error) {
return data, nil return data, nil
} }
func zstdCompressor() func([]byte) []byte { func zstdCompressor() (func([]byte) []byte, error) {
enc, err := zstd.NewWriter(nil) enc, err := zstd.NewWriter(nil)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return func(data []byte) []byte { return func(data []byte) []byte {
return enc.EncodeAll(data, make([]byte, 0, len(data))) 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) dec, err := zstd.NewReader(nil)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return func(data []byte) ([]byte, error) { return func(data []byte) ([]byte, error) {
return dec.DecodeAll(data, nil) return dec.DecodeAll(data, nil)
} }, nil
} }