2020-11-17 15:23:15 +03:00
|
|
|
package shard
|
|
|
|
|
2020-11-18 14:52:26 +03:00
|
|
|
import (
|
2023-03-07 16:38:26 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2020-11-18 14:52:26 +03:00
|
|
|
"github.com/mr-tron/base58"
|
2022-07-19 14:27:12 +03:00
|
|
|
"go.uber.org/zap"
|
2020-11-18 14:52:26 +03:00
|
|
|
)
|
|
|
|
|
2020-11-17 15:23:15 +03: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 14:52:26 +03:00
|
|
|
return base58.Encode(id)
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns Shard identifier.
|
|
|
|
func (s *Shard) ID() *ID {
|
2020-11-19 16:53:45 +03:00
|
|
|
return s.info.ID
|
2020-11-17 15:23:15 +03:00
|
|
|
}
|
2022-03-01 11:59:05 +03:00
|
|
|
|
|
|
|
// UpdateID reads shard ID saved in the metabase and updates it if it is missing.
|
|
|
|
func (s *Shard) UpdateID() (err error) {
|
2022-06-28 16:42:50 +03:00
|
|
|
if err = s.metaBase.Open(false); err != nil {
|
2022-03-01 11:59:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cErr := s.metaBase.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
id, err := s.metaBase.ReadShardID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(id) != 0 {
|
|
|
|
s.info.ID = NewIDFromBytes(id)
|
2022-10-12 20:55:35 +03:00
|
|
|
|
|
|
|
if s.cfg.metricsWriter != nil {
|
|
|
|
s.cfg.metricsWriter.SetShardID(s.info.ID.String())
|
|
|
|
}
|
2022-07-19 14:27:12 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 10:41:01 +03:00
|
|
|
s.log = &logger.Logger{Logger: s.log.With(zap.String("shard_id", s.info.ID.String()))}
|
2022-07-19 14:27:12 +03:00
|
|
|
s.metaBase.SetLogger(s.log)
|
|
|
|
s.blobStor.SetLogger(s.log)
|
|
|
|
if s.hasWriteCache() {
|
|
|
|
s.writeCache.SetLogger(s.log)
|
|
|
|
}
|
2023-06-07 14:39:03 +03:00
|
|
|
s.metaBase.SetParentID(s.info.ID.String())
|
|
|
|
s.blobStor.SetParentID(s.info.ID.String())
|
2023-06-07 17:06:02 +03:00
|
|
|
if s.pilorama != nil {
|
|
|
|
s.pilorama.SetParentID(s.info.ID.String())
|
|
|
|
}
|
2022-07-19 14:27:12 +03:00
|
|
|
|
|
|
|
if len(id) != 0 {
|
2022-03-01 11:59:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.metaBase.WriteShardID(*s.info.ID)
|
|
|
|
}
|