Skip to content

Commit

Permalink
Revert target creator helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
utr1903 committed Jan 2, 2024
1 parent 93aa713 commit fa5c0c5
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 28 deletions.
15 changes: 1 addition & 14 deletions cmd/otel-allocator/allocation/allocatortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,14 @@ import (
"github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/target"
)

func MakeNNewTargetsWithoutPreAssigningCollectors(n int, startingIndex int) map[string]*target.Item {
toReturn := map[string]*target.Item{}
for i := startingIndex; i < n+startingIndex; i++ {
label := model.LabelSet{
"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, "")
toReturn[newTarget.Hash()] = newTarget
}
return toReturn
}

func colIndex(index, numCols int) int {
if numCols == 0 {
return -1
}
return index % numCols
}

func MakeNNewTargetsWithPreAssigningCollectors(n int, numCollectors int, startingIndex int) map[string]*target.Item {
func MakeNNewTargets(n int, numCollectors int, startingIndex int) map[string]*target.Item {
toReturn := map[string]*target.Item{}
for i := startingIndex; i < n+startingIndex; i++ {
collector := fmt.Sprintf("collector-%d", colIndex(i, numCollectors))
Expand Down
8 changes: 4 additions & 4 deletions cmd/otel-allocator/allocation/consistent_hashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestCanSetSingleTarget(t *testing.T) {
cols := MakeNCollectors(3, 0)
c := newConsistentHashingAllocator(logger)
c.SetCollectors(cols)
c.SetTargets(MakeNNewTargetsWithPreAssigningCollectors(1, 3, 0))
c.SetTargets(MakeNNewTargets(1, 3, 0))
actualTargetItems := c.TargetItems()
assert.Len(t, actualTargetItems, 1)
for _, item := range actualTargetItems {
Expand All @@ -40,7 +40,7 @@ func TestRelativelyEvenDistribution(t *testing.T) {
expectedDelta := (expectedPerCollector * 1.5) - expectedPerCollector
c := newConsistentHashingAllocator(logger)
c.SetCollectors(cols)
c.SetTargets(MakeNNewTargetsWithPreAssigningCollectors(numItems, 0, 0))
c.SetTargets(MakeNNewTargets(numItems, 0, 0))
actualTargetItems := c.TargetItems()
assert.Len(t, actualTargetItems, numItems)
actualCollectors := c.Collectors()
Expand All @@ -54,7 +54,7 @@ func TestFullReallocation(t *testing.T) {
cols := MakeNCollectors(10, 0)
c := newConsistentHashingAllocator(logger)
c.SetCollectors(cols)
c.SetTargets(MakeNNewTargetsWithPreAssigningCollectors(10000, 10, 0))
c.SetTargets(MakeNNewTargets(10000, 10, 0))
actualTargetItems := c.TargetItems()
assert.Len(t, actualTargetItems, 10000)
actualCollectors := c.Collectors()
Expand All @@ -79,7 +79,7 @@ func TestNumRemapped(t *testing.T) {
cols := MakeNCollectors(numInitialCols, 0)
c := newConsistentHashingAllocator(logger)
c.SetCollectors(cols)
c.SetTargets(MakeNNewTargetsWithPreAssigningCollectors(numItems, numInitialCols, 0))
c.SetTargets(MakeNNewTargets(numItems, numInitialCols, 0))
actualTargetItems := c.TargetItems()
assert.Len(t, actualTargetItems, numItems)
actualCollectors := c.Collectors()
Expand Down
14 changes: 7 additions & 7 deletions cmd/otel-allocator/allocation/least_weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAddingAndRemovingTargets(t *testing.T) {
cols := MakeNCollectors(3, 0)
s.SetCollectors(cols)

initTargets := MakeNNewTargetsWithPreAssigningCollectors(6, 3, 0)
initTargets := MakeNNewTargets(6, 3, 0)

// test that targets and collectors are added properly
s.SetTargets(initTargets)
Expand All @@ -61,7 +61,7 @@ func TestAddingAndRemovingTargets(t *testing.T) {
assert.Len(t, s.TargetItems(), expectedTargetLen)

// prepare second round of targets
tar := MakeNNewTargetsWithPreAssigningCollectors(4, 3, 0)
tar := MakeNNewTargets(4, 3, 0)

// test that fewer targets are found - removed
s.SetTargets(tar)
Expand Down Expand Up @@ -126,7 +126,7 @@ func TestNoCollectorReassignment(t *testing.T) {
for _, i := range cols {
assert.NotNil(t, s.Collectors()[i.Name])
}
initTargets := MakeNNewTargetsWithPreAssigningCollectors(6, 3, 0)
initTargets := MakeNNewTargets(6, 3, 0)

// test that targets and collectors are added properly
s.SetTargets(initTargets)
Expand Down Expand Up @@ -161,7 +161,7 @@ func TestNoAssignmentToNewCollector(t *testing.T) {
}

initialColsBeforeAddingNewCol := s.Collectors()
initTargets := MakeNNewTargetsWithoutPreAssigningCollectors(6, 0)
initTargets := MakeNNewTargets(6, 0, 0)

// test that targets and collectors are added properly
s.SetTargets(initTargets)
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestSmartCollectorReassignment(t *testing.T) {
for _, i := range cols {
assert.NotNil(t, s.Collectors()[i.Name])
}
initTargets := MakeNNewTargetsWithPreAssigningCollectors(6, 0, 0)
initTargets := MakeNNewTargets(6, 0, 0)
// test that targets and collectors are added properly
s.SetTargets(initTargets)

Expand Down Expand Up @@ -253,7 +253,7 @@ func TestCollectorBalanceWhenAddingAndRemovingAtRandom(t *testing.T) {
cols := MakeNCollectors(3, 0)
s.SetCollectors(cols)

targets := MakeNNewTargetsWithPreAssigningCollectors(27, 3, 0)
targets := MakeNNewTargets(27, 3, 0)
s.SetTargets(targets)

// Divisor needed to get 15%
Expand Down Expand Up @@ -292,7 +292,7 @@ func TestCollectorBalanceWhenAddingAndRemovingAtRandom(t *testing.T) {
assert.InDelta(t, i.NumTargets, count, math.Round(percent))
}
// adding targets at 'random'
for _, item := range MakeNNewTargetsWithPreAssigningCollectors(13, 3, 100) {
for _, item := range MakeNNewTargets(13, 3, 100) {
targets[item.Hash()] = item
}
s.SetTargets(targets)
Expand Down
4 changes: 2 additions & 2 deletions cmd/otel-allocator/allocation/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func BenchmarkGetAllTargetsByCollectorAndJob(b *testing.B) {
b.Fail()
}
cols := MakeNCollectors(v.numCollectors, 0)
jobs := MakeNNewTargetsWithPreAssigningCollectors(v.numJobs, v.numCollectors, 0)
jobs := MakeNNewTargets(v.numJobs, v.numCollectors, 0)
a.SetCollectors(cols)
a.SetTargets(jobs)
b.Run(fmt.Sprintf("%s_num_cols_%d_num_jobs_%d", s, v.numCollectors, v.numJobs), func(b *testing.B) {
Expand Down Expand Up @@ -76,7 +76,7 @@ func Benchmark_Setting(b *testing.B) {
for _, v := range table {
a, _ := New(s, logger)
cols := MakeNCollectors(v.numCollectors, 0)
targets := MakeNNewTargetsWithPreAssigningCollectors(v.numTargets, v.numCollectors, 0)
targets := MakeNNewTargets(v.numTargets, v.numCollectors, 0)
b.Run(fmt.Sprintf("%s_num_cols_%d_num_jobs_%d", s, v.numCollectors, v.numTargets), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion cmd/otel-allocator/server/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func BenchmarkServerTargetsHandler(b *testing.B) {
for _, v := range table {
a, _ := allocation.New(allocatorName, logger)
cols := allocation.MakeNCollectors(v.numCollectors, 0)
targets := allocation.MakeNNewTargetsWithPreAssigningCollectors(v.numJobs, v.numCollectors, 0)
targets := allocation.MakeNNewTargets(v.numJobs, v.numCollectors, 0)
listenAddr := ":8080"
a.SetCollectors(cols)
a.SetTargets(targets)
Expand Down

0 comments on commit fa5c0c5

Please sign in to comment.