[#969] policer: Make error skip thread-safe

Introduces in afd2ba9a66.
Refs #914

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-02-08 10:01:27 +03:00
parent a6c9a337cd
commit 6010dfdf3d

View file

@ -3,6 +3,7 @@ package policer
import ( import (
"context" "context"
"errors" "errors"
"sync"
"time" "time"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs" "git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
@ -35,9 +36,7 @@ func (p *Policer) shardPolicyWorker(ctx context.Context) {
p.log.Warn(logs.PolicerFailureAtObjectSelectForReplication, zap.Error(err)) p.log.Warn(logs.PolicerFailureAtObjectSelectForReplication, zap.Error(err))
} }
// contains all errors logged in this iteration for each container skipMap := newSkipMap()
cnrErrSkip := make(map[cid.ID][]error)
for i := range addrs { for i := range addrs {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -59,12 +58,10 @@ func (p *Policer) shardPolicyWorker(ctx context.Context) {
if p.objsInWork.add(addr.Address) { if p.objsInWork.add(addr.Address) {
err := p.processObject(ctx, addr) err := p.processObject(ctx, addr)
if err != nil && !skipLog(cnrErrSkip[addr.Address.Container()], err) { if err != nil && !skipMap.addSeenError(addr.Address.Container(), err) {
p.log.Error(logs.PolicerUnableToProcessObj, p.log.Error(logs.PolicerUnableToProcessObj,
zap.Stringer("object", addr.Address), zap.Stringer("object", addr.Address),
zap.String("error", err.Error())) zap.String("error", err.Error()))
cnrErrSkip[addr.Address.Container()] = append(cnrErrSkip[addr.Address.Container()], err)
} }
p.cache.Add(addr.Address, time.Now()) p.cache.Add(addr.Address, time.Now())
p.objsInWork.remove(addr.Address) p.objsInWork.remove(addr.Address)
@ -79,11 +76,28 @@ func (p *Policer) shardPolicyWorker(ctx context.Context) {
} }
} }
func skipLog(errs []error, err error) bool { type errMap struct {
for _, e := range errs { 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] {
if errors.Is(err, e) { if errors.Is(err, e) {
return true return true
} }
} }
m.skipMap[cnr] = append(m.skipMap[cnr], err)
return false return false
} }