|
| 1 | +/* |
| 2 | +Copyright 2026 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 requestcontrol |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + errcommon "github.com/llm-d/llm-d-router/pkg/common/error" |
| 29 | + logutil "github.com/llm-d/llm-d-router/pkg/common/observability/logging" |
| 30 | + "github.com/llm-d/llm-d-router/pkg/epp/flowcontrol/contracts/mocks" |
| 31 | + fccontroller "github.com/llm-d/llm-d-router/pkg/epp/flowcontrol/controller" |
| 32 | + fcregistry "github.com/llm-d/llm-d-router/pkg/epp/flowcontrol/registry" |
| 33 | + fwkdl "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/datalayer" |
| 34 | + "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/flowcontrol" |
| 35 | + fwksched "github.com/llm-d/llm-d-router/pkg/epp/framework/interface/scheduling" |
| 36 | + "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/fairness/globalstrict" |
| 37 | + "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/ordering/fcfs" |
| 38 | + "github.com/llm-d/llm-d-router/pkg/epp/framework/plugins/flowcontrol/usagelimits" |
| 39 | + "github.com/llm-d/llm-d-router/pkg/epp/handlers" |
| 40 | + testutils "github.com/llm-d/llm-d-router/test/utils" |
| 41 | +) |
| 42 | + |
| 43 | +// This file covers the admission-to-controller seam with a REAL FlowController and FlowRegistry |
| 44 | +// behind FlowControlAdmissionController, complementing admission_test.go (which mocks the flow |
| 45 | +// controller) and the flowcontrol integration suite (which enters at EnqueueAndWait). It asserts |
| 46 | +// the externally visible contract of Admit for every terminal flow control outcome: the |
| 47 | +// errcommon.Error code and the x-llm-d-request-dropped-reason header. |
| 48 | + |
| 49 | +// realFlowControlHarness wires a FlowControlAdmissionController to a real FlowController and |
| 50 | +// FlowRegistry, following the construction pattern of the flowcontrol integration suite |
| 51 | +// (pkg/epp/flowcontrol/integration_helpers_test.go). |
| 52 | +type realFlowControlHarness struct { |
| 53 | + cancel context.CancelFunc |
| 54 | + ac *FlowControlAdmissionController |
| 55 | + reg *fcregistry.FlowRegistry |
| 56 | +} |
| 57 | + |
| 58 | +// realFlowControlOpts selects the knobs each outcome needs: a fixed saturation level (1.0 keeps |
| 59 | +// requests queued), the candidate pool (empty vs. non-empty steers capacity-rejection |
| 60 | +// classification), the controller-default request TTL, and an optional per-band request cap. |
| 61 | +type realFlowControlOpts struct { |
| 62 | + saturation float64 |
| 63 | + candidates []fwkdl.Endpoint |
| 64 | + requestTTL time.Duration // Defaults to one minute (effectively "never" for these tests). |
| 65 | + bandMaxRequests uint64 // Zero means unbounded. |
| 66 | +} |
| 67 | + |
| 68 | +func newRealFlowControlHarness(t *testing.T, opts realFlowControlOpts) *realFlowControlHarness { |
| 69 | + t.Helper() |
| 70 | + ctx, cancel := context.WithCancel(context.Background()) |
| 71 | + t.Cleanup(cancel) |
| 72 | + |
| 73 | + handle := testutils.NewTestHandle(ctx) |
| 74 | + orderingPlugin, err := fcfs.FCFSOrderingPolicyFactory("fcfs", nil, handle) |
| 75 | + require.NoError(t, err) |
| 76 | + fairnessPlugin, err := globalstrict.GlobalStrictFairnessPolicyFactory("global-strict", nil, handle) |
| 77 | + require.NoError(t, err) |
| 78 | + defaults := fcregistry.PriorityBandPolicyDefaults{ |
| 79 | + OrderingPolicy: orderingPlugin.(flowcontrol.OrderingPolicy), |
| 80 | + FairnessPolicy: fairnessPlugin.(flowcontrol.FairnessPolicy), |
| 81 | + } |
| 82 | + |
| 83 | + bandOpts := []fcregistry.PriorityBandConfigOption{fcregistry.WithBandMaxBytes(10_000_000)} |
| 84 | + if opts.bandMaxRequests > 0 { |
| 85 | + bandOpts = append(bandOpts, fcregistry.WithBandMaxRequests(opts.bandMaxRequests)) |
| 86 | + } |
| 87 | + band, err := fcregistry.NewPriorityBandConfig(0, defaults, bandOpts...) |
| 88 | + require.NoError(t, err) |
| 89 | + regCfg, err := fcregistry.NewConfig(defaults, fcregistry.WithPriorityBand(band)) |
| 90 | + require.NoError(t, err) |
| 91 | + reg := fcregistry.NewFlowRegistry(regCfg, logr.Discard()) |
| 92 | + go reg.RunMaintenanceLoop(ctx) |
| 93 | + |
| 94 | + requestTTL := opts.requestTTL |
| 95 | + if requestTTL == 0 { |
| 96 | + requestTTL = time.Minute |
| 97 | + } |
| 98 | + detector := &mockSaturationDetector{ |
| 99 | + SaturationFunc: func(context.Context, []fwkdl.Endpoint) float64 { return opts.saturation }, |
| 100 | + } |
| 101 | + fc := fccontroller.NewFlowController(ctx, "test-pool", &fccontroller.Config{ |
| 102 | + DefaultRequestTTL: requestTTL, |
| 103 | + ExpiryCleanupInterval: 10 * time.Millisecond, |
| 104 | + EnqueueChannelBufferSize: 100, |
| 105 | + }, fccontroller.Deps{ |
| 106 | + Registry: reg, |
| 107 | + SaturationDetector: detector, |
| 108 | + EndpointCandidates: &mocks.MockEndpointCandidates{Candidates: opts.candidates}, |
| 109 | + UsageLimitPolicy: usagelimits.DefaultPolicy(), |
| 110 | + }) |
| 111 | + |
| 112 | + return &realFlowControlHarness{ |
| 113 | + cancel: cancel, |
| 114 | + ac: NewFlowControlAdmissionController(fc, "test-pool"), |
| 115 | + reg: reg, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// newSeamRequestContext builds the minimal RequestContext the admission controller adapts into a |
| 120 | +// FlowControlRequest. FairnessID must be non-empty: the registry rejects empty flow IDs. |
| 121 | +func newSeamRequestContext(id string) *handlers.RequestContext { |
| 122 | + return &handlers.RequestContext{ |
| 123 | + SchedulingRequest: &fwksched.InferenceRequest{RequestID: id, FairnessID: "seam-flow"}, |
| 124 | + Request: &handlers.Request{Metadata: map[string]any{}}, |
| 125 | + RequestSize: 100, |
| 126 | + RequestReceivedTimestamp: time.Now(), |
| 127 | + IncomingModelName: "test-model", |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// admitAsync runs Admit in a goroutine so the test can drive queue state (e.g. cancel a context |
| 132 | +// or submit an overflow request) while the call blocks inside EnqueueAndWait. |
| 133 | +func admitAsync(ctx context.Context, ac *FlowControlAdmissionController, id string) <-chan error { |
| 134 | + result := make(chan error, 1) |
| 135 | + go func() { result <- ac.Admit(ctx, newSeamRequestContext(id), 0) }() |
| 136 | + return result |
| 137 | +} |
| 138 | + |
| 139 | +// waitAdmit unblocks a pending admitAsync result. The generous timeout mirrors the flowcontrol |
| 140 | +// integration suite; every path under test finalizes deterministically well before it. |
| 141 | +func waitAdmit(t *testing.T, result <-chan error) error { |
| 142 | + t.Helper() |
| 143 | + select { |
| 144 | + case err := <-result: |
| 145 | + return err |
| 146 | + case <-time.After(10 * time.Second): |
| 147 | + t.Fatal("Admit did not return a terminal outcome within 10s") |
| 148 | + return nil |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// requireDropped asserts the errcommon.Error contract for a dropped request: the canonical error |
| 153 | +// code (which drives the HTTP status) and the dropped-reason response header. |
| 154 | +func requireDropped(t *testing.T, err error, wantCode string, wantReason errcommon.RequestDroppedReason) { |
| 155 | + t.Helper() |
| 156 | + require.Error(t, err) |
| 157 | + var e errcommon.Error |
| 158 | + require.ErrorAs(t, err, &e, "error should be of type errcommon.Error") |
| 159 | + assert.Equal(t, wantCode, e.Code, "incorrect error code") |
| 160 | + assert.Equal(t, string(wantReason), e.Headers[errcommon.RequestDroppedReasonHeaderKey], |
| 161 | + "incorrect dropped-reason header") |
| 162 | +} |
| 163 | + |
| 164 | +func nonEmptyEndpoints() []fwkdl.Endpoint { |
| 165 | + return []fwkdl.Endpoint{fwkdl.NewEndpoint(nil, nil)} |
| 166 | +} |
| 167 | + |
| 168 | +// TestFlowControlAdmissionController_RealControllerSeam drives every terminal flow control |
| 169 | +// outcome through the real controller stack and asserts the status code + header contract that |
| 170 | +// admission_test.go only covers against a mock. |
| 171 | +func TestFlowControlAdmissionController_RealControllerSeam(t *testing.T) { |
| 172 | + t.Parallel() |
| 173 | + ctx := logutil.NewTestLoggerIntoContext(context.Background()) |
| 174 | + |
| 175 | + t.Run("dispatched_returns_nil", func(t *testing.T) { |
| 176 | + t.Parallel() |
| 177 | + // Unsaturated detector + non-empty pool: the dispatch cycle admits immediately. |
| 178 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 179 | + saturation: 0.0, |
| 180 | + candidates: nonEmptyEndpoints(), |
| 181 | + }) |
| 182 | + |
| 183 | + err := waitAdmit(t, admitAsync(ctx, h.ac, "dispatch-req")) |
| 184 | + require.NoError(t, err, "an unsaturated system should admit the request") |
| 185 | + }) |
| 186 | + |
| 187 | + t.Run("rejected_capacity_returns_429_saturated", func(t *testing.T) { |
| 188 | + t.Parallel() |
| 189 | + // A saturated detector parks a filler request in the only queue slot (bandMaxRequests=1); |
| 190 | + // the next request overflows band capacity against a non-empty pool -> RejectedCapacity. |
| 191 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 192 | + saturation: 1.0, |
| 193 | + candidates: nonEmptyEndpoints(), |
| 194 | + bandMaxRequests: 1, |
| 195 | + }) |
| 196 | + |
| 197 | + filler := admitAsync(ctx, h.ac, "filler-req") |
| 198 | + require.Eventually(t, func() bool { return h.reg.Stats().TotalLen == 1 }, |
| 199 | + time.Second, time.Millisecond, "filler request should be queued before the overflow request") |
| 200 | + |
| 201 | + err := waitAdmit(t, admitAsync(ctx, h.ac, "overflow-req")) |
| 202 | + requireDropped(t, err, errcommon.ResourceExhausted, errcommon.RequestDroppedReasonSaturated) |
| 203 | + |
| 204 | + // Shut the controller down and drain the filler so the goroutine ends inside the test. |
| 205 | + h.cancel() |
| 206 | + require.Error(t, waitAdmit(t, filler), "queued filler should be evicted on shutdown") |
| 207 | + }) |
| 208 | + |
| 209 | + t.Run("rejected_no_endpoints_returns_503_no_endpoints", func(t *testing.T) { |
| 210 | + t.Parallel() |
| 211 | + // Same capacity overflow, but against an EMPTY candidate pool the processor classifies |
| 212 | + // the rejection as RejectedNoEndpoints (genuine unavailability, e.g. scale-from-zero). |
| 213 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 214 | + saturation: 1.0, |
| 215 | + candidates: nil, |
| 216 | + bandMaxRequests: 1, |
| 217 | + }) |
| 218 | + |
| 219 | + filler := admitAsync(ctx, h.ac, "filler-req") |
| 220 | + require.Eventually(t, func() bool { return h.reg.Stats().TotalLen == 1 }, |
| 221 | + time.Second, time.Millisecond, "filler request should be queued before the overflow request") |
| 222 | + |
| 223 | + err := waitAdmit(t, admitAsync(ctx, h.ac, "overflow-req")) |
| 224 | + requireDropped(t, err, errcommon.ServiceUnavailable, errcommon.RequestDroppedReasonNoEndpoints) |
| 225 | + |
| 226 | + h.cancel() |
| 227 | + require.Error(t, waitAdmit(t, filler), "queued filler should be evicted on shutdown") |
| 228 | + }) |
| 229 | + |
| 230 | + t.Run("evicted_ttl_returns_503_ttl_expired", func(t *testing.T) { |
| 231 | + t.Parallel() |
| 232 | + // The admission adapter always defers to the controller-default TTL |
| 233 | + // (flowControlRequest.InitialEffectiveTTL() == 0), so a short DefaultRequestTTL plus a |
| 234 | + // saturated detector forces a TTL eviction of the queued request. |
| 235 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 236 | + saturation: 1.0, |
| 237 | + candidates: nonEmptyEndpoints(), |
| 238 | + requestTTL: 100 * time.Millisecond, |
| 239 | + }) |
| 240 | + |
| 241 | + err := waitAdmit(t, admitAsync(ctx, h.ac, "ttl-req")) |
| 242 | + requireDropped(t, err, errcommon.ServiceUnavailable, errcommon.RequestDroppedReasonTTLExpired) |
| 243 | + }) |
| 244 | + |
| 245 | + t.Run("evicted_context_cancelled_returns_503_context_cancelled", func(t *testing.T) { |
| 246 | + t.Parallel() |
| 247 | + // Cancelling the caller's context while the request sits in the queue (saturated |
| 248 | + // detector, long TTL) simulates a client disconnect. |
| 249 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 250 | + saturation: 1.0, |
| 251 | + candidates: nonEmptyEndpoints(), |
| 252 | + }) |
| 253 | + |
| 254 | + admitCtx, admitCancel := context.WithCancel(ctx) |
| 255 | + defer admitCancel() |
| 256 | + result := admitAsync(admitCtx, h.ac, "cancel-req") |
| 257 | + require.Eventually(t, func() bool { return h.reg.Stats().TotalLen == 1 }, |
| 258 | + time.Second, time.Millisecond, "request should be queued before cancelling") |
| 259 | + admitCancel() |
| 260 | + |
| 261 | + err := waitAdmit(t, result) |
| 262 | + requireDropped(t, err, errcommon.ServiceUnavailable, errcommon.RequestDroppedReasonContextCancelled) |
| 263 | + }) |
| 264 | + |
| 265 | + t.Run("shutdown_returns_503_shutting_down", func(t *testing.T) { |
| 266 | + t.Parallel() |
| 267 | + // A request admitted after the controller's context is cancelled is rejected with |
| 268 | + // ErrFlowControllerNotRunning, which maps to 503 + the shutting-down reason. |
| 269 | + h := newRealFlowControlHarness(t, realFlowControlOpts{ |
| 270 | + saturation: 0.0, |
| 271 | + candidates: nonEmptyEndpoints(), |
| 272 | + }) |
| 273 | + h.cancel() |
| 274 | + |
| 275 | + err := waitAdmit(t, admitAsync(ctx, h.ac, "shutdown-req")) |
| 276 | + requireDropped(t, err, errcommon.ServiceUnavailable, errcommon.RequestDroppedReasonShuttingDown) |
| 277 | + }) |
| 278 | +} |
0 commit comments