2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2023-08-31 16:26:47 +00:00
|
|
|
"context"
|
2024-01-30 15:18:58 +00:00
|
|
|
"errors"
|
2020-11-17 12:26:03 +00:00
|
|
|
"fmt"
|
2024-01-30 15:18:58 +00:00
|
|
|
"sync"
|
2023-05-19 15:06:20 +00:00
|
|
|
"sync/atomic"
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"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/local_object_storage/util/logicerr"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/hrw"
|
2020-11-17 12:26:03 +00:00
|
|
|
"github.com/google/uuid"
|
2021-10-08 12:25:45 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2022-09-26 21:39:34 +00:00
|
|
|
"go.uber.org/zap"
|
2024-01-30 15:18:58 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2020-11-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
2022-10-31 07:02:30 +00:00
|
|
|
var errShardNotFound = logicerr.New("shard not found")
|
2020-11-19 13:04:04 +00:00
|
|
|
|
2023-02-27 13:16:37 +00:00
|
|
|
type hashedShard struct {
|
|
|
|
shardWrapper
|
|
|
|
hash uint64
|
|
|
|
}
|
2020-11-30 14:58:44 +00:00
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
type metricsWithID struct {
|
|
|
|
id string
|
|
|
|
mw MetricRegister
|
|
|
|
}
|
|
|
|
|
2022-10-12 17:55:35 +00:00
|
|
|
func (m *metricsWithID) SetShardID(id string) {
|
|
|
|
// concurrent settings are not expected =>
|
|
|
|
// no mutex protection
|
|
|
|
m.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) SetObjectCounter(objectType string, v uint64) {
|
2022-09-09 11:37:35 +00:00
|
|
|
m.mw.SetObjectCounter(m.id, objectType, v)
|
2022-08-19 17:24:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:55:35 +00:00
|
|
|
func (m *metricsWithID) AddToObjectCounter(objectType string, delta int) {
|
2022-09-09 11:37:35 +00:00
|
|
|
m.mw.AddToObjectCounter(m.id, objectType, delta)
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:55:35 +00:00
|
|
|
func (m *metricsWithID) IncObjectCounter(objectType string) {
|
2022-09-09 11:37:35 +00:00
|
|
|
m.mw.AddToObjectCounter(m.id, objectType, +1)
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 08:00:44 +00:00
|
|
|
func (m *metricsWithID) SetMode(mode mode.Mode) {
|
|
|
|
m.mw.SetMode(m.id, mode)
|
2022-12-09 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 11:59:22 +00:00
|
|
|
func (m *metricsWithID) AddToContainerSize(cnr string, size int64) {
|
|
|
|
m.mw.AddToContainerSize(cnr, size)
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:01:25 +00:00
|
|
|
func (m *metricsWithID) AddToPayloadSize(size int64) {
|
|
|
|
m.mw.AddToPayloadCounter(m.id, size)
|
|
|
|
}
|
|
|
|
|
2023-06-01 14:28:04 +00:00
|
|
|
func (m *metricsWithID) IncErrorCounter() {
|
|
|
|
m.mw.IncErrorCounter(m.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) ClearErrorCounter() {
|
|
|
|
m.mw.ClearErrorCounter(m.id)
|
|
|
|
}
|
|
|
|
|
2023-06-13 16:48:15 +00:00
|
|
|
func (m *metricsWithID) DeleteShardMetrics() {
|
|
|
|
m.mw.DeleteShardMetrics(m.id)
|
2023-06-01 14:28:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
func (m *metricsWithID) SetContainerObjectsCount(cnrID string, objectType string, value uint64) {
|
|
|
|
m.mw.SetContainerObjectCounter(m.id, cnrID, objectType, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) IncContainerObjectsCount(cnrID string, objectType string) {
|
|
|
|
m.mw.IncContainerObjectCounter(m.id, cnrID, objectType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) SubContainerObjectsCount(cnrID string, objectType string, value uint64) {
|
|
|
|
m.mw.SubContainerObjectCounter(m.id, cnrID, objectType, value)
|
|
|
|
}
|
|
|
|
|
2024-03-12 14:36:26 +00:00
|
|
|
func (m *metricsWithID) IncRefillObjectsCount(path string, size int, success bool) {
|
|
|
|
m.mw.IncRefillObjectsCount(m.id, path, size, success)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) SetRefillPercent(path string, percent uint32) {
|
|
|
|
m.mw.SetRefillPercent(m.id, path, percent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metricsWithID) SetRefillStatus(path string, status string) {
|
|
|
|
m.mw.SetRefillStatus(m.id, path, status)
|
|
|
|
}
|
|
|
|
|
2024-09-03 09:18:10 +00:00
|
|
|
func (m *metricsWithID) SetEvacuationInProgress(value bool) {
|
|
|
|
m.mw.SetEvacuationInProgress(m.id, value)
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
// AddShard adds a new shard to the storage engine.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow adding a shard.
|
|
|
|
// Otherwise returns the ID of the added shard.
|
2023-08-31 16:26:47 +00:00
|
|
|
func (e *StorageEngine) AddShard(ctx context.Context, opts ...shard.Option) (*shard.ID, error) {
|
|
|
|
sh, err := e.createShard(ctx, opts)
|
2022-09-27 21:43:38 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return nil, fmt.Errorf("create a shard: %w", err)
|
2022-09-27 21:43:38 +00:00
|
|
|
}
|
2022-09-26 21:39:34 +00:00
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
err = e.addShard(sh)
|
2022-03-01 08:59:05 +00:00
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return nil, fmt.Errorf("add %s shard: %w", sh.ID().String(), err)
|
2022-03-11 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 07:40:56 +00:00
|
|
|
e.cfg.metrics.SetMode(sh.ID().String(), sh.GetMode())
|
2022-12-09 13:52:13 +00:00
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
return sh.ID(), nil
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
func (e *StorageEngine) createShard(ctx context.Context, opts []shard.Option) (*shard.Shard, error) {
|
2022-03-11 07:13:33 +00:00
|
|
|
id, err := generateShardID()
|
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return nil, fmt.Errorf("generate shard ID: %w", err)
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 08:23:17 +00:00
|
|
|
opts = e.appendMetrics(id, opts)
|
|
|
|
|
|
|
|
sh := shard.New(append(opts,
|
|
|
|
shard.WithID(id),
|
|
|
|
shard.WithExpiredTombstonesCallback(e.processExpiredTombstones),
|
|
|
|
shard.WithExpiredLocksCallback(e.processExpiredLocks),
|
|
|
|
shard.WithDeletedLockCallback(e.processDeletedLocks),
|
2024-10-04 11:58:45 +00:00
|
|
|
shard.WithReportErrorFunc(e.reportShardErrorByID),
|
2023-12-27 15:58:36 +00:00
|
|
|
shard.WithZeroSizeCallback(e.processZeroSizeContainers),
|
2023-12-27 15:23:12 +00:00
|
|
|
shard.WithZeroCountCallback(e.processZeroCountContainers),
|
2023-06-20 08:23:17 +00:00
|
|
|
)...)
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
if err := sh.UpdateID(ctx); err != nil {
|
|
|
|
e.log.Warn(ctx, logs.FailedToUpdateShardID, zap.Stringer("shard_id", sh.ID()), zap.String("metabase_path", sh.DumpInfo().MetaBaseInfo.Path), zap.Error(err))
|
2023-06-20 08:23:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 10:39:21 +00:00
|
|
|
return sh, nil
|
2023-06-20 08:23:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) appendMetrics(id *shard.ID, opts []shard.Option) []shard.Option {
|
2022-09-27 21:43:38 +00:00
|
|
|
e.mtx.RLock()
|
2023-06-20 08:23:17 +00:00
|
|
|
defer e.mtx.RUnlock()
|
2022-09-27 21:43:38 +00:00
|
|
|
|
2024-10-03 07:40:56 +00:00
|
|
|
opts = append(opts,
|
|
|
|
shard.WithMetricsWriter(
|
|
|
|
&metricsWithID{
|
|
|
|
id: id.String(),
|
|
|
|
mw: e.metrics,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
shard.WithWriteCacheMetrics(
|
|
|
|
&writeCacheMetrics{
|
|
|
|
shardID: id.String(),
|
|
|
|
metrics: e.metrics.WriteCache(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
shard.WithGCMetrics(
|
|
|
|
&gcMetrics{
|
|
|
|
storage: e.metrics.GC(),
|
|
|
|
shardID: id.String(),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2022-08-19 16:49:09 +00:00
|
|
|
|
2023-06-20 08:23:17 +00:00
|
|
|
return opts
|
2022-09-27 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) addShard(sh *shard.Shard) error {
|
|
|
|
e.mtx.Lock()
|
|
|
|
defer e.mtx.Unlock()
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(int(e.shardPoolSize), ants.WithNonblocking(true))
|
|
|
|
if err != nil {
|
2024-12-18 06:38:14 +00:00
|
|
|
return fmt.Errorf("create pool: %w", err)
|
2022-09-27 21:43:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 08:59:05 +00:00
|
|
|
strID := sh.ID().String()
|
|
|
|
if _, ok := e.shards[strID]; ok {
|
2022-09-27 21:43:38 +00:00
|
|
|
return fmt.Errorf("shard with id %s was already added", strID)
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|
2021-10-08 12:25:45 +00:00
|
|
|
|
2023-02-27 13:16:37 +00:00
|
|
|
e.shards[strID] = hashedShard{
|
|
|
|
shardWrapper: shardWrapper{
|
2023-05-19 15:06:20 +00:00
|
|
|
errorCount: new(atomic.Uint32),
|
2023-02-27 13:16:37 +00:00
|
|
|
Shard: sh,
|
|
|
|
},
|
2023-06-02 12:39:16 +00:00
|
|
|
hash: hrw.StringHash(strID),
|
2022-01-31 14:58:32 +00:00
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2021-10-08 12:25:45 +00:00
|
|
|
e.shardPools[strID] = pool
|
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
return nil
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 21:39:34 +00:00
|
|
|
// removeShards removes specified shards. Skips non-existent shards.
|
2022-09-30 14:01:48 +00:00
|
|
|
// Logs errors about shards that it could not Close after the removal.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (e *StorageEngine) removeShards(ctx context.Context, ids ...string) {
|
2022-09-30 14:01:48 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-27 13:16:37 +00:00
|
|
|
ss := make([]hashedShard, 0, len(ids))
|
2022-09-26 21:39:34 +00:00
|
|
|
|
2022-09-30 14:01:48 +00:00
|
|
|
e.mtx.Lock()
|
2022-09-26 21:39:34 +00:00
|
|
|
for _, id := range ids {
|
|
|
|
sh, found := e.shards[id]
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-04 12:07:20 +00:00
|
|
|
e.metrics.DeleteShardMetrics(id)
|
2023-06-01 14:28:04 +00:00
|
|
|
|
2022-09-30 14:01:48 +00:00
|
|
|
ss = append(ss, sh)
|
2022-09-26 21:39:34 +00:00
|
|
|
delete(e.shards, id)
|
|
|
|
|
|
|
|
pool, ok := e.shardPools[id]
|
|
|
|
if ok {
|
|
|
|
pool.Release()
|
|
|
|
delete(e.shardPools, id)
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
e.log.Info(ctx, logs.EngineShardHasBeenRemoved,
|
2022-09-26 21:39:34 +00:00
|
|
|
zap.String("id", id))
|
|
|
|
}
|
2022-09-30 14:01:48 +00:00
|
|
|
e.mtx.Unlock()
|
2022-09-26 21:39:34 +00:00
|
|
|
|
2022-09-30 14:01:48 +00:00
|
|
|
for _, sh := range ss {
|
2024-10-21 08:56:38 +00:00
|
|
|
err := sh.SetMode(ctx, mode.Disabled)
|
2023-06-20 08:59:18 +00:00
|
|
|
if err != nil {
|
2024-10-21 08:56:38 +00:00
|
|
|
e.log.Error(ctx, logs.EngineCouldNotChangeShardModeToDisabled,
|
2023-06-20 08:59:18 +00:00
|
|
|
zap.Stringer("id", sh.ID()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
2024-10-21 08:56:38 +00:00
|
|
|
err = sh.Close(ctx)
|
2022-09-30 14:01:48 +00:00
|
|
|
if err != nil {
|
2024-10-21 08:56:38 +00:00
|
|
|
e.log.Error(ctx, logs.EngineCouldNotCloseRemovedShard,
|
2022-09-30 14:01:48 +00:00
|
|
|
zap.Stringer("id", sh.ID()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 21:39:34 +00:00
|
|
|
}
|
|
|
|
|
2021-04-14 08:47:42 +00:00
|
|
|
func generateShardID() (*shard.ID, error) {
|
2020-11-17 12:26:03 +00:00
|
|
|
uid, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bin, err := uid.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return shard.NewIDFromBytes(bin), nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:19:52 +00:00
|
|
|
func (e *StorageEngine) sortShards(objAddr interface{ EncodeToString() string }) []hashedShard {
|
2020-11-18 12:06:47 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
2023-06-02 12:39:16 +00:00
|
|
|
h := hrw.StringHash(objAddr.EncodeToString())
|
2020-11-30 14:58:44 +00:00
|
|
|
shards := make([]hashedShard, 0, len(e.shards))
|
2020-11-17 12:26:03 +00:00
|
|
|
for _, sh := range e.shards {
|
2022-01-31 14:58:32 +00:00
|
|
|
shards = append(shards, hashedShard(sh))
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2024-02-26 08:19:52 +00:00
|
|
|
hrw.SortHasherSliceByValue(shards, h)
|
2020-11-17 12:26:03 +00:00
|
|
|
return shards
|
|
|
|
}
|
|
|
|
|
2020-12-01 10:18:25 +00:00
|
|
|
func (e *StorageEngine) unsortedShards() []hashedShard {
|
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
shards := make([]hashedShard, 0, len(e.shards))
|
|
|
|
|
|
|
|
for _, sh := range e.shards {
|
2022-01-31 14:58:32 +00:00
|
|
|
shards = append(shards, hashedShard(sh))
|
2020-12-01 10:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return shards
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *StorageEngine) iterateOverSortedShards(addr oid.Address, handler func(int, hashedShard) (stop bool)) {
|
2024-02-26 08:19:52 +00:00
|
|
|
for i, sh := range e.sortShards(addr) {
|
2022-01-31 14:58:32 +00:00
|
|
|
if handler(i, sh) {
|
2020-12-01 10:18:25 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
func (e *StorageEngine) iterateOverUnsortedShards(handler func(hashedShard) (stop bool)) {
|
2020-12-01 10:18:25 +00:00
|
|
|
for _, sh := range e.unsortedShards() {
|
2022-01-31 14:58:32 +00:00
|
|
|
if handler(sh) {
|
2020-11-17 12:26:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 13:04:04 +00:00
|
|
|
|
|
|
|
// SetShardMode sets mode of the shard with provided identifier.
|
|
|
|
//
|
|
|
|
// Returns an error if shard mode was not set, or shard was not found in storage engine.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (e *StorageEngine) SetShardMode(ctx context.Context, id *shard.ID, m mode.Mode, resetErrorCounter bool) error {
|
2020-11-19 13:04:04 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
for shID, sh := range e.shards {
|
|
|
|
if id.String() == shID {
|
2022-01-31 15:15:33 +00:00
|
|
|
if resetErrorCounter {
|
|
|
|
sh.errorCount.Store(0)
|
2024-10-04 12:07:20 +00:00
|
|
|
e.metrics.ClearErrorCounter(shID)
|
2022-01-31 15:15:33 +00:00
|
|
|
}
|
2024-10-21 08:56:38 +00:00
|
|
|
return sh.SetMode(ctx, m)
|
2020-11-19 13:04:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errShardNotFound
|
|
|
|
}
|
2020-11-30 14:58:44 +00:00
|
|
|
|
2022-09-26 06:30:41 +00:00
|
|
|
// HandleNewEpoch notifies every shard about NewEpoch event.
|
2024-01-25 08:02:57 +00:00
|
|
|
func (e *StorageEngine) HandleNewEpoch(ctx context.Context, epoch uint64) {
|
2022-09-26 06:30:41 +00:00
|
|
|
ev := shard.EventNewEpoch(epoch)
|
|
|
|
|
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
for _, sh := range e.shards {
|
2024-01-25 08:02:57 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case sh.NotificationChannel() <- ev:
|
2024-07-10 08:27:40 +00:00
|
|
|
default:
|
2024-10-21 07:22:54 +00:00
|
|
|
e.log.Debug(ctx, logs.ShardEventProcessingInProgress,
|
2024-07-10 08:27:40 +00:00
|
|
|
zap.Uint64("epoch", epoch), zap.Stringer("shard", sh.ID()))
|
2024-01-25 08:02:57 +00:00
|
|
|
}
|
2022-09-26 06:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
func (e *StorageEngine) DetachShards(ctx context.Context, ids []*shard.ID) error {
|
2024-01-30 15:18:58 +00:00
|
|
|
if len(ids) == 0 {
|
|
|
|
return logicerr.New("ids must be non-empty")
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
deletedShards, err := e.deleteShards(ctx, ids)
|
2024-01-30 15:18:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
return e.closeShards(ctx, deletedShards)
|
2024-01-30 15:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// closeShards closes deleted shards. Tries to close all shards.
|
|
|
|
// Returns single error with joined shard errors.
|
2024-10-21 08:56:38 +00:00
|
|
|
func (e *StorageEngine) closeShards(ctx context.Context, deletedShards []hashedShard) error {
|
2024-01-30 15:18:58 +00:00
|
|
|
var multiErr error
|
|
|
|
var multiErrGuard sync.Mutex
|
|
|
|
var eg errgroup.Group
|
|
|
|
for _, sh := range deletedShards {
|
|
|
|
eg.Go(func() error {
|
2024-10-21 08:56:38 +00:00
|
|
|
err := sh.SetMode(ctx, mode.Disabled)
|
2024-01-30 15:18:58 +00:00
|
|
|
if err != nil {
|
2024-10-21 08:56:38 +00:00
|
|
|
e.log.Error(ctx, logs.EngineCouldNotChangeShardModeToDisabled,
|
2024-01-30 15:18:58 +00:00
|
|
|
zap.Stringer("id", sh.ID()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
multiErrGuard.Lock()
|
2024-12-18 06:38:14 +00:00
|
|
|
multiErr = errors.Join(multiErr, fmt.Errorf("change shard (id:%s) mode to disabled: %w", sh.ID(), err))
|
2024-01-30 15:18:58 +00:00
|
|
|
multiErrGuard.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-10-21 08:56:38 +00:00
|
|
|
err = sh.Close(ctx)
|
2024-01-30 15:18:58 +00:00
|
|
|
if err != nil {
|
2024-10-21 08:56:38 +00:00
|
|
|
e.log.Error(ctx, logs.EngineCouldNotCloseRemovedShard,
|
2024-01-30 15:18:58 +00:00
|
|
|
zap.Stringer("id", sh.ID()),
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
multiErrGuard.Lock()
|
2024-12-18 06:38:14 +00:00
|
|
|
multiErr = errors.Join(multiErr, fmt.Errorf("close removed shard (id:%s): %w", sh.ID(), err))
|
2024-01-30 15:18:58 +00:00
|
|
|
multiErrGuard.Unlock()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return multiErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteShards deletes shards with specified ids from engine shard list
|
|
|
|
// and releases all engine resources associated with shards.
|
|
|
|
// Returns deleted shards or error if some shard could not be deleted.
|
2024-10-21 13:27:28 +00:00
|
|
|
func (e *StorageEngine) deleteShards(ctx context.Context, ids []*shard.ID) ([]hashedShard, error) {
|
2024-01-30 15:18:58 +00:00
|
|
|
ss := make([]hashedShard, 0, len(ids))
|
|
|
|
|
|
|
|
e.mtx.Lock()
|
|
|
|
defer e.mtx.Unlock()
|
|
|
|
|
|
|
|
for _, id := range ids {
|
|
|
|
idStr := id.String()
|
|
|
|
sh, found := e.shards[idStr]
|
|
|
|
if !found {
|
|
|
|
return nil, errShardNotFound
|
|
|
|
}
|
|
|
|
ss = append(ss, sh)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ss) == len(e.shards) {
|
|
|
|
return nil, logicerr.New("could not delete all the shards")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, sh := range ss {
|
|
|
|
idStr := sh.ID().String()
|
|
|
|
|
2024-10-04 12:07:20 +00:00
|
|
|
e.metrics.DeleteShardMetrics(idStr)
|
2024-01-30 15:18:58 +00:00
|
|
|
|
|
|
|
delete(e.shards, idStr)
|
|
|
|
|
|
|
|
pool, ok := e.shardPools[idStr]
|
|
|
|
if ok {
|
|
|
|
pool.Release()
|
|
|
|
delete(e.shardPools, idStr)
|
|
|
|
}
|
|
|
|
|
2024-10-21 13:27:28 +00:00
|
|
|
e.log.Info(ctx, logs.EngineShardHasBeenRemoved,
|
2024-01-30 15:18:58 +00:00
|
|
|
zap.String("id", idStr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:58:44 +00:00
|
|
|
func (s hashedShard) Hash() uint64 {
|
2023-02-27 13:16:37 +00:00
|
|
|
return s.hash
|
2020-11-30 14:58:44 +00:00
|
|
|
}
|