2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
2020-11-18 11:52:26 +00:00
|
|
|
import (
|
2024-03-19 09:40:00 +00:00
|
|
|
"errors"
|
2024-01-18 10:39:21 +00:00
|
|
|
"fmt"
|
2023-08-31 16:26:47 +00:00
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
|
2020-11-18 11:52:26 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2022-07-19 11:27:12 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-18 11:52:26 +00:00
|
|
|
)
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
// ID represents Shard identifier.
|
|
|
|
//
|
|
|
|
// Each shard should have the unique ID within
|
|
|
|
// a single instance of local storage.
|
|
|
|
type ID []byte
|
|
|
|
|
|
|
|
// NewIDFromBytes constructs ID from byte slice.
|
|
|
|
func NewIDFromBytes(v []byte) *ID {
|
|
|
|
return (*ID)(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ID) String() string {
|
2020-11-18 11:52:26 +00:00
|
|
|
return base58.Encode(id)
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns Shard identifier.
|
|
|
|
func (s *Shard) ID() *ID {
|
2020-11-19 13:53:45 +00:00
|
|
|
return s.info.ID
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
2022-03-01 08:59:05 +00:00
|
|
|
|
|
|
|
// UpdateID reads shard ID saved in the metabase and updates it if it is missing.
|
2024-03-19 09:40:00 +00:00
|
|
|
func (s *Shard) UpdateID() (err error) {
|
2024-01-18 10:39:21 +00:00
|
|
|
var idFromMetabase []byte
|
2024-03-19 09:40:00 +00:00
|
|
|
modeDegraded := s.GetMode().NoMetabase()
|
|
|
|
if !modeDegraded {
|
|
|
|
if idFromMetabase, err = s.metaBase.GetShardID(mode.ReadOnly); err != nil {
|
2024-01-18 10:39:21 +00:00
|
|
|
err = fmt.Errorf("failed to read shard id from metabase: %w", err)
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-12 17:55:35 +00:00
|
|
|
|
2024-01-18 10:39:21 +00:00
|
|
|
if len(idFromMetabase) != 0 {
|
|
|
|
s.info.ID = NewIDFromBytes(idFromMetabase)
|
2022-07-19 11:27:12 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 06:09:47 +00:00
|
|
|
shardID := s.info.ID.String()
|
2024-10-03 07:23:59 +00:00
|
|
|
s.cfg.metricsWriter.SetShardID(shardID)
|
2024-02-13 06:09:47 +00:00
|
|
|
if s.writeCache != nil && s.writeCache.GetMetrics() != nil {
|
|
|
|
s.writeCache.GetMetrics().SetShardID(shardID)
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log = s.log.With(zap.Stringer("shard_id", s.info.ID))
|
2022-07-19 11:27:12 +00:00
|
|
|
s.metaBase.SetLogger(s.log)
|
|
|
|
s.blobStor.SetLogger(s.log)
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
s.writeCache.SetLogger(s.log)
|
|
|
|
}
|
2023-06-07 11:39:03 +00:00
|
|
|
s.metaBase.SetParentID(s.info.ID.String())
|
|
|
|
s.blobStor.SetParentID(s.info.ID.String())
|
2023-06-07 14:06:02 +00:00
|
|
|
if s.pilorama != nil {
|
|
|
|
s.pilorama.SetParentID(s.info.ID.String())
|
|
|
|
}
|
2022-07-19 11:27:12 +00:00
|
|
|
|
2024-03-19 09:40:00 +00:00
|
|
|
if len(idFromMetabase) == 0 && !modeDegraded {
|
|
|
|
if setErr := s.metaBase.SetShardID(*s.info.ID, s.GetMode()); setErr != nil {
|
|
|
|
err = errors.Join(err, fmt.Errorf("failed to write shard id to metabase: %w", setErr))
|
2024-01-18 10:39:21 +00:00
|
|
|
}
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|
2024-01-18 10:39:21 +00:00
|
|
|
return
|
2022-03-01 08:59:05 +00:00
|
|
|
}
|