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

Add test for adding new collector after completed initial allocation #2478

Merged
merged 4 commits into from
Jan 2, 2024
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
5 changes: 2 additions & 3 deletions cmd/otel-allocator/allocation/allocatortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions cmd/otel-allocator/allocation/least_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package allocation

import (
"fmt"
"math"
"math/rand"
"testing"
Expand Down Expand Up @@ -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)
Expand Down
Loading