-
Notifications
You must be signed in to change notification settings - Fork 204
Merge shuffle score pods logic #1552
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,9 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "math/rand" | ||
| "slices" | ||
| "time" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep import groups separated and not combine |
||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
|
|
@@ -85,15 +82,8 @@ func (p *MaxScorePicker) Pick(ctx context.Context, cycleState *types.CycleState, | |
| log.FromContext(ctx).V(logutil.DEBUG).Info("Selecting pods from candidates sorted by max score", "max-num-of-endpoints", p.maxNumOfEndpoints, | ||
| "num-of-candidates", len(scoredPods), "scored-pods", scoredPods) | ||
|
|
||
| // TODO: merge this with the logic in RandomPicker | ||
| // Rand package is not safe for concurrent use, so we create a new instance. | ||
| // Source: https://pkg.go.dev/math/rand#pkg-overview | ||
| randomGenerator := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
|
||
| // Shuffle in-place - needed for random tie break when scores are equal | ||
| randomGenerator.Shuffle(len(scoredPods), func(i, j int) { | ||
| scoredPods[i], scoredPods[j] = scoredPods[j], scoredPods[i] | ||
| }) | ||
| shuffleScoredPods(scoredPods) | ||
|
|
||
| slices.SortStableFunc(scoredPods, func(i, j *types.ScoredPod) int { // highest score first | ||
| if i.Score > j.Score { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,11 +20,8 @@ import ( | |
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "math/rand" | ||
| "time" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/log" | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/plugins" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework" | ||
| "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types" | ||
|
|
@@ -84,15 +81,8 @@ func (p *RandomPicker) Pick(ctx context.Context, _ *types.CycleState, scoredPods | |
| log.FromContext(ctx).V(logutil.DEBUG).Info("Selecting pods from candidates randomly", "max-num-of-endpoints", p.maxNumOfEndpoints, | ||
| "num-of-candidates", len(scoredPods), "scored-pods", scoredPods) | ||
|
|
||
| // TODO: merge this with the logic in MaxScorePicker | ||
| // Rand package is not safe for concurrent use, so we create a new instance. | ||
| // Source: https://pkg.go.dev/math/rand#pkg-overview | ||
| randomGenerator := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
|
|
||
| // Shuffle in-place | ||
| randomGenerator.Shuffle(len(scoredPods), func(i, j int) { | ||
| scoredPods[i], scoredPods[j] = scoredPods[j], scoredPods[i] | ||
| }) | ||
| shuffleScoredPods(scoredPods) | ||
|
|
||
| // if we have enough pods to return keep only the relevant subset | ||
| if p.maxNumOfEndpoints < len(scoredPods) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you compared using
sync.Poolvs creating a new random source every time?I don't expect much latency difference but a
sync.Poolwould reduce GC pressure as these are created and collected per request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested it locally and the test result is as follows. If there is any problem, please point it out. If the test results are accurate, do you think it needs to be optimized?
host@hostdeMacBook-Pro picker % go test -bench=. -benchmem goos: darwin goarch: amd64 pkg: sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/picker cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz BenchmarkShuffleScoredPods-8 911576 1245 ns/op 912 B/op 2 allocs/op BenchmarkShuffleScoredPodsWithPool-8 1000000 1130 ns/op 896 B/op 1 allocs/op BenchmarkShuffleScoredPods_Small-8 4351080 247.4 ns/op 96 B/op 2 allocs/op BenchmarkShuffleScoredPodsWithPool_Small-8 7934428 139.8 ns/op 80 B/op 1 allocs/op BenchmarkShuffleScoredPods_Large-8 115184 10405 ns/op 8208 B/op 2 allocs/op BenchmarkShuffleScoredPodsWithPool_Large-8 115098 10628 ns/op 8195 B/op 1 allocs/op PASS ok sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/picker 7.652s