frostfs-node/pkg/innerring/state.go
Ekaterina Lebedeva 2d4c0a0f4a
All checks were successful
DCO action / DCO (pull_request) Successful in 3m19s
Vulncheck / Vulncheck (pull_request) Successful in 3m25s
Build / Build Components (1.21) (pull_request) Successful in 4m27s
Build / Build Components (1.20) (pull_request) Successful in 4m41s
Tests and linters / Staticcheck (pull_request) Successful in 5m5s
Tests and linters / Lint (pull_request) Successful in 5m57s
Tests and linters / Tests (1.20) (pull_request) Successful in 13m25s
Tests and linters / Tests (1.21) (pull_request) Successful in 14m13s
Tests and linters / Tests with -race (pull_request) Successful in 16m15s
[#552] Add systemd notifications to ir service
Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
2023-12-13 17:51:41 +03:00

195 lines
5.4 KiB
Go

package innerring
import (
"fmt"
"sort"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/innerring/processors/governance"
control "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control/ir"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/sdnotify"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/state"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/spf13/viper"
"go.uber.org/zap"
)
const voteMethod = "vote"
var (
persistateMainChainLastBlockKey = []byte("main_chain_last_processed_block")
persistateSideChainLastBlockKey = []byte("side_chain_last_processed_block")
)
// EpochCounter is a getter for a global epoch counter.
func (s *Server) EpochCounter() uint64 {
return s.epochCounter.Load()
}
// SetEpochCounter is a setter for contract processors to update global
// epoch counter.
func (s *Server) SetEpochCounter(val uint64) {
s.epochCounter.Store(val)
if s.irMetrics != nil {
s.irMetrics.SetEpoch(val)
}
}
// EpochDuration is a getter for a global epoch duration.
func (s *Server) EpochDuration() uint64 {
return s.epochDuration.Load()
}
// SetEpochDuration is a setter for the Netmap processor to update global
// epoch duration.
func (s *Server) SetEpochDuration(val uint64) {
s.epochDuration.Store(val)
}
// IsActive is a getter for a global active flag state.
func (s *Server) IsActive() bool {
return s.InnerRingIndex() >= 0
}
// IsAlphabet is a getter for a global alphabet flag state.
func (s *Server) IsAlphabet() bool {
return s.AlphabetIndex() >= 0
}
// InnerRingIndex is a getter for a global index of node in inner ring list. Negative
// index means that node is not in the inner ring list.
func (s *Server) InnerRingIndex() int {
index, err := s.statusIndex.InnerRingIndex()
if err != nil {
s.log.Error(logs.InnerringCantGetInnerRingIndex, zap.String("error", err.Error()))
return -1
}
return int(index)
}
// InnerRingSize is a getter for a global size of inner ring list. This value
// paired with inner ring index.
func (s *Server) InnerRingSize() int {
size, err := s.statusIndex.InnerRingSize()
if err != nil {
s.log.Error(logs.InnerringCantGetInnerRingSize, zap.String("error", err.Error()))
return 0
}
return int(size)
}
// AlphabetIndex is a getter for a global index of node in alphabet list.
// Negative index means that node is not in the alphabet list.
func (s *Server) AlphabetIndex() int {
index, err := s.statusIndex.AlphabetIndex()
if err != nil {
s.log.Error(logs.InnerringCantGetAlphabetIndex, zap.String("error", err.Error()))
return -1
}
return int(index)
}
func (s *Server) voteForSidechainValidator(prm governance.VoteValidatorPrm) error {
validators := prm.Validators
index := s.InnerRingIndex()
if s.contracts.alphabet.indexOutOfRange(index) {
s.log.Info(logs.InnerringIgnoreValidatorVoteNodeNotInAlphabetRange)
return nil
}
if len(validators) == 0 {
s.log.Info(logs.InnerringIgnoreValidatorVoteEmptyValidatorsList)
return nil
}
epoch := s.EpochCounter()
var (
nonce uint32 = 1
vub uint32
vubP *uint32
err error
)
if prm.Hash != nil {
nonce, vub, err = s.morphClient.CalculateNonceAndVUB(prm.Hash)
if err != nil {
return fmt.Errorf("could not calculate nonce and `validUntilBlock` values: %w", err)
}
vubP = &vub
}
s.contracts.alphabet.iterate(func(letter GlagoliticLetter, contract util.Uint160) {
_, err := s.morphClient.NotaryInvoke(contract, s.feeConfig.SideChainFee(), nonce, vubP, voteMethod, epoch, validators)
if err != nil {
s.log.Warn(logs.InnerringCantInvokeVoteMethodInAlphabetContract,
zap.Int8("alphabet_index", int8(letter)),
zap.Uint64("epoch", epoch),
zap.String("error", err.Error()))
}
})
return nil
}
// VoteForSidechainValidator calls vote method on alphabet contracts with
// the provided list of keys.
func (s *Server) VoteForSidechainValidator(prm governance.VoteValidatorPrm) error {
sort.Sort(prm.Validators)
return s.voteForSidechainValidator(prm)
}
// ResetEpochTimer resets the block timer that produces events to update epoch
// counter in the netmap contract. It is used to synchronize this even production
// based on the block with a notification of the last epoch.
func (s *Server) ResetEpochTimer(h uint32) error {
s.epochTimer.Tick(h)
return s.epochTimer.Reset()
}
func (s *Server) setHealthStatus(hs control.HealthStatus) {
s.healthStatus.Store(int32(hs))
s.notifySystemd(hs)
if s.irMetrics != nil {
s.irMetrics.SetHealth(int32(hs))
}
}
// HealthStatus returns the current health status of the IR application.
func (s *Server) HealthStatus() control.HealthStatus {
return control.HealthStatus(s.healthStatus.Load())
}
func initPersistentStateStorage(cfg *viper.Viper) (*state.PersistentStorage, error) {
persistPath := cfg.GetString("node.persistent_state.path")
persistStorage, err := state.NewPersistentStorage(persistPath)
if err != nil {
return nil, fmt.Errorf("persistent state init error: %w", err)
}
return persistStorage, nil
}
func (s *Server) notifySystemd(st control.HealthStatus) {
if !s.sdNotify {
return
}
var err error
switch st {
case control.HealthStatus_READY:
err = sdnotify.FlagAndStatus(sdnotify.ReadyEnabled)
case control.HealthStatus_SHUTTING_DOWN:
err = sdnotify.FlagAndStatus(sdnotify.StoppingEnabled)
default:
err = sdnotify.Status(fmt.Sprintf("%v", st))
}
if err != nil {
s.log.Error(logs.FailedToReportStatusToSystemd, zap.Error(err))
}
}