Repository navigation
[TRTLLM-17262][refactor] Shared middleware package for the aggregated and disaggregated servers - #21447
[TRTLLM-17262][refactor] Shared middleware package for the aggregated and disaggregated servers#21447JunyiXu-nv wants to merge 2 commits into
Conversation
8971d38 to
a0aa0df
Compare
…eware package branch as the base for the shared app assembly Base for the shared app assembly: both open PRs it builds on, NVIDIA#21447 (shared middleware package for the aggregated and disaggregated servers) and NVIDIA#21448 (router skeleton and the ops.system endpoints), merged into one branch. Conflicts resolved by keeping both sides: the mypy file list in pyproject.toml and the import-direction contracts in tests/unittest/serve/test_layering.py. Signed-off-by: JunyiXu-nv <[email protected]>
Signed-off-by: JunyiXu-nv <[email protected]>
a0aa0df to
d1ed8ce
Compare
|
No actionable comments were generated in the recent review. WalkthroughThe change moves arrival-time and performance-metrics middleware into a dedicated package, centralizes built-in middleware registration for both serving apps, and relocates user middleware loading from the serve command. ChangesServing Middleware
Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Refactor Sequence Diagram(s)sequenceDiagram
participant openai_server
participant register_middlewares
participant FastAPI
openai_server->>register_middlewares: Pass app and middleware options
register_middlewares->>FastAPI: Add ServerArrivalTimeMiddleware
opt Performance metrics collection enabled
register_middlewares->>FastAPI: Add PerfMetricsMiddleware
end
Merge Risk:
|
|
Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements. |
|
/bot run |
|
PR_Github #86938 [ run ] triggered by Bot. Commit: |
|
PR_Github #86938 [ run ] completed with state
|
|
/bot run |
|
PR_Github #87162 [ run ] triggered by Bot. Commit: |
|
PR_Github #87162 [ run ] completed with state
|
|
/bot run |
|
PR_Github #87222 [ run ] triggered by Bot. Commit: |
|
PR_Github #87222 [ run ] completed with state
|
Signed-off-by: JunyiXu-nv <[email protected]>
|
/bot run |
1 similar comment
|
/bot run |
|
PR_Github #87367 [ run ] triggered by Bot. Commit: |
|
PR_Github #87367 [ run ] completed with state
|
Summary
Move the built-in arrival-time middleware, performance-metrics middleware and CLI middleware loader to
tensorrt_llm/serve/middleware/. Both server constructors use the same registration function, while retaining their distinct arguments. This is a single behavior-preserving refactor; it does not add request tracing or change how request state is initialized.Request-state behavior is unchanged. On HTTP requests
ServerArrivalTimeMiddlewarestill assigns a freshscope["state"]and then setsserver_arrival_time. It does not keep keys from an outer middleware or lifespan, and it does not mutate the previous dictionary. Non-HTTP scopes still pass through untouched. The three relocated definitions match their pre-move source text exactly.For contributors
serve.responses_utils.ServerArrivalTimeMiddleware→serve.middleware.arrival_time.ServerArrivalTimeMiddlewareserve.perf_metrics.PerfMetricsMiddleware→serve.middleware.perf_metrics.PerfMetricsMiddlewarecommands.serve._apply_fastapi_middlewares→serve.middleware.user_middleware._apply_fastapi_middlewaresPaths above are relative to
tensorrt_llm. Import or patch the moved definitions in their new modules; no compatibility re-exports are added. The existing serving instructions point contributors toserve/middleware/register.py; no agent instruction files are added. Theunittest/serveCPU entry already collects the new tests, and the middleware package is included in the existing multi-GPU trigger table.Validation
At
d1ed8cefcdec, the same 160 CPU cases passed on both FastAPI 0.136.3 and 0.139.2 (158 core and two VisualGen route cases per version, no failures, setup errors or skips). Each run used the worktree's Python source and test factory with the official native build for its exact base,b963a7ab7565. An interrupted earlier run was not counted.Four characterization cases cover absent, empty and populated state and a real FastAPI outer-middleware/lifespan request. Substituting the discarded
setdefaultimplementation makes the three existing-state cases fail by assertion; the absent-state case still passes. Registration was also compared over 32 bounded input combinations; both deliberately introduced registration defects were detected.The relocation check and independent source comparison found three verbatim definitions and no undeclared differences. Normal hooks and strict checks on the new product modules passed. The advisory type check is not clean across all changed files: four unchanged-line diagnostics were retained in the explicit scoped hook runs. No GPU/model run or restricted socket-fixture coverage is claimed. The earlier graceful-shutdown cleanup discrepancy remains a separate, unwaived observation.
Before merging, reconcile intervening edits, including #20280 if it lands first, and complete normal owner review and required CI.
Complete relocation comparison
🤖 Generated with Claude Code
Dev Engineer Review
PerfMetricsMiddlewareleavestensorrt_llm/serve/perf_metrics.py,ServerArrivalTimeMiddlewareleavestensorrt_llm/serve/responses_utils.py, and_apply_fastapi_middlewaresleavestensorrt_llm/commands/serve.py. External importers of these names fail.register_middlewaresinmiddleware/register.pyinstallsServerArrivalTimeMiddlewarefirst andPerfMetricsMiddlewaresecond, only whencollect_perf_metricsis true. The last added middleware is outermost, so metrics wraps arrival time. The PR objectives describe the order as "metrics register before arrival-time middleware." Confirm the intended order against the code.ServerArrivalTimeMiddlewarereplacesscope["state"]with a new dictionary. Check this against existing consumers of scope state.ruff-legacy-baseline.jsonadds one D212 violation forarrival_time.py.L0_MergeRequest_PRrun (#72362) failed, though itsPR_Githubjob succeeded. Failure analysis is linked in the PR.jenkins/L0_MergeRequest.groovynow routestensorrt_llm/serve/middleware/changes to multi-GPU testing.#20280and owner review are pending.QA Engineer Review
Test files changed:
tests/unittest/serve/middleware/test_register.py(350 lines). It covers registration order and arguments for both app types, arrival-time state replacement, outermost user middleware, metrics records and headers, and observer behavior on success, failure, and liveness requests. Failing requests must not submit metrics or emit a server-timing header.tests/unittest/serve/middleware/__init__.py(license header only).test_disagg_openai_client.py,test_anthropic_routes.py,test_openai_input_metrics.py,test_request_metrics.py,test_config_database.py,test_middleware_order.py,test_serve_test_helpers.py.tests/unittest/serve/test_layering.pyforbids middleware from importingserve.api, the OpenAI server modules, orserve.responses_utils.Coverage:
Verdict: needs follow-up. Confirm the test-db listing. Resolve the graceful-shutdown discrepancy. Run the GPU-backed serving path before merge.
Per-File QA Perspective
tensorrt_llm/serve/middleware/register.py: Verify metrics-off and metrics-on paths, and clock and observer forwarding.tensorrt_llm/serve/middleware/arrival_time.py: Verifyserver_arrival_timeis set on HTTP requests and non-HTTP scopes pass through.tensorrt_llm/serve/middleware/perf_metrics.py: Verify metrics headers, SSE append, JSONL submission, and that/health_generateis excluded from the observer.tensorrt_llm/serve/middleware/user_middleware.py: Verify--middlewareaccepts only<module>.<object>and raisesValueErrorfor invalid objects.tensorrt_llm/serve/middleware/__init__.py: Header and docstring only.tensorrt_llm/serve/openai_server.py: Verify metrics-enabled and metrics-disabled registration paths.tensorrt_llm/serve/openai_disagg_server.py: Verify metrics stay disabled when not configured, and that clock and observer are not passed.tensorrt_llm/serve/perf_metrics.py: Class removed. Verify no remaining importers.tensorrt_llm/serve/responses_utils.py: Middleware removed. Verifyget_steady_clock_now_in_secondsremains importable.tensorrt_llm/commands/serve.py: Verifytrtllm-serve --middlewarestill loads user middleware.jenkins/L0_MergeRequest.groovy: Middleware changes now trigger multi-GPU testing. Expect longer CI for these changes..pre-commit-config.yaml,legacy-files.txt,pyproject.toml,ruff-legacy.toml,ruff-legacy-baseline.json: Lint and legacy-list updates for the moved files. Lint results are not established.tensorrt_llm/serve/AGENTS.md: Guidance namesregister.pyand states that middleware order must not change.