Skip to content

Add max score picker #51

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 2 commits into from
Apr 24, 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
54 changes: 54 additions & 0 deletions pkg/epp/scheduling/plugins/pickers/max-score.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pickers

import (
"fmt"

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

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

var _ types.Picker = &MaxScorePicker{}

// Name returns the name of the picker.
func (msp *MaxScorePicker) Name() string {
return "max-score"
}

// Pick selects the pod with the maximum score from the list of candidates.
func (msp *MaxScorePicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
debugLogger := ctx.Logger.V(logutil.DEBUG).WithName("max-score-picker")
debugLogger.Info(fmt.Sprintf("Selecting the pod with the max score from %d candidates: %+v",
len(pods), pods))

winners := make([]types.Pod, 0)

maxScore := 0.0
for _, pod := range pods {
score := pod.Score()
if score > maxScore {
maxScore = score
winners = []types.Pod{pod}
} else if score == maxScore {
winners = append(winners, pod)
}
}

if len(winners) == 0 {
return nil, nil
}

if len(winners) > 1 {
debugLogger.Info(fmt.Sprintf("Multiple pods have the same max score (%f): %+v",
maxScore, winners))

randomPicker := RandomPicker{}
return randomPicker.Pick(ctx, winners)
}

debugLogger.Info(fmt.Sprintf("Selected pod with max score (%f): %+v", maxScore, winners[0]))
return &types.Result{TargetPod: winners[0]}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package plugins
package pickers

import (
"fmt"
Expand All @@ -24,12 +24,17 @@ import (
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)

// RandomPicker picks a pod randomly from the list of candidates.
type RandomPicker struct{}

var _ types.Picker = &RandomPicker{}

// Name returns the name of the picker.
func (rp *RandomPicker) Name() string {
return "random"
}

// Pick selects a random pod from the list of candidates.
func (rp *RandomPicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a random pod from %d candidates: %+v", len(pods), pods))
i := rand.Intn(len(pods))
Expand Down
5 changes: 3 additions & 2 deletions pkg/epp/scheduling/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/plugins/pickers"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
)
Expand Down Expand Up @@ -74,7 +75,7 @@ func NewScheduler(datastore Datastore) *Scheduler {
postSchedulePlugins: []types.PostSchedule{},
scorers: []types.Scorer{},
filters: []types.Filter{defaultPlugin},
picker: defaultPlugin,
picker: &pickers.MaxScorePicker{},
}
}

Expand Down Expand Up @@ -198,7 +199,7 @@ func runScorersForPod(ctx *types.Context, scorers []types.Scorer, pod types.Pod)
}

type defaultPlugin struct {
plugins.RandomPicker
pickers.RandomPicker
}

func (p *defaultPlugin) Name() string {
Expand Down
1 change: 1 addition & 0 deletions pkg/epp/scheduling/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Filter interface {
}

// Scorer defines the interface for scoring pods based on context.
// Scorers must return a score between 0 and 1, where 1 is the best score.
type Scorer interface {
Plugin
Score(ctx *Context, pod Pod) (float64, error)
Expand Down