Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions cmd/epp/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/saturationdetector"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/filter"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/multi/prefix"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/picker"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/scheduling/framework/plugins/profile"
Expand Down Expand Up @@ -292,7 +291,6 @@ func (r *Runner) initializeScheduler() (*scheduling.Scheduler, error) {
kvCacheScorerWeight := envutil.GetEnvInt("KV_CACHE_SCORE_WEIGHT", scorer.DefaultKVCacheScorerWeight, setupLog)

schedulerProfile := framework.NewSchedulerProfile().
WithFilters(filter.NewSubsetFilter()).
WithScorers(framework.NewWeightedScorer(scorer.NewQueueScorer(), queueScorerWeight),
framework.NewWeightedScorer(scorer.NewKVCacheScorer(), kvCacheScorerWeight)).
WithPicker(picker.NewMaxScorePicker())
Expand Down
4 changes: 2 additions & 2 deletions pkg/epp/backend/metrics/metrics_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"time"
)

// newMetricsState initializes a new MetricsState and returns its pointer.
func newMetricsState() *MetricsState {
// NewMetricsState initializes a new MetricsState and returns its pointer.
func NewMetricsState() *MetricsState {
return &MetricsState{
ActiveModels: make(map[string]int),
WaitingModels: make(map[string]int),
Expand Down
2 changes: 1 addition & 1 deletion pkg/epp/backend/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.
logger: log.FromContext(parentCtx).WithValues("pod", pod.NamespacedName),
}
pm.pod.Store(pod)
pm.metrics.Store(newMetricsState())
pm.metrics.Store(NewMetricsState())

pm.startRefreshLoop(parentCtx)
return pm
Expand Down
6 changes: 3 additions & 3 deletions pkg/epp/handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type RequestContext struct {

type Request struct {
Headers map[string]string
Body map[string]interface{}
Body map[string]any
Metadata map[string]any
}
type Response struct {
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *StreamingServer) Process(srv extProcPb.ExternalProcessor_ProcessServer)
RequestState: RequestReceived,
Request: &Request{
Headers: make(map[string]string),
Body: make(map[string]interface{}),
Body: make(map[string]any),
Metadata: make(map[string]any),
},
Response: &Response{
Expand All @@ -152,7 +152,7 @@ func (s *StreamingServer) Process(srv extProcPb.ExternalProcessor_ProcessServer)
}

var body []byte
var responseBody map[string]interface{}
var responseBody map[string]any

// Create error handling var as each request should only report once for
// error metrics. This doesn't cover the error "Cannot receive stream request" because
Expand Down
68 changes: 57 additions & 11 deletions pkg/epp/requestcontrol/director.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Comment on lines -139 to -141
Copy link
Contributor Author

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

candidatePods := schedulingtypes.ToSchedulerPodMetrics(d.datastore.PodGetAll())
// --- 3. Call Scheduler (with the relevant candidate pods) ---
candidatePods, err := d.getCandidatePodsForScheduling(reqCtx.Request.Metadata)
if err != nil {
return reqCtx, errutil.Error{Code: errutil.BadRequest, Msg: fmt.Errorf("failed to find candidate pods: %w", err).Error()}
Copy link
Contributor

Choose a reason for hiding this comment

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

If the list was empty, it may well mean the proxy can't see any ready pods, so it is not a BadRequest error in this case. I recommend getCandidatePodsForScheduling to not return an error when the metadata exists but empty; and here we check the length of candidate pods, if zero we return a 503 (service unavailable).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

}
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()}
Expand Down Expand Up @@ -177,6 +184,45 @@ 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(requestMetadata map[string]any) ([]schedulingtypes.Pod, error) {
subsetMap, found := requestMetadata[subsetHintNamespace].(map[string]any)
if !found {
return schedulingtypes.ToSchedulerPodMetrics(d.datastore.PodGetAll()), nil
}

// 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()), nil
} else if len(endpointSubsetList) == 0 {
return nil, fmt.Errorf("'%s' metadata cannot be empty", subsetHintKey)
}

// 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
}

podFitleredList := d.datastore.PodList(func(pm backendmetrics.PodMetrics) bool {
if _, found := endpoints[pm.GetPod().Address]; found {
return true
}
return false
})
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


return schedulingtypes.ToSchedulerPodMetrics(podFitleredList), nil
}

