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
|
|
|
|
2022-11-12 13:48:44 +00:00
|
|
|
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-11-10 08:34:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-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) {
|
|
|
|
defer func() {
|
|
|
|
p.log.Info("routine stopped")
|
|
|
|
}()
|
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
go p.poolCapacityWorker(ctx)
|
|
|
|
p.shardPolicyWorker(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Policer) shardPolicyWorker(ctx context.Context) {
|
|
|
|
var (
|
2022-11-12 13:48:44 +00:00
|
|
|
addrs []objectcore.AddressWithType
|
2021-11-10 08:34:00 +00:00
|
|
|
cursor *engine.Cursor
|
|
|
|
err error
|
2020-10-21 09:24:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2021-12-20 08:36:24 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
addrs, cursor, err = p.jobQueue.Select(cursor, p.batchSize)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, engine.ErrEndOfListing) {
|
|
|
|
time.Sleep(time.Second) // finished whole cycle, sleep a bit
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.log.Warn("failure at object select for replication", zap.Error(err))
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
err = p.taskPool.Submit(func() {
|
2022-11-15 11:28:15 +00:00
|
|
|
v, ok := p.cache.Get(addr.Address)
|
2021-11-10 08:34:00 +00:00
|
|
|
if ok && time.Since(v.(time.Time)) < p.evictDuration {
|
|
|
|
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
|
|
|
|
|
|
|
p.cache.Add(addr, 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 {
|
|
|
|
p.log.Warn("pool submission", zap.Error(err))
|
|
|
|
}
|
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)
|
|
|
|
for {
|
2020-10-21 09:24:02 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-11-10 08:34:00 +00:00
|
|
|
ticker.Stop()
|
2020-10-21 09:24:02 +00:00
|
|
|
return
|
2021-11-10 08:34:00 +00:00
|
|
|
case <-ticker.C:
|
|
|
|
neofsSysLoad := p.loader.ObjectServiceLoad()
|
|
|
|
newCapacity := int((1.0 - neofsSysLoad) * float64(p.maxCapacity))
|
|
|
|
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)
|
|
|
|
p.log.Debug("tune replication capacity",
|
|
|
|
zap.Float64("system_load", neofsSysLoad),
|
|
|
|
zap.Int("new_capacity", newCapacity))
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|