From 3f293cb55e4b4420d27f65c2d1edfa95a8cba38a Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 31 Aug 2021 14:04:58 +0300 Subject: [PATCH] [#762] storage/shard: Try to read headers from write-cache Shard should try to read object headers from write-cache if it is enabled. Extend `writecache.Cache` interface with `Head` method. Call the method in `Shard.Head` if `Shard.hasWriteCache` returns true. Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/shard/head.go | 29 +++++++++++++++++-- .../writecache/writecache.go | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/local_object_storage/shard/head.go b/pkg/local_object_storage/shard/head.go index 12a90322..5528a300 100644 --- a/pkg/local_object_storage/shard/head.go +++ b/pkg/local_object_storage/shard/head.go @@ -1,6 +1,9 @@ package shard import ( + "errors" + "fmt" + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-node/pkg/core/object" meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" @@ -48,13 +51,33 @@ func (r *HeadRes) Object() *object.Object { // // Returns any error encountered. func (s *Shard) Head(prm *HeadPrm) (*HeadRes, error) { + // object can be saved in write-cache (if enabled) or in metabase + + if s.hasWriteCache() { + // try to read header from write-cache + header, err := s.writeCache.Head(prm.addr) + if err == nil { + return &HeadRes{ + obj: header, + }, nil + } else if !errors.Is(err, object.ErrNotFound) { + // in this case we think that object is presented in write-cache, but corrupted + return nil, fmt.Errorf("could not read header from write-cache: %w", err) + } + + // otherwise object seems to be flushed to metabase + } + headParams := new(meta.GetPrm). WithAddress(prm.addr). WithRaw(prm.raw) - head, err := s.metaBase.Get(headParams) + res, err := s.metaBase.Get(headParams) + if err != nil { + return nil, err + } return &HeadRes{ - obj: head.Header(), - }, err + obj: res.Header(), + }, nil } diff --git a/pkg/local_object_storage/writecache/writecache.go b/pkg/local_object_storage/writecache/writecache.go index 01163e47..593437ba 100644 --- a/pkg/local_object_storage/writecache/writecache.go +++ b/pkg/local_object_storage/writecache/writecache.go @@ -13,6 +13,7 @@ import ( // Cache represents write-cache for objects. type Cache interface { Get(*objectSDK.Address) (*object.Object, error) + Head(*objectSDK.Address) (*object.Object, error) Delete(*objectSDK.Address) error Put(*object.Object) error