2021-01-27 19:00:35 +00:00
|
|
|
package loadcontroller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
2021-01-27 19:00:35 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StartPrm groups the required parameters of the Controller.Start method.
|
|
|
|
type StartPrm struct {
|
|
|
|
// Epoch number by which you want to select
|
|
|
|
// the values of the used space of containers.
|
|
|
|
Epoch uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type commonContext struct {
|
|
|
|
epoch uint64
|
|
|
|
|
|
|
|
ctrl *Controller
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
type announcer struct {
|
2021-01-27 19:00:35 +00:00
|
|
|
commonContext
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
// Start starts the processing of container.SizeEstimation values.
|
2021-01-27 19:00:35 +00:00
|
|
|
//
|
|
|
|
// Single Start operation overtakes all data from LocalMetrics to
|
|
|
|
// LocalAnnouncementTarget (Controller's parameters).
|
|
|
|
// No filter by epoch is used for the iterator, since it is expected
|
|
|
|
// that the source of metrics does not track the change of epochs.
|
|
|
|
//
|
|
|
|
// Each call acquires an announcement context for an Epoch parameter.
|
|
|
|
// At the very end of the operation, the context is released.
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *Controller) Start(ctx context.Context, prm StartPrm) {
|
|
|
|
var announcer *announcer
|
2021-01-27 19:00:35 +00:00
|
|
|
// acquire announcement
|
2023-04-05 13:28:09 +00:00
|
|
|
ctx, announcer = c.acquireAnnouncement(ctx, prm)
|
|
|
|
if announcer == nil {
|
2021-01-27 19:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally stop and free the announcement
|
2023-04-05 13:28:09 +00:00
|
|
|
defer announcer.freeAnnouncement()
|
2021-01-27 19:00:35 +00:00
|
|
|
|
|
|
|
// announce local values
|
2023-04-05 13:28:09 +00:00
|
|
|
announcer.announce(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *announcer) announce(ctx context.Context) {
|
2021-01-27 19:00:35 +00:00
|
|
|
c.log.Debug("starting to announce the values of the metrics")
|
|
|
|
|
|
|
|
var (
|
|
|
|
metricsIterator Iterator
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// initialize iterator over locally collected metrics
|
2023-04-05 11:52:13 +00:00
|
|
|
metricsIterator, err = c.ctrl.prm.LocalMetrics.InitIterator()
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not initialize iterator over locally collected metrics",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize target of local announcements
|
2023-04-05 11:52:13 +00:00
|
|
|
targetWriter, err := c.ctrl.prm.LocalAnnouncementTarget.InitWriter(nil)
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not initialize announcement accumulator",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all collected metrics and write them to the target
|
|
|
|
err = metricsIterator.Iterate(
|
2022-06-28 07:01:05 +00:00
|
|
|
func(container.SizeEstimation) bool {
|
2021-01-27 19:00:35 +00:00
|
|
|
return true // local metrics don't know about epochs
|
|
|
|
},
|
2022-06-28 07:01:05 +00:00
|
|
|
func(a container.SizeEstimation) error {
|
2021-01-27 19:00:35 +00:00
|
|
|
a.SetEpoch(c.epoch) // set epoch explicitly
|
|
|
|
return targetWriter.Put(a)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("iterator over locally collected metrics aborted",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish writing
|
2023-04-05 13:28:09 +00:00
|
|
|
err = targetWriter.Close(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not finish writing local announcements",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-01 19:01:45 +00:00
|
|
|
c.log.Debug("trust announcement successfully finished")
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *Controller) acquireAnnouncement(ctx context.Context, prm StartPrm) (context.Context, *announcer) {
|
|
|
|
started := true
|
2021-01-27 19:00:35 +00:00
|
|
|
c.announceMtx.Lock()
|
|
|
|
{
|
|
|
|
if cancel := c.mAnnounceCtx[prm.Epoch]; cancel == nil {
|
2023-04-05 13:28:09 +00:00
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
c.mAnnounceCtx[prm.Epoch] = cancel
|
2023-04-05 13:28:09 +00:00
|
|
|
started = false
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.announceMtx.Unlock()
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
log := &logger.Logger{Logger: c.opts.log.With(
|
2021-01-27 19:00:35 +00:00
|
|
|
zap.Uint64("epoch", prm.Epoch),
|
2022-09-28 07:41:01 +00:00
|
|
|
)}
|
2021-01-27 19:00:35 +00:00
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
if started {
|
2021-01-27 19:00:35 +00:00
|
|
|
log.Debug("announcement is already started")
|
2023-04-05 13:28:09 +00:00
|
|
|
return ctx, nil
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
return ctx, &announcer{
|
2021-01-27 19:00:35 +00:00
|
|
|
commonContext: commonContext{
|
|
|
|
epoch: prm.Epoch,
|
|
|
|
ctrl: c,
|
|
|
|
log: log,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commonContext) freeAnnouncement() {
|
|
|
|
var stopped bool
|
|
|
|
|
|
|
|
c.ctrl.announceMtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
cancel, stopped = c.ctrl.mAnnounceCtx[c.epoch]
|
|
|
|
|
|
|
|
if stopped {
|
|
|
|
cancel()
|
|
|
|
delete(c.ctrl.mAnnounceCtx, c.epoch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ctrl.announceMtx.Unlock()
|
|
|
|
|
|
|
|
if stopped {
|
2022-06-22 15:35:57 +00:00
|
|
|
c.log.Debug("announcement successfully interrupted")
|
2021-01-27 19:00:35 +00:00
|
|
|
} else {
|
2022-06-22 15:35:57 +00:00
|
|
|
c.log.Debug("announcement is not started or already interrupted")
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopPrm groups the required parameters of the Controller.Stop method.
|
|
|
|
type StopPrm struct {
|
|
|
|
// Epoch number the analysis of the values of which must be interrupted.
|
|
|
|
Epoch uint64
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
type reporter struct {
|
2021-01-27 19:00:35 +00:00
|
|
|
commonContext
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:01:05 +00:00
|
|
|
// Stop interrupts the processing of container.SizeEstimation values.
|
2021-01-27 19:00:35 +00:00
|
|
|
//
|
|
|
|
// Single Stop operation releases an announcement context and overtakes
|
|
|
|
// all data from AnnouncementAccumulator to ResultReceiver (Controller's
|
|
|
|
// parameters). Only values for the specified Epoch parameter are processed.
|
|
|
|
//
|
|
|
|
// Each call acquires a report context for an Epoch parameter.
|
|
|
|
// At the very end of the operation, the context is released.
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *Controller) Stop(ctx context.Context, prm StopPrm) {
|
|
|
|
var reporter *reporter
|
|
|
|
ctx, reporter = c.acquireReport(ctx, prm)
|
|
|
|
if reporter == nil {
|
2021-01-27 19:00:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally stop and free reporting
|
2023-04-05 13:28:09 +00:00
|
|
|
defer reporter.freeReport()
|
2021-01-27 19:00:35 +00:00
|
|
|
|
|
|
|
// interrupt announcement
|
2023-04-05 13:28:09 +00:00
|
|
|
reporter.freeAnnouncement()
|
2021-01-27 19:00:35 +00:00
|
|
|
|
|
|
|
// report the estimations
|
2023-04-05 13:28:09 +00:00
|
|
|
reporter.report(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *Controller) acquireReport(ctx context.Context, prm StopPrm) (context.Context, *reporter) {
|
|
|
|
started := true
|
2021-01-27 19:00:35 +00:00
|
|
|
|
|
|
|
c.reportMtx.Lock()
|
|
|
|
{
|
|
|
|
if cancel := c.mReportCtx[prm.Epoch]; cancel == nil {
|
2023-04-05 13:28:09 +00:00
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
c.mReportCtx[prm.Epoch] = cancel
|
2023-04-05 13:28:09 +00:00
|
|
|
started = false
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.reportMtx.Unlock()
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
log := &logger.Logger{Logger: c.opts.log.With(
|
2021-01-27 19:00:35 +00:00
|
|
|
zap.Uint64("epoch", prm.Epoch),
|
2022-09-28 07:41:01 +00:00
|
|
|
)}
|
2021-01-27 19:00:35 +00:00
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
if started {
|
2021-01-27 19:00:35 +00:00
|
|
|
log.Debug("report is already started")
|
2023-04-05 13:28:09 +00:00
|
|
|
return ctx, nil
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
return ctx, &reporter{
|
2021-01-27 19:00:35 +00:00
|
|
|
commonContext: commonContext{
|
|
|
|
epoch: prm.Epoch,
|
|
|
|
ctrl: c,
|
|
|
|
log: log,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commonContext) freeReport() {
|
|
|
|
var stopped bool
|
|
|
|
|
|
|
|
c.ctrl.reportMtx.Lock()
|
|
|
|
|
|
|
|
{
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
|
|
|
|
cancel, stopped = c.ctrl.mReportCtx[c.epoch]
|
|
|
|
|
|
|
|
if stopped {
|
|
|
|
cancel()
|
|
|
|
delete(c.ctrl.mReportCtx, c.epoch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ctrl.reportMtx.Unlock()
|
|
|
|
|
|
|
|
if stopped {
|
2022-06-22 15:35:57 +00:00
|
|
|
c.log.Debug("announcement successfully interrupted")
|
2021-01-27 19:00:35 +00:00
|
|
|
} else {
|
2022-06-22 15:35:57 +00:00
|
|
|
c.log.Debug("announcement is not started or already interrupted")
|
2021-01-27 19:00:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-05 13:28:09 +00:00
|
|
|
func (c *reporter) report(ctx context.Context) {
|
2021-01-27 19:00:35 +00:00
|
|
|
var (
|
|
|
|
localIterator Iterator
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// initialize iterator over locally accumulated announcements
|
2023-04-05 11:52:13 +00:00
|
|
|
localIterator, err = c.ctrl.prm.AnnouncementAccumulator.InitIterator()
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not initialize iterator over locally accumulated announcements",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize final destination of load estimations
|
2023-04-05 11:52:13 +00:00
|
|
|
resultWriter, err := c.ctrl.prm.ResultReceiver.InitWriter(nil)
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not initialize result target",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate over all accumulated announcements and write them to the target
|
|
|
|
err = localIterator.Iterate(
|
|
|
|
usedSpaceFilterEpochEQ(c.epoch),
|
|
|
|
resultWriter.Put,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("iterator over local announcements aborted",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish writing
|
2023-04-05 13:28:09 +00:00
|
|
|
err = resultWriter.Close(ctx)
|
2021-01-27 19:00:35 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Debug("could not finish writing load estimations",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|