2021-03-23 18:28:05 +00:00
|
|
|
package trustcontroller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2021-03-23 18:28:05 +00:00
|
|
|
|
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/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-03-23 18:28:05 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReportPrm groups the required parameters of the Controller.Report method.
|
|
|
|
type ReportPrm struct {
|
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets epoch number to select reputation values.
|
|
|
|
func (p *ReportPrm) SetEpoch(e uint64) {
|
|
|
|
p.epoch = e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report reports local reputation values.
|
|
|
|
//
|
|
|
|
// Single Report operation overtakes all data from LocalTrustSource
|
|
|
|
// to LocalTrustTarget (Controller's parameters).
|
|
|
|
//
|
|
|
|
// Each call acquires a report context for an Epoch parameter.
|
|
|
|
// At the very end of the operation, the context is released.
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *Controller) Report(ctx context.Context, prm ReportPrm) {
|
2021-03-23 18:28:05 +00:00
|
|
|
// acquire report
|
2023-04-05 07:44:40 +00:00
|
|
|
rCtx, reporter := c.acquireReporter(ctx, prm.epoch)
|
|
|
|
if reporter == nil {
|
2021-03-23 18:28:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// report local trust values
|
2023-04-05 07:44:40 +00:00
|
|
|
reporter.report(rCtx)
|
2021-03-23 18:28:05 +00:00
|
|
|
|
2021-04-02 16:21:05 +00:00
|
|
|
// finally stop and free the report
|
2023-04-05 07:44:40 +00:00
|
|
|
c.freeReport(prm.epoch, reporter.log)
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
type reporter struct {
|
2021-03-23 18:28:05 +00:00
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
ctrl *Controller
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
ep common.EpochProvider
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
type epochProvider struct {
|
2021-03-23 18:28:05 +00:00
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c epochProvider) Epoch() uint64 {
|
2021-03-23 18:28:05 +00:00
|
|
|
return c.epoch
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *Controller) acquireReporter(ctx context.Context, epoch uint64) (context.Context, *reporter) {
|
|
|
|
started := true
|
2021-03-23 18:28:05 +00:00
|
|
|
|
|
|
|
c.mtx.Lock()
|
|
|
|
{
|
|
|
|
if cancel := c.mCtx[epoch]; cancel == nil {
|
2023-04-05 07:44:40 +00:00
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2021-03-23 18:28:05 +00:00
|
|
|
c.mCtx[epoch] = cancel
|
2023-04-05 07:44:40 +00:00
|
|
|
started = false
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
log := &logger.Logger{Logger: c.opts.log.With(
|
2021-03-23 18:28:05 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
2022-09-28 07:41:01 +00:00
|
|
|
)}
|
2021-03-23 18:28:05 +00:00
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
if started {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.ControllerReportIsAlreadyStarted)
|
2023-04-05 07:44:40 +00:00
|
|
|
return ctx, nil
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
return ctx, &reporter{
|
2021-03-23 18:28:05 +00:00
|
|
|
epoch: epoch,
|
|
|
|
ctrl: c,
|
|
|
|
log: log,
|
2023-04-05 07:44:40 +00:00
|
|
|
ep: &epochProvider{
|
|
|
|
epoch: epoch,
|
2021-03-23 18:28:05 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
func (c *reporter) report(ctx context.Context) {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerStartingToReportLocalTrustValues)
|
2021-03-23 18:28:05 +00:00
|
|
|
|
|
|
|
// initialize iterator over locally collected values
|
2023-04-05 07:44:40 +00:00
|
|
|
iterator, err := c.ctrl.prm.LocalTrustSource.InitIterator(c.ep)
|
2021-03-23 18:28:05 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerCouldNotInitializeIteratorOverLocalTrustValues,
|
2021-03-23 18:28:05 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize target of local trust values
|
2023-04-05 07:44:40 +00:00
|
|
|
targetWriter, err := c.ctrl.prm.LocalTrustTarget.InitWriter(c.ep)
|
2021-03-23 18:28:05 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerCouldNotInitializeLocalTrustTarget,
|
2021-03-23 18:28:05 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all values and write them to the target
|
|
|
|
err = iterator.Iterate(
|
|
|
|
func(t reputation.Trust) error {
|
|
|
|
// check if context is done
|
2023-04-05 07:44:40 +00:00
|
|
|
if err := ctx.Err(); err != nil {
|
2021-03-23 18:28:05 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-05 07:44:40 +00:00
|
|
|
return targetWriter.Write(ctx, t)
|
2021-03-23 18:28:05 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerIteratorOverLocalTrustFailed,
|
2021-03-23 18:28:05 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish writing
|
2023-04-05 07:44:40 +00:00
|
|
|
err = targetWriter.Close(ctx)
|
2021-03-23 18:28:05 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerCouldNotFinishWritingLocalTrustValues,
|
2021-03-23 18:28:05 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.ControllerReportingSuccessfullyFinished)
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) freeReport(epoch uint64, log *logger.Logger) {
|
|
|
|
var stopped bool
|
|
|
|
|
|
|
|
c.mtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
cancel, stopped = c.mCtx[epoch]
|
|
|
|
|
|
|
|
if stopped {
|
|
|
|
cancel()
|
|
|
|
delete(c.mCtx, epoch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mtx.Unlock()
|
|
|
|
|
|
|
|
if stopped {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.ControllerReportingSuccessfullyInterrupted)
|
2021-03-23 18:28:05 +00:00
|
|
|
} else {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.ControllerReportingIsNotStartedOrAlreadyInterrupted)
|
2021-03-23 18:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopPrm groups the required parameters of the Controller.Stop method.
|
|
|
|
type StopPrm struct {
|
|
|
|
epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets epoch number the processing of the values of which must be interrupted.
|
|
|
|
func (p *StopPrm) SetEpoch(e uint64) {
|
|
|
|
p.epoch = e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop interrupts the processing of local trust values.
|
|
|
|
//
|
|
|
|
// Releases acquired report context.
|
|
|
|
func (c *Controller) Stop(prm StopPrm) {
|
|
|
|
c.freeReport(
|
|
|
|
prm.epoch,
|
2022-09-28 07:41:01 +00:00
|
|
|
&logger.Logger{Logger: c.opts.log.With(zap.Uint64("epoch", prm.epoch))},
|
2021-03-23 18:28:05 +00:00
|
|
|
)
|
|
|
|
}
|