From dba3e58dc57504553df86b960bf6d2eddfeda887 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 15 Feb 2022 21:22:06 +0300 Subject: [PATCH] [#1176] metabase: Ignore locked objects in `IterateExpired` Make `DB.IterateExpired` to not pass locked objects to the handler. The method is used by GC, therefore it will not consider them as candidates for deletion. Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/metabase/iterators.go | 11 ++++++++++- pkg/local_object_storage/metabase/iterators_test.go | 8 ++++++++ pkg/local_object_storage/metabase/lock.go | 13 +++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/local_object_storage/metabase/iterators.go b/pkg/local_object_storage/metabase/iterators.go index 64a2c0eb0e..b1df0ec51a 100644 --- a/pkg/local_object_storage/metabase/iterators.go +++ b/pkg/local_object_storage/metabase/iterators.go @@ -38,7 +38,8 @@ type ExpiredObjectHandler func(*ExpiredObject) error var ErrInterruptIterator = errors.New("iterator is interrupted") // IterateExpired iterates over all objects in DB which are out of date -// relative to epoch. +// relative to epoch. Locked objects are not included (do not confuse +// with objects of type LOCK). // // If h returns ErrInterruptIterator, nil returns immediately. // Returns other errors of h directly. @@ -83,6 +84,14 @@ func (db *DB) iterateExpired(tx *bbolt.Tx, epoch uint64, h ExpiredObjectHandler) return fmt.Errorf("could not parse container ID of expired bucket: %w", err) } + // Ignore locked objects. + // + // To slightly optimize performance we can check only REGULAR objects + // (only they can be locked), but it's more reliable. + if objectLocked(tx, *cnrID, *id) { + return nil + } + addr := addressSDK.NewAddress() addr.SetContainerID(cnrID) addr.SetObjectID(id) diff --git a/pkg/local_object_storage/metabase/iterators_test.go b/pkg/local_object_storage/metabase/iterators_test.go index bcab53e82d..2763f7ca93 100644 --- a/pkg/local_object_storage/metabase/iterators_test.go +++ b/pkg/local_object_storage/metabase/iterators_test.go @@ -9,6 +9,8 @@ import ( meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase" "github.com/nspcc-dev/neofs-sdk-go/object" addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address" + 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" ) @@ -30,11 +32,17 @@ func TestDB_IterateExpired(t *testing.T) { mExpired[typ] = putWithExpiration(t, db, typ, epoch-1) } + expiredLocked := putWithExpiration(t, db, object.TypeRegular, epoch-1) + + require.NoError(t, db.Lock(*expiredLocked.ContainerID(), *oidtest.ID(), []oid.ID{*expiredLocked.ObjectID()})) + err := db.IterateExpired(epoch, func(exp *meta.ExpiredObject) error { if addr, ok := mAlive[exp.Type()]; ok { require.NotEqual(t, addr, exp.Address()) } + require.NotEqual(t, expiredLocked, exp.Address()) + addr, ok := mExpired[exp.Type()] require.True(t, ok) require.Equal(t, addr, exp.Address()) diff --git a/pkg/local_object_storage/metabase/lock.go b/pkg/local_object_storage/metabase/lock.go index eb22e4a20f..ecf14ec17d 100644 --- a/pkg/local_object_storage/metabase/lock.go +++ b/pkg/local_object_storage/metabase/lock.go @@ -97,3 +97,16 @@ func (db *DB) Lock(cnr cid.ID, locker oid.ID, locked []oid.ID) error { return nil }) } + +// checks if specified object is locked in the specified container. +func objectLocked(tx *bbolt.Tx, idCnr cid.ID, idObj oid.ID) bool { + bucketLocked := tx.Bucket(bucketNameLocked) + if bucketLocked != nil { + bucketLockedContainer := bucketLocked.Bucket([]byte(idCnr.String())) + if bucketLockedContainer != nil { + return bucketLockedContainer.Get(objectKey(&idObj)) != nil + } + } + + return false +}