-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: dev
Are you sure you want to change the base?
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 |
---|---|---|
@@ -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 | ||
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. 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 { | ||
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. There is no reason to test
|
||
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 | ||
} |
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 | ||
} |
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.
This file seems like it belongs to a different PR and not to a session affinity scorer