diff --git a/cmd/otel-allocator/allocation/allocatortest.go b/cmd/otel-allocator/allocation/allocatortest.go index 88312a80a5..74dc8a44e1 100644 --- a/cmd/otel-allocator/allocation/allocatortest.go +++ b/cmd/otel-allocator/allocation/allocatortest.go @@ -35,9 +35,8 @@ func MakeNNewTargets(n int, numCollectors int, startingIndex int) map[string]*ta for i := startingIndex; i < n+startingIndex; i++ { collector := fmt.Sprintf("collector-%d", colIndex(i, numCollectors)) label := model.LabelSet{ - "collector": model.LabelValue(collector), - "i": model.LabelValue(strconv.Itoa(i)), - "total": model.LabelValue(strconv.Itoa(n + startingIndex)), + "i": model.LabelValue(strconv.Itoa(i)), + "total": model.LabelValue(strconv.Itoa(n + startingIndex)), } newTarget := target.NewItem(fmt.Sprintf("test-job-%d", i), fmt.Sprintf("test-url-%d", i), label, collector) toReturn[newTarget.Hash()] = newTarget diff --git a/cmd/otel-allocator/allocation/least_weighted_test.go b/cmd/otel-allocator/allocation/least_weighted_test.go index 417c0e5ed3..c8c379e4a2 100644 --- a/cmd/otel-allocator/allocation/least_weighted_test.go +++ b/cmd/otel-allocator/allocation/least_weighted_test.go @@ -15,6 +15,7 @@ package allocation import ( + "fmt" "math" "math/rand" "testing" @@ -144,6 +145,56 @@ func TestNoCollectorReassignment(t *testing.T) { } +// Tests that the newly added collector instance does not get assigned any target when the targets remain the same. +func TestNoAssignmentToNewCollector(t *testing.T) { + s, _ := New("least-weighted", logger) + + // instantiate only 1 collector + cols := MakeNCollectors(1, 0) + s.SetCollectors(cols) + + expectedColLen := len(cols) + assert.Len(t, s.Collectors(), expectedColLen) + + for _, i := range cols { + assert.NotNil(t, s.Collectors()[i.Name]) + } + + initialColsBeforeAddingNewCol := s.Collectors() + initTargets := MakeNNewTargets(6, 0, 0) + + // test that targets and collectors are added properly + s.SetTargets(initTargets) + + // verify + expectedTargetLen := len(initTargets) + targetItems := s.TargetItems() + assert.Len(t, targetItems, expectedTargetLen) + + // add another collector + newColName := fmt.Sprintf("collector-%d", len(cols)) + cols[newColName] = &Collector{ + Name: newColName, + NumTargets: 0, + } + s.SetCollectors(cols) + + // targets shall not change + newTargetItems := s.TargetItems() + assert.Equal(t, targetItems, newTargetItems) + + // initial collectors still should have the same targets + for colName, col := range s.Collectors() { + if colName != newColName { + assert.Equal(t, initialColsBeforeAddingNewCol[colName], col) + } + } + + // new collector should have no targets + newCollector := s.Collectors()[newColName] + assert.Equal(t, newCollector.NumTargets, 0) +} + func TestSmartCollectorReassignment(t *testing.T) { t.Skip("This test is flaky and fails frequently, see issue 1291") s, _ := New("least-weighted", logger)