Dmitrii Stepanov
931a5e9aaf
All checks were successful
DCO action / DCO (pull_request) Successful in 1m55s
Build / Build Components (1.20) (pull_request) Successful in 2m14s
Vulncheck / Vulncheck (pull_request) Successful in 2m15s
Build / Build Components (1.21) (pull_request) Successful in 4m6s
Tests and linters / Tests (1.21) (pull_request) Successful in 5m58s
Tests and linters / Tests (1.20) (pull_request) Successful in 6m24s
Tests and linters / Staticcheck (pull_request) Successful in 6m11s
Tests and linters / Lint (pull_request) Successful in 6m37s
Tests and linters / Tests with -race (pull_request) Successful in 6m35s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package shard
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
"github.com/mr-tron/base58"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// 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 {
|
|
return base58.Encode(id)
|
|
}
|
|
|
|
// ID returns Shard identifier.
|
|
func (s *Shard) ID() *ID {
|
|
return s.info.ID
|
|
}
|
|
|
|
// UpdateID reads shard ID saved in the metabase and updates it if it is missing.
|
|
func (s *Shard) UpdateID(ctx context.Context) (err error) {
|
|
var metabaseOpened bool
|
|
var idFromMetabase []byte
|
|
if err = s.metaBase.Open(ctx, false); err != nil {
|
|
err = fmt.Errorf("failed to open metabase: %w", err)
|
|
} else {
|
|
metabaseOpened = true
|
|
}
|
|
if metabaseOpened {
|
|
defer func() {
|
|
cErr := s.metaBase.Close()
|
|
if cErr != nil {
|
|
err = fmt.Errorf("failed to close metabase: %w", cErr)
|
|
}
|
|
}()
|
|
if idFromMetabase, err = s.metaBase.ReadShardID(); err != nil {
|
|
err = fmt.Errorf("failed to read shard id from metabase: %w", err)
|
|
}
|
|
}
|
|
|
|
if len(idFromMetabase) != 0 {
|
|
s.info.ID = NewIDFromBytes(idFromMetabase)
|
|
}
|
|
|
|
if s.cfg.metricsWriter != nil {
|
|
s.cfg.metricsWriter.SetShardID(s.info.ID.String())
|
|
}
|
|
s.log = &logger.Logger{Logger: s.log.With(zap.Stringer("shard_id", s.info.ID))}
|
|
s.metaBase.SetLogger(s.log)
|
|
s.blobStor.SetLogger(s.log)
|
|
if s.hasWriteCache() {
|
|
s.writeCache.SetLogger(s.log)
|
|
}
|
|
s.metaBase.SetParentID(s.info.ID.String())
|
|
s.blobStor.SetParentID(s.info.ID.String())
|
|
if s.pilorama != nil {
|
|
s.pilorama.SetParentID(s.info.ID.String())
|
|
}
|
|
|
|
if len(idFromMetabase) == 0 && metabaseOpened {
|
|
if err = s.metaBase.WriteShardID(*s.info.ID); err != nil {
|
|
err = fmt.Errorf("failed to write shard id to metabase: %w", err)
|
|
}
|
|
}
|
|
return
|
|
}
|