2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/hrw"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2022-06-28 14:05:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard/mode"
|
2022-10-26 12:23:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-10-08 12:25:45 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2022-01-31 14:58:32 +00:00
|
|
|
"go.uber.org/atomic"
|
2022-09-26 21:39:34 +00:00
|
|
|
"go.uber.org/zap"
|
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
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
type hashedShard shardWrapper
|
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
|
|
|
}
|
|
|
|
|
2022-10-12 17:55:35 +00:00
|
|
|
func (m *metricsWithID) DecObjectCounter(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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (e *StorageEngine) AddShard(opts ...shard.Option) (*shard.ID, error) {
|
2022-09-27 21:43:38 +00:00
|
|
|
sh, err := e.createShard(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create a shard: %w", err)
|
|
|
|
}
|
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 {
|
2022-09-27 21:43:38 +00:00
|
|
|
return nil, fmt.Errorf("could not add %s shard: %w", sh.ID().String(), err)
|
2022-03-11 07:13:33 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
return sh.ID(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) createShard(opts []shard.Option) (*shard.Shard, error) {
|
2022-03-11 07:13:33 +00:00
|
|
|
id, err := generateShardID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate shard ID: %w", err)
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
opts = append(opts, shard.WithMetricsWriter(
|
2022-10-12 17:55:35 +00:00
|
|
|
&metricsWithID{
|
2022-08-19 16:49:09 +00:00
|
|
|
id: id.String(),
|
|
|
|
mw: e.metrics,
|
|
|
|
},
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
e.mtx.RUnlock()
|
|
|
|
|
2022-03-01 08:59:05 +00:00
|
|
|
sh := shard.New(append(opts,
|
|
|
|
shard.WithID(id),
|
2022-03-10 17:58:58 +00:00
|
|
|
shard.WithExpiredTombstonesCallback(e.processExpiredTombstones),
|
|
|
|
shard.WithExpiredLocksCallback(e.processExpiredLocks),
|
2022-06-06 14:01:21 +00:00
|
|
|
shard.WithDeletedLockCallback(e.processDeletedLocks),
|
2022-10-20 10:40:25 +00:00
|
|
|
shard.WithReportErrorFunc(e.reportShardErrorBackground),
|
2022-03-01 08:59:05 +00:00
|
|
|
)...)
|
|
|
|
|
|
|
|
if err := sh.UpdateID(); err != nil {
|
2022-09-26 21:39:34 +00:00
|
|
|
return nil, fmt.Errorf("could not update shard ID: %w", err)
|
2021-10-08 12:25:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 21:43:38 +00:00
|
|
|
return sh, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("could not create pool: %w", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.shards[strID] = shardWrapper{
|
|
|
|
errorCount: atomic.NewUint32(0),
|
2022-03-01 08:59:05 +00:00
|
|
|
Shard: sh,
|
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.
|
|
|
|
func (e *StorageEngine) removeShards(ids ...string) {
|
|
|
|
if len(ids) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ss := make([]shardWrapper, 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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.log.Info("shard has been removed",
|
|
|
|
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 {
|
|
|
|
err := sh.Close()
|
|
|
|
if err != nil {
|
|
|
|
e.log.Error("could not close removed shard",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) shardWeight(sh *shard.Shard) float64 {
|
|
|
|
weightValues := sh.WeightValues()
|
|
|
|
|
|
|
|
return float64(weightValues.FreeSpace)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *StorageEngine) sortShardsByWeight(objAddr interface{ EncodeToString() string }) []hashedShard {
|
2020-11-18 12:06:47 +00:00
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
2020-11-30 14:58:44 +00:00
|
|
|
shards := make([]hashedShard, 0, len(e.shards))
|
2020-11-17 12:26:03 +00:00
|
|
|
weights := make([]float64, 0, len(e.shards))
|
|
|
|
|
|
|
|
for _, sh := range e.shards {
|
2022-01-31 14:58:32 +00:00
|
|
|
shards = append(shards, hashedShard(sh))
|
|
|
|
weights = append(weights, e.shardWeight(sh.Shard))
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
hrw.SortSliceByWeightValue(shards, weights, hrw.Hash([]byte(objAddr.EncodeToString())))
|
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)) {
|
2020-12-01 10:18:25 +00:00
|
|
|
for i, sh := range e.sortShardsByWeight(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.
|
2022-06-28 14:05:08 +00:00
|
|
|
func (e *StorageEngine) SetShardMode(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)
|
|
|
|
}
|
2020-11-19 13:04:04 +00:00
|
|
|
return sh.SetMode(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
func (e *StorageEngine) HandleNewEpoch(epoch uint64) {
|
|
|
|
ev := shard.EventNewEpoch(epoch)
|
|
|
|
|
|
|
|
e.mtx.RLock()
|
|
|
|
defer e.mtx.RUnlock()
|
|
|
|
|
|
|
|
for _, sh := range e.shards {
|
|
|
|
sh.NotificationChannel() <- ev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 14:58:44 +00:00
|
|
|
func (s hashedShard) Hash() uint64 {
|
|
|
|
return hrw.Hash(
|
2022-01-31 14:58:32 +00:00
|
|
|
[]byte(s.Shard.ID().String()),
|
2020-11-30 14:58:44 +00:00
|
|
|
)
|
|
|
|
}
|