2021-04-10 12:02:51 +00:00
|
|
|
package eigentrustctrl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util"
|
2021-04-10 12:02:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Prm groups the required parameters of the Controller's constructor.
|
|
|
|
//
|
|
|
|
// All values must comply with the requirements imposed on them.
|
|
|
|
// Passing incorrect parameter values will result in constructor
|
|
|
|
// failure (error or panic depending on the implementation).
|
|
|
|
type Prm struct {
|
|
|
|
// Component of computing iteration of EigenTrust algorithm.
|
|
|
|
//
|
|
|
|
// Must not be nil.
|
|
|
|
DaughtersTrustCalculator DaughtersTrustCalculator
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
// IterationsProvider provides information about numbers
|
|
|
|
// of iterations for algorithm.
|
|
|
|
IterationsProvider IterationsProvider
|
|
|
|
|
2021-04-10 12:02:51 +00:00
|
|
|
// Routine execution pool for single epoch iteration.
|
|
|
|
WorkerPool util.WorkerPool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controller represents EigenTrust algorithm transient controller.
|
|
|
|
//
|
|
|
|
// Controller's main goal is to separate the two main stages of
|
|
|
|
// the calculation:
|
2022-08-15 16:20:20 +00:00
|
|
|
// 1. reporting local values to manager nodes
|
|
|
|
// 2. calculating global trusts of child nodes
|
2021-04-10 12:02:51 +00:00
|
|
|
//
|
|
|
|
// Calculation stages are controlled based on external signals
|
|
|
|
// that come from the application through the Controller's API.
|
|
|
|
//
|
|
|
|
// For correct operation, the controller must be created
|
|
|
|
// using the constructor (New) based on the required parameters
|
|
|
|
// and optional components. After successful creation,
|
|
|
|
// the constructor is immediately ready to work through
|
|
|
|
// API of external control of calculations and data transfer.
|
|
|
|
type Controller struct {
|
|
|
|
prm Prm
|
|
|
|
|
|
|
|
opts *options
|
|
|
|
|
|
|
|
mtx sync.Mutex
|
2023-04-04 14:47:00 +00:00
|
|
|
mCtx map[uint64]*iterContext
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const invalidPrmValFmt = "invalid parameter %s (%T):%v"
|
|
|
|
|
2023-02-21 11:42:45 +00:00
|
|
|
func panicOnPrmValue(n string, v any) {
|
2021-04-10 12:02:51 +00:00
|
|
|
panic(fmt.Sprintf(invalidPrmValFmt, n, v, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new instance of the Controller.
|
|
|
|
//
|
|
|
|
// Panics if at least one value of the parameters is invalid.
|
|
|
|
//
|
|
|
|
// The created Controller does not require additional
|
|
|
|
// initialization and is completely ready for work.
|
|
|
|
func New(prm Prm, opts ...Option) *Controller {
|
|
|
|
switch {
|
2021-04-29 05:30:41 +00:00
|
|
|
case prm.IterationsProvider == nil:
|
|
|
|
panicOnPrmValue("IterationNumber", prm.IterationsProvider)
|
2021-04-10 12:02:51 +00:00
|
|
|
case prm.WorkerPool == nil:
|
|
|
|
panicOnPrmValue("WorkerPool", prm.WorkerPool)
|
|
|
|
case prm.DaughtersTrustCalculator == nil:
|
|
|
|
panicOnPrmValue("DaughtersTrustCalculator", prm.DaughtersTrustCalculator)
|
|
|
|
}
|
|
|
|
|
|
|
|
o := defaultOpts()
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Controller{
|
2022-03-02 16:31:56 +00:00
|
|
|
prm: prm,
|
|
|
|
opts: o,
|
2023-04-04 14:47:00 +00:00
|
|
|
mCtx: make(map[uint64]*iterContext),
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
}
|