2022-09-12 11:48:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-23 14:59:14 +00:00
|
|
|
"context"
|
2022-09-19 10:31:55 +00:00
|
|
|
"errors"
|
2022-09-12 11:48:21 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
objectCore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/fstree"
|
2023-03-20 14:10:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
2023-03-07 13:38:26 +00:00
|
|
|
meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-09-12 11:48:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
2022-09-19 10:31:55 +00:00
|
|
|
func newEngineEvacuate(t *testing.T, shardNum int, objPerShard int) (*StorageEngine, []*shard.ID, []*objectSDK.Object) {
|
2023-05-05 07:38:59 +00:00
|
|
|
dir := t.TempDir()
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-03-30 11:58:20 +00:00
|
|
|
te := testNewEngine(t, WithShardPoolSize(1)).
|
|
|
|
setShardsNumOpts(t, shardNum, func(id int) []shard.Option {
|
|
|
|
return []shard.Option{
|
|
|
|
shard.WithLogger(&logger.Logger{Logger: zaptest.NewLogger(t)}),
|
|
|
|
shard.WithBlobStorOptions(
|
|
|
|
blobstor.WithStorages([]blobstor.SubStorage{{
|
|
|
|
Storage: fstree.New(
|
|
|
|
fstree.WithPath(filepath.Join(dir, strconv.Itoa(id))),
|
|
|
|
fstree.WithDepth(1)),
|
|
|
|
}})),
|
|
|
|
shard.WithMetaBaseOptions(
|
|
|
|
meta.WithPath(filepath.Join(dir, fmt.Sprintf("%d.metabase", id))),
|
|
|
|
meta.WithPermissions(0700),
|
|
|
|
meta.WithEpochState(epochState{})),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
e, ids := te.engine, te.shardIDs
|
2022-09-12 11:48:21 +00:00
|
|
|
require.NoError(t, e.Open())
|
2023-03-23 14:59:14 +00:00
|
|
|
require.NoError(t, e.Init(context.Background()))
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
objects := make([]*objectSDK.Object, 0, objPerShard*len(ids))
|
2023-01-16 06:38:50 +00:00
|
|
|
|
|
|
|
for _, sh := range ids {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObjectWithCID(cidtest.ID())
|
2023-01-16 06:38:50 +00:00
|
|
|
objects = append(objects, obj)
|
|
|
|
|
|
|
|
var putPrm shard.PutPrm
|
|
|
|
putPrm.SetObject(obj)
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := e.shards[sh.String()].Put(context.Background(), putPrm)
|
2023-01-16 06:38:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:48:21 +00:00
|
|
|
for i := 0; ; i++ {
|
2023-03-20 14:10:26 +00:00
|
|
|
objects = append(objects, testutil.GenerateObjectWithCID(cidtest.ID()))
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
var putPrm PutPrm
|
2023-01-16 06:38:50 +00:00
|
|
|
putPrm.WithObject(objects[len(objects)-1])
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
err := e.Put(context.Background(), putPrm)
|
2022-09-12 11:48:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-09-19 10:31:55 +00:00
|
|
|
res, err := e.shards[ids[len(ids)-1].String()].List()
|
2022-09-12 11:48:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if len(res.AddressList()) == objPerShard {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-09-19 10:31:55 +00:00
|
|
|
return e, ids, objects
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvacuateShard(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-09-19 10:31:55 +00:00
|
|
|
const objPerShard = 3
|
|
|
|
|
|
|
|
e, ids, objects := newEngineEvacuate(t, 3, objPerShard)
|
|
|
|
|
|
|
|
evacuateShardID := ids[2].String()
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
checkHasObjects := func(t *testing.T) {
|
|
|
|
for i := range objects {
|
|
|
|
var prm GetPrm
|
|
|
|
prm.WithAddress(objectCore.AddressOf(objects[i]))
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
_, err := e.Get(context.Background(), prm)
|
2022-09-12 11:48:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkHasObjects(t)
|
|
|
|
|
|
|
|
var prm EvacuateShardPrm
|
2022-10-10 17:54:14 +00:00
|
|
|
prm.WithShardIDList(ids[2:3])
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
t.Run("must be read-only", func(t *testing.T) {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2023-04-14 06:38:29 +00:00
|
|
|
require.ErrorIs(t, err, ErrMustBeReadOnly)
|
2022-09-12 11:48:21 +00:00
|
|
|
require.Equal(t, 0, res.Count())
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, e.shards[evacuateShardID].SetMode(mode.ReadOnly))
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-09-12 11:48:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, objPerShard, res.count)
|
|
|
|
|
|
|
|
// We check that all objects are available both before and after shard removal.
|
|
|
|
// First case is a real-world use-case. It ensures that an object can be put in presense
|
|
|
|
// of all metabase checks/marks.
|
|
|
|
// Second case ensures that all objects are indeed moved and available.
|
|
|
|
checkHasObjects(t)
|
|
|
|
|
2022-09-13 11:18:00 +00:00
|
|
|
// Calling it again is OK, but all objects are already moved, so no new PUTs should be done.
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = e.Evacuate(context.Background(), prm)
|
2022-09-13 11:18:00 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, res.count)
|
|
|
|
|
|
|
|
checkHasObjects(t)
|
|
|
|
|
2022-09-12 11:48:21 +00:00
|
|
|
e.mtx.Lock()
|
|
|
|
delete(e.shards, evacuateShardID)
|
|
|
|
delete(e.shardPools, evacuateShardID)
|
|
|
|
e.mtx.Unlock()
|
|
|
|
|
|
|
|
checkHasObjects(t)
|
|
|
|
}
|
2022-09-19 10:31:55 +00:00
|
|
|
|
|
|
|
func TestEvacuateNetwork(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-09-19 10:31:55 +00:00
|
|
|
var errReplication = errors.New("handler error")
|
|
|
|
|
2023-05-02 11:16:13 +00:00
|
|
|
acceptOneOf := func(objects []*objectSDK.Object, max int) func(context.Context, oid.Address, *objectSDK.Object) error {
|
2022-09-19 10:31:55 +00:00
|
|
|
var n int
|
2023-05-02 11:16:13 +00:00
|
|
|
return func(_ context.Context, addr oid.Address, obj *objectSDK.Object) error {
|
2022-09-19 10:31:55 +00:00
|
|
|
if n == max {
|
|
|
|
return errReplication
|
|
|
|
}
|
|
|
|
|
|
|
|
n++
|
|
|
|
for i := range objects {
|
|
|
|
if addr == objectCore.AddressOf(objects[i]) {
|
|
|
|
require.Equal(t, objects[i], obj)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.FailNow(t, "handler was called with an unexpected object: %s", addr)
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("single shard", func(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
2022-09-19 10:31:55 +00:00
|
|
|
e, ids, objects := newEngineEvacuate(t, 1, 3)
|
|
|
|
evacuateShardID := ids[0].String()
|
|
|
|
|
|
|
|
require.NoError(t, e.shards[evacuateShardID].SetMode(mode.ReadOnly))
|
|
|
|
|
|
|
|
var prm EvacuateShardPrm
|
2022-10-10 17:54:14 +00:00
|
|
|
prm.shardID = ids[0:1]
|
2022-09-19 10:31:55 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-09-19 10:31:55 +00:00
|
|
|
require.ErrorIs(t, err, errMustHaveTwoShards)
|
|
|
|
require.Equal(t, 0, res.Count())
|
|
|
|
|
|
|
|
prm.handler = acceptOneOf(objects, 2)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = e.Evacuate(context.Background(), prm)
|
2022-09-19 10:31:55 +00:00
|
|
|
require.ErrorIs(t, err, errReplication)
|
|
|
|
require.Equal(t, 2, res.Count())
|
|
|
|
})
|
2022-10-10 17:54:14 +00:00
|
|
|
t.Run("multiple shards, evacuate one", func(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
2022-09-19 10:31:55 +00:00
|
|
|
e, ids, objects := newEngineEvacuate(t, 2, 3)
|
|
|
|
|
|
|
|
require.NoError(t, e.shards[ids[0].String()].SetMode(mode.ReadOnly))
|
|
|
|
require.NoError(t, e.shards[ids[1].String()].SetMode(mode.ReadOnly))
|
|
|
|
|
|
|
|
var prm EvacuateShardPrm
|
2022-10-10 17:54:14 +00:00
|
|
|
prm.shardID = ids[1:2]
|
2022-09-19 10:31:55 +00:00
|
|
|
prm.handler = acceptOneOf(objects, 2)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-09-19 10:31:55 +00:00
|
|
|
require.ErrorIs(t, err, errReplication)
|
|
|
|
require.Equal(t, 2, res.Count())
|
|
|
|
|
|
|
|
t.Run("no errors", func(t *testing.T) {
|
|
|
|
prm.handler = acceptOneOf(objects, 3)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-09-19 10:31:55 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, res.Count())
|
|
|
|
})
|
|
|
|
})
|
2022-10-10 17:54:14 +00:00
|
|
|
t.Run("multiple shards, evacuate many", func(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
2022-10-10 17:54:14 +00:00
|
|
|
e, ids, objects := newEngineEvacuate(t, 4, 5)
|
|
|
|
evacuateIDs := ids[0:3]
|
|
|
|
|
|
|
|
var totalCount int
|
|
|
|
for i := range evacuateIDs {
|
|
|
|
res, err := e.shards[ids[i].String()].List()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
totalCount += len(res.AddressList())
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range ids {
|
|
|
|
require.NoError(t, e.shards[ids[i].String()].SetMode(mode.ReadOnly))
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm EvacuateShardPrm
|
|
|
|
prm.shardID = evacuateIDs
|
|
|
|
prm.handler = acceptOneOf(objects, totalCount-1)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-10-10 17:54:14 +00:00
|
|
|
require.ErrorIs(t, err, errReplication)
|
|
|
|
require.Equal(t, totalCount-1, res.Count())
|
|
|
|
|
|
|
|
t.Run("no errors", func(t *testing.T) {
|
|
|
|
prm.handler = acceptOneOf(objects, totalCount)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := e.Evacuate(context.Background(), prm)
|
2022-10-10 17:54:14 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, totalCount, res.Count())
|
|
|
|
})
|
|
|
|
})
|
2022-09-19 10:31:55 +00:00
|
|
|
}
|
2023-05-02 11:16:13 +00:00
|
|
|
|
|
|
|
func TestEvacuateCancellation(t *testing.T) {
|
2023-05-05 13:17:29 +00:00
|
|
|
t.Parallel()
|
2023-05-02 11:16:13 +00:00
|
|
|
e, ids, _ := newEngineEvacuate(t, 2, 3)
|
|
|
|
|
|
|
|
require.NoError(t, e.shards[ids[0].String()].SetMode(mode.ReadOnly))
|
|
|
|
require.NoError(t, e.shards[ids[1].String()].SetMode(mode.ReadOnly))
|
|
|
|
|
|
|
|
var prm EvacuateShardPrm
|
|
|
|
prm.shardID = ids[1:2]
|
|
|
|
prm.handler = func(ctx context.Context, a oid.Address, o *objectSDK.Object) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
res, err := e.Evacuate(ctx, prm)
|
|
|
|
require.ErrorContains(t, err, "context canceled")
|
|
|
|
require.Equal(t, 0, res.Count())
|
|
|
|
}
|