2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-10 08:34:00 +00:00
|
|
|
"errors"
|
|
|
|
"time"
|
2020-10-21 09:24:02 +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/local_object_storage/engine"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Policer) Run(ctx context.Context) {
|
2021-11-10 08:34:00 +00:00
|
|
|
p.shardPolicyWorker(ctx)
|
2023-06-29 09:13:01 +00:00
|
|
|
p.log.Info(logs.PolicerRoutineStopped)
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policer) shardPolicyWorker(ctx context.Context) {
|
2020-10-21 09:24:02 +00:00
|
|
|
for {
|
2021-12-20 08:36:24 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:13:01 +00:00
|
|
|
addrs, err := p.keySpaceIterator.Next(ctx, p.batchSize)
|
2021-11-10 08:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, engine.ErrEndOfListing) {
|
2023-07-06 15:23:52 +00:00
|
|
|
p.keySpaceIterator.Rewind()
|
2023-07-06 15:29:16 +00:00
|
|
|
time.Sleep(p.sleepDuration) // finished whole cycle, sleep a bit
|
2021-11-10 08:34:00 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Warn(logs.PolicerFailureAtObjectSelectForReplication, zap.Error(err))
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
for i := range addrs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-10-21 09:24:02 +00:00
|
|
|
return
|
2021-11-10 08:34:00 +00:00
|
|
|
default:
|
2022-11-15 11:28:15 +00:00
|
|
|
addr := addrs[i]
|
|
|
|
if p.objsInWork.inWork(addr.Address) {
|
2022-06-09 16:45:51 +00:00
|
|
|
// do not process an object
|
|
|
|
// that is in work
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:13:01 +00:00
|
|
|
err := p.taskPool.Submit(func() {
|
2022-11-15 11:28:15 +00:00
|
|
|
v, ok := p.cache.Get(addr.Address)
|
2022-12-30 09:47:38 +00:00
|
|
|
if ok && time.Since(v) < p.evictDuration {
|
2021-11-10 08:34:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-30 07:12:17 +00:00
|
|
|
if p.objsInWork.add(addr.Address) {
|
2023-09-04 09:00:49 +00:00
|
|
|
err := p.processObject(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error(logs.PolicerUnableToProcessObj,
|
|
|
|
zap.Stringer("object", addr.Address),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
2023-06-30 07:12:17 +00:00
|
|
|
p.cache.Add(addr.Address, time.Now())
|
|
|
|
p.objsInWork.remove(addr.Address)
|
2023-10-04 16:20:59 +00:00
|
|
|
p.metrics.IncProcessedObjects()
|
2023-06-30 07:12:17 +00:00
|
|
|
}
|
2021-11-10 08:34:00 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Warn(logs.PolicerPoolSubmission, zap.Error(err))
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|