2021-02-16 15:56:59 +00:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2022-03-03 14:19:05 +00:00
|
|
|
object2 "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-02-16 15:56:59 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-02-15 18:22:06 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2021-02-16 15:56:59 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDB_IterateExpired(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
|
|
|
const epoch = 13
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
mAlive := map[object.Type]oid.Address{}
|
|
|
|
mExpired := map[object.Type]oid.Address{}
|
2021-02-16 15:56:59 +00:00
|
|
|
|
|
|
|
for _, typ := range []object.Type{
|
|
|
|
object.TypeRegular,
|
|
|
|
object.TypeTombstone,
|
|
|
|
object.TypeStorageGroup,
|
2022-02-15 12:51:56 +00:00
|
|
|
object.TypeLock,
|
2021-02-16 15:56:59 +00:00
|
|
|
} {
|
|
|
|
mAlive[typ] = putWithExpiration(t, db, typ, epoch)
|
|
|
|
mExpired[typ] = putWithExpiration(t, db, typ, epoch-1)
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:22:06 +00:00
|
|
|
expiredLocked := putWithExpiration(t, db, object.TypeRegular, epoch-1)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
require.NoError(t, db.Lock(expiredLocked.Container(), oidtest.ID(), []oid.ID{expiredLocked.Object()}))
|
2022-02-15 18:22:06 +00:00
|
|
|
|
2021-02-16 15:56:59 +00:00
|
|
|
err := db.IterateExpired(epoch, func(exp *meta.ExpiredObject) error {
|
|
|
|
if addr, ok := mAlive[exp.Type()]; ok {
|
|
|
|
require.NotEqual(t, addr, exp.Address())
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:22:06 +00:00
|
|
|
require.NotEqual(t, expiredLocked, exp.Address())
|
|
|
|
|
2021-02-16 15:56:59 +00:00
|
|
|
addr, ok := mExpired[exp.Type()]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, addr, exp.Address())
|
|
|
|
|
|
|
|
delete(mExpired, exp.Type())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Empty(t, mExpired)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func putWithExpiration(t *testing.T, db *meta.DB, typ object.Type, expiresAt uint64) oid.Address {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj := generateObject(t)
|
|
|
|
obj.SetType(typ)
|
|
|
|
addAttribute(obj, objectV2.SysAttributeExpEpoch, strconv.FormatUint(expiresAt, 10))
|
2021-02-16 15:56:59 +00:00
|
|
|
|
|
|
|
require.NoError(t, putBig(db, obj))
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return object2.AddressOf(obj)
|
2021-02-16 15:56:59 +00:00
|
|
|
}
|
2021-02-17 12:22:13 +00:00
|
|
|
|
|
|
|
func TestDB_IterateCoveredByTombstones(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
ts := oidtest.Address()
|
|
|
|
protected1 := oidtest.Address()
|
|
|
|
protected2 := oidtest.Address()
|
|
|
|
protectedLocked := oidtest.Address()
|
|
|
|
garbage := oidtest.Address()
|
2021-02-17 12:22:13 +00:00
|
|
|
|
|
|
|
prm := new(meta.InhumePrm)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
_, err = db.Inhume(prm.
|
|
|
|
WithTombstoneAddress(ts).
|
2022-02-15 18:24:12 +00:00
|
|
|
WithAddresses(protected1, protected2, protectedLocked),
|
2021-02-17 12:22:13 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = db.Inhume(prm.
|
|
|
|
WithAddresses(garbage).
|
|
|
|
WithGCMark(),
|
|
|
|
)
|
2022-02-15 18:24:12 +00:00
|
|
|
require.NoError(t, err)
|
2021-02-17 12:22:13 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var handled []oid.Address
|
2021-02-17 12:22:13 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
tss := map[string]oid.Address{
|
|
|
|
ts.EncodeToString(): ts,
|
2021-02-17 12:22:13 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
err = db.IterateCoveredByTombstones(tss, func(addr oid.Address) error {
|
2021-02-17 12:22:13 +00:00
|
|
|
handled = append(handled, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-15 18:24:12 +00:00
|
|
|
require.Len(t, handled, 3)
|
2021-02-17 12:22:13 +00:00
|
|
|
require.Contains(t, handled, protected1)
|
|
|
|
require.Contains(t, handled, protected2)
|
2022-02-15 18:24:12 +00:00
|
|
|
require.Contains(t, handled, protectedLocked)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
err = db.Lock(protectedLocked.Container(), oidtest.ID(), []oid.ID{protectedLocked.Object()})
|
2022-02-15 18:24:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
handled = handled[:0]
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
err = db.IterateCoveredByTombstones(tss, func(addr oid.Address) error {
|
2022-02-15 18:24:12 +00:00
|
|
|
handled = append(handled, addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, handled, 2)
|
|
|
|
require.NotContains(t, handled, protectedLocked)
|
2021-02-17 12:22:13 +00:00
|
|
|
}
|