Skip to content

[Bug] Reflect KubernetesJob worker counts and usage on finalize - #2404

Merged
zhangjyr merged 2 commits into
vllm-project:mainfrom
V-3604:varshith/bug_fix_self_hosted_finalize_stats
Jul 6, 2026
Merged

zhangjyr merged 2 commits into
vllm-project:mainfrom
V-3604:varshith/bug_fix_self_hosted_finalize_stats

Conversation

@V-3604

@V-3604 V-3604 commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request Description

In KubernetesJob (self-hosting) mode the worker pod ran its BaseJobDriver without worker_mode, so _should_finalize() was True and the worker ran finalize_job. Since #2360 that calls finalize_job_output_data unconditionally, which aggregates the per-request batch:<job_id>:done/* markers, completes the multipart upload, and consumes the markers, all inside the Job pod. _await_self_hosted_run on the metadata service then only flipped the job to FINALIZING with its stale in-memory copy; when it aggregated it found the markers already consumed, recomputed completed/failed as 0 and usage as null, and committed an empty multipart over the worker's output. A completed KubernetesJob batch therefore persisted completed = 0 and usage = null while the correct records had been assembled and then overwritten.

The worker's BaseJobDriver is now built with worker_mode=True, so it dispatches requests and writes its output parts and per-request done markers but does not finalize. SingleJobRunner.mark_job_done is pod-local, so this only affects the worker's own status. _await_self_hosted_run now calls finalize_job on the metadata service, which aggregates from the surviving markers, assembles the output/error files, and records the real request_counts / usage before marking the job done. The Deployment path keeps its endpoint and aggregates in the metadata process, so it is unchanged.

test_self_hosted_run_finalizes_on_success asserts the self-hosted run aggregates through finalize_job; it fails on the current code, where the run only sets FINALIZING, and passes with this change. test_run_constructs_driver_in_worker_mode asserts the worker builds its driver with worker_mode=True.

pytest tests/batch/test_k8s_job_driver.py::test_self_hosted_run_finalizes_on_success tests/batch/test_worker.py::test_run_constructs_driver_in_worker_mode -v
tests/batch/test_k8s_job_driver.py::test_self_hosted_run_finalizes_on_success PASSED
tests/batch/test_worker.py::test_run_constructs_driver_in_worker_mode PASSED
2 passed

Both affected files pass with no regressions (9 passed).

Related Issues

Resolves #2263

Important: Before submitting, please complete the description above and review the checklist below.


Contribution Guidelines (Expand for Details)

We appreciate your contribution to aibrix! To ensure a smooth review process and maintain high code quality, please adhere to the following guidelines:

Pull Request Title Format

Your PR title should start with one of these prefixes to indicate the nature of the change:

  • [Bug]: Corrections to existing functionality
  • [CI]: Changes to build process or CI pipeline
  • [Docs]: Updates or additions to documentation
  • [API]: Modifications to aibrix's API or interface
  • [CLI]: Changes or additions to the Command Line Interface
  • [Misc]: For changes not covered above (use sparingly)

Note: For changes spanning multiple categories, use multiple prefixes in order of importance.

Submission Checklist

  • PR title includes appropriate prefix(es)
  • Changes are clearly explained in the PR description
  • New and existing tests pass successfully
  • Code adheres to project style and best practices
  • Documentation updated to reflect changes (if applicable)
  • Thorough testing completed, no regressions introduced

By submitting this PR, you confirm that you've read these guidelines and your changes align with the project's contribution standards.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to reflect a self-hosted worker's authoritative output statistics (completed/failed counts and token usage) onto the batch job status before finalizing, avoiding overwriting the worker's output. Feedback suggests optimizing reflect_job_output_stats to count records and sum token usage in a single pass over the output file to avoid reading the file twice and reduce I/O latency.

@Jeffwan
Jeffwan requested a review from zhangjyr June 30, 2026 21:00
@Jeffwan

Jeffwan commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

@zhangjyr please help review this change

@zhangjyr zhangjyr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @V-3604, thank you for your work on fixing k8s job finalization.

Although we are currently focused more on k8s deployment support, the k8s job path has drifted a bit from the intended design, especially after the recent state-fixing patch.

At a high level, _await_self_hosted_run() is the k8s job equivalent of execute_worker(job_id). Even though it is simplified, the expected flow should still be:

  1. Wait for runtime completion.
  2. Finalize the job.

The current _await_self_hosted_run() implementation deviates from this design because it sets the finalizing state directly, instead of calling finalize_job() and letting _progress_manager.mark_job_finalizing() handle the state transition.

I would suggest calling finalize_job() directly inside _await_self_hosted_run() rather than reimplementing the finalization logic there.

For testing, python/aibrix/tests/batch/test_e2e_openai_batch_api.py::test_openai_batch_api_success_workflow can be used for end-to-end validation by specifying the test backend with the -k k8s_job keyword. It looks like the k8s_job keyword was accidentally removed in the current version. Could you please add it back?

@V-3604

