generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 180
convert subset filter from a plugin to logic in director #1088
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,12 +24,14 @@ import ( | |
"math/rand" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/go-logr/logr" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend" | ||
backendmetrics "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/backend/metrics" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/handlers" | ||
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics" | ||
|
@@ -39,6 +41,11 @@ import ( | |
requtil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/request" | ||
) | ||
|
||
const ( | ||
subsetHintNamespace = "envoy.lb.subset_hint" | ||
subsetHintKey = "x-gateway-destination-endpoint-subset" | ||
) | ||
|
||
// Scheduler defines the interface required by the Director for scheduling. | ||
type Scheduler interface { | ||
Schedule(ctx context.Context, request *schedulingtypes.LLMRequest, candidatePods []schedulingtypes.Pod) (result *schedulingtypes.SchedulingResult, err error) | ||
|
@@ -118,12 +125,12 @@ func (d *Director) HandleRequest(ctx context.Context, reqCtx *handlers.RequestCo | |
} | ||
|
||
// Prepare LLMRequest (needed for both saturation detection and Scheduler) | ||
reqCtx.SchedulingRequest = schedulingtypes.NewLLMRequest( | ||
reqCtx.Request.Headers[requtil.RequestIdHeaderKey], | ||
reqCtx.ResolvedTargetModel, | ||
prompt, | ||
reqCtx.Request.Headers, | ||
reqCtx.Request.Metadata) | ||
reqCtx.SchedulingRequest = &schedulingtypes.LLMRequest{ | ||
RequestId: reqCtx.Request.Headers[requtil.RequestIdHeaderKey], | ||
TargetModel: reqCtx.ResolvedTargetModel, | ||
Prompt: prompt, | ||
Headers: reqCtx.Request.Headers, | ||
} | ||
|
||
logger = logger.WithValues("model", reqCtx.Model, "resolvedTargetModel", reqCtx.ResolvedTargetModel, "criticality", requestCriticality) | ||
|
||
|
@@ -135,11 +142,11 @@ func (d *Director) HandleRequest(ctx context.Context, reqCtx *handlers.RequestCo | |
return reqCtx, err | ||
} | ||
|
||
// --- 3. Call Scheduler --- | ||
// Snapshot pod metrics from the datastore to: | ||
// 1. Reduce concurrent access to the datastore. | ||
// 2. Ensure consistent data during the scheduling operation of a request between all scheduling cycles. | ||
candidatePods := schedulingtypes.ToSchedulerPodMetrics(d.datastore.PodGetAll()) | ||
// --- 3. Call Scheduler (with the relevant candidate pods) --- | ||
candidatePods := d.getCandidatePodsForScheduling(ctx, reqCtx.Request.Metadata) | ||
if len(candidatePods) == 0 { | ||
return reqCtx, errutil.Error{Code: errutil.ServiceUnavailable, Msg: "failed to find candidate pods for serving the request"} | ||
} | ||
results, err := d.scheduler.Schedule(ctx, reqCtx.SchedulingRequest, candidatePods) | ||
if err != nil { | ||
return reqCtx, errutil.Error{Code: errutil.InferencePoolResourceExhausted, Msg: fmt.Errorf("failed to find target pod: %w", err).Error()} | ||
|
@@ -177,6 +184,52 @@ func (d *Director) admitRequest(ctx context.Context, requestCriticality v1alpha2 | |
return nil | ||
} | ||
|
||
// getCandidatePodsForScheduling gets the list of relevant endpoints for the scheduling cycle from the datastore. | ||
// according to EPP protocol, if "x-gateway-destination-endpoint-subset" is set on the request metadata and specifies | ||
// a subset of endpoints, only these endpoints will be considered as candidates for the scheduler. | ||
// Snapshot pod metrics from the datastore to: | ||
// 1. Reduce concurrent access to the datastore. | ||
// 2. Ensure consistent data during the scheduling operation of a request between all scheduling cycles. | ||
func (d *Director) getCandidatePodsForScheduling(ctx context.Context, requestMetadata map[string]any) []schedulingtypes.Pod { | ||
loggerTrace := log.FromContext(ctx).V(logutil.TRACE) | ||
|
||
subsetMap, found := requestMetadata[subsetHintNamespace].(map[string]any) | ||
if !found { | ||
return schedulingtypes.ToSchedulerPodMetrics(d.datastore.PodGetAll()) | ||
} | ||
|
||
// Check if endpoint key is present in the subset map and ensure there is at least one value | ||
endpointSubsetList, found := subsetMap[subsetHintKey].([]any) | ||
if !found { | ||
return schedulingtypes.ToSchedulerPodMetrics(d.datastore.PodGetAll()) | ||
} else if len(endpointSubsetList) == 0 { | ||
loggerTrace.Info("found empty subset filter in request metadata, filtering all pods") | ||
return []schedulingtypes.Pod{} | ||
} | ||
|
||
// Create a map of endpoint addresses for easy lookup | ||
endpoints := make(map[string]bool) | ||
for _, endpoint := range endpointSubsetList { | ||
// Extract address from endpoint | ||
// The endpoint is formatted as "<address>:<port>" (ex. "10.0.1.0:8080") | ||
epStr := strings.Split(endpoint.(string), ":")[0] | ||
endpoints[epStr] = true | ||
} | ||
|
||
podTotalCount := 0 | ||
podFitleredList := d.datastore.PodList(func(pm backendmetrics.PodMetrics) bool { | ||
podTotalCount++ | ||
if _, found := endpoints[pm.GetPod().Address]; found { | ||
return true | ||
} | ||
return false | ||
}) | ||
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. Can we pls a trace log line indicating if the subset key is set, and the number of endpoints it included vs what the datastore has? 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. done |
||
|
||
loggerTrace.Info("filtered candidate pods by subset filtering", "podTotalCount", podTotalCount, "filteredCount", len(podFitleredList)) | ||
|
||
return schedulingtypes.ToSchedulerPodMetrics(podFitleredList) | ||
} | ||
|
||
// prepareRequest populates the RequestContext and calls the registered PreRequest plugins | ||
// for allowing plugging customized logic based on the scheduling results. | ||
func (d *Director) prepareRequest(ctx context.Context, reqCtx *handlers.RequestContext, result *schedulingtypes.SchedulingResult) (*handlers.RequestContext, error) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 comment was moved to the helper function godoc