Skip to content

add max score picker #752

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

Merged
merged 3 commits into from
Apr 28, 2025
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
49 changes: 49 additions & 0 deletions pkg/epp/scheduling/plugins/picker/max_score_picker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package picker

import (
"fmt"

"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)

var _ plugins.Picker = &MaxScorePicker{}

func NewMaxScorePicker() plugins.Picker {
return &MaxScorePicker{
random: &RandomPicker{},
}
}

// MaxScorePicker picks the pod with the maximum score from the list of candidates.
type MaxScorePicker struct {
random *RandomPicker
}

// Name returns the name of the picker.
func (p *MaxScorePicker) Name() string {
return "max_score"
}

// Pick selects the pod with the maximum score from the list of candidates.
func (p *MaxScorePicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a pod with the max score from %d candidates: %+v", len(scoredPods), scoredPods))

highestScorePods := []*types.ScoredPod{}
maxScore := -1.0 // pods min score is 0, putting value lower than 0 in order to find at least one pod as highest
for _, pod := range scoredPods {
if pod.Score > maxScore {
maxScore = pod.Score
highestScorePods = []*types.ScoredPod{pod}
} else if pod.Score == maxScore {
highestScorePods = append(highestScorePods, pod)
}
}

if len(highestScorePods) > 1 {
return p.random.Pick(ctx, highestScorePods) // pick randomly from the highest score pods
}

return &types.Result{TargetPod: highestScorePods[0]}
}
6 changes: 3 additions & 3 deletions pkg/epp/scheduling/plugins/picker/random_picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ var _ plugins.Picker = &RandomPicker{}
// RandomPicker picks a random pod from the list of candidates.
type RandomPicker struct{}

func (rp *RandomPicker) Name() string {
func (p *RandomPicker) Name() string {
return "random"
}

func (rp *RandomPicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
func (p *RandomPicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a random pod from %d candidates: %+v", len(scoredPods), scoredPods))
i := rand.Intn(len(scoredPods))
return &types.Result{TargetPod: scoredPods[i].Pod}
return &types.Result{TargetPod: scoredPods[i]}
}
46 changes: 25 additions & 21 deletions pkg/epp/scheduling/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,19 @@ func TestSchedule(t *testing.T) {
},
},
wantRes: &types.Result{
TargetPod: &types.PodMetrics{
Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}},
Metrics: &backendmetrics.Metrics{
WaitingQueueSize: 3,
KVCacheUsagePercent: 0.1,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo": 1,
"critical": 1,
TargetPod: &types.ScoredPod{
Pod: &types.PodMetrics{
Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod2"}},
Metrics: &backendmetrics.Metrics{
WaitingQueueSize: 3,
KVCacheUsagePercent: 0.1,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo": 1,
"critical": 1,
},
WaitingModels: map[string]int{},
},
WaitingModels: map[string]int{},
},
},
},
Expand Down Expand Up @@ -154,17 +156,19 @@ func TestSchedule(t *testing.T) {
},
},
wantRes: &types.Result{
TargetPod: &types.PodMetrics{
Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}},
Metrics: &backendmetrics.Metrics{
WaitingQueueSize: 0,
KVCacheUsagePercent: 0.2,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo": 1,
"bar": 1,
TargetPod: &types.ScoredPod{
Pod: &types.PodMetrics{
Pod: &backendmetrics.Pod{NamespacedName: k8stypes.NamespacedName{Name: "pod1"}},
Metrics: &backendmetrics.Metrics{
WaitingQueueSize: 0,
KVCacheUsagePercent: 0.2,
MaxActiveModels: 2,
ActiveModels: map[string]int{
"foo": 1,
"bar": 1,
},
WaitingModels: map[string]int{},
},
WaitingModels: map[string]int{},
},
},
},
Expand Down Expand Up @@ -505,7 +509,7 @@ func findPods(ctx *types.SchedulingContext, names ...k8stypes.NamespacedName) []
func getPodScore(scoredPods []*types.ScoredPod, selectedPod types.Pod) float64 {
finalScore := 0.0
for _, scoredPod := range scoredPods {
if scoredPod.Pod.GetPod().NamespacedName.String() == selectedPod.GetPod().NamespacedName.String() {
if scoredPod.GetPod().NamespacedName.String() == selectedPod.GetPod().NamespacedName.String() {
finalScore = scoredPod.Score
break
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/epp/scheduling/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Pod interface {
}

type ScoredPod struct {
Pod Pod
Pod
Score float64
}

Expand Down