Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] simplify allocation Option and registry #3645

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/otel-allocator/allocation/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

var _ Allocator = &allocator{}

func newAllocator(log logr.Logger, strategy Strategy, opts ...AllocationOption) Allocator {
func newAllocator(log logr.Logger, strategy Strategy, opts ...Option) Allocator {
chAllocator := &allocator{
strategy: strategy,
collectors: make(map[string]*Collector),
Expand Down
52 changes: 14 additions & 38 deletions cmd/otel-allocator/allocation/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package allocation

import (
"errors"
"fmt"

"github.com/buraksezer/consistent"
Expand All @@ -26,12 +25,14 @@ import (
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
)

type AllocatorProvider func(log logr.Logger, opts ...AllocationOption) Allocator
type AllocatorProvider func(log logr.Logger, opts ...Option) Allocator

var (
strategies = map[string]Strategy{}

registry = map[string]AllocatorProvider{}
strategies = map[string]Strategy{
leastWeightedStrategyName: newleastWeightedStrategy(),
consistentHashingStrategyName: newConsistentHashingStrategy(),
perNodeStrategyName: newPerNodeStrategy(),
}

// TargetsPerCollector records how many targets have been assigned to each collector.
// It is currently the responsibility of the strategy to track this information.
Expand All @@ -57,19 +58,19 @@ var (
})
)

type AllocationOption func(Allocator)
type Option func(Allocator)

type Filter interface {
Apply(map[string]*target.Item) map[string]*target.Item
}

func WithFilter(filter Filter) AllocationOption {
func WithFilter(filter Filter) Option {
return func(allocator Allocator) {
allocator.SetFilter(filter)
}
}

func WithFallbackStrategy(fallbackStrategy string) AllocationOption {
func WithFallbackStrategy(fallbackStrategy string) Option {
var strategy, ok = strategies[fallbackStrategy]
if fallbackStrategy != "" && !ok {
panic(fmt.Errorf("unregistered strategy used as fallback: %s", fallbackStrategy))
Expand All @@ -83,24 +84,16 @@ func RecordTargetsKept(targets map[string]*target.Item) {
TargetsRemaining.Set(float64(len(targets)))
}

func New(name string, log logr.Logger, opts ...AllocationOption) (Allocator, error) {
if p, ok := registry[name]; ok {
return p(log.WithValues("allocator", name), opts...), nil
func New(name string, log logr.Logger, opts ...Option) (Allocator, error) {
if strategy, ok := strategies[name]; ok {
return newAllocator(log.WithValues("allocator", name), strategy, opts...), nil
}
return nil, fmt.Errorf("unregistered strategy: %s", name)
}

func Register(name string, provider AllocatorProvider) error {
if _, ok := registry[name]; ok {
return errors.New("already registered")
}
registry[name] = provider
return nil
}

func GetRegisteredAllocatorNames() []string {
var names []string
for s := range registry {
for s := range strategies {
names = append(names, s)
}
return names
Expand All @@ -123,7 +116,7 @@ type Strategy interface {
// SetCollectors call. Strategies which don't need this information can just ignore it.
SetCollectors(map[string]*Collector)
GetName() string
// Add fallback strategy for strategies whose main allocation method can sometimes leave targets unassigned
// SetFallbackStrategy adds fallback strategy for strategies whose main allocation method can sometimes leave targets unassigned
SetFallbackStrategy(Strategy)
}

Expand All @@ -149,20 +142,3 @@ func (c Collector) String() string {
func NewCollector(name, node string) *Collector {
return &Collector{Name: name, NodeName: node}
}

func init() {
strategies = map[string]Strategy{
leastWeightedStrategyName: newleastWeightedStrategy(),
consistentHashingStrategyName: newConsistentHashingStrategy(),
perNodeStrategyName: newPerNodeStrategy(),
}

for strategyName, strategy := range strategies {
err := Register(strategyName, func(log logr.Logger, opts ...AllocationOption) Allocator {
return newAllocator(log, strategy, opts...)
})
if err != nil {
panic(err)
}
}
}
8 changes: 1 addition & 7 deletions cmd/otel-allocator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ func main() {
log := ctrl.Log.WithName("allocator")

allocatorPrehook = prehook.New(cfg.FilterStrategy, log)

var allocationOptions []allocation.AllocationOption
allocationOptions = append(allocationOptions, allocation.WithFilter(allocatorPrehook))
if cfg.AllocationFallbackStrategy != "" {
allocationOptions = append(allocationOptions, allocation.WithFallbackStrategy(cfg.AllocationFallbackStrategy))
}
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocationOptions...)
allocator, err = allocation.New(cfg.AllocationStrategy, log, allocation.WithFilter(allocatorPrehook), allocation.WithFallbackStrategy(cfg.AllocationFallbackStrategy))
if err != nil {
setupLog.Error(err, "Unable to initialize allocation strategy")
os.Exit(1)
Expand Down
Loading