[#488] reputation/eigentrust/calculator: Implement calc wrapper
Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
parent
d3c1fc7dda
commit
ea781664cf
14 changed files with 294 additions and 53 deletions
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
// ContinuePrm groups the required parameters of Continue operation.
|
||||
type ContinuePrm struct {
|
||||
epoch uint64
|
||||
Epoch uint64
|
||||
}
|
||||
|
||||
type iterContext struct {
|
||||
|
@ -35,20 +35,24 @@ func (c *Controller) Continue(prm ContinuePrm) {
|
|||
c.mtx.Lock()
|
||||
|
||||
{
|
||||
iterCtx, ok := c.mCtx[prm.epoch]
|
||||
iterCtx, ok := c.mCtx[prm.Epoch]
|
||||
if !ok {
|
||||
iterCtx := new(iterContextCancel)
|
||||
c.mCtx[prm.epoch] = iterCtx
|
||||
iterCtx = new(iterContextCancel)
|
||||
c.mCtx[prm.Epoch] = iterCtx
|
||||
|
||||
iterCtx.Context, iterCtx.cancel = context.WithCancel(context.Background())
|
||||
iterCtx.EpochIteration.SetEpoch(prm.Epoch)
|
||||
} else {
|
||||
iterCtx.cancel()
|
||||
}
|
||||
|
||||
iterCtx.last = iterCtx.I() == c.prm.IterationNumber
|
||||
iterCtx.last = iterCtx.I() == c.iterationNumber-1
|
||||
|
||||
err := c.prm.WorkerPool.Submit(func() {
|
||||
c.prm.DaughtersTrustCalculator.Calculate(iterCtx.iterContext)
|
||||
|
||||
// iteration++
|
||||
iterCtx.Increment()
|
||||
})
|
||||
if err != nil {
|
||||
c.opts.log.Debug("iteration submit failure",
|
||||
|
@ -57,11 +61,21 @@ func (c *Controller) Continue(prm ContinuePrm) {
|
|||
}
|
||||
|
||||
if iterCtx.last {
|
||||
delete(c.mCtx, prm.epoch)
|
||||
// In this case and worker pool failure we can mark epoch
|
||||
// number as already processed, but in any case it grows up
|
||||
// during normal operation of the system. Also, such information
|
||||
// will only live while the application is alive.
|
||||
// during normal operation of the system. Also, such information
|
||||
// number as already processed, but in any case it grows up
|
||||
// In this case and worker pool failure we can mark epoch
|
||||
delete(c.mCtx, prm.Epoch)
|
||||
|
||||
iterations, err := c.prm.IterationsProvider.EigenTrustIterations()
|
||||
if err != nil {
|
||||
c.opts.log.Debug(
|
||||
"could not get iteration numbers",
|
||||
zap.String("error", err.Error()),
|
||||
)
|
||||
} else {
|
||||
c.iterationNumber = uint32(iterations)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,14 +13,15 @@ import (
|
|||
// Passing incorrect parameter values will result in constructor
|
||||
// failure (error or panic depending on the implementation).
|
||||
type Prm struct {
|
||||
// Number of iterations
|
||||
IterationNumber uint32
|
||||
|
||||
// Component of computing iteration of EigenTrust algorithm.
|
||||
//
|
||||
// Must not be nil.
|
||||
DaughtersTrustCalculator DaughtersTrustCalculator
|
||||
|
||||
// IterationsProvider provides information about numbers
|
||||
// of iterations for algorithm.
|
||||
IterationsProvider IterationsProvider
|
||||
|
||||
// Routine execution pool for single epoch iteration.
|
||||
WorkerPool util.WorkerPool
|
||||
}
|
||||
|
@ -45,6 +46,9 @@ type Controller struct {
|
|||
|
||||
opts *options
|
||||
|
||||
// Number of iterations
|
||||
iterationNumber uint32
|
||||
|
||||
mtx sync.Mutex
|
||||
mCtx map[uint64]*iterContextCancel
|
||||
}
|
||||
|
@ -63,14 +67,19 @@ func panicOnPrmValue(n string, v interface{}) {
|
|||
// initialization and is completely ready for work.
|
||||
func New(prm Prm, opts ...Option) *Controller {
|
||||
switch {
|
||||
case prm.IterationNumber == 0:
|
||||
panicOnPrmValue("IterationNumber", prm.IterationNumber)
|
||||
case prm.IterationsProvider == nil:
|
||||
panicOnPrmValue("IterationNumber", prm.IterationsProvider)
|
||||
case prm.WorkerPool == nil:
|
||||
panicOnPrmValue("WorkerPool", prm.WorkerPool)
|
||||
case prm.DaughtersTrustCalculator == nil:
|
||||
panicOnPrmValue("DaughtersTrustCalculator", prm.DaughtersTrustCalculator)
|
||||
}
|
||||
|
||||
iterations, err := prm.IterationsProvider.EigenTrustIterations()
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not init EigenTrust controller: could not get num of iterations: %w", err))
|
||||
}
|
||||
|
||||
o := defaultOpts()
|
||||
|
||||
for _, opt := range opts {
|
||||
|
@ -78,8 +87,9 @@ func New(prm Prm, opts ...Option) *Controller {
|
|||
}
|
||||
|
||||
return &Controller{
|
||||
prm: prm,
|
||||
opts: o,
|
||||
mCtx: make(map[uint64]*iterContextCancel),
|
||||
iterationNumber: uint32(iterations),
|
||||
prm: prm,
|
||||
opts: o,
|
||||
mCtx: make(map[uint64]*iterContextCancel),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,3 +33,9 @@ type DaughtersTrustCalculator interface {
|
|||
// Execution should be interrupted if ctx.Last().
|
||||
Calculate(ctx IterationContext)
|
||||
}
|
||||
|
||||
// IterationsProvider must provides information about numbers
|
||||
// of iterations for algorithm.
|
||||
type IterationsProvider interface {
|
||||
EigenTrustIterations() (uint64, error)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue