2022-09-12 11:48:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-09-12 11:48:21 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
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/util"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2022-09-12 11:48:21 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EvacuateShardPrm represents parameters for the EvacuateShard operation.
|
|
|
|
type EvacuateShardPrm struct {
|
2022-10-10 17:54:14 +00:00
|
|
|
shardID []*shard.ID
|
2022-09-19 10:31:55 +00:00
|
|
|
handler func(oid.Address, *objectSDK.Object) error
|
2022-09-12 11:48:21 +00:00
|
|
|
ignoreErrors bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// EvacuateShardRes represents result of the EvacuateShard operation.
|
|
|
|
type EvacuateShardRes struct {
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
// WithShardIDList sets shard ID.
|
|
|
|
func (p *EvacuateShardPrm) WithShardIDList(id []*shard.ID) {
|
2022-09-12 11:48:21 +00:00
|
|
|
p.shardID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithIgnoreErrors sets flag to ignore errors.
|
|
|
|
func (p *EvacuateShardPrm) WithIgnoreErrors(ignore bool) {
|
|
|
|
p.ignoreErrors = ignore
|
|
|
|
}
|
|
|
|
|
2022-09-19 10:31:55 +00:00
|
|
|
// WithFaultHandler sets handler to call for objects which cannot be saved on other shards.
|
|
|
|
func (p *EvacuateShardPrm) WithFaultHandler(f func(oid.Address, *objectSDK.Object) error) {
|
|
|
|
p.handler = f
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:48:21 +00:00
|
|
|
// Count returns amount of evacuated objects.
|
2022-09-19 10:31:55 +00:00
|
|
|
// Objects for which handler returned no error are also assumed evacuated.
|
2022-09-12 11:48:21 +00:00
|
|
|
func (p EvacuateShardRes) Count() int {
|
|
|
|
return p.count
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultEvacuateBatchSize = 100
|
|
|
|
|
|
|
|
type pooledShard struct {
|
|
|
|
hashedShard
|
|
|
|
pool util.WorkerPool
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
var errMustHaveTwoShards = errors.New("must have at least 1 spare shard")
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
// Evacuate moves data from one shard to the others.
|
|
|
|
// The shard being moved must be in read-only mode.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) Evacuate(ctx context.Context, prm EvacuateShardPrm) (EvacuateShardRes, error) {
|
2023-03-31 08:33:08 +00:00
|
|
|
shardIDs := make([]string, len(prm.shardID))
|
2022-10-10 17:54:14 +00:00
|
|
|
for i := range prm.shardID {
|
2023-03-31 08:33:08 +00:00
|
|
|
shardIDs[i] = prm.shardID[i].String()
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
shards, weights, err := e.getActualShards(shardIDs, prm.handler != nil)
|
|
|
|
if err != nil {
|
|
|
|
return EvacuateShardRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
shardsToEvacuate := make(map[string]*shard.Shard)
|
|
|
|
for i := range shardIDs {
|
|
|
|
for j := range shards {
|
|
|
|
if shards[j].ID().String() == shardIDs[i] {
|
|
|
|
shardsToEvacuate[shardIDs[i]] = shards[j].Shard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.log.Info("started shards evacuation", zap.Strings("shard_ids", shardIDs))
|
|
|
|
|
|
|
|
var res EvacuateShardRes
|
|
|
|
|
|
|
|
for _, shardID := range shardIDs {
|
2023-03-13 11:37:35 +00:00
|
|
|
if err = e.evacuateShard(ctx, shardID, prm, &res, shards, weights, shardsToEvacuate); err != nil {
|
2023-03-31 08:33:08 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.log.Info("finished shards evacuation", zap.Strings("shard_ids", shardIDs))
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) evacuateShard(ctx context.Context, shardID string, prm EvacuateShardPrm, res *EvacuateShardRes,
|
2023-03-31 08:33:08 +00:00
|
|
|
shards []pooledShard, weights []float64, shardsToEvacuate map[string]*shard.Shard) error {
|
|
|
|
var listPrm shard.ListWithCursorPrm
|
|
|
|
listPrm.WithCount(defaultEvacuateBatchSize)
|
|
|
|
|
|
|
|
sh := shardsToEvacuate[shardID]
|
|
|
|
|
|
|
|
var c *meta.Cursor
|
|
|
|
for {
|
|
|
|
listPrm.WithCursor(c)
|
|
|
|
|
|
|
|
// TODO (@fyrchik): #1731 this approach doesn't work in degraded modes
|
|
|
|
// because ListWithCursor works only with the metabase.
|
|
|
|
listRes, err := sh.ListWithCursor(listPrm)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, meta.ErrEndOfListing) || errors.Is(err, shard.ErrDegradedMode) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
if err = e.evacuateObjects(ctx, sh, listRes.AddressList(), prm, res, shards, weights, shardsToEvacuate); err != nil {
|
2023-03-31 08:33:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c = listRes.Cursor()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) getActualShards(shardIDs []string, handlerDefined bool) ([]pooledShard, []float64, error) {
|
2022-09-12 11:48:21 +00:00
|
|
|
e.mtx.RLock()
|
2023-03-31 08:33:08 +00:00
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
for i := range shardIDs {
|
|
|
|
sh, ok := e.shards[shardIDs[i]]
|
2022-10-10 17:54:14 +00:00
|
|
|
if !ok {
|
2023-03-31 08:33:08 +00:00
|
|
|
return nil, nil, errShardNotFound
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
if !sh.GetMode().ReadOnly() {
|
2023-03-31 08:33:08 +00:00
|
|
|
return nil, nil, shard.ErrMustBeReadOnly
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
if len(e.shards)-len(shardIDs) < 1 && !handlerDefined {
|
|
|
|
return nil, nil, errMustHaveTwoShards
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We must have all shards, to have correct information about their
|
|
|
|
// indexes in a sorted slice and set appropriate marks in the metabase.
|
|
|
|
// Evacuated shard is skipped during put.
|
|
|
|
shards := make([]pooledShard, 0, len(e.shards))
|
|
|
|
for id := range e.shards {
|
|
|
|
shards = append(shards, pooledShard{
|
|
|
|
hashedShard: hashedShard(e.shards[id]),
|
|
|
|
pool: e.shardPools[id],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
weights := make([]float64, 0, len(shards))
|
|
|
|
for i := range shards {
|
|
|
|
weights = append(weights, e.shardWeight(shards[i].Shard))
|
|
|
|
}
|
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
return shards, weights, nil
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) evacuateObjects(ctx context.Context, sh *shard.Shard, toEvacuate []object.AddressWithType, prm EvacuateShardPrm, res *EvacuateShardRes,
|
2023-03-31 08:33:08 +00:00
|
|
|
shards []pooledShard, weights []float64, shardsToEvacuate map[string]*shard.Shard) error {
|
|
|
|
for i := range toEvacuate {
|
|
|
|
addr := toEvacuate[i].Address
|
|
|
|
|
|
|
|
var getPrm shard.GetPrm
|
|
|
|
getPrm.SetAddress(addr)
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
getRes, err := sh.Get(ctx, getPrm)
|
2023-03-31 08:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if prm.ignoreErrors {
|
|
|
|
continue
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
2023-03-31 08:33:08 +00:00
|
|
|
return err
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
if e.tryEvacuateObject(ctx, addr, getRes.Object(), sh, res, shards, weights, shardsToEvacuate) {
|
2023-03-31 08:33:08 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
if prm.handler == nil {
|
|
|
|
// Do not check ignoreErrors flag here because
|
|
|
|
// ignoring errors on put make this command kinda useless.
|
|
|
|
return fmt.Errorf("%w: %s", errPutShard, toEvacuate[i])
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
err = prm.handler(addr, getRes.Object())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
res.count++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
func (e *StorageEngine) tryEvacuateObject(ctx context.Context, addr oid.Address, object *objectSDK.Object, sh *shard.Shard, res *EvacuateShardRes,
|
2023-03-31 08:33:08 +00:00
|
|
|
shards []pooledShard, weights []float64, shardsToEvacuate map[string]*shard.Shard) bool {
|
|
|
|
hrw.SortHasherSliceByWeightValue(shards, weights, hrw.Hash([]byte(addr.EncodeToString())))
|
|
|
|
for j := range shards {
|
|
|
|
if _, ok := shardsToEvacuate[shards[j].ID().String()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-13 11:37:35 +00:00
|
|
|
putDone, exists := e.putToShard(ctx, shards[j].hashedShard, j, shards[j].pool, addr, object)
|
2023-03-31 08:33:08 +00:00
|
|
|
if putDone || exists {
|
|
|
|
if putDone {
|
|
|
|
e.log.Debug("object is moved to another shard",
|
|
|
|
zap.Stringer("from", sh.ID()),
|
|
|
|
zap.Stringer("to", shards[j].ID()),
|
|
|
|
zap.Stringer("addr", addr))
|
2022-10-10 17:54:14 +00:00
|
|
|
res.count++
|
2022-09-19 10:31:55 +00:00
|
|
|
}
|
2023-03-31 08:33:08 +00:00
|
|
|
return true
|
2022-10-10 17:54:14 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
2022-10-10 17:54:14 +00:00
|
|
|
|
2023-03-31 08:33:08 +00:00
|
|
|
return false
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|