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-12-23 17:35:35 +00:00
|
|
|
objectcore "github.com/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
"github.com/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) {
|
|
|
|
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
|
|
|
|
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 {
|
|
|
|
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:
|
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)
|
|
|
|
p.log.Debug("tune replication capacity",
|
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
|
|
|
}
|
|
|
|
}
|