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
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
|
|
|
|
headsvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/head"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object_manager/placement"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/replicator"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
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"
|
2021-11-10 08:34:00 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2020-10-21 09:24:02 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-11-10 08:34:00 +00:00
|
|
|
// NodeLoader provides application load statistics.
|
|
|
|
type nodeLoader interface {
|
|
|
|
// ObjectServiceLoad returns object service load value in [0:1] range.
|
|
|
|
ObjectServiceLoad() float64
|
|
|
|
}
|
|
|
|
|
2022-06-09 16:45:51 +00:00
|
|
|
type objectsInWork struct {
|
|
|
|
m sync.RWMutex
|
|
|
|
objs map[oid.Address]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oiw *objectsInWork) inWork(addr oid.Address) bool {
|
|
|
|
oiw.m.RLock()
|
|
|
|
_, ok := oiw.objs[addr]
|
|
|
|
oiw.m.RUnlock()
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oiw *objectsInWork) remove(addr oid.Address) {
|
|
|
|
oiw.m.Lock()
|
|
|
|
delete(oiw.objs, addr)
|
|
|
|
oiw.m.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oiw *objectsInWork) add(addr oid.Address) {
|
|
|
|
oiw.m.Lock()
|
|
|
|
oiw.objs[addr] = struct{}{}
|
|
|
|
oiw.m.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
// Policer represents the utility that verifies
|
|
|
|
// compliance with the object storage policy.
|
|
|
|
type Policer struct {
|
2022-12-30 11:29:51 +00:00
|
|
|
cfg
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-12-30 09:47:38 +00:00
|
|
|
cache *lru.Cache[oid.Address, time.Time]
|
2022-06-09 16:45:51 +00:00
|
|
|
|
2022-12-30 11:29:51 +00:00
|
|
|
objsInWork objectsInWork
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option is an option for Policer constructor.
|
|
|
|
type Option func(*cfg)
|
|
|
|
|
2021-02-19 15:21:46 +00:00
|
|
|
// RedundantCopyCallback is a callback to pass
|
|
|
|
// the redundant local copy of the object.
|
2022-05-31 17:00:41 +00:00
|
|
|
type RedundantCopyCallback func(oid.Address)
|
2021-02-19 15:21:46 +00:00
|
|
|
|
2020-10-21 09:24:02 +00:00
|
|
|
type cfg struct {
|
|
|
|
headTimeout time.Duration
|
|
|
|
|
|
|
|
log *logger.Logger
|
|
|
|
|
|
|
|
jobQueue jobQueue
|
|
|
|
|
|
|
|
cnrSrc container.Source
|
|
|
|
|
|
|
|
placementBuilder placement.Builder
|
|
|
|
|
|
|
|
remoteHeader *headsvc.RemoteHeader
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
netmapKeys netmap.AnnouncedKeys
|
2020-10-21 11:50:48 +00:00
|
|
|
|
|
|
|
replicator *replicator.Replicator
|
2021-02-19 15:21:46 +00:00
|
|
|
|
|
|
|
cbRedundantCopy RedundantCopyCallback
|
2021-11-10 08:34:00 +00:00
|
|
|
|
|
|
|
taskPool *ants.Pool
|
|
|
|
|
|
|
|
loader nodeLoader
|
|
|
|
|
|
|
|
maxCapacity int
|
|
|
|
|
|
|
|
batchSize, cacheSize uint32
|
|
|
|
|
|
|
|
rebalanceFreq, evictDuration time.Duration
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 11:29:51 +00:00
|
|
|
func (c *cfg) initDefault() {
|
|
|
|
c.log = &logger.Logger{Logger: zap.L()}
|
|
|
|
c.batchSize = 10
|
|
|
|
c.cacheSize = 1024 // 1024 * address size = 1024 * 64 = 64 MiB
|
|
|
|
c.rebalanceFreq = 1 * time.Second
|
|
|
|
c.evictDuration = 30 * time.Second
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates, initializes and returns Policer instance.
|
|
|
|
func New(opts ...Option) *Policer {
|
2022-12-30 11:29:51 +00:00
|
|
|
var p Policer
|
|
|
|
p.cfg.initDefault()
|
2020-10-21 09:24:02 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
2022-12-30 11:29:51 +00:00
|
|
|
opts[i](&p.cfg)
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-30 11:29:51 +00:00
|
|
|
p.log = &logger.Logger{Logger: p.cfg.log.With(zap.String("component", "Object Policer"))}
|
2020-10-21 09:24:02 +00:00
|
|
|
|
2022-12-30 11:29:51 +00:00
|
|
|
cache, err := lru.New[oid.Address, time.Time](int(p.cacheSize))
|
2021-11-10 08:34:00 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-12-30 11:29:51 +00:00
|
|
|
p.cache = cache
|
|
|
|
p.objsInWork.objs = make(map[oid.Address]struct{}, p.maxCapacity)
|
|
|
|
return &p
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithHeadTimeout returns option to set Head timeout of Policer.
|
|
|
|
func WithHeadTimeout(v time.Duration) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.headTimeout = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger returns option to set Logger of Policer.
|
|
|
|
func WithLogger(v *logger.Logger) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.log = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLocalStorage returns option to set local object storage of Policer.
|
2020-11-19 08:22:43 +00:00
|
|
|
func WithLocalStorage(v *engine.StorageEngine) Option {
|
2020-10-21 09:24:02 +00:00
|
|
|
return func(c *cfg) {
|
|
|
|
c.jobQueue.localStorage = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContainerSource returns option to set container source of Policer.
|
|
|
|
func WithContainerSource(v container.Source) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.cnrSrc = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPlacementBuilder returns option to set object placement builder of Policer.
|
|
|
|
func WithPlacementBuilder(v placement.Builder) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.placementBuilder = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithRemoteHeader returns option to set object header receiver of Policer.
|
|
|
|
func WithRemoteHeader(v *headsvc.RemoteHeader) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.remoteHeader = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:17:14 +00:00
|
|
|
// WithNetmapKeys returns option to set tool to work with announced public keys.
|
|
|
|
func WithNetmapKeys(v netmap.AnnouncedKeys) Option {
|
2020-10-21 09:24:02 +00:00
|
|
|
return func(c *cfg) {
|
2021-09-06 12:17:14 +00:00
|
|
|
c.netmapKeys = v
|
2020-10-21 09:24:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 11:50:48 +00:00
|
|
|
|
|
|
|
// WithReplicator returns option to set object replicator of Policer.
|
|
|
|
func WithReplicator(v *replicator.Replicator) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.replicator = v
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 15:21:46 +00:00
|
|
|
|
|
|
|
// WithRedundantCopyCallback returns option to set
|
|
|
|
// callback to pass redundant local object copies
|
|
|
|
// detected by Policer.
|
|
|
|
func WithRedundantCopyCallback(cb RedundantCopyCallback) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.cbRedundantCopy = cb
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 08:34:00 +00:00
|
|
|
|
|
|
|
// WithMaxCapacity returns option to set max capacity
|
|
|
|
// that can be set to the pool.
|
2022-10-17 12:37:01 +00:00
|
|
|
func WithMaxCapacity(capacity int) Option {
|
2021-11-10 08:34:00 +00:00
|
|
|
return func(c *cfg) {
|
2022-10-17 12:37:01 +00:00
|
|
|
c.maxCapacity = capacity
|
2021-11-10 08:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithPool returns option to set pool for
|
|
|
|
// policy and replication operations.
|
|
|
|
func WithPool(p *ants.Pool) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.taskPool = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 15:59:38 +00:00
|
|
|
// WithNodeLoader returns option to set FrostFS node load source.
|
2021-11-10 08:34:00 +00:00
|
|
|
func WithNodeLoader(l nodeLoader) Option {
|
|
|
|
return func(c *cfg) {
|
|
|
|
c.loader = l
|
|
|
|
}
|
|
|
|
}
|