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
|
|
|
go p.poolCapacityWorker(ctx)
|
|
|
|
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) {
|
|
|
|
time.Sleep(time.Second) // finished whole cycle, sleep a bit
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-15 11:28:15 +00:00
|
|
|
p.objsInWork.add(addr.Address)
|
2022-06-09 16:45:51 +00:00
|
|
|
|
2022-11-15 11:28:15 +00:00
|
|
|
p.processObject(ctx, addr)
|
2022-06-09 16:45:51 +00:00
|
|
|
|
2022-11-16 08:01:53 +00:00
|
|
|
p.cache.Add(addr.Address, time.Now())
|
2022-11-15 11:28:15 +00:00
|
|
|
p.objsInWork.remove(addr.Address)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
func (p *Policer) poolCapacityWorker(ctx context.Context) {
|
|
|
|
ticker := time.NewTicker(p.rebalanceFreq)
|
2023-06-29 09:13:01 +00:00
|
|
|
defer ticker.Stop()
|
2021-11-10 08:34:00 +00:00
|
|
|
for {
|
2020-10-21 09:24:02 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2021-11-10 08:34:00 +00:00
|
|
|
case <-ticker.C:
|
2022-12-23 17:35:35 +00:00
|
|
|
frostfsSysLoad := p.loader.ObjectServiceLoad()
|
|
|
|
newCapacity := int((1.0 - frostfsSysLoad) * float64(p.maxCapacity))
|
2021-11-10 08:34:00 +00:00
|
|
|
if newCapacity == 0 {
|
|
|
|
newCapacity++
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
if p.taskPool.Cap() != newCapacity {
|
|
|
|
p.taskPool.Tune(newCapacity)
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Debug(logs.PolicerTuneReplicationCapacity,
|
2022-12-23 17:35:35 +00:00
|
|
|
zap.Float64("system_load", frostfsSysLoad),
|
2021-11-10 08:34:00 +00:00
|
|
|
zap.Int("new_capacity", newCapacity))
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|