Skip to content

Commit 7dc0fa9

Browse files
authored
Merge branch 'main' into kaushikmitra/latency-predictor-encoder-cache-feature
2 parents cc5615e + 2121d7a commit 7dc0fa9

29 files changed

Lines changed: 2019 additions & 236 deletions

File tree

cmd/epp/runner/runner.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import (
8585
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/saturationdetector/utilization"
8686
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/usagelimits"
8787
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/usagelimits/priorityholdback"
88+
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/usagelimits/softreflectiveceiling"
8889
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/requestcontrol/admitter/latencyslo"
8990
"github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/requestcontrol/admitter/probabilisticadmitter"
9091
reqdataprodprefix "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/requestcontrol/dataproducer/approximateprefix"
@@ -610,6 +611,7 @@ func (r *Runner) registerInTreePlugins() {
610611
fwkplugin.Register(slodeadline.SLODeadlineOrderingPolicyType, slodeadline.SLODeadlineOrderingPolicyFactory)
611612
fwkplugin.Register(usagelimits.StaticUsageLimitPolicyType, usagelimits.StaticPolicyFactory)
612613
fwkplugin.Register(priorityholdback.PolicyType, priorityholdback.PolicyFactory)
614+
fwkplugin.Register(softreflectiveceiling.PolicyType, softreflectiveceiling.Factory)
613615

614616
// Register Request level data producer plugins as defaults for their respective data keys.
615617
fwkplugin.RegisterAsDefaultProducer(reqdataprodprefix.ApproxPrefixCachePluginType, reqdataprodprefix.ApproxPrefixCacheFactory, attrprefix.PrefixCacheMatchInfoDataKey)

cmd/epp/runner/test_runner.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package runner
1919
import (
2020
"context"
2121
"encoding/json"
22+
"net"
2223

2324
"k8s.io/client-go/rest"
2425
ctrl "sigs.k8s.io/controller-runtime"
@@ -33,8 +34,9 @@ import (
3334
// NewTestRunnerSetup creates a setup runner dedicated for integration tests. When mockDataSource is
3435
// non-nil, its plugin type is registered as a factory that returns the provided instance, so the
3536
// YAML config can reference it by type name and the runner wires it into the endpoint factory
36-
// automatically.
37-
func NewTestRunnerSetup(ctx context.Context, cfg *rest.Config, opts *runserver.Options, mockDataSource fwkdl.DataSource) (*Runner, ctrl.Manager, datastore.Datastore, error) {
37+
// automatically. When grpcListener is non-nil the ext_proc server serves on it and
38+
// opts.GRPCPort is ignored.
39+
func NewTestRunnerSetup(ctx context.Context, cfg *rest.Config, opts *runserver.Options, mockDataSource fwkdl.DataSource, grpcListener net.Listener) (*Runner, ctrl.Manager, datastore.Datastore, error) {
3840
runner := NewRunner()
3941

4042
if mockDataSource != nil {
@@ -51,13 +53,17 @@ func NewTestRunnerSetup(ctx context.Context, cfg *rest.Config, opts *runserver.O
5153
managerOverrides := []func(*ctrl.Options){
5254
func(o *ctrl.Options) {
5355
o.Controller.SkipNameValidation = &skipNameValidation
56+
// The kernel assigns the port, so the bind cannot lose a race to
57+
// another listener the way a port number chosen in advance can.
58+
o.Metrics.BindAddress = "127.0.0.1:0"
5459
},
5560
}
5661

5762
manager, ds, err := runner.setup(ctx, cfg, opts, managerOverrides)
5863
if err != nil {
5964
return runner, manager, ds, err
6065
}
66+
runner.serverRunner.GrpcListener = grpcListener
6167

6268
// Production runs the ext_proc and health servers on a context that outlives
6369
// the manager (see Runner.runWithGracefulShutdown). Integration tests drive

config/charts/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ helm install my-standalone-router ./config/charts/llm-d-router-standalone \
5252
--set router.proxy.mode=service \
5353
--set router.proxy.replicas=3
5454
```
55+
5556
---
5657

5758
### 2. Gateway Mode (`llm-d-router-gateway`)
@@ -513,17 +514,17 @@ httpRoute:
513514

514515
### Standalone Mode Configuration
515516

516-
Configures EPP to run with a proxy (Envoy proxy or Agentgateway proxy) that intercepts and routes client traffic directly to model servers (exclusive to `llm-d-router-standalone`). The proxy runs as a sidecar in the EPP pod by default, or as a separate horizontally scalable service when `router.proxy.mode=service` (Envoy only).
517+
Configures EPP to run with a proxy (Envoy proxy or Agentgateway proxy) that intercepts and routes client traffic directly to model servers (exclusive to `llm-d-router-standalone`). The proxy runs as a sidecar in the EPP pod by default, or as a separate horizontally scalable service when `router.proxy.mode=service`.
517518

518519
#### Proxy Sidecar Parameters
519520

520521
| **Parameter Name** | **Description** | **Default** |
521522
| :--- | :--- | :--- |
522523
| `router.proxy.enabled` | Enable the proxy (Envoy or Agentgateway) in front of EPP. | `false` |
523524
| `router.proxy.proxyType` | Type of proxy. Options: `[envoy, agentgateway]`. | `envoy` |
524-
| `router.proxy.mode` | Proxy deployment mode. `sidecar` runs the proxy in the EPP pod; `service` runs it as its own horizontally scalable Deployment and Service reaching EPP over the EPP Service (Envoy only). | `sidecar` |
525+
| `router.proxy.mode` | Proxy deployment mode. `sidecar` runs the proxy in the EPP pod; `service` runs it as its own horizontally scalable Deployment and Service reaching EPP over the EPP Service. | `sidecar` |
525526
| `router.proxy.replicas` | Replica count for the proxy Deployment when `mode=service`. | `2` |
526-
| `router.proxy.failOpen` | Whether the proxy passes traffic through (fail-open) when EPP is unreachable. | `true` |
527+
| `router.proxy.failOpen` | Whether the proxy passes traffic through (fail-open) when EPP is unreachable. Applies to `proxyType=envoy` only; Agentgateway exposes no fail-open setting and rejects requests (fails closed) when EPP is unreachable. | `true` |
527528
| `router.proxy.name` | Name of the sidecar container. | `""` |
528529
| `router.proxy.image` | Sidecar container image. | `""` |
529530
| `router.proxy.imagePullPolicy` | Sidecar container image pull policy. | `IfNotPresent` |

config/charts/llm-d-router-standalone/templates/_validations.tpl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ standalone validations
3333
{{- if not $proxy.enabled -}}
3434
{{- fail ".Values.router.proxy.enabled must be true when .Values.router.proxy.mode=service" -}}
3535
{{- end -}}
36-
{{- $proxyType := default "envoy" ($proxy.proxyType | default "envoy") | lower -}}
37-
{{- if ne $proxyType "envoy" -}}
38-
{{- fail (printf ".Values.router.proxy.mode=service currently supports only proxyType=envoy, got %q" $proxyType) -}}
39-
{{- end -}}
4036
{{- $hasHTTP := false -}}
4137
{{- range $servicePort := (.Values.router.extraServicePorts | default (list)) -}}
4238
{{- if eq (toString (index $servicePort "name")) "http" -}}
@@ -62,7 +58,7 @@ standalone validations
6258
{{- $listenerPort := include "llm-d-router.standaloneProxyListenerPort" . -}}
6359
{{- $flags := .Values.router.epp.flags | default dict -}}
6460
{{- if and (hasKey $flags "secure-serving") (ne (toString (index $flags "secure-serving")) "false") -}}
65-
{{- fail ".Values.router.epp.flags.secure-serving must be false when proxyType=agentgateway; standalone agentgateway uses plaintext gRPC to EPP over localhost" -}}
61+
{{- fail ".Values.router.epp.flags.secure-serving must be false when proxyType=agentgateway; standalone agentgateway uses plaintext gRPC to EPP (over loopback in sidecar mode, or the EPP Service in service mode)" -}}
6662
{{- end -}}
6763
{{- end -}}
6864
{{- end -}}

config/charts/routerlib/templates/_helpers.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ binds:
415415
policies:
416416
inferenceRouting:
417417
endpointPicker:
418-
host: {{ printf "127.0.0.1:%v" (.Values.router.epp.extProcPort | default 9002) | quote }}
418+
host: {{ printf "%s:%v" (include "llm-d-router.proxy.extProcHost" .) (.Values.router.epp.extProcPort | default 9002) | quote }}
419419
destinationMode: passthrough
420420
services:
421421
- name: {{ $serviceName | quote }}

config/charts/routerlib/templates/_proxy-deployment.yaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{{- define "llm-d-epp.proxy-deployment" -}}
22
{{- $proxy := include "llm-d-router.proxy" . | fromYaml | default dict -}}
3+
{{- $proxyType := include "llm-d-router.proxyType" . | trim -}}
4+
{{- $proxyConfigMap := index $proxy "configMap" | default dict -}}
5+
{{- $proxyConfigMapName := index $proxyConfigMap "name" | default "" -}}
36
{{- if and $proxy.enabled (eq (include "llm-d-router.proxyMode" .) "service") -}}
47
apiVersion: apps/v1
58
kind: Deployment
@@ -62,14 +65,31 @@ spec:
6265
resources:
6366
{{- toYaml . | nindent 12 }}
6467
{{- end }}
65-
{{- with $proxy.volumeMounts }}
68+
{{- if or (and (eq $proxyType "agentgateway") $proxyConfigMapName) $proxy.volumeMounts }}
6669
volumeMounts:
70+
{{- if and (eq $proxyType "agentgateway") $proxyConfigMapName }}
71+
- name: agentgateway-config-template
72+
mountPath: /config
73+
readOnly: true
74+
{{- end }}
75+
{{- with $proxy.volumeMounts }}
6776
{{- toYaml . | nindent 12 }}
77+
{{- end }}
6878
{{- end }}
69-
{{- with $proxy.volumes }}
79+
{{- if or (and (eq $proxyType "agentgateway") $proxyConfigMapName) $proxy.volumes }}
7080
volumes:
81+
{{- if and (eq $proxyType "agentgateway") $proxyConfigMapName }}
82+
- name: agentgateway-config-template
83+
configMap:
84+
name: {{ $proxyConfigMapName }}
85+
items:
86+
- key: config.yaml
87+
path: config.yaml
88+
{{- end }}
89+
{{- with $proxy.volumes }}
7190
{{- tpl (toYaml .) $ | nindent 8 }}
7291
{{- end }}
92+
{{- end }}
7393
---
7494
{{- end -}}
7595
{{- end -}}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# EPP configuration for the soft-reflective-ceiling usage limit policy.
2+
#
3+
# The policy applies a graduated, priority-aware dispatch ceiling: the
4+
# highest-priority band is never gated, and progressively lower-priority
5+
# bands are gated first as pool saturation rises. Gated items remain in
6+
# their queues; the ceiling controls dispatch, not admission.
7+
apiVersion: llm-d.ai/v1alpha1
8+
kind: EndpointPickerConfig
9+
featureGates:
10+
- flowControl
11+
plugins:
12+
- type: queue-scorer
13+
- type: kv-cache-utilization-scorer
14+
- type: prefix-cache-scorer
15+
- type: soft-reflective-ceiling-policy
16+
schedulingProfiles:
17+
- name: default
18+
plugins:
19+
- pluginRef: queue-scorer
20+
weight: 2.0
21+
- pluginRef: kv-cache-utilization-scorer
22+
weight: 2.0
23+
- pluginRef: prefix-cache-scorer
24+
weight: 3.0
25+
flowControl:
26+
usageLimitPolicyPluginRef: soft-reflective-ceiling-policy

hack/verify-helm.sh

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ test_cases_llm_d_router_standalone["latency-predictor"]="--set router.latencyPre
116116
test_cases_llm_d_router_standalone["llm-d-router-gateway"]="--set router.inferencePool.create=true --set router.modelServers.matchLabels.app=llm-instance-gateway"
117117
test_cases_llm_d_router_standalone["agentgateway"]="--set router.proxy.proxyType=agentgateway --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set 'router.modelServers.targetPorts[0].number=8000'"
118118
test_cases_llm_d_router_standalone["proxy-service"]="--set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set router.proxy.mode=service --set router.proxy.replicas=3"
119+
test_cases_llm_d_router_standalone["agentgateway-service"]="--set router.proxy.proxyType=agentgateway --set router.proxy.mode=service --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set 'router.modelServers.targetPorts[0].number=8000'"
119120
test_cases_llm_d_router_standalone["triton"]="--set router.modelServers.type=triton --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false"
120121

121122

@@ -190,13 +191,6 @@ if eval "${mismatched_agentgateway_listener_target_port_command}"; then
190191
exit 1
191192
fi
192193

193-
unsupported_proxy_service_agentgateway_command="${HELM} template ${SCRIPT_ROOT}/config/charts/llm-d-router-standalone --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set router.proxy.mode=service --set router.proxy.proxyType=agentgateway --set router.proxy.agentgateway.service.name=llm-instance-gateway --set 'router.proxy.agentgateway.service.ports[0]=8000' --set 'router.modelServers.targetPorts[0].number=8000' >/dev/null"
194-
echo "Executing: ${unsupported_proxy_service_agentgateway_command}"
195-
if eval "${unsupported_proxy_service_agentgateway_command}"; then
196-
echo "Helm template unexpectedly succeeded for proxy mode=service with proxyType=agentgateway"
197-
exit 1
198-
fi
199-
200194
invalid_failopen_command="${HELM} template ${SCRIPT_ROOT}/config/charts/llm-d-router-standalone --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set router.proxy.failOpen=notabool >/dev/null"
201195
echo "Executing: ${invalid_failopen_command}"
202196
if eval "${invalid_failopen_command}"; then
@@ -294,3 +288,33 @@ if ! grep -q -- 'llm-d-router-proxy: proxy-svc-proxy' "${proxy_service_render_ou
294288
echo "Proxy service mode did not render the dedicated proxy selector label"
295289
exit 1
296290
fi
291+
292+
echo "Verifying llm-d-router-standalone agentgateway in service mode reaches EPP over the Service..."
293+
agentgateway_service_mode_output="${TEMP_DIR}/llm-d-router-standalone-agentgateway-service-render.yaml"
294+
agentgateway_service_mode_command="${HELM} template ag-svc ${SCRIPT_ROOT}/config/charts/llm-d-router-standalone --namespace ag-ns --set router.proxy.proxyType=agentgateway --set router.proxy.mode=service --set router.modelServers.matchLabels.app=llm-instance-gateway --set router.inferencePool.create=false --set 'router.modelServers.targetPorts[0].number=8000' > ${agentgateway_service_mode_output}"
295+
echo "Executing: ${agentgateway_service_mode_command}"
296+
eval "${agentgateway_service_mode_command}"
297+
if ! grep -q -- 'name: ag-svc-proxy' "${agentgateway_service_mode_output}"; then
298+
echo "Agentgateway service mode did not render the separate proxy Deployment/Service named ag-svc-proxy"
299+
exit 1
300+
fi
301+
# The agentgateway container lives only in the standalone proxy Deployment, not the EPP pod.
302+
agentgateway_container_count=$(grep -c -- '- name: agentgateway-proxy$' "${agentgateway_service_mode_output}")
303+
if [ "${agentgateway_container_count}" -ne 1 ]; then
304+
echo "Agentgateway service mode rendered ${agentgateway_container_count} agentgateway-proxy containers, expected exactly 1 (in the proxy Deployment)"
305+
exit 1
306+
fi
307+
# endpointPicker must target the EPP Service FQDN, not loopback.
308+
if ! grep -q -- 'host: "ag-svc-epp.ag-ns.svc.cluster.local:9002"' "${agentgateway_service_mode_output}"; then
309+
echo "Agentgateway service mode did not point endpointPicker.host at the EPP Service FQDN"
310+
exit 1
311+
fi
312+
if grep -q -- 'host: "127.0.0.1:9002"' "${agentgateway_service_mode_output}"; then
313+
echo "Agentgateway service mode still points endpointPicker.host at loopback"
314+
exit 1
315+
fi
316+
# The agentgateway config must be mounted in the proxy Deployment.
317+
if ! grep -q -- 'agentgateway-config-template' "${agentgateway_service_mode_output}"; then
318+
echo "Agentgateway service mode did not mount the agentgateway config in the proxy Deployment"
319+
exit 1
320+
fi

internal/runnable/grpc.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"net"
2323

24+
"github.com/go-logr/logr"
2425
"google.golang.org/grpc"
2526
ctrl "sigs.k8s.io/controller-runtime"
2627
"sigs.k8s.io/controller-runtime/pkg/manager"
@@ -34,33 +35,48 @@ func GRPCServer(name string, srv *grpc.Server, port int) manager.Runnable {
3435
log := ctrl.Log.WithValues("name", name)
3536
log.Info("gRPC server starting")
3637

37-
// Start listening.
3838
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
3939
if err != nil {
4040
return fmt.Errorf("gRPC server failed to listen - %w", err)
4141
}
42+
log.Info("gRPC server listening", "port", lis.Addr().(*net.TCPAddr).Port)
43+
return serveGRPC(ctx, log, srv, lis)
44+
})
45+
}
4246

43-
log.Info("gRPC server listening", "port", port)
44-
45-
// Shutdown on context closed.
46-
// Terminate the server on context closed.
47-
// Make sure the goroutine does not leak.
48-
doneCh := make(chan struct{})
49-
defer close(doneCh)
50-
go func() {
51-
select {
52-
case <-ctx.Done():
53-
log.Info("gRPC server shutting down")
54-
srv.GracefulStop()
55-
case <-doneCh:
56-
}
57-
}()
47+
// GRPCServerOnListener converts the given gRPC server into a runnable serving on
48+
// an already-bound listener. Mirrors manager.Server's Listener field: reserving
49+
// the port in advance of the runnable starting removes the window in which
50+
// another process can take a port that was selected but not yet bound.
51+
// Takes ownership of the listener: grpc.Server.Serve closes it on return.
52+
func GRPCServerOnListener(name string, srv *grpc.Server, lis net.Listener) manager.Runnable {
53+
return manager.RunnableFunc(func(ctx context.Context) error {
54+
log := ctrl.Log.WithValues("name", name)
55+
log.Info("gRPC server starting")
56+
log.Info("gRPC server listening", "address", lis.Addr().String())
57+
return serveGRPC(ctx, log, srv, lis)
58+
})
59+
}
5860

59-
// Keep serving until terminated.
60-
if err := srv.Serve(lis); err != nil && err != grpc.ErrServerStopped {
61-
return fmt.Errorf("gRPC server failed - %w", err)
61+
func serveGRPC(ctx context.Context, log logr.Logger, srv *grpc.Server, lis net.Listener) error {
62+
// Shutdown on context closed.
63+
// Terminate the server on context closed.
64+
// Make sure the goroutine does not leak.
65+
doneCh := make(chan struct{})
66+
defer close(doneCh)
67+
go func() {
68+
select {
69+
case <-ctx.Done():
70+
log.Info("gRPC server shutting down")
71+
srv.GracefulStop()
72+
case <-doneCh:
6273
}
63-
log.Info("gRPC server terminated")
64-
return nil
65-
})
74+
}()
75+
76+
// Keep serving until terminated.
77+
if err := srv.Serve(lis); err != nil && err != grpc.ErrServerStopped {
78+
return fmt.Errorf("gRPC server failed - %w", err)
79+
}
80+
log.Info("gRPC server terminated")
81+
return nil
6682
}

pkg/epp/flowcontrol/integration_helpers_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,43 @@ type dispatchResult struct {
143143
err error
144144
}
145145

146+
// fillQueue enqueues n requests of byteSize bytes that stay queued (the caller supplies a
147+
// detector that blocks dispatch) and waits until settled reports the fill is observable in
148+
// registry stats. Sending overflow traffic only after the fill is observable is what makes a
149+
// subsequent capacity rejection deterministic; a fixed enqueue stagger would race with goroutine
150+
// scheduling under CI load. The returned release func cancels the queued requests' context,
151+
// drains their results, and asserts none was rejected for capacity.
152+
func (h *integrationHarness) fillQueue(
153+
t *testing.T, key flowcontrol.FlowKey, n int, byteSize uint64, settled func() bool,
154+
) (release func()) {
155+
t.Helper()
156+
queuedCtx, queuedCancel := context.WithCancel(h.ctx)
157+
t.Cleanup(queuedCancel)
158+
queued := make(chan dispatchResult, n)
159+
for i := 0; i < n; i++ {
160+
id := fmt.Sprintf("queued-req-%d", i)
161+
go func() {
162+
req := &testRequest{id: id, key: key, byteSize: byteSize, ttl: 5 * time.Minute}
163+
outcome, err := h.fc.EnqueueAndWait(queuedCtx, req)
164+
queued <- dispatchResult{id: id, outcome: outcome, err: err}
165+
}()
166+
}
167+
require.Eventually(t, settled, time.Second, time.Millisecond,
168+
"%d filling requests should be observably queued before proceeding", n)
169+
return func() {
170+
queuedCancel()
171+
for i := 0; i < n; i++ {
172+
select {
173+
case r := <-queued:
174+
require.NotEqual(t, fcTypes.QueueOutcomeRejectedCapacity, r.outcome,
175+
"queued request %s should have been admitted, not capacity-rejected", r.id)
176+
case <-time.After(5 * time.Second):
177+
t.Fatalf("timed out waiting for queued request %d after cancellation", i)
178+
}
179+
}
180+
}
181+
}
182+
146183
// --- Producer and Detector ---
147184

148185
type producerAndDetector struct {

0 commit comments

Comments
 (0)