From b127607ac68ad6f430faf084be87109af89c9043 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 17 Nov 2020 19:29:00 +0300 Subject: [PATCH] [#176] localstore: Implement primary BlobStor Implement primary local BLOB storage based on filesystem tree. Signed-off-by: Leonard Lyubich --- go.mod | 1 + go.sum | Bin 59822 -> 60001 bytes pkg/local_object_storage/blobstor/blobstor.go | 100 ++++++++++++++++++ pkg/local_object_storage/blobstor/compress.go | 35 ++++++ pkg/local_object_storage/blobstor/delete.go | 54 ++++++++++ pkg/local_object_storage/blobstor/fstree.go | 49 +++++++++ pkg/local_object_storage/blobstor/get.go | 83 +++++++++++++++ pkg/local_object_storage/blobstor/getrange.go | 82 ++++++++++++++ pkg/local_object_storage/blobstor/put.go | 61 +++++++++++ 9 files changed, 465 insertions(+) create mode 100644 pkg/local_object_storage/blobstor/blobstor.go create mode 100644 pkg/local_object_storage/blobstor/compress.go create mode 100644 pkg/local_object_storage/blobstor/delete.go create mode 100644 pkg/local_object_storage/blobstor/fstree.go create mode 100644 pkg/local_object_storage/blobstor/get.go create mode 100644 pkg/local_object_storage/blobstor/getrange.go create mode 100644 pkg/local_object_storage/blobstor/put.go diff --git a/go.mod b/go.mod index 701c683bf..bdff4ed39 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.1.1 github.com/hashicorp/golang-lru v0.5.4 + github.com/klauspost/compress v1.11.3 github.com/mitchellh/go-homedir v1.1.0 github.com/mr-tron/base58 v1.1.3 github.com/multiformats/go-multiaddr v0.2.0 diff --git a/go.sum b/go.sum index 38bd80187c5dff57e2c62b9bd874d5b7183ed8c2..6ffdced8960a196efb79dd32c54f2715ab5ec642 100644 GIT binary patch delta 136 zcmZ2?nfc)r<_!-1ll%R+jSUU;j1@8rtx}v!obn7x{EW*}oT~yW@`5dj49v2_%uNdY z^g|re5n>9-v@aB diff --git a/pkg/local_object_storage/blobstor/blobstor.go b/pkg/local_object_storage/blobstor/blobstor.go new file mode 100644 index 000000000..e0d4dc1e3 --- /dev/null +++ b/pkg/local_object_storage/blobstor/blobstor.go @@ -0,0 +1,100 @@ +package blobstor + +import ( + "encoding/hex" + "os" + "sync" +) + +// BlobStor represents NeoFS local BLOB storage. +type BlobStor struct { + *cfg + + mtx *sync.RWMutex +} + +// Option represents BlobStor's constructor option. +type Option func(*cfg) + +type cfg struct { + fsTree fsTree + + compressor func([]byte) []byte + + decompressor func([]byte) ([]byte, error) +} + +const ( + defaultShallowDepth = 4 + defaultPerm = 0700 +) + +func defaultCfg() *cfg { + return &cfg{ + fsTree: fsTree{ + depth: defaultShallowDepth, + dirNameLen: hex.EncodedLen(dirNameLen), + perm: defaultPerm, + rootDir: "./", + }, + compressor: noOpCompressor, + decompressor: noOpDecompressor, + } +} + +// New creates, initializes and returns new BlobStor instance. +func New(opts ...Option) *BlobStor { + c := defaultCfg() + + for i := range opts { + opts[i](c) + } + + return &BlobStor{ + cfg: c, + } +} + +// WithShallowDepth returns option to set the +// depth of the object file subdirectory tree. +// +// Depth is reduced to maximum value in case of overflow. +func WithShallowDepth(depth int) Option { + return func(c *cfg) { + if depth <= maxDepth { + depth = maxDepth + } + + c.fsTree.depth = depth + } +} + +// WithCompressObjects returns option to toggle +// compression of the stored objects. +func WithCompressObjects(comp bool) Option { + return func(c *cfg) { + if comp { + c.compressor = zstdCompressor() + c.decompressor = zstdDecompressor() + } else { + c.compressor = noOpCompressor + c.decompressor = noOpDecompressor + } + } +} + +// WithTreeRootPath returns option to set path to root directory +// of the fs tree to write the objects. +func WithTreeRootPath(rootDir string) Option { + return func(c *cfg) { + c.fsTree.rootDir = rootDir + } +} + +// WithTreeRootPerm returns option to set permission +// bits of the fs tree. +func WithTreeRootPerm(perm os.FileMode) Option { + return func(c *cfg) { + c.fsTree.perm = perm + } +} diff --git a/pkg/local_object_storage/blobstor/compress.go b/pkg/local_object_storage/blobstor/compress.go new file mode 100644 index 000000000..01eee1006 --- /dev/null +++ b/pkg/local_object_storage/blobstor/compress.go @@ -0,0 +1,35 @@ +package blobstor + +import ( + "github.com/klauspost/compress/zstd" +) + +func noOpCompressor(data []byte) []byte { + return data +} + +func noOpDecompressor(data []byte) ([]byte, error) { + return data, nil +} + +func zstdCompressor() func([]byte) []byte { + enc, err := zstd.NewWriter(nil) + if err != nil { + panic(err) + } + + return func(data []byte) []byte { + return enc.EncodeAll(data, make([]byte, 0, len(data))) + } +} + +func zstdDecompressor() func([]byte) ([]byte, error) { + dec, err := zstd.NewReader(nil) + if err != nil { + panic(err) + } + + return func(data []byte) ([]byte, error) { + return dec.DecodeAll(data, nil) + } +} diff --git a/pkg/local_object_storage/blobstor/delete.go b/pkg/local_object_storage/blobstor/delete.go new file mode 100644 index 000000000..e219d62dd --- /dev/null +++ b/pkg/local_object_storage/blobstor/delete.go @@ -0,0 +1,54 @@ +package blobstor + +import ( + "os" + + "github.com/nspcc-dev/neofs-api-go/pkg/object" + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/pkg/errors" +) + +// DeletePrm groups the parameters of Delete operation. +type DeletePrm struct { + addr *objectSDK.Address +} + +// DeleteRes groups resulting values of Delete operation. +type DeleteRes struct{} + +// WithAddress is a Delete option to set the address of the object to delete. +// +// Option is required. +func (p *DeletePrm) WithAddress(addr *objectSDK.Address) *DeletePrm { + if p != nil { + p.addr = addr + } + + return p +} + +// Delete removes object from BLOB storage. +// +// Returns any error encountered that did not allow +// to completely remove the object. +func (b *BlobStor) Delete(prm *DeletePrm) (*DeleteRes, error) { + b.mtx.Lock() + defer b.mtx.Unlock() + + err := b.fsTree.delete(prm.addr) + if errors.Is(err, errFileNotFound) { + err = nil + } + + return nil, err +} + +func (t *fsTree) delete(addr *object.Address) error { + p := t.treePath(addr) + + if _, err := os.Stat(p); os.IsNotExist(err) { + return errFileNotFound + } + + return os.Remove(p) +} diff --git a/pkg/local_object_storage/blobstor/fstree.go b/pkg/local_object_storage/blobstor/fstree.go new file mode 100644 index 000000000..29dd6048c --- /dev/null +++ b/pkg/local_object_storage/blobstor/fstree.go @@ -0,0 +1,49 @@ +package blobstor + +import ( + "crypto/sha256" + "encoding/hex" + "errors" + "os" + "path" + + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" +) + +type fsTree struct { + depth int + + dirNameLen int + + perm os.FileMode + + rootDir string +} + +const dirNameLen = 2 // in bytes + +var maxDepth = (hex.EncodedLen(sha256.Size) - 1) / dirNameLen + +var errFileNotFound = errors.New("file not found") + +func stringifyAddress(addr *objectSDK.Address) string { + h := sha256.Sum256([]byte(addr.String())) + + return hex.EncodeToString(h[:]) +} + +func (t *fsTree) treePath(addr *objectSDK.Address) string { + sAddr := stringifyAddress(addr) + + dirs := make([]string, 0, t.depth+1+1) // 1 for root, 1 for file + dirs = append(dirs, t.rootDir) + + for i := 0; i < t.depth; i++ { + dirs = append(dirs, sAddr[:t.dirNameLen]) + sAddr = sAddr[t.dirNameLen:] + } + + dirs = append(dirs, sAddr) + + return path.Join(dirs...) +} diff --git a/pkg/local_object_storage/blobstor/get.go b/pkg/local_object_storage/blobstor/get.go new file mode 100644 index 000000000..94826b50f --- /dev/null +++ b/pkg/local_object_storage/blobstor/get.go @@ -0,0 +1,83 @@ +package blobstor + +import ( + "io/ioutil" + "os" + + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/pkg/errors" +) + +// GetPrm groups the parameters of Get operation. +type GetPrm struct { + addr *objectSDK.Address +} + +// GetRes groups resulting values of Get operation. +type GetRes struct { + obj *object.Object +} + +// ErrObjectNotFound is returns on read operations requested on a missing object. +var ErrObjectNotFound = errors.New("object not found") + +// WithAddress is a Get option to set the address of the requested object. +// +// Option is required. +func (p *GetPrm) WithAddress(addr *objectSDK.Address) *GetPrm { + if p != nil { + p.addr = addr + } + + return p +} + +// Object returns the requested object. +func (r *GetRes) Object() *object.Object { + return r.obj +} + +// Get reads the object from BLOB storage. +// +// Returns any error encountered that +// did not allow to completely read the object part. +func (b *BlobStor) Get(prm *GetPrm) (*GetRes, error) { + b.mtx.RLock() + defer b.mtx.RUnlock() + + // get compressed object data + data, err := b.fsTree.get(prm.addr) + if err != nil { + if errors.Is(err, errFileNotFound) { + return nil, ErrObjectNotFound + } + + return nil, errors.Wrap(err, "could not read object from fs tree") + } + + data, err = b.decompressor(data) + if err != nil { + return nil, errors.Wrap(err, "could not decompress object data") + } + + // unmarshal the object + obj := object.New() + if err := obj.Unmarshal(data); err != nil { + return nil, errors.Wrap(err, "could not unmarshal the object") + } + + return &GetRes{ + obj: obj, + }, nil +} + +func (t *fsTree) get(addr *objectSDK.Address) ([]byte, error) { + p := t.treePath(addr) + + if _, err := os.Stat(p); os.IsNotExist(err) { + return nil, errFileNotFound + } + + return ioutil.ReadFile(p) +} diff --git a/pkg/local_object_storage/blobstor/getrange.go b/pkg/local_object_storage/blobstor/getrange.go new file mode 100644 index 000000000..021608eff --- /dev/null +++ b/pkg/local_object_storage/blobstor/getrange.go @@ -0,0 +1,82 @@ +package blobstor + +import ( + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/pkg/errors" +) + +// GetRangePrm groups the parameters of GetRange operation. +type GetRangePrm struct { + off, ln uint64 + + addr *objectSDK.Address +} + +// GetRangeRes groups resulting values of GetRange operation. +type GetRangeRes struct { + rngData []byte +} + +// WithAddress is a GetRange option to set the address of the requested object. +// +// Option is required. +func (p *GetRangePrm) WithAddress(addr *objectSDK.Address) *GetRangePrm { + if p != nil { + p.addr = addr + } + + return p +} + +// WithPayloadRange is a GetRange option to set range of requested payload data. +// +// Option is required. +func (p *GetRangePrm) WithPayloadRange(off, ln uint64) *GetRangePrm { + if p != nil { + p.off, p.ln = off, ln + } + + return p +} + +// RangeData returns data of the requested payload range. +func (r *GetRangeRes) RangeData() []byte { + return r.rngData +} + +// GetRange reads data of object payload range from BLOB storage. +// +// Returns any error encountered that +// did not allow to completely read the object payload range. +func (b *BlobStor) GetRange(prm *GetRangePrm) (*GetRangeRes, error) { + b.mtx.RLock() + defer b.mtx.RUnlock() + + // get compressed object data + data, err := b.fsTree.get(prm.addr) + if err != nil { + return nil, errors.Wrap(err, "could not read object from fs tree") + } + + data, err = b.decompressor(data) + if err != nil { + return nil, errors.Wrap(err, "could not decompress object data") + } + + // unmarshal the object + obj := object.New() + if err := obj.Unmarshal(data); err != nil { + return nil, errors.Wrap(err, "could not unmarshal the object") + } + + payload := obj.Payload() + if pLen := uint64(len(payload)); pLen < prm.ln+prm.off { + return nil, errors.Errorf("range is out-of-bounds (payload %d, off %d, ln %d)", + pLen, prm.off, prm.ln) + } + + return &GetRangeRes{ + rngData: payload[prm.off : prm.off+prm.ln], + }, nil +} diff --git a/pkg/local_object_storage/blobstor/put.go b/pkg/local_object_storage/blobstor/put.go new file mode 100644 index 000000000..112988bda --- /dev/null +++ b/pkg/local_object_storage/blobstor/put.go @@ -0,0 +1,61 @@ +package blobstor + +import ( + "io/ioutil" + "os" + "path" + + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/pkg/errors" +) + +// PutPrm groups the parameters of Put operation. +type PutPrm struct { + obj *object.Object +} + +// PutRes groups resulting values of Put operation. +type PutRes struct{} + +// WithObject is a Put option to set object to save. +// +// Option is required. +func (p *PutPrm) WithObject(obj *object.Object) *PutPrm { + if p != nil { + p.obj = obj + } + + return p +} + +// Put saves the object in BLOB storage. +// +// Returns any error encountered that +// did not allow to completely save the object. +func (b *BlobStor) Put(prm *PutPrm) (*PutRes, error) { + b.mtx.Lock() + defer b.mtx.Unlock() + + // marshal object + data, err := prm.obj.Marshal() + if err != nil { + return nil, errors.Wrap(err, "could not marshal the object") + } + + // compress object data + data = b.compressor(data) + + // save object in fs tree + return nil, b.fsTree.put(prm.obj.Address(), data) +} + +func (t *fsTree) put(addr *objectSDK.Address, data []byte) error { + p := t.treePath(addr) + + if err := os.MkdirAll(path.Dir(p), t.perm); err != nil { + return err + } + + return ioutil.WriteFile(p, data, t.perm) +}