// 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) {
Expand Down
145 changes: 144 additions & 1 deletion pkg/epp/requestcontrol/director_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -355,7 +356,7 @@ func TestDirector_HandleRequest(t *testing.T) {
reqCtx := &handlers.RequestContext{
Request: &handlers.Request{
// Create a copy of the map for each test run to avoid mutation issues.
Body: make(map[string]interface{}),
Body: make(map[string]any),
Headers: map[string]string{
requtil.RequestIdHeaderKey: "test-req-id-" + test.name, // Ensure a default request ID
},
Expand Down Expand Up @@ -396,6 +397,148 @@ func TestDirector_HandleRequest(t *testing.T) {
}
}

// TestGetCandidatePodsForScheduling is testing getCandidatePodsForScheduling and more specifically the functionality of SubsetFilter.
func TestGetCandidatePodsForScheduling(t *testing.T) {
var makeFilterMetadata = func(data []any) map[string]any {
return map[string]any{
"envoy.lb.subset_hint": map[string]any{
"x-gateway-destination-endpoint-subset": data,
},
}
}

testInput := []*corev1.Pod{
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
},
Status: corev1.PodStatus{
PodIP: "10.0.0.1",
},
},
&corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod2",
},
Status: corev1.PodStatus{
PodIP: "10.0.0.2",
},
},
}

outputPod1 := &backend.Pod{
NamespacedName: types.NamespacedName{Name: "pod1"},
Address: "10.0.0.1",
Labels: map[string]string{},
}

outputPod2 := &backend.Pod{
NamespacedName: types.NamespacedName{Name: "pod2"},
Address: "10.0.0.2",
Labels: map[string]string{},
}

tests := []struct {
name string
metadata map[string]any
output []schedulingtypes.Pod
wantErr bool
}{
{
name: "SubsetFilter, filter not present — return all pods",
metadata: map[string]any{},
output: []schedulingtypes.Pod{
&schedulingtypes.PodMetrics{
Pod: outputPod1,
MetricsState: backendmetrics.NewMetricsState(),
},
&schedulingtypes.PodMetrics{
Pod: outputPod2,
MetricsState: backendmetrics.NewMetricsState(),
},
},
},
{
name: "SubsetFilter, namespace present filter not present — return all pods",
metadata: map[string]any{"envoy.lb.subset_hint": map[string]any{}},
output: []schedulingtypes.Pod{
&schedulingtypes.PodMetrics{
Pod: outputPod1,
MetricsState: backendmetrics.NewMetricsState(),
},
&schedulingtypes.PodMetrics{
Pod: outputPod2,
MetricsState: backendmetrics.NewMetricsState(),
},
},
},
{
name: "SubsetFilter, filter present with empty list — return error",
metadata: makeFilterMetadata([]any{}),
output: nil,
wantErr: true,
},
{
name: "SubsetFilter, subset with one matching pod",
metadata: makeFilterMetadata([]any{"10.0.0.1"}),
output: []schedulingtypes.Pod{
&schedulingtypes.PodMetrics{
Pod: outputPod1,
MetricsState: backendmetrics.NewMetricsState(),
},
},
},
{
name: "SubsetFilter, subset with multiple matching pods",
metadata: makeFilterMetadata([]any{"10.0.0.1", "10.0.0.2", "10.0.0.3"}),
output: []schedulingtypes.Pod{
&schedulingtypes.PodMetrics{
Pod: outputPod1,
MetricsState: backendmetrics.NewMetricsState(),
},
&schedulingtypes.PodMetrics{
Pod: outputPod2,
MetricsState: backendmetrics.NewMetricsState(),
},
},
},
{
name: "SubsetFilter, subset with no matching pods",
metadata: makeFilterMetadata([]any{"10.0.0.3"}),
output: []schedulingtypes.Pod{},
},
}

pmf := backendmetrics.NewPodMetricsFactory(&backendmetrics.FakePodMetricsClient{}, time.Second)
ds := datastore.NewDatastore(t.Context(), pmf)
for _, testPod := range testInput {
ds.PodUpdateOrAddIfNotExist(testPod)
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
director := NewDirectorWithConfig(ds, &mockScheduler{}, &mockSaturationDetector{}, NewConfig())

got, err := director.getCandidatePodsForScheduling(test.metadata)

if test.wantErr && err == nil {
t.Fatalf("expected an error, but didn't receive")
}

if err != nil && !test.wantErr {
t.Fatalf("Unexpected error, got %v", err)
}

diff := cmp.Diff(test.output, got, cmpopts.SortSlices(func(a, b schedulingtypes.Pod) bool {
return a.GetPod().NamespacedName.String() < b.GetPod().NamespacedName.String()
}))
if diff != "" {
t.Errorf("Unexpected output (-want +got): %v", diff)
}
})
}
}

func TestRandomWeightedDraw(t *testing.T) {
logger := logutil.NewTestLogger()
// Note: These tests verify deterministic outcomes for a fixed seed (420).
Expand Down
Loading