|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package besthead provides a `framework.InterFlowDispatchPolicy` that selects the queue containing the single "best" |
| 18 | +// item from across all queues in a priority band. |
| 19 | +package besthead |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + |
| 24 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/flowcontrol/framework" |
| 25 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/flowcontrol/framework/plugins/policies/interflow/dispatch" |
| 26 | + "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/flowcontrol/types" |
| 27 | +) |
| 28 | + |
| 29 | +// BestHeadPolicyName is the name of the Best Head policy implementation. |
| 30 | +const BestHeadPolicyName = "BestHead" |
| 31 | + |
| 32 | +func init() { |
| 33 | + dispatch.MustRegisterPolicy(dispatch.RegisteredPolicyName(BestHeadPolicyName), |
| 34 | + func() (framework.InterFlowDispatchPolicy, error) { |
| 35 | + return newBestHead(), nil |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +type bestHead struct{} |
| 40 | + |
| 41 | +func newBestHead() *bestHead { |
| 42 | + return &bestHead{} |
| 43 | +} |
| 44 | + |
| 45 | +// Name returns the name of the policy. |
| 46 | +func (p *bestHead) Name() string { |
| 47 | + return BestHeadPolicyName |
| 48 | +} |
| 49 | + |
| 50 | +// SelectQueue implements a greedy strategy that bypasses fairness concerns to select the queue containing the single |
| 51 | +// "best" item from across all queues in the priority band. It iterates through all non-empty queues, peeks at their |
| 52 | +// head items, and uses the `framework.ItemComparator` from each queue to find the highest-priority item overall. |
| 53 | +// |
| 54 | +// This policy is useful for maximizing utilization when fairness between flows is not a concern. It requires that all |
| 55 | +// queues being compared have a compatible `framework.ScoreType` to ensure the comparison is meaningful. If an |
| 56 | +// incompatible comparator is found, the selection fails with an error. |
| 57 | +func (p *bestHead) SelectQueue(band framework.PriorityBandAccessor) (framework.FlowQueueAccessor, error) { |
| 58 | + if band == nil { |
| 59 | + return nil, nil |
| 60 | + } |
| 61 | + |
| 62 | + var bestQueue framework.FlowQueueAccessor |
| 63 | + var bestItem types.QueueItemAccessor |
| 64 | + |
| 65 | + var iterationErr error |
| 66 | + band.IterateQueues(func(queue framework.FlowQueueAccessor) (keepIterating bool) { |
| 67 | + if queue == nil || queue.Len() == 0 { |
| 68 | + return true |
| 69 | + } |
| 70 | + |
| 71 | + item, err := queue.PeekHead() |
| 72 | + if err != nil || item == nil { |
| 73 | + return true |
| 74 | + } |
| 75 | + |
| 76 | + if bestQueue == nil { |
| 77 | + bestQueue = queue |
| 78 | + bestItem = item |
| 79 | + return true |
| 80 | + } |
| 81 | + |
| 82 | + if queue.Comparator().ScoreType() != bestQueue.Comparator().ScoreType() { |
| 83 | + iterationErr = fmt.Errorf("%w: expected %q, got %q", framework.ErrIncompatiblePriorityType, |
| 84 | + bestQueue.Comparator().ScoreType(), queue.Comparator().ScoreType()) |
| 85 | + return false |
| 86 | + } |
| 87 | + |
| 88 | + if bestQueue.Comparator().Func()(item, bestItem) { |
| 89 | + bestQueue = queue |
| 90 | + bestItem = item |
| 91 | + } |
| 92 | + return true |
| 93 | + }) |
| 94 | + |
| 95 | + if iterationErr != nil { |
| 96 | + return nil, iterationErr |
| 97 | + } |
| 98 | + return bestQueue, nil |
| 99 | +} |
0 commit comments