Skip to content

Commit 99fdfa8

Browse files
committed
minor changes in max score picker
Signed-off-by: Nir Rozenbaum <[email protected]>
1 parent cd568dc commit 99fdfa8

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

pkg/epp/scheduling/plugins/picker/max_score_picker.go

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,42 @@ import (
88
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
99
)
1010

11-
// MaxScorePicker picks the pod with the maximum score from the list of
12-
// candidates.
13-
type MaxScorePicker struct{}
14-
1511
var _ plugins.Picker = &MaxScorePicker{}
1612

13+
func NewMaxScorePicker() plugins.Picker {
14+
return &MaxScorePicker{
15+
random: &RandomPicker{},
16+
}
17+
}
18+
19+
// MaxScorePicker picks the pod with the maximum score from the list of candidates.
20+
type MaxScorePicker struct {
21+
random *RandomPicker
22+
}
23+
1724
// Name returns the name of the picker.
18-
func (msp *MaxScorePicker) Name() string {
25+
func (p *MaxScorePicker) Name() string {
1926
return "max_score"
2027
}
2128

2229
// Pick selects the pod with the maximum score from the list of candidates.
23-
func (msp *MaxScorePicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
24-
debugLogger := ctx.Logger.V(logutil.DEBUG).WithName("max-score-picker")
25-
debugLogger.Info(fmt.Sprintf("Selecting the pod with the max score from %d candidates: %+v",
26-
len(scoredPods), scoredPods))
27-
28-
winners := make([]*types.ScoredPod, 0)
30+
func (p *MaxScorePicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
31+
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a pod with the max score from %d candidates: %+v", len(scoredPods), scoredPods))
2932

30-
maxScore := 0.0
33+
highestScorePods := []*types.ScoredPod{}
34+
maxScore := -1.0 // pods min score is 0, putting value lower than 0 in order to find at least one pod as highest
3135
for _, pod := range scoredPods {
32-
score := pod.Score
33-
if score > maxScore {
34-
maxScore = score
35-
winners = []*types.ScoredPod{pod}
36-
} else if score == maxScore {
37-
winners = append(winners, pod)
36+
if pod.Score > maxScore {
37+
maxScore = pod.Score
38+
highestScorePods = []*types.ScoredPod{pod}
39+
} else if pod.Score == maxScore {
40+
highestScorePods = append(highestScorePods, pod)
3841
}
3942
}
4043

41-
if len(winners) == 0 {
42-
return nil
43-
}
44-
45-
if len(winners) > 1 {
46-
debugLogger.Info(fmt.Sprintf("Multiple pods have the same max score (%f): %+v",
47-
maxScore, winners))
48-
49-
randomPicker := RandomPicker{}
50-
return randomPicker.Pick(ctx, winners)
44+
if len(highestScorePods) > 1 {
45+
return p.random.Pick(ctx, highestScorePods) // pick randomly from the highest score pods
5146
}
5247

53-
debugLogger.Info(fmt.Sprintf("Selected pod with max score (%f): %+v", maxScore, winners[0]))
54-
return &types.Result{TargetPod: winners[0]}
48+
return &types.Result{TargetPod: highestScorePods[0]}
5549
}

pkg/epp/scheduling/plugins/picker/random_picker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ var _ plugins.Picker = &RandomPicker{}
3030
// RandomPicker picks a random pod from the list of candidates.
3131
type RandomPicker struct{}
3232

33-
func (rp *RandomPicker) Name() string {
33+
func (p *RandomPicker) Name() string {
3434
return "random"
3535
}
3636

37-
func (rp *RandomPicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
37+
func (p *RandomPicker) Pick(ctx *types.SchedulingContext, scoredPods []*types.ScoredPod) *types.Result {
3838
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a random pod from %d candidates: %+v", len(scoredPods), scoredPods))
3939
i := rand.Intn(len(scoredPods))
4040
return &types.Result{TargetPod: scoredPods[i]}

0 commit comments

Comments
 (0)