2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-10 08:34:00 +00:00
|
|
|
"errors"
|
2024-02-08 07:01:27 +00:00
|
|
|
"sync"
|
2021-11-10 08:34:00 +00:00
|
|
|
"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"
|
2024-01-30 05:39:34 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
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)
|
2024-10-21 07:22:54 +00:00
|
|
|
p.log.Info(ctx, 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():
|
2024-01-24 11:48:47 +00:00
|
|
|
p.taskPool.Release()
|
2021-12-20 08:36:24 +00:00
|
|
|
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
|
|
|
|
}
|
2024-10-21 07:22:54 +00:00
|
|
|
p.log.Warn(ctx, logs.PolicerFailureAtObjectSelectForReplication, zap.Error(err))
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2024-02-08 07:01:27 +00:00
|
|
|
skipMap := newSkipMap()
|
2021-11-10 08:34:00 +00:00
|
|
|
for i := range addrs {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-01-24 11:48:47 +00:00
|
|
|
p.taskPool.Release()
|
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)
|
2024-02-08 07:01:27 +00:00
|
|
|
if err != nil && !skipMap.addSeenError(addr.Address.Container(), err) {
|
2024-10-21 07:22:54 +00:00
|
|
|
p.log.Error(ctx, logs.PolicerUnableToProcessObj,
|
2023-09-04 09:00:49 +00:00
|
|
|
zap.Stringer("object", addr.Address),
|
2024-12-13 08:44:56 +00:00
|
|
|
zap.Error(err))
|
2023-09-04 09:00:49 +00:00
|
|
|
}
|
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 {
|
2024-10-21 07:22:54 +00:00
|
|
|
p.log.Warn(ctx, logs.PolicerPoolSubmission, zap.Error(err))
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-30 05:39:34 +00:00
|
|
|
|
2024-02-08 07:01:27 +00:00
|
|
|
type errMap struct {
|
|
|
|
sync.Mutex
|
|
|
|
skipMap map[cid.ID][]error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSkipMap() *errMap {
|
|
|
|
return &errMap{
|
|
|
|
skipMap: make(map[cid.ID][]error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addSeenError marks err as seen error for the container.
|
|
|
|
// Returns true is the error has already been added.
|
|
|
|
func (m *errMap) addSeenError(cnr cid.ID, err error) bool {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
for _, e := range m.skipMap[cnr] {
|
2024-01-30 05:39:34 +00:00
|
|
|
if errors.Is(err, e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 07:05:02 +00:00
|
|
|
|
|
|
|
// Restrict list length to avoid possible OOM if some random error is added in future.
|
|
|
|
const maxErrListLength = 10
|
|
|
|
|
|
|
|
lst := m.skipMap[cnr]
|
|
|
|
if len(lst) < maxErrListLength {
|
|
|
|
m.skipMap[cnr] = append(lst, err)
|
|
|
|
}
|
2024-01-30 05:39:34 +00:00
|
|
|
return false
|
|
|
|
}
|