From 34e8d2ba563a5a26724518b30984cf676771d21f Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Sat, 12 Nov 2022 14:59:58 +0300 Subject: [PATCH] [#1502] shard: Add `IsLocked` method Signed-off-by: Pavel Karpy --- pkg/local_object_storage/shard/lock.go | 20 +++++++++++ pkg/local_object_storage/shard/lock_test.go | 37 ++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/local_object_storage/shard/lock.go b/pkg/local_object_storage/shard/lock.go index 0b8ffeb6..8961187c 100644 --- a/pkg/local_object_storage/shard/lock.go +++ b/pkg/local_object_storage/shard/lock.go @@ -3,6 +3,7 @@ package shard import ( "fmt" + meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" ) @@ -28,3 +29,22 @@ func (s *Shard) Lock(idCnr cid.ID, locker oid.ID, locked []oid.ID) error { return nil } + +// IsLocked checks object locking relation of the provided object. Not found object is +// considered as not locked. Requires healthy metabase, returns ErrDegradedMode otherwise. +func (s *Shard) IsLocked(addr oid.Address) (bool, error) { + m := s.GetMode() + if m.NoMetabase() { + return false, ErrDegradedMode + } + + var prm meta.IsLockedPrm + prm.SetAddress(addr) + + res, err := s.metaBase.IsLocked(prm) + if err != nil { + return false, err + } + + return res.Locked(), nil +} diff --git a/pkg/local_object_storage/shard/lock_test.go b/pkg/local_object_storage/shard/lock_test.go index 09084d5e..7be64731 100644 --- a/pkg/local_object_storage/shard/lock_test.go +++ b/pkg/local_object_storage/shard/lock_test.go @@ -16,6 +16,7 @@ import ( cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test" "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" + oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test" "github.com/stretchr/testify/require" "go.uber.org/zap" ) @@ -138,5 +139,39 @@ func TestShard_Lock(t *testing.T) { _, err = sh.Get(getPrm) require.ErrorAs(t, err, new(apistatus.ObjectNotFound)) }) - +} + +func TestShard_IsLocked(t *testing.T) { + sh := newShard(t, false) + + cnr := cidtest.ID() + obj := generateObjectWithCID(t, cnr) + cnrID, _ := obj.ContainerID() + objID, _ := obj.ID() + + lockID := oidtest.ID() + + // put the object + + var putPrm shard.PutPrm + putPrm.SetObject(obj) + + _, err := sh.Put(putPrm) + require.NoError(t, err) + + // not locked object is not locked + + locked, err := sh.IsLocked(objectcore.AddressOf(obj)) + require.NoError(t, err) + + require.False(t, locked) + + // locked object is locked + + require.NoError(t, sh.Lock(cnrID, lockID, []oid.ID{objID})) + + locked, err = sh.IsLocked(objectcore.AddressOf(obj)) + require.NoError(t, err) + + require.True(t, locked) }