-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Availability Zones are allocated fairly (on creation and resize) if not specified or number of nodes exceeds those specified
- Loading branch information
Showing
11 changed files
with
368 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package lib | ||
|
||
import ( | ||
"math" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
type CountedItem struct { | ||
Name string | ||
Count int | ||
} | ||
|
||
func AllocateN(entries []CountedItem, n int) []string { | ||
total := len(entries) | ||
if n <= 0 || total == 0 { | ||
return nil | ||
} | ||
|
||
sum := n | ||
for _, e := range entries { | ||
sum += e.Count | ||
} | ||
|
||
average := int(math.Ceil(float64(sum) / float64(total))) | ||
ls := make([]string, 0, n) | ||
|
||
slices.SortStableFunc(entries, func(a, b CountedItem) int { // allocation to start with items having the lowest counts | ||
if a.Count == b.Count { | ||
return strings.Compare(a.Name, b.Name) | ||
} | ||
|
||
return a.Count - b.Count | ||
}) | ||
|
||
for _, e := range entries { | ||
if e.Count > average { // skip the ones with more than average, they get nothing | ||
continue | ||
} | ||
|
||
w := average - e.Count // how many more to add to reach average | ||
|
||
if w > n { // do not exceed what we need | ||
w = n | ||
} | ||
|
||
n -= w | ||
|
||
for i := 0; i < w; i++ { | ||
ls = append(ls, e.Name) | ||
} | ||
|
||
if n == 0 { // we do not need more | ||
return ls | ||
} | ||
} | ||
|
||
return ls | ||
} |
Oops, something went wrong.