2021-04-10 12:02:51 +00:00
|
|
|
package eigentrustcalc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/reputation/eigentrust"
|
|
|
|
apireputation "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/reputation"
|
2021-04-10 12:02:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CalculatePrm struct {
|
|
|
|
last bool
|
|
|
|
|
|
|
|
ei eigentrust.EpochIteration
|
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
func (p *CalculatePrm) SetLast(last bool) {
|
|
|
|
p.last = last
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 05:30:41 +00:00
|
|
|
func (p *CalculatePrm) SetEpochIteration(ei eigentrust.EpochIteration) {
|
|
|
|
p.ei = ei
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *Calculator) Calculate(ctx context.Context, prm CalculatePrm) {
|
2021-04-29 06:03:24 +00:00
|
|
|
alpha, err := c.prm.AlphaProvider.EigenTrustAlpha()
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Debug(
|
2023-04-12 14:35:10 +00:00
|
|
|
logs.CalculatorFailedToGetAlphaParam,
|
2021-04-29 06:03:24 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.alpha = reputation.TrustValueFromFloat64(alpha)
|
|
|
|
c.beta = reputation.TrustValueFromFloat64(1 - alpha)
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
epochIteration := prm.ei
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
iter := epochIteration.I()
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2021-03-01 19:01:45 +00:00
|
|
|
log := c.opts.log.With(
|
2023-04-05 07:44:40 +00:00
|
|
|
zap.Uint64("epoch", epochIteration.Epoch()),
|
2021-03-01 19:01:45 +00:00
|
|
|
zap.Uint32("iteration", iter),
|
|
|
|
)
|
|
|
|
|
2021-04-10 12:02:51 +00:00
|
|
|
if iter == 0 {
|
2023-04-05 07:44:40 +00:00
|
|
|
c.sendInitialValues(ctx, epochIteration)
|
2021-04-10 12:02:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrement iteration number to select the values collected
|
|
|
|
// on the previous stage
|
2023-04-05 07:44:40 +00:00
|
|
|
epochIteration.SetI(iter - 1)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
consumersIter, err := c.prm.DaughterTrustSource.InitConsumersIterator(epochIteration)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.CalculatorConsumersTrustIteratorsInitFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// continue with initial iteration number
|
2023-04-05 07:44:40 +00:00
|
|
|
epochIteration.SetI(iter)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
err = consumersIter.Iterate(func(daughter apireputation.PeerID, iter TrustIterator) error {
|
2021-04-10 12:02:51 +00:00
|
|
|
err := c.prm.WorkerPool.Submit(func() {
|
2023-04-05 07:44:40 +00:00
|
|
|
c.iterateDaughter(ctx, iterDaughterPrm{
|
2021-04-10 12:02:51 +00:00
|
|
|
lastIter: prm.last,
|
2023-04-05 07:44:40 +00:00
|
|
|
ei: epochIteration,
|
2021-04-10 12:02:51 +00:00
|
|
|
id: daughter,
|
|
|
|
consumersIter: iter,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.CalculatorWorkerPoolSubmitFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't stop trying
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.CalculatorIterateDaughtersConsumersFailed,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type iterDaughterPrm struct {
|
|
|
|
lastIter bool
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
ei EpochIterationInfo
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
id apireputation.PeerID
|
2021-04-10 12:02:51 +00:00
|
|
|
|
|
|
|
consumersIter TrustIterator
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *Calculator) iterateDaughter(ctx context.Context, p iterDaughterPrm) {
|
2021-04-10 12:02:51 +00:00
|
|
|
initTrust, err := c.prm.InitialTrustSource.InitialTrust(p.id)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorGetInitialTrustFailure,
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("daughter", p.id),
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
daughterIter, err := c.prm.DaughterTrustSource.InitDaughterIterator(p.ei, p.id)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorDaughterTrustIteratorsInitFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := reputation.TrustZero
|
|
|
|
|
|
|
|
err = p.consumersIter.Iterate(func(trust reputation.Trust) error {
|
|
|
|
if !p.lastIter {
|
|
|
|
select {
|
2023-04-05 07:44:40 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
2021-04-10 12:02:51 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sum.Add(trust.Value())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorIterateOverDaughtersTrustsFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alpha * Pd
|
|
|
|
initTrust.Mul(c.alpha)
|
|
|
|
|
|
|
|
sum.Mul(c.beta)
|
|
|
|
sum.Add(initTrust)
|
|
|
|
|
|
|
|
var intermediateTrust eigentrust.IterationTrust
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
intermediateTrust.SetEpoch(p.ei.Epoch())
|
2021-04-29 05:30:41 +00:00
|
|
|
intermediateTrust.SetPeer(p.id)
|
2023-04-05 07:44:40 +00:00
|
|
|
intermediateTrust.SetI(p.ei.I())
|
2021-04-10 12:02:51 +00:00
|
|
|
|
|
|
|
if p.lastIter {
|
2023-04-05 08:46:56 +00:00
|
|
|
c.processLastIteration(p, intermediateTrust, sum)
|
|
|
|
} else {
|
|
|
|
c.processIntermediateIteration(ctx, p, daughterIter, sum)
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
func (c *Calculator) processLastIteration(p iterDaughterPrm, intermediateTrust eigentrust.IterationTrust, sum reputation.TrustValue) {
|
|
|
|
finalWriter, err := c.prm.FinalResultTarget.InitIntermediateWriter(p.ei)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorInitWriterFailure,
|
2023-04-05 08:46:56 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
intermediateTrust.SetValue(sum)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
err = finalWriter.WriteIntermediateTrust(intermediateTrust)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorWriteFinalResultFailure,
|
2023-04-05 08:46:56 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
func (c *Calculator) processIntermediateIteration(ctx context.Context, p iterDaughterPrm, daughterIter TrustIterator, sum reputation.TrustValue) {
|
|
|
|
intermediateWriter, err := c.prm.IntermediateValueTarget.InitWriter(p.ei)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorInitWriterFailure,
|
2023-04-05 08:46:56 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
err = daughterIter.Iterate(func(trust reputation.Trust) error {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
val := trust.Value()
|
|
|
|
val.Mul(sum)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
trust.SetValue(val)
|
2021-04-29 05:30:41 +00:00
|
|
|
|
2023-04-05 08:46:56 +00:00
|
|
|
err := intermediateWriter.Write(ctx, trust)
|
2021-04-29 05:30:41 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorWriteValueFailure,
|
2021-04-29 05:30:41 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2023-04-05 08:46:56 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorIterateDaughterTrustsFailure,
|
2023-04-05 08:46:56 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = intermediateWriter.Close(ctx)
|
|
|
|
if err != nil {
|
|
|
|
c.opts.log.Error(
|
|
|
|
"could not close writer",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *Calculator) sendInitialValues(ctx context.Context, epochInfo EpochIterationInfo) {
|
|
|
|
daughterIter, err := c.prm.DaughterTrustSource.InitAllDaughtersIterator(epochInfo)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorAllDaughtersTrustIteratorsInitFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
intermediateWriter, err := c.prm.IntermediateValueTarget.InitWriter(epochInfo)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorInitWriterFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-04 13:24:51 +00:00
|
|
|
err = daughterIter.Iterate(func(daughter apireputation.PeerID, iterator TrustIterator) error {
|
2021-04-10 12:02:51 +00:00
|
|
|
return iterator.Iterate(func(trust reputation.Trust) error {
|
|
|
|
trusted := trust.Peer()
|
|
|
|
|
|
|
|
initTrust, err := c.prm.InitialTrustSource.InitialTrust(trusted)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorGetInitialTrustFailure,
|
2022-07-04 13:24:51 +00:00
|
|
|
zap.Stringer("peer", trusted),
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
// don't stop on single failure
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
initTrust.Mul(trust.Value())
|
2021-04-29 05:30:41 +00:00
|
|
|
trust.SetValue(initTrust)
|
2021-04-10 12:02:51 +00:00
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
err = intermediateWriter.Write(ctx, trust)
|
2021-04-10 12:02:51 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorWriteValueFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
// don't stop on single failure
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorIterateOverAllDaughtersFailure,
|
2021-04-10 12:02:51 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-29 05:30:41 +00:00
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
err = intermediateWriter.Close(ctx)
|
2021-04-29 05:30:41 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.opts.log.Debug(logs.CalculatorCouldNotCloseWriter,
|
2021-04-29 05:30:41 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2021-04-10 12:02:51 +00:00
|
|
|
}
|