2022-09-12 11:48:21 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
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-20 07:01:27 +00:00
|
|
|
//
|
|
|
|
// nolint: funlen
|
2022-09-12 11:48:21 +00:00
|
|
|
func (e *StorageEngine) Evacuate(prm EvacuateShardPrm) (EvacuateShardRes, error) {
|
2022-10-10 17:54:14 +00:00
|
|
|
sidList := make([]string, len(prm.shardID))
|
|
|
|
for i := range prm.shardID {
|
|
|
|
sidList[i] = prm.shardID[i].String()
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
|
|
|
e.mtx.RLock()
|
2022-10-10 17:54:14 +00:00
|
|
|
for i := range sidList {
|
|
|
|
sh, ok := e.shards[sidList[i]]
|
|
|
|
if !ok {
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
return EvacuateShardRes{}, errShardNotFound
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
if !sh.GetMode().ReadOnly() {
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
return EvacuateShardRes{}, shard.ErrMustBeReadOnly
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
if len(e.shards)-len(sidList) < 1 && prm.handler == nil {
|
2022-09-12 11:48:21 +00:00
|
|
|
e.mtx.RUnlock()
|
2022-10-10 17:54:14 +00:00
|
|
|
return EvacuateShardRes{}, errMustHaveTwoShards
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 08:35:58 +00:00
|
|
|
e.log.Info("started shards evacuation", zap.Strings("shard_ids", sidList))
|
|
|
|
|
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],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
|
|
|
weights := make([]float64, 0, len(shards))
|
|
|
|
for i := range shards {
|
|
|
|
weights = append(weights, e.shardWeight(shards[i].Shard))
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
shardMap := make(map[string]*shard.Shard)
|
|
|
|
for i := range sidList {
|
|
|
|
for j := range shards {
|
|
|
|
if shards[j].ID().String() == sidList[i] {
|
|
|
|
shardMap[sidList[i]] = shards[j].Shard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-12 11:48:21 +00:00
|
|
|
var listPrm shard.ListWithCursorPrm
|
|
|
|
listPrm.WithCount(defaultEvacuateBatchSize)
|
|
|
|
|
|
|
|
var res EvacuateShardRes
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
mainLoop:
|
|
|
|
for n := range sidList {
|
|
|
|
sh := shardMap[sidList[n]]
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
var c *meta.Cursor
|
|
|
|
for {
|
|
|
|
listPrm.WithCursor(c)
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
// TODO (@fyrchik): #1731 this approach doesn't work in degraded modes
|
|
|
|
// because ListWithCursor works only with the metabase.
|
|
|
|
listRes, err := sh.ListWithCursor(listPrm)
|
2022-09-12 11:48:21 +00:00
|
|
|
if err != nil {
|
2022-10-26 06:12:09 +00:00
|
|
|
if errors.Is(err, meta.ErrEndOfListing) || errors.Is(err, shard.ErrDegradedMode) {
|
2022-10-10 17:54:14 +00:00
|
|
|
continue mainLoop
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
// TODO (@fyrchik): #1731 parallelize the loop
|
|
|
|
lst := listRes.AddressList()
|
|
|
|
|
|
|
|
loop:
|
|
|
|
for i := range lst {
|
2022-11-12 13:48:44 +00:00
|
|
|
addr := lst[i].Address
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
var getPrm shard.GetPrm
|
2022-11-12 13:48:44 +00:00
|
|
|
getPrm.SetAddress(addr)
|
2022-10-10 17:54:14 +00:00
|
|
|
|
|
|
|
getRes, err := sh.Get(getPrm)
|
|
|
|
if err != nil {
|
|
|
|
if prm.ignoreErrors {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return res, err
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
2022-10-10 17:54:14 +00:00
|
|
|
|
2023-02-27 13:39:15 +00:00
|
|
|
hrw.SortHasherSliceByWeightValue(shards, weights, hrw.Hash([]byte(addr.EncodeToString())))
|
2022-10-10 17:54:14 +00:00
|
|
|
for j := range shards {
|
|
|
|
if _, ok := shardMap[shards[j].ID().String()]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-12 13:48:44 +00:00
|
|
|
putDone, exists := e.putToShard(shards[j].hashedShard, j, shards[j].pool, addr, getRes.Object())
|
2022-10-10 17:54:14 +00:00
|
|
|
if putDone || exists {
|
|
|
|
if putDone {
|
|
|
|
e.log.Debug("object is moved to another shard",
|
|
|
|
zap.String("from", sidList[n]),
|
|
|
|
zap.Stringer("to", shards[j].ID()),
|
2022-11-12 13:48:44 +00:00
|
|
|
zap.Stringer("addr", addr))
|
2022-10-10 17:54:14 +00:00
|
|
|
|
|
|
|
res.count++
|
|
|
|
}
|
|
|
|
continue loop
|
2022-09-13 11:18:00 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
if prm.handler == nil {
|
|
|
|
// Do not check ignoreErrors flag here because
|
|
|
|
// ignoring errors on put make this command kinda useless.
|
|
|
|
return res, fmt.Errorf("%w: %s", errPutShard, lst[i])
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
err = prm.handler(addr, getRes.Object())
|
2022-10-10 17:54:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
res.count++
|
2022-09-19 10:31:55 +00:00
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
|
2022-10-10 17:54:14 +00:00
|
|
|
c = listRes.Cursor()
|
|
|
|
}
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|
2022-10-10 17:54:14 +00:00
|
|
|
|
2023-01-23 08:35:58 +00:00
|
|
|
e.log.Info("finished shards evacuation",
|
|
|
|
zap.Strings("shard_ids", sidList))
|
2022-10-10 17:54:14 +00:00
|
|
|
return res, nil
|
2022-09-12 11:48:21 +00:00
|
|
|
}
|