Skip to content

Commit 12e1282

Browse files
committed
Add least-weighted as fallback strategy for per-node strategy
1 parent 7e6b3cf commit 12e1282

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

cmd/otel-allocator/allocation/per_node.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ const perNodeStrategyName = "per-node"
2525
var _ Strategy = &perNodeStrategy{}
2626

2727
type perNodeStrategy struct {
28-
collectorByNode map[string]*Collector
28+
collectorByNode map[string]*Collector
29+
fallbackStrategy Strategy
2930
}
3031

31-
func newPerNodeStrategy() Strategy {
32+
func newPerNodeStrategy(fallbackStrategy Strategy) Strategy {
3233
return &perNodeStrategy{
33-
collectorByNode: make(map[string]*Collector),
34+
collectorByNode: make(map[string]*Collector),
35+
fallbackStrategy: fallbackStrategy,
3436
}
3537
}
3638

@@ -40,6 +42,10 @@ func (s *perNodeStrategy) GetName() string {
4042

4143
func (s *perNodeStrategy) GetCollectorForTarget(collectors map[string]*Collector, item *target.Item) (*Collector, error) {
4244
targetNodeName := item.GetNodeName()
45+
if targetNodeName == "" {
46+
return s.fallbackStrategy.GetCollectorForTarget(collectors, item)
47+
}
48+
4349
collector, ok := s.collectorByNode[targetNodeName]
4450
if !ok {
4551
return nil, fmt.Errorf("could not find collector for node %s", targetNodeName)
@@ -54,4 +60,5 @@ func (s *perNodeStrategy) SetCollectors(collectors map[string]*Collector) {
5460
s.collectorByNode[collector.NodeName] = collector
5561
}
5662
}
63+
s.fallbackStrategy.SetCollectors(collectors)
5764
}

cmd/otel-allocator/allocation/per_node_test.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ import (
2626

2727
var loggerPerNode = logf.Log.WithName("unit-tests")
2828

29+
func GetTargetsWithNodeName(targets []*target.Item) (targetsWithNodeName []*target.Item) {
30+
for _, item := range targets {
31+
if item.GetNodeName() != "" {
32+
targetsWithNodeName = append(targetsWithNodeName, item)
33+
}
34+
}
35+
return targetsWithNodeName
36+
}
37+
2938
// Tests that two targets with the same target url and job name but different label set are both added.
3039
func TestAllocationPerNode(t *testing.T) {
3140
// prepare allocator with initial targets and collectors
@@ -83,13 +92,18 @@ func TestAllocationPerNode(t *testing.T) {
8392
// only the first two targets should be allocated
8493
itemsForCollector := s.GetTargetsForCollectorAndJob(actualItem.CollectorName, actualItem.JobName)
8594

86-
// first two should be assigned one to each collector; if third target, should not be assigned
95+
// first two should be assigned one to each collector; if third target, it should be assigned
96+
// according to the fallback strategy which may assign it to the otherwise empty collector or
97+
// one of the others, depending on the strategy and collector loop order
8798
if targetHash == thirdTarget.Hash() {
88-
assert.Len(t, itemsForCollector, 0)
99+
assert.Empty(t, item.GetNodeName())
100+
assert.LessOrEqual(t, len(itemsForCollector), 2)
89101
continue
90102
}
91-
assert.Len(t, itemsForCollector, 1)
92-
assert.Equal(t, actualItem, itemsForCollector[0])
103+
104+
// Only check targets that have been assigned using the per-node (not fallback) strategy here
105+
assert.Len(t, GetTargetsWithNodeName(itemsForCollector), 1)
106+
assert.Equal(t, actualItem, GetTargetsWithNodeName(itemsForCollector)[0])
93107
}
94108
}
95109

cmd/otel-allocator/allocation/strategy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func init() {
149149
panic(err)
150150
}
151151
err = Register(perNodeStrategyName, func(log logr.Logger, opts ...AllocationOption) Allocator {
152-
return newAllocator(log, newPerNodeStrategy(), opts...)
152+
return newAllocator(log, newPerNodeStrategy(newleastWeightedStrategy()), opts...)
153153
})
154154
if err != nil {
155155
panic(err)

0 commit comments

Comments
 (0)