V-3604 commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@zhangjyr Hi, I rebased on main. #2360 removed the _aggregate_on_finalize gate, so finalize_job() now calls finalize_job_output_data() unconditionally. For a KubernetesJob the worker assembles the output and consumes the per-request metastore markers itself, so on the self-hosted path finalize_job_output_data() recomputes completed/failed as 0 and re-commits an empty multipart upload, overwriting the worker output. The reflected counts also don't survive, since mark_job_finalizing() reloads the job from the entity store.
So switching _await_self_hosted_run() to finalize_job() alone still hits the unconditional aggregation. Do you want the self-hosted path to keep a skip-aggregation gate in finalize_job(), or should finalize_job_output_data() recover counts from the assembled output/error files for the KubernetesJob case instead of the consumed markers? I'll implement whichever you prefer and wire the k8s_job E2E backend to validate it.

@zhangjyr

zhangjyr commented Jul 2, 2026 •

Copy link
Copy Markdown
Collaborator

@V-3604 After reviewing the existing code, I found there is a fundamental misunderstanding of the existing worker code.

Worker: complete with other workers to win requests and process them; no finalization should be made on the worker.

driver = BaseJobDriver(
    runner,
    ExternalRuntime(GatewayEndpointSource(self.llm_engine_base_url)),
    worker_mode = True, # Missing part.
)

MDS controller: wait for the k8s_job runtime to complete and finalize.

With this clarified, I think the worker will not consume metastore markers, and the existing finalize_job should work properly.

@V-3604

V-3604 commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Confirmed, thanks. The worker should win and process requests only, not finalize. worker.py builds the worker BaseJobDriver without worker_mode=True, so _should_finalize() returns True and the worker runs finalize_job, consuming the shared batch:<job_id>:done/ markers the MDS controller later reads. Setting worker_mode=True keeps the worker to dispatch plus writing its parts and markers, and the controller's existing finalize_job aggregates from the surviving markers.

I'll set worker_mode=True in worker.py, switch _await_self_hosted_run to call finalize_job() per your earlier review instead of setting FINALIZING inline, and drop the reflect_job_output_stats path. On the E2E, -k k8s_job is more than a keyword: #2360 moved backend selection to the E2E_BACKENDS registry in conftest.py, which has no k8s_job entry or fake KubernetesJob runtime, so nothing selects that backend. I'll cover this with driver and worker unit tests and re-add the k8s_job E2E as a follow-up, unless you'd prefer it in this PR.

@V-3604
V-3604 force-pushed the varshith/bug_fix_self_hosted_finalize_stats branch from 2456b28 to 989b3bf Compare July 2, 2026 10:30
@V-3604

V-3604 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

Pushed the rework as a single commit over the two earlier ones: worker_mode=True in worker.py, _await_self_hosted_run now calls finalize_job() instead of setting FINALIZING inline, and the reflect_job_output_stats path is gone. Covered by a driver-level test that finalize runs on the coordinator and a worker test that the driver is built in worker_mode.

One open question: the k8s_job E2E needs a new E2E_BACKENDS entry and a fake KubernetesJob runtime (the deployment fake dispatches from the controller, so it doesn't cover the self-hosted path). Happy to add it as a follow-up, or fold it into this PR if you'd rather have it here.

@zhangjyr

zhangjyr commented Jul 2, 2026 •

Copy link
Copy Markdown
Collaborator

@V-3604 Thanks. Yes, I would prefer that k8s_job be added back to E2E_BACKENDS of E2E tests. In fact, the E2E series tests support real k8s jobs. The naming conventions of backends are like:

  1. "local_job_using_[xxxx]": these tests use local storage and metastore, and would rely on a fake runtime to work.
  2. "job_using_[k8s_job]": should use s3/tos as storage and redis as metastore, and can be used to test a real k8s job locally.

#2415 provides an example of how to define job_using_k8s_job.

@V-3604

V-3604 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Added local_job_using_k8s_job to E2E_BACKENDS in the latest commit, following the local_job_using_[xxxx] convention with local storage and metastore behind a fake runtime. A FakeBatchV1Api backs the runtime, patched in over the real-cluster fallback since the slotted InfrastructureContext has no batch_v1_api field to inject through. The worker runs in-process in worker_mode as a stand-in for the Job's pod, so it writes the output parts and per-request done markers and leaves finalization to the coordinator, which aggregates the surviving markers in finalize_job. That is the self-hosted path the deployment backend does not reach, since the deployment fake dispatches from the control plane.

I kept the real job_using_k8s_job on TOS plus Redis out of this PR. It depends on the Redis-backed metastore wiring from #2415, and #2415 already notes the fake K8s Job backend should land first, so it belongs alongside that change.

Verified with pytest tests/batch/test_e2e_openai_batch_api.py::test_openai_batch_api_success_workflow -k local_job_using_k8s_job (1 passed).

@zhangjyr
zhangjyr merged commit d42e0e6 into vllm-project:main Jul 6, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

KubernetesJob batches finalize with wrong request_counts and missing usage (metadata doesn't reflect the worker's final state)

3 participants