2020-12-22 00:26:36 +00:00
|
|
|
package audittask
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/audit"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/audit/auditor"
|
2020-12-22 00:26:36 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Listen starts the process of processing tasks from the queue.
|
|
|
|
//
|
|
|
|
// The listener is terminated by context.
|
|
|
|
func (m *Manager) Listen(ctx context.Context) {
|
|
|
|
m.log.Info("process routine",
|
2021-08-25 14:24:19 +00:00
|
|
|
zap.Uint32("queue_capacity", m.queueCap),
|
2020-12-22 00:26:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
m.ch = make(chan *audit.Task, m.queueCap)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
m.log.Warn("stop listener by context",
|
|
|
|
zap.String("error", ctx.Err().Error()),
|
|
|
|
)
|
2021-10-19 13:34:34 +00:00
|
|
|
m.workerPool.Release()
|
2020-12-22 00:26:36 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
case task, ok := <-m.ch:
|
|
|
|
if !ok {
|
|
|
|
m.log.Warn("queue channel is closed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
tCtx, tCancel := context.WithCancel(ctx) // cancel task in case of listen cancel
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-tCtx.Done(): // listen cancelled or task completed
|
|
|
|
return
|
|
|
|
case <-task.CancelChannel(): // new epoch
|
|
|
|
tCancel()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
m.handleTask(tCtx, task, tCancel)
|
2020-12-22 00:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
func (m *Manager) handleTask(ctx context.Context, task *audit.Task, onCompleted func()) {
|
2020-12-23 16:53:11 +00:00
|
|
|
pdpPool, err := m.pdpPoolGenerator()
|
|
|
|
if err != nil {
|
|
|
|
m.log.Error("could not generate PDP worker pool",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2023-04-06 08:36:25 +00:00
|
|
|
onCompleted()
|
2020-12-23 16:53:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:24:25 +00:00
|
|
|
porPool, err := m.pdpPoolGenerator()
|
|
|
|
if err != nil {
|
|
|
|
m.log.Error("could not generate PoR worker pool",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2023-04-06 08:36:25 +00:00
|
|
|
onCompleted()
|
2020-12-24 11:24:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:53:11 +00:00
|
|
|
auditContext := m.generateContext(task).
|
2020-12-24 11:24:25 +00:00
|
|
|
WithPDPWorkerPool(pdpPool).
|
|
|
|
WithPoRWorkerPool(porPool)
|
2020-12-23 16:53:11 +00:00
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
if err := m.workerPool.Submit(func() { auditContext.Execute(ctx, onCompleted) }); err != nil {
|
2020-12-22 00:26:36 +00:00
|
|
|
// may be we should report it
|
|
|
|
m.log.Warn("could not submit audit task")
|
2023-04-06 08:36:25 +00:00
|
|
|
onCompleted()
|
2020-12-22 00:26:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) generateContext(task *audit.Task) *auditor.Context {
|
|
|
|
return auditor.NewContext(m.ctxPrm).
|
|
|
|
WithTask(task)
|
|
|
|
}
|