[#1210] reputation: Resolve race condition

Make all epoch independent in reputation process. Do not reset any timers
related to reputation. Make it possible to finish iteration after the
unexpected `NewEpoch` event.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-03-02 19:31:56 +03:00 committed by Alex Vanin
parent 77d847dbea
commit c3db12d71b
6 changed files with 66 additions and 115 deletions

View file

@ -3,83 +3,41 @@ package main
import (
"sync"
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-node/pkg/morph/timer"
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/ticker"
)
type (
// EigenTrustDuration is a structure that provides duration of one
// eigen trust iteration round in blocks for block timer.
EigenTrustDuration struct {
sync.Mutex
type eigenTrustTickers struct {
m sync.Mutex
nm *nmClient.Client
val uint32
}
)
// NewEigenTrustDuration returns instance of EigenTrustDuration.
func NewEigenTrustDuration(nm *nmClient.Client) *EigenTrustDuration {
return &EigenTrustDuration{
nm: nm,
}
timers map[uint64]*ticker.IterationsTicker
}
// Value returns number of blocks between two iterations of EigenTrust
// calculation. This value is not changed between `Update` calls.
func (e *EigenTrustDuration) Value() (uint32, error) {
e.Lock()
defer e.Unlock()
func (e *eigenTrustTickers) addEpochTimer(epoch uint64, timer *ticker.IterationsTicker) {
e.m.Lock()
defer e.m.Unlock()
if e.val == 0 {
e.update()
}
return e.val, nil
e.timers[epoch] = timer
}
// Update function recalculate duration of EigenTrust iteration based on
// NeoFS epoch duration and amount of iteration rounds from global config.
func (e *EigenTrustDuration) Update() {
e.Lock()
defer e.Unlock()
func (e *eigenTrustTickers) tick() {
e.m.Lock()
defer e.m.Unlock()
e.update()
}
func (e *EigenTrustDuration) update() {
iterationAmount, err := e.nm.EigenTrustIterations()
if err != nil {
return
}
epochDuration, err := e.nm.EpochDuration()
if err != nil {
return
}
e.val = uint32(epochDuration / iterationAmount)
}
func startBlockTimers(c *cfg) {
for i := range c.cfgMorph.blockTimers {
if err := c.cfgMorph.blockTimers[i].Reset(); err != nil {
fatalOnErr(err)
for epoch, t := range e.timers {
if !t.Tick() {
delete(e.timers, epoch)
}
}
}
func tickBlockTimers(c *cfg) {
for i := range c.cfgMorph.blockTimers {
c.cfgMorph.blockTimers[i].Tick()
c.cfgMorph.eigenTrustTicker.tick()
}
func newEigenTrustIterTimer(c *cfg) {
c.cfgMorph.eigenTrustTicker = &eigenTrustTickers{
// it is expected to have max 2 concurrent epoch
// in normal mode work
timers: make(map[uint64]*ticker.IterationsTicker, 2),
}
}
func newEigenTrustIterTimer(c *cfg, it *EigenTrustDuration, handler timer.BlockTickHandler) {
c.cfgMorph.eigenTrustTimer = timer.NewBlockTimer(
it.Value,
handler,
)
c.cfgMorph.blockTimers = append(c.cfgMorph.blockTimers, c.cfgMorph.eigenTrustTimer)
}