From fcdbf5e50929cbb82f61a927c2ebc7d53fe8be3c Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 26 Oct 2022 15:23:12 +0300 Subject: [PATCH] [#1969] local_object_storage: Add a type for logical errors All logic errors are wrapped in `logicerr.Logical` type and do not affect shard error counter. Signed-off-by: Evgenii Stratonikov --- pkg/core/object/errors.go | 8 ++- .../blobstor/blobovniczatree/delete.go | 9 +-- .../blobstor/blobovniczatree/get.go | 9 +-- .../blobstor/blobovniczatree/get_range.go | 11 ++-- .../blobstor/common/errors.go | 10 +++- .../blobstor/fstree/fstree.go | 11 ++-- pkg/local_object_storage/blobstor/get.go | 4 +- .../blobstor/get_range.go | 4 +- pkg/local_object_storage/blobstor/put.go | 3 +- pkg/local_object_storage/engine/container.go | 11 +--- pkg/local_object_storage/engine/delete.go | 12 +--- pkg/local_object_storage/engine/engine.go | 13 +++++ pkg/local_object_storage/engine/exists.go | 6 +- pkg/local_object_storage/engine/get.go | 5 +- pkg/local_object_storage/engine/head.go | 5 +- pkg/local_object_storage/engine/head_test.go | 4 +- pkg/local_object_storage/engine/lock.go | 7 ++- pkg/local_object_storage/engine/range.go | 5 +- pkg/local_object_storage/engine/select.go | 6 +- pkg/local_object_storage/engine/shards.go | 3 +- pkg/local_object_storage/metabase/control.go | 3 +- pkg/local_object_storage/metabase/exists.go | 13 ++--- .../metabase/exists_test.go | 10 ++-- pkg/local_object_storage/metabase/get.go | 27 +++------ pkg/local_object_storage/metabase/get_test.go | 7 +-- pkg/local_object_storage/metabase/inhume.go | 3 +- .../metabase/iterators.go | 3 +- pkg/local_object_storage/metabase/list.go | 3 +- pkg/local_object_storage/metabase/lock.go | 3 +- pkg/local_object_storage/metabase/version.go | 3 +- .../pilorama/interface.go | 3 +- pkg/local_object_storage/shard/dump.go | 3 +- pkg/local_object_storage/shard/get.go | 5 +- pkg/local_object_storage/shard/get_test.go | 8 +-- pkg/local_object_storage/shard/mode.go | 5 +- pkg/local_object_storage/shard/range.go | 3 +- pkg/local_object_storage/shard/restore.go | 3 +- pkg/local_object_storage/shard/tree.go | 3 +- .../util/logicerr/error.go | 21 +++++++ .../util/logicerr/error_test.go | 58 +++++++++++++++++++ pkg/local_object_storage/writecache/get.go | 9 +-- pkg/local_object_storage/writecache/mode.go | 3 +- 42 files changed, 206 insertions(+), 139 deletions(-) create mode 100644 pkg/local_object_storage/util/logicerr/error.go create mode 100644 pkg/local_object_storage/util/logicerr/error_test.go diff --git a/pkg/core/object/errors.go b/pkg/core/object/errors.go index 6ef6b1a00..d8c9a182c 100644 --- a/pkg/core/object/errors.go +++ b/pkg/core/object/errors.go @@ -1,8 +1,12 @@ package object -import "errors" +import ( + "errors" + + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" +) // ErrObjectIsExpired is returned when the requested object's // epoch is less than the current one. Such objects are considered // as removed and should not be returned from the Storage Engine. -var ErrObjectIsExpired = errors.New("object is expired") +var ErrObjectIsExpired = logicerr.Wrap(errors.New("object is expired")) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/delete.go b/pkg/local_object_storage/blobstor/blobovniczatree/delete.go index fe904de2d..e949f8d5f 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/delete.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/delete.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "go.uber.org/zap" ) @@ -62,9 +63,7 @@ func (b *Blobovniczas) Delete(prm common.DeletePrm) (res common.DeleteRes, err e if err == nil && !objectFound { // not found in any blobovnicza - var errNotFound apistatus.ObjectNotFound - - return common.DeleteRes{}, errNotFound + return common.DeleteRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } return @@ -117,9 +116,7 @@ func (b *Blobovniczas) deleteObjectFromLevel(prm blobovnicza.DeletePrm, blzPath // and it's pointless to open them). if u64FromHexString(filepath.Base(blzPath)) > active.ind { b.log.Debug("index is too big", zap.String("path", blzPath)) - var errNotFound apistatus.ObjectNotFound - - return common.DeleteRes{}, errNotFound + return common.DeleteRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } // open blobovnicza (cached inside) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/get.go b/pkg/local_object_storage/blobstor/blobovniczatree/get.go index 24103554e..b87f1be6a 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/get.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/get.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" "go.uber.org/zap" @@ -54,9 +55,7 @@ func (b *Blobovniczas) Get(prm common.GetPrm) (res common.GetRes, err error) { if err == nil && res.Object == nil { // not found in any blobovnicza - var errNotFound apistatus.ObjectNotFound - - return res, errNotFound + return res, logicerr.Wrap(apistatus.ObjectNotFound{}) } return @@ -110,9 +109,7 @@ func (b *Blobovniczas) getObjectFromLevel(prm blobovnicza.GetPrm, blzPath string // and it's pointless to open them). if u64FromHexString(filepath.Base(blzPath)) > active.ind { b.log.Debug("index is too big", zap.String("path", blzPath)) - var errNotFound apistatus.ObjectNotFound - - return common.GetRes{}, errNotFound + return common.GetRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } // open blobovnicza (cached inside) diff --git a/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go b/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go index fb3591dcf..40bb10e25 100644 --- a/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go +++ b/pkg/local_object_storage/blobstor/blobovniczatree/get_range.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobovnicza" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" "go.uber.org/zap" @@ -58,9 +59,7 @@ func (b *Blobovniczas) GetRange(prm common.GetRangePrm) (res common.GetRangeRes, if err == nil && !objectFound { // not found in any blobovnicza - var errNotFound apistatus.ObjectNotFound - - return common.GetRangeRes{}, errNotFound + return common.GetRangeRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } return @@ -125,9 +124,7 @@ func (b *Blobovniczas) getRangeFromLevel(prm common.GetRangePrm, blzPath string, if u64FromHexString(filepath.Base(blzPath)) > active.ind { b.log.Debug("index is too big", zap.String("path", blzPath)) - var errNotFound apistatus.ObjectNotFound - - return common.GetRangeRes{}, errNotFound + return common.GetRangeRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } // open blobovnicza (cached inside) @@ -170,7 +167,7 @@ func (b *Blobovniczas) getObjectRange(blz *blobovnicza.Blobovnicza, prm common.G payload := obj.Payload() if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to { - return common.GetRangeRes{}, apistatus.ObjectOutOfRange{} + return common.GetRangeRes{}, logicerr.Wrap(apistatus.ObjectOutOfRange{}) } return common.GetRangeRes{ diff --git a/pkg/local_object_storage/blobstor/common/errors.go b/pkg/local_object_storage/blobstor/common/errors.go index 8581e7877..c26d4ddea 100644 --- a/pkg/local_object_storage/blobstor/common/errors.go +++ b/pkg/local_object_storage/blobstor/common/errors.go @@ -1,10 +1,14 @@ package common -import "errors" +import ( + "errors" + + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" +) // ErrReadOnly MUST be returned for modifying operations when the storage was opened // in readonly mode. -var ErrReadOnly = errors.New("opened as read-only") +var ErrReadOnly = logicerr.Wrap(errors.New("opened as read-only")) // ErrNoSpace MUST be returned when there is no space to put an object on the device. -var ErrNoSpace = errors.New("no free space") +var ErrNoSpace = logicerr.Wrap(errors.New("no free space")) diff --git a/pkg/local_object_storage/blobstor/fstree/fstree.go b/pkg/local_object_storage/blobstor/fstree/fstree.go index 601d772ae..e33267432 100644 --- a/pkg/local_object_storage/blobstor/fstree/fstree.go +++ b/pkg/local_object_storage/blobstor/fstree/fstree.go @@ -12,6 +12,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/compression" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/util" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" @@ -192,15 +193,14 @@ func (t *FSTree) Delete(prm common.DeletePrm) (common.DeleteRes, error) { p, err := t.getPath(prm.Address) if err != nil { if os.IsNotExist(err) { - var errNotFound apistatus.ObjectNotFound - err = errNotFound + err = logicerr.Wrap(apistatus.ObjectNotFound{}) } return common.DeleteRes{}, err } err = os.Remove(p) if err != nil && os.IsNotExist(err) { - err = apistatus.ObjectNotFound{} + err = logicerr.Wrap(apistatus.ObjectNotFound{}) } return common.DeleteRes{}, err } @@ -274,8 +274,7 @@ func (t *FSTree) Get(prm common.GetPrm) (common.GetRes, error) { p := t.treePath(prm.Address) if _, err := os.Stat(p); os.IsNotExist(err) { - var errNotFound apistatus.ObjectNotFound - return common.GetRes{}, errNotFound + return common.GetRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } data, err := os.ReadFile(p) @@ -308,7 +307,7 @@ func (t *FSTree) GetRange(prm common.GetRangePrm) (common.GetRangeRes, error) { to := from + prm.Range.GetLength() if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to { - return common.GetRangeRes{}, apistatus.ObjectOutOfRange{} + return common.GetRangeRes{}, logicerr.Wrap(apistatus.ObjectOutOfRange{}) } return common.GetRangeRes{ diff --git a/pkg/local_object_storage/blobstor/get.go b/pkg/local_object_storage/blobstor/get.go index da90999a9..692038de1 100644 --- a/pkg/local_object_storage/blobstor/get.go +++ b/pkg/local_object_storage/blobstor/get.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" ) @@ -19,8 +20,7 @@ func (b *BlobStor) Get(prm common.GetPrm) (common.GetRes, error) { } } - var errNotFound apistatus.ObjectNotFound - return common.GetRes{}, errNotFound + return common.GetRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } if len(prm.StorageID) == 0 { return b.storage[len(b.storage)-1].Storage.Get(prm) diff --git a/pkg/local_object_storage/blobstor/get_range.go b/pkg/local_object_storage/blobstor/get_range.go index 722a667f4..1d37cf862 100644 --- a/pkg/local_object_storage/blobstor/get_range.go +++ b/pkg/local_object_storage/blobstor/get_range.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" ) @@ -19,8 +20,7 @@ func (b *BlobStor) GetRange(prm common.GetRangePrm) (common.GetRangeRes, error) } } - var errNotFound apistatus.ObjectNotFound - return common.GetRangeRes{}, errNotFound + return common.GetRangeRes{}, logicerr.Wrap(apistatus.ObjectNotFound{}) } if len(prm.StorageID) == 0 { return b.storage[len(b.storage)-1].Storage.GetRange(prm) diff --git a/pkg/local_object_storage/blobstor/put.go b/pkg/local_object_storage/blobstor/put.go index 32f28abd7..978c5d93f 100644 --- a/pkg/local_object_storage/blobstor/put.go +++ b/pkg/local_object_storage/blobstor/put.go @@ -6,12 +6,13 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" ) // ErrNoPlaceFound is returned when object can't be saved to any sub-storage component // because of the policy. -var ErrNoPlaceFound = errors.New("couldn't find a place to store an object") +var ErrNoPlaceFound = logicerr.Wrap(errors.New("couldn't find a place to store an object")) // Put saves the object in BLOB storage. // diff --git a/pkg/local_object_storage/engine/container.go b/pkg/local_object_storage/engine/container.go index fcda01c29..94eeebe18 100644 --- a/pkg/local_object_storage/engine/container.go +++ b/pkg/local_object_storage/engine/container.go @@ -1,8 +1,6 @@ package engine import ( - "errors" - "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "go.uber.org/zap" @@ -77,10 +75,9 @@ func (e *StorageEngine) containerSize(prm ContainerSizePrm) (res ContainerSizeRe csPrm.SetContainerID(prm.cnr) csRes, err := sh.Shard.ContainerSize(csPrm) - if err != nil && !errors.Is(err, shard.ErrDegradedMode) { + if err != nil { e.reportShardError(sh, "can't get container size", err, - zap.Stringer("container_id", prm.cnr), - ) + zap.Stringer("container_id", prm.cnr)) return false } @@ -126,9 +123,7 @@ func (e *StorageEngine) listContainers() (ListContainersRes, error) { e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) { res, err := sh.Shard.ListContainers(shard.ListContainersPrm{}) if err != nil { - if !errors.Is(err, shard.ErrDegradedMode) { - e.reportShardError(sh, "can't get list of containers", err) - } + e.reportShardError(sh, "can't get list of containers", err) return false } diff --git a/pkg/local_object_storage/engine/delete.go b/pkg/local_object_storage/engine/delete.go index fe0cc4603..12810c5b9 100644 --- a/pkg/local_object_storage/engine/delete.go +++ b/pkg/local_object_storage/engine/delete.go @@ -77,8 +77,8 @@ func (e *StorageEngine) delete(prm DeletePrm) (DeleteRes, error) { return true } - splitErr, ok := err.(*objectSDK.SplitInfoError) - if !ok { + var splitErr *objectSDK.SplitInfoError + if !errors.As(err, &splitErr) { if !shard.IsErrNotFound(err) { e.reportShardError(sh, "could not check object existence", err) } @@ -97,13 +97,7 @@ func (e *StorageEngine) delete(prm DeletePrm) (DeleteRes, error) { _, err = sh.Inhume(shPrm) if err != nil { - if errors.Is(err, shard.ErrReadOnlyMode) || errors.Is(err, shard.ErrDegradedMode) { - e.log.Warn("could not inhume object in shard", - zap.Stringer("shard_id", sh.ID()), - zap.String("error", err.Error())) - } else { - e.reportShardError(sh, "could not inhume object in shard", err) - } + e.reportShardError(sh, "could not inhume object in shard", err) locked.is = errors.As(err, &locked.err) diff --git a/pkg/local_object_storage/engine/engine.go b/pkg/local_object_storage/engine/engine.go index 33ae46945..579d0d8ee 100644 --- a/pkg/local_object_storage/engine/engine.go +++ b/pkg/local_object_storage/engine/engine.go @@ -1,10 +1,12 @@ package engine import ( + "errors" "sync" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/util/logger" "go.uber.org/atomic" @@ -40,6 +42,13 @@ func (e *StorageEngine) reportShardError( msg string, err error, fields ...zap.Field) { + if isLogical(err) { + e.log.Warn(msg, + zap.Stringer("shard_id", sh.ID()), + zap.String("error", err.Error())) + return + } + sid := sh.ID() errCount := sh.errorCount.Inc() e.log.Warn(msg, append([]zap.Field{ @@ -77,6 +86,10 @@ func (e *StorageEngine) reportShardError( } } +func isLogical(err error) bool { + return errors.As(err, &logicerr.Logical{}) +} + // Option represents StorageEngine's constructor option. type Option func(*cfg) diff --git a/pkg/local_object_storage/engine/exists.go b/pkg/local_object_storage/engine/exists.go index d4027994e..16b5d8fb9 100644 --- a/pkg/local_object_storage/engine/exists.go +++ b/pkg/local_object_storage/engine/exists.go @@ -1,6 +1,8 @@ package engine import ( + "errors" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" @@ -22,8 +24,8 @@ func (e *StorageEngine) exists(addr oid.Address) (bool, error) { return true } - _, ok := err.(*objectSDK.SplitInfoError) - if ok { + var siErr *objectSDK.SplitInfoError + if errors.As(err, &siErr) { return true } diff --git a/pkg/local_object_storage/engine/get.go b/pkg/local_object_storage/engine/get.go index 3e7efce70..6eb50143c 100644 --- a/pkg/local_object_storage/engine/get.go +++ b/pkg/local_object_storage/engine/get.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -90,8 +91,6 @@ func (e *StorageEngine) get(prm GetPrm) (GetRes, error) { case shard.IsErrNotFound(err): return false // ignore, go to next shard case errors.As(err, &siErr): - siErr = err.(*objectSDK.SplitInfoError) - if outSI == nil { outSI = objectSDK.NewSplitInfo() } @@ -128,7 +127,7 @@ func (e *StorageEngine) get(prm GetPrm) (GetRes, error) { }) if outSI != nil { - return GetRes{}, objectSDK.NewSplitInfoError(outSI) + return GetRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI)) } if obj == nil { diff --git a/pkg/local_object_storage/engine/head.go b/pkg/local_object_storage/engine/head.go index 553ab4109..5f7851542 100644 --- a/pkg/local_object_storage/engine/head.go +++ b/pkg/local_object_storage/engine/head.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -86,8 +87,6 @@ func (e *StorageEngine) head(prm HeadPrm) (HeadRes, error) { case shard.IsErrNotFound(err): return false // ignore, go to next shard case errors.As(err, &siErr): - siErr = err.(*objectSDK.SplitInfoError) - if outSI == nil { outSI = objectSDK.NewSplitInfo() } @@ -127,7 +126,7 @@ func (e *StorageEngine) head(prm HeadPrm) (HeadRes, error) { }) if outSI != nil { - return HeadRes{}, objectSDK.NewSplitInfoError(outSI) + return HeadRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI)) } if head == nil { diff --git a/pkg/local_object_storage/engine/head_test.go b/pkg/local_object_storage/engine/head_test.go index 6f046d8fd..2ebf641d1 100644 --- a/pkg/local_object_storage/engine/head_test.go +++ b/pkg/local_object_storage/engine/head_test.go @@ -68,8 +68,8 @@ func TestHeadRaw(t *testing.T) { _, err = e.Head(headPrm) require.Error(t, err) - si, ok := err.(*object.SplitInfoError) - require.True(t, ok) + var si *object.SplitInfoError + require.ErrorAs(t, err, &si) // SplitInfoError should contain info from both shards require.Equal(t, splitID, si.SplitInfo().SplitID()) diff --git a/pkg/local_object_storage/engine/lock.go b/pkg/local_object_storage/engine/lock.go index 4709eccb2..9a6440719 100644 --- a/pkg/local_object_storage/engine/lock.go +++ b/pkg/local_object_storage/engine/lock.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" @@ -28,13 +29,13 @@ func (e *StorageEngine) lock(idCnr cid.ID, locker oid.ID, locked []oid.ID) error for i := range locked { switch e.lockSingle(idCnr, locker, locked[i], true) { case 1: - return apistatus.LockNonRegularObject{} + return logicerr.Wrap(apistatus.LockNonRegularObject{}) case 0: switch e.lockSingle(idCnr, locker, locked[i], false) { case 1: - return apistatus.LockNonRegularObject{} + return logicerr.Wrap(apistatus.LockNonRegularObject{}) case 0: - return errLockFailed + return logicerr.Wrap(errLockFailed) } } } diff --git a/pkg/local_object_storage/engine/range.go b/pkg/local_object_storage/engine/range.go index 1694d895c..6ade45aa7 100644 --- a/pkg/local_object_storage/engine/range.go +++ b/pkg/local_object_storage/engine/range.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -103,8 +104,6 @@ func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) { case shard.IsErrNotFound(err): return false // ignore, go to next shard case errors.As(err, &siErr): - siErr = err.(*objectSDK.SplitInfoError) - if outSI == nil { outSI = objectSDK.NewSplitInfo() } @@ -138,7 +137,7 @@ func (e *StorageEngine) getRange(prm RngPrm) (RngRes, error) { }) if outSI != nil { - return RngRes{}, objectSDK.NewSplitInfoError(outSI) + return RngRes{}, logicerr.Wrap(objectSDK.NewSplitInfoError(outSI)) } if obj == nil { diff --git a/pkg/local_object_storage/engine/select.go b/pkg/local_object_storage/engine/select.go index f06e6a60f..0e5a8ca22 100644 --- a/pkg/local_object_storage/engine/select.go +++ b/pkg/local_object_storage/engine/select.go @@ -1,8 +1,6 @@ package engine import ( - "errors" - "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -111,9 +109,7 @@ func (e *StorageEngine) list(limit uint64) (SelectRes, error) { e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) { res, err := sh.List() // consider limit result of shard iterator if err != nil { - if !errors.Is(err, shard.ErrDegradedMode) { - e.reportShardError(sh, "could not select objects from shard", err) - } + e.reportShardError(sh, "could not select objects from shard", err) } else { for _, addr := range res.AddressList() { // save only unique values if _, ok := uniqueMap[addr.EncodeToString()]; !ok { diff --git a/pkg/local_object_storage/engine/shards.go b/pkg/local_object_storage/engine/shards.go index 77946f973..7b1d61f20 100644 --- a/pkg/local_object_storage/engine/shards.go +++ b/pkg/local_object_storage/engine/shards.go @@ -8,13 +8,14 @@ import ( "github.com/nspcc-dev/hrw" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/panjf2000/ants/v2" "go.uber.org/atomic" "go.uber.org/zap" ) -var errShardNotFound = errors.New("shard not found") +var errShardNotFound = logicerr.Wrap(errors.New("shard not found")) type hashedShard shardWrapper diff --git a/pkg/local_object_storage/metabase/control.go b/pkg/local_object_storage/metabase/control.go index 93d62df6b..877a82377 100644 --- a/pkg/local_object_storage/metabase/control.go +++ b/pkg/local_object_storage/metabase/control.go @@ -6,13 +6,14 @@ import ( "path/filepath" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/util" "go.etcd.io/bbolt" "go.uber.org/zap" ) // ErrDegradedMode is returned when metabase is in a degraded mode. -var ErrDegradedMode = errors.New("metabase is in a degraded mode") +var ErrDegradedMode = logicerr.Wrap(errors.New("metabase is in a degraded mode")) // Open boltDB instance for metabase. func (db *DB) Open(readOnly bool) error { diff --git a/pkg/local_object_storage/metabase/exists.go b/pkg/local_object_storage/metabase/exists.go index 34da4d206..33ff196f3 100644 --- a/pkg/local_object_storage/metabase/exists.go +++ b/pkg/local_object_storage/metabase/exists.go @@ -7,6 +7,7 @@ import ( objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" @@ -24,7 +25,7 @@ type ExistsRes struct { exists bool } -var ErrLackSplitInfo = errors.New("no split info on parent object") +var ErrLackSplitInfo = logicerr.Wrap(errors.New("no split info on parent object")) // SetAddress is an Exists option to set object checked for existence. func (p *ExistsPrm) SetAddress(addr oid.Address) { @@ -60,13 +61,9 @@ func (db *DB) exists(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (exists b // check graveyard and object expiration first switch objectStatus(tx, addr, currEpoch) { case 1: - var errNotFound apistatus.ObjectNotFound - - return false, errNotFound + return false, logicerr.Wrap(apistatus.ObjectNotFound{}) case 2: - var errRemoved apistatus.ObjectAlreadyRemoved - - return false, errRemoved + return false, logicerr.Wrap(apistatus.ObjectAlreadyRemoved{}) case 3: return false, object.ErrObjectIsExpired } @@ -88,7 +85,7 @@ func (db *DB) exists(tx *bbolt.Tx, addr oid.Address, currEpoch uint64) (exists b return false, err } - return false, objectSDK.NewSplitInfoError(splitInfo) + return false, logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo)) } // if parent bucket is empty, then check if object exists in typed buckets diff --git a/pkg/local_object_storage/metabase/exists_test.go b/pkg/local_object_storage/metabase/exists_test.go index 93afd977c..ca8f829f1 100644 --- a/pkg/local_object_storage/metabase/exists_test.go +++ b/pkg/local_object_storage/metabase/exists_test.go @@ -128,9 +128,8 @@ func TestDB_Exists(t *testing.T) { _, err = metaExists(db, object.AddressOf(parent)) require.Error(t, err) - si, ok := err.(*objectSDK.SplitInfoError) - require.True(t, ok) - + var si *objectSDK.SplitInfoError + require.ErrorAs(t, err, &si) require.Equal(t, splitID, si.SplitInfo().SplitID()) id1, _ := child.ID() @@ -152,9 +151,8 @@ func TestDB_Exists(t *testing.T) { _, err = metaExists(db, object.AddressOf(parent)) require.Error(t, err) - si, ok := err.(*objectSDK.SplitInfoError) - require.True(t, ok) - + var si *objectSDK.SplitInfoError + require.ErrorAs(t, err, &si) require.Equal(t, splitID, si.SplitInfo().SplitID()) id1, _ := child.ID() diff --git a/pkg/local_object_storage/metabase/get.go b/pkg/local_object_storage/metabase/get.go index 1a34b398f..b3e008adb 100644 --- a/pkg/local_object_storage/metabase/get.go +++ b/pkg/local_object_storage/metabase/get.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neofs-node/pkg/core/object" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" @@ -66,13 +67,9 @@ func (db *DB) get(tx *bbolt.Tx, addr oid.Address, key []byte, checkStatus, raw b if checkStatus { switch objectStatus(tx, addr, currEpoch) { case 1: - var errNotFound apistatus.ObjectNotFound - - return nil, errNotFound + return nil, logicerr.Wrap(apistatus.ObjectNotFound{}) case 2: - var errRemoved apistatus.ObjectAlreadyRemoved - - return nil, errRemoved + return nil, logicerr.Wrap(apistatus.ObjectAlreadyRemoved{}) case 3: return nil, object.ErrObjectIsExpired } @@ -128,9 +125,7 @@ func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSD bucketName := make([]byte, bucketKeySize) parentBucket := tx.Bucket(parentBucketName(cnr, bucketName)) if parentBucket == nil { - var errNotFound apistatus.ObjectNotFound - - return nil, errNotFound + return nil, logicerr.Wrap(apistatus.ObjectNotFound{}) } relativeLst, err := decodeList(parentBucket.Get(key)) @@ -139,9 +134,7 @@ func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSD } if len(relativeLst) == 0 { // this should never happen though - var errNotFound apistatus.ObjectNotFound - - return nil, errNotFound + return nil, logicerr.Wrap(apistatus.ObjectNotFound{}) } // pick last item, for now there is not difference which address to pick @@ -160,9 +153,7 @@ func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSD par := child.Parent() if par == nil { // this should never happen though - var errNotFound apistatus.ObjectNotFound - - return nil, errNotFound + return nil, logicerr.Wrap(apistatus.ObjectNotFound{}) } return par, nil @@ -171,10 +162,8 @@ func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSD func getSplitInfoError(tx *bbolt.Tx, cnr cid.ID, key []byte) error { splitInfo, err := getSplitInfo(tx, cnr, key) if err == nil { - return objectSDK.NewSplitInfoError(splitInfo) + return logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo)) } - var errNotFound apistatus.ObjectNotFound - - return errNotFound + return logicerr.Wrap(apistatus.ObjectNotFound{}) } diff --git a/pkg/local_object_storage/metabase/get_test.go b/pkg/local_object_storage/metabase/get_test.go index a645a0b68..38842a663 100644 --- a/pkg/local_object_storage/metabase/get_test.go +++ b/pkg/local_object_storage/metabase/get_test.go @@ -95,16 +95,15 @@ func TestDB_Get(t *testing.T) { _, err = metaGet(db, object.AddressOf(parent), true) require.Error(t, err) - siErr, ok := err.(*objectSDK.SplitInfoError) - require.True(t, ok) - + var siErr *objectSDK.SplitInfoError + require.ErrorAs(t, err, &siErr) require.Equal(t, splitID, siErr.SplitInfo().SplitID()) id1, _ := child.ID() id2, _ := siErr.SplitInfo().LastPart() require.Equal(t, id1, id2) - _, ok = siErr.SplitInfo().Link() + _, ok := siErr.SplitInfo().Link() require.False(t, ok) }) diff --git a/pkg/local_object_storage/metabase/inhume.go b/pkg/local_object_storage/metabase/inhume.go index 3dcd2c230..6596f8ee2 100644 --- a/pkg/local_object_storage/metabase/inhume.go +++ b/pkg/local_object_storage/metabase/inhume.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -78,7 +79,7 @@ var errBreakBucketForEach = errors.New("bucket ForEach break") // ErrLockObjectRemoval is returned when inhume operation is being // performed on lock object, and it is not a forced object removal. -var ErrLockObjectRemoval = errors.New("lock object removal") +var ErrLockObjectRemoval = logicerr.Wrap(errors.New("lock object removal")) // Inhume marks objects as removed but not removes it from metabase. // diff --git a/pkg/local_object_storage/metabase/iterators.go b/pkg/local_object_storage/metabase/iterators.go index fcbba3fbb..3ecbc7898 100644 --- a/pkg/local_object_storage/metabase/iterators.go +++ b/pkg/local_object_storage/metabase/iterators.go @@ -6,6 +6,7 @@ import ( "strconv" objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -34,7 +35,7 @@ type ExpiredObjectHandler func(*ExpiredObject) error // ErrInterruptIterator is returned by iteration handlers // as a "break" keyword. -var ErrInterruptIterator = errors.New("iterator is interrupted") +var ErrInterruptIterator = logicerr.Wrap(errors.New("iterator is interrupted")) // IterateExpired iterates over all objects in DB which are out of date // relative to epoch. Locked objects are not included (do not confuse diff --git a/pkg/local_object_storage/metabase/list.go b/pkg/local_object_storage/metabase/list.go index 5ab883045..3e3266f9d 100644 --- a/pkg/local_object_storage/metabase/list.go +++ b/pkg/local_object_storage/metabase/list.go @@ -3,6 +3,7 @@ package meta import ( "errors" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "go.etcd.io/bbolt" @@ -11,7 +12,7 @@ import ( // ErrEndOfListing is returned from object listing with cursor // when storage can't return any more objects after provided // cursor. Use nil cursor object to start listing again. -var ErrEndOfListing = errors.New("end of object listing") +var ErrEndOfListing = logicerr.Wrap(errors.New("end of object listing")) // Cursor is a type for continuous object listing. type Cursor struct { diff --git a/pkg/local_object_storage/metabase/lock.go b/pkg/local_object_storage/metabase/lock.go index 580c5d28d..786b3d7c6 100644 --- a/pkg/local_object_storage/metabase/lock.go +++ b/pkg/local_object_storage/metabase/lock.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -41,7 +42,7 @@ func (db *DB) Lock(cnr cid.ID, locker oid.ID, locked []oid.ID) error { return db.boltDB.Update(func(tx *bbolt.Tx) error { if firstIrregularObjectType(tx, cnr, bucketKeysLocked...) != object.TypeRegular { - return apistatus.LockNonRegularObject{} + return logicerr.Wrap(apistatus.LockNonRegularObject{}) } bucketLocked := tx.Bucket(bucketNameLocked) diff --git a/pkg/local_object_storage/metabase/version.go b/pkg/local_object_storage/metabase/version.go index df1981601..53cc95cac 100644 --- a/pkg/local_object_storage/metabase/version.go +++ b/pkg/local_object_storage/metabase/version.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "go.etcd.io/bbolt" ) @@ -16,7 +17,7 @@ var versionKey = []byte("version") // ErrOutdatedVersion is returned on initializing // an existing metabase that is not compatible with // the current code version. -var ErrOutdatedVersion = errors.New("invalid version, resynchronization is required") +var ErrOutdatedVersion = logicerr.Wrap(errors.New("invalid version, resynchronization is required")) func checkVersion(tx *bbolt.Tx, initialized bool) error { var knownVersion bool diff --git a/pkg/local_object_storage/pilorama/interface.go b/pkg/local_object_storage/pilorama/interface.go index 34df19f24..f70465ba9 100644 --- a/pkg/local_object_storage/pilorama/interface.go +++ b/pkg/local_object_storage/pilorama/interface.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" cidSDK "github.com/nspcc-dev/neofs-sdk-go/container/id" ) @@ -67,7 +68,7 @@ type CIDDescriptor struct { // ErrInvalidCIDDescriptor is returned when info about tne node position // in the container is invalid. -var ErrInvalidCIDDescriptor = errors.New("cid descriptor is invalid") +var ErrInvalidCIDDescriptor = logicerr.Wrap(errors.New("cid descriptor is invalid")) func (d CIDDescriptor) checkValid() bool { return 0 <= d.Position && d.Position < d.Size diff --git a/pkg/local_object_storage/shard/dump.go b/pkg/local_object_storage/shard/dump.go index 522261441..322554fcf 100644 --- a/pkg/local_object_storage/shard/dump.go +++ b/pkg/local_object_storage/shard/dump.go @@ -7,6 +7,7 @@ import ( "os" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" ) @@ -46,7 +47,7 @@ func (r DumpRes) Count() int { return r.count } -var ErrMustBeReadOnly = errors.New("shard must be in read-only mode") +var ErrMustBeReadOnly = logicerr.Wrap(errors.New("shard must be in read-only mode")) // Dump dumps all objects from the shard to a file or stream. // diff --git a/pkg/local_object_storage/shard/get.go b/pkg/local_object_storage/shard/get.go index bbc16cddf..bb8395b0f 100644 --- a/pkg/local_object_storage/shard/get.go +++ b/pkg/local_object_storage/shard/get.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" @@ -125,9 +126,7 @@ func (s *Shard) fetchObjectData(addr oid.Address, skipMeta bool, cb storFetcher, } if !exists { - var errNotFound apistatus.ObjectNotFound - - return nil, false, errNotFound + return nil, false, logicerr.Wrap(apistatus.ObjectNotFound{}) } var mPrm meta.StorageIDPrm diff --git a/pkg/local_object_storage/shard/get_test.go b/pkg/local_object_storage/shard/get_test.go index 7756b2ba5..e0642b3c6 100644 --- a/pkg/local_object_storage/shard/get_test.go +++ b/pkg/local_object_storage/shard/get_test.go @@ -97,12 +97,10 @@ func testShardGet(t *testing.T, hasWriteCache bool) { _, err = testGet(t, sh, getPrm, hasWriteCache) - var expectedErr *objectSDK.SplitInfoError - require.True(t, errors.As(err, &expectedErr)) + var si *objectSDK.SplitInfoError + require.True(t, errors.As(err, &si)) - si, ok := err.(*objectSDK.SplitInfoError) - require.True(t, ok) - _, ok = si.SplitInfo().Link() + _, ok := si.SplitInfo().Link() require.False(t, ok) id1, _ := child.ID() id2, _ := si.SplitInfo().LastPart() diff --git a/pkg/local_object_storage/shard/mode.go b/pkg/local_object_storage/shard/mode.go index 09dab4155..c716ef2c9 100644 --- a/pkg/local_object_storage/shard/mode.go +++ b/pkg/local_object_storage/shard/mode.go @@ -4,14 +4,15 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" ) // ErrReadOnlyMode is returned when it is impossible to apply operation // that changes shard's memory due to the "read-only" shard's mode. -var ErrReadOnlyMode = errors.New("shard is in read-only mode") +var ErrReadOnlyMode = logicerr.Wrap(errors.New("shard is in read-only mode")) // ErrDegradedMode is returned when operation requiring metabase is executed in degraded mode. -var ErrDegradedMode = errors.New("shard is in degraded mode") +var ErrDegradedMode = logicerr.Wrap(errors.New("shard is in degraded mode")) // SetMode sets mode of the shard. // diff --git a/pkg/local_object_storage/shard/range.go b/pkg/local_object_storage/shard/range.go index 8131767f2..9a8eb583a 100644 --- a/pkg/local_object_storage/shard/range.go +++ b/pkg/local_object_storage/shard/range.go @@ -3,6 +3,7 @@ package shard import ( "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/writecache" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/object" @@ -94,7 +95,7 @@ func (s *Shard) GetRange(prm RngPrm) (RngRes, error) { from := prm.off to := from + prm.ln if pLen := uint64(len(payload)); to < from || pLen < from || pLen < to { - return nil, apistatus.ObjectOutOfRange{} + return nil, logicerr.Wrap(apistatus.ObjectOutOfRange{}) } obj := object.New() diff --git a/pkg/local_object_storage/shard/restore.go b/pkg/local_object_storage/shard/restore.go index e42cfa408..bfb3b8b86 100644 --- a/pkg/local_object_storage/shard/restore.go +++ b/pkg/local_object_storage/shard/restore.go @@ -7,11 +7,12 @@ import ( "io" "os" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" "github.com/nspcc-dev/neofs-sdk-go/object" ) // ErrInvalidMagic is returned when dump format is invalid. -var ErrInvalidMagic = errors.New("invalid magic") +var ErrInvalidMagic = logicerr.Wrap(errors.New("invalid magic")) // RestorePrm groups the parameters of Restore operation. type RestorePrm struct { diff --git a/pkg/local_object_storage/shard/tree.go b/pkg/local_object_storage/shard/tree.go index 9a8c4f2d8..bfc456dbf 100644 --- a/pkg/local_object_storage/shard/tree.go +++ b/pkg/local_object_storage/shard/tree.go @@ -4,13 +4,14 @@ import ( "errors" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/pilorama" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" cidSDK "github.com/nspcc-dev/neofs-sdk-go/container/id" ) var _ pilorama.Forest = (*Shard)(nil) // ErrPiloramaDisabled is returned when pilorama was disabled in the configuration. -var ErrPiloramaDisabled = errors.New("pilorama is disabled") +var ErrPiloramaDisabled = logicerr.Wrap(errors.New("pilorama is disabled")) // TreeMove implements the pilorama.Forest interface. func (s *Shard) TreeMove(d pilorama.CIDDescriptor, treeID string, m *pilorama.Move) (*pilorama.LogMove, error) { diff --git a/pkg/local_object_storage/util/logicerr/error.go b/pkg/local_object_storage/util/logicerr/error.go new file mode 100644 index 000000000..7f1c3c159 --- /dev/null +++ b/pkg/local_object_storage/util/logicerr/error.go @@ -0,0 +1,21 @@ +package logicerr + +// Logical is a wrapper for logical errors. +type Logical struct { + err error +} + +// Error implements the error interface. +func (e Logical) Error() string { + return e.err.Error() +} + +// Wrap wraps arbitrary error into a logical one. +func Wrap(err error) Logical { + return Logical{err: err} +} + +// Unwrap returns underlying error. +func (e Logical) Unwrap() error { + return e.err +} diff --git a/pkg/local_object_storage/util/logicerr/error_test.go b/pkg/local_object_storage/util/logicerr/error_test.go new file mode 100644 index 000000000..c2d06cac4 --- /dev/null +++ b/pkg/local_object_storage/util/logicerr/error_test.go @@ -0,0 +1,58 @@ +package logicerr + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestError(t *testing.T) { + t.Run("errors.Is", func(t *testing.T) { + e1 := errors.New("some error") + ee := Wrap(e1) + require.ErrorIs(t, ee, e1) + + e2 := fmt.Errorf("wrap: %w", e1) + ee = Wrap(e2) + require.ErrorIs(t, ee, e1) + require.ErrorIs(t, ee, e2) + + require.Equal(t, errors.Unwrap(ee), e2) + }) + + t.Run("errors.As", func(t *testing.T) { + e1 := testError{42} + ee := Wrap(e1) + + { + var actual testError + require.ErrorAs(t, ee, &actual) + require.Equal(t, e1.data, actual.data) + } + { + var actual Logical + require.ErrorAs(t, ee, &actual) + require.Equal(t, e1, actual.err) + } + + e2 := fmt.Errorf("wrap: %w", e1) + ee = Wrap(e2) + + { + var actual testError + require.ErrorAs(t, ee, &actual) + require.Equal(t, e1.data, actual.data) + } + }) +} + +type testError struct { + data uint64 +} + +func (e testError) Error() string { + return strconv.FormatUint(e.data, 10) +} diff --git a/pkg/local_object_storage/writecache/get.go b/pkg/local_object_storage/writecache/get.go index dc7bb2d1c..2e591fda4 100644 --- a/pkg/local_object_storage/writecache/get.go +++ b/pkg/local_object_storage/writecache/get.go @@ -3,6 +3,7 @@ package writecache import ( "github.com/nspcc-dev/neo-go/pkg/util/slice" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/common" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" objectSDK "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -24,9 +25,7 @@ func (c *cache) Get(addr oid.Address) (*objectSDK.Object, error) { res, err := c.fsTree.Get(common.GetPrm{Address: addr}) if err != nil { - var errNotFound apistatus.ObjectNotFound - - return nil, errNotFound + return nil, logicerr.Wrap(apistatus.ObjectNotFound{}) } c.flushed.Get(saddr) @@ -58,9 +57,7 @@ func Get(db *bbolt.DB, key []byte) ([]byte, error) { } value = b.Get(key) if value == nil { - var errNotFound apistatus.ObjectNotFound - - return errNotFound + return logicerr.Wrap(apistatus.ObjectNotFound{}) } value = slice.Copy(value) return nil diff --git a/pkg/local_object_storage/writecache/mode.go b/pkg/local_object_storage/writecache/mode.go index 7e83959d1..5d5532498 100644 --- a/pkg/local_object_storage/writecache/mode.go +++ b/pkg/local_object_storage/writecache/mode.go @@ -6,10 +6,11 @@ import ( "time" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode" + "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr" ) // ErrReadOnly is returned when Put/Write is performed in a read-only mode. -var ErrReadOnly = errors.New("write-cache is in read-only mode") +var ErrReadOnly = logicerr.Wrap(errors.New("write-cache is in read-only mode")) // SetMode sets write-cache mode of operation. // When shard is put in read-only mode all objects in memory are flushed to disk