Skip to content

Session affinity scorer #52

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions pkg/epp/scheduling/max_score_picker.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file seems like it belongs to a different PR and not to a session affinity scorer

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scheduling

import (
"fmt"
"math/rand/v2"

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

type maxScorePicker struct {
}

func (p *maxScorePicker) Name() string {
return "max score picker"
}

func (p *maxScorePicker) Pick(ctx *types.Context, pods []types.Pod) (*types.Result, error) {
ctx.Logger.V(logutil.DEBUG).Info(fmt.Sprintf("Selecting a pod with maximum score from %d candidates: %+v", len(pods), pods))

// select pod with maximum score, if more than one with the max score - use random pods from the list
var highestScoreTargets []types.Pod
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not sure if it makes sense to have such long variable names in a relatively short function. I don't think we have a style guide, but Go seems to favor shorter names in smaller scopes

// score weights cound be negative
maxScore := 0.0
isFirst := true

for _, pod := range pods {
if isFirst {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to test isFirst on every iteration, by doing some work outside the loop:

  • set the first pod and its score as the maximum Pod and score
  • set maxScore = -math.MaxFloat64

maxScore = pod.Score()
highestScoreTargets = []types.Pod{pod}
isFirst = false
} else {
if pod.Score() > maxScore {
maxScore = pod.Score()
highestScoreTargets = []types.Pod{pod}
} else if pod.Score() == maxScore {
highestScoreTargets = append(highestScoreTargets, pod)
}
}
}

// single pod with max score
if len(highestScoreTargets) == 1 {
return &types.Result{TargetPod: highestScoreTargets[0]}, nil
}

// select random pod from list of pods with max score
return &types.Result{TargetPod: highestScoreTargets[rand.IntN(len(highestScoreTargets))]}, nil
}
4 changes: 2 additions & 2 deletions pkg/epp/scheduling/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func NewScheduler(datastore Datastore) *Scheduler {
datastore: datastore,
preSchedulePlugins: []types.PreSchedule{},
postSchedulePlugins: []types.PostSchedule{},
scorers: []types.Scorer{},
scorers: []types.Scorer{&SessionAffinityScorer{1.0, datastore}},
filters: []types.Filter{defaultPlugin},
picker: defaultPlugin,
picker: &maxScorePicker{},
}
}

Expand Down
56 changes: 56 additions & 0 deletions pkg/epp/scheduling/session_affinity_scorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scheduling

import (
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/types"
)

// sessionAffinity is a routing scorer that routes subsequent
// requests in a session to the same pod as the first request in the
// session was sent to, by giving that pod the specified weight and assigning
// zero score to the rest of the targets
type SessionAffinityScorer struct {
weight float64
datastore Datastore
}

func NewSessionAffinityScorer(weight float64, datastore Datastore) types.Scorer {
return SessionAffinityScorer{
weight: weight,
datastore: datastore,
}
}

func (s SessionAffinityScorer) Name() string {
return "session affinity scorer"
}

func (s SessionAffinityScorer) Score(ctx *types.Context, pod types.Pod) (float64, error) {
score := 0.0

if ctx.Req.SessionID != "" {
podForSession := s.datastore.GetPodForSession(ctx.Req.SessionID)
if podForSession != nil {
if podForSession.NamespacedName.String() == pod.GetPod().NamespacedName.String() {
score = 1.0
}
}
}

return score, nil
}