2020-10-21 09:24:02 +00:00
|
|
|
package policer
|
|
|
|
|
|
|
|
import (
|
2022-06-09 16:45:51 +00:00
|
|
|
"sync"
|
2020-10-21 09:24:02 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2022-12-30 09:47:38 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-06-09 16:45:51 +00:00
|
|
|
type objectsInWork struct {
|
2023-06-30 07:08:36 +00:00
|
|
|
sync.RWMutex
|
2022-06-09 16:45:51 +00:00
|
|
|
objs map[oid.Address]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oiw *objectsInWork) inWork(addr oid.Address) bool {
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.RLock()
|
2022-06-09 16:45:51 +00:00
|
|
|
_, ok := oiw.objs[addr]
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.RUnlock()
|
2022-06-09 16:45:51 +00:00
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oiw *objectsInWork) remove(addr oid.Address) {
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.Lock()
|
2022-06-09 16:45:51 +00:00
|
|
|
delete(oiw.objs, addr)
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.Unlock()
|
2022-06-09 16:45:51 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 07:12:17 +00:00
|
|
|
func (oiw *objectsInWork) add(addr oid.Address) bool {
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.Lock()
|
2023-06-30 07:12:17 +00:00
|
|
|
_, exists := oiw.objs[addr]
|
2022-06-09 16:45:51 +00:00
|
|
|
oiw.objs[addr] = struct{}{}
|
2023-06-30 07:08:36 +00:00
|
|
|
oiw.Unlock()
|
2023-06-30 07:12:17 +00:00
|
|
|
return !exists
|
2022-06-09 16:45:51 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
// Policer represents the utility that verifies
|
|
|
|
// compliance with the object storage policy.
|
|
|
|
type Policer struct {
|
|
|
|
*cfg
|
|
|
|
|
2022-12-30 09:47:38 +00:00
|
|
|
cache *lru.Cache[oid.Address, time.Time]
|
2022-06-09 16:45:51 +00:00
|
|
|
|
|
|
|
objsInWork *objectsInWork
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns Policer instance.
|
|
|
|
func New(opts ...Option) *Policer {
|
|
|
|
c := defaultCfg()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](c)
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
c.log = c.log.With(zap.String("component", "Object Policer"))
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-12-30 09:47:38 +00:00
|
|
|
cache, err := lru.New[oid.Address, time.Time](int(c.cacheSize))
|
2021-11-10 08:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
return &Policer{
|
2021-11-10 08:34:00 +00:00
|
|
|
cfg: c,
|
|
|
|
cache: cache,
|
2022-06-09 16:45:51 +00:00
|
|
|
objsInWork: &objectsInWork{
|
2024-02-02 17:14:47 +00:00
|
|
|
objs: make(map[oid.Address]struct{}, c.taskPool.Cap()),
|
2022-06-09 16:45:51 +00:00
|
|
|
},
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|