Skip to content

fix(ess): skip COMPLETED and STOPPED work units in stop RPCs - #1692

Open
AntonTyb wants to merge 6 commits into
mainfrom
grain/65c50381/task-48-eka-batchstopworkunits-moves
Open

AntonTyb wants to merge 6 commits into
mainfrom
grain/65c50381/task-48-eka-batchstopworkunits-moves

Conversation

@AntonTyb

@AntonTyb AntonTyb commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Skip work units already in COMPLETED or STOPPED state (or lacking a current execution) in StopWorkUnit and BatchStopWorkUnits so stopping an experiment or a finished trial does not persist redundant STOP operations in Spanner or enqueue redundant DeleteAction tasks in Redis.

Motivation & Context

While UpdateWorkUnitExecutions row locks and ShouldUpdateWorkUnitExecutionState already reject state transitions out of STOPPED, COMPLETED, and FAILED, filtering COMPLETED and STOPPED work units up front avoids unnecessary operation records and actuator DeleteAction tasks while preserving:

  • FAILED cleanup: Startup failures (such as ImagePullBackOff / ErrImagePull) mark the execution FAILED while the Kueue-admitted JobSet is still alive and holding ClusterQueue quota and node capacity. StopWorkUnit and BatchStopWorkUnits continue to enqueue a DeleteAction for FAILED work units while ShouldUpdateWorkUnitExecutionState preserves their FAILED state in Spanner.
  • STOPPING retry recovery: Re-issuing StopWorkUnit or BatchStopWorkUnits on a STOPPING work unit continues to enqueue an idempotent DeleteAction to recover from lost/DLQ'd or out-of-order DeleteAction tasks.

Contract & Behavior Disclosures

  • Work units in COMPLETED or STOPPED are skipped; STOPPING and FAILED remain stoppable.
  • A work unit with no current execution (GetCurrentWorkUnitExecution() == nil) now returns &lropb.Operation{Done: true} instead of failing downstream execution-name validation with InvalidArgument.
  • When StopWorkUnit targets a skipped work unit (or BatchStopWorkUnits matches no stoppable work units), the RPC returns &lropb.Operation{Done: true} with an empty name and unset response/error rather than persisting a new STOP operation.

Key Changes

  • cmd/experiment_state_server/server/batchstopworkunits.go & stopworkunit.go:
    • Introduced stopSkippedStates = {STOPPED, COMPLETED}, isWorkUnitStoppable, and filterStoppableWorkUnits to skip COMPLETED and STOPPED work units (and work units with nil current execution) before enqueueWorkUnitStop.
    • Returned &lropb.Operation{Done: true} in StopWorkUnit when !isWorkUnitStoppable(workUnit) and in BatchStopWorkUnits when filterStoppableWorkUnits leaves no stoppable work units.
    • Logged skipped work units as "non-stoppable work unit" so work units without a current execution (WORK_UNIT_STATE_UNSPECIFIED) are not mislabeled as completed or stopped.
  • cmd/experiment_state_server/server/utils_test.go, batchstopworkunits_test.go, stopworkunit_test.go:
    • Extracted mustSetWorkUnitState helper in utils_test.go and refactored state-transition test setup.
    • Added table-driven tests (TestBatchStopWorkUnits_StateFiltering, TestStopWorkUnit_ByCurrentState, TestStopWorkUnit_SkippedStateDoesNotAddOperation, TestIsWorkUnitStoppable) covering RUNNING, COMPLETED, FAILED, STOPPED, STOPPING, and nil executions.
  • api/protos/experiment/api.proto, docs/flows/work_unit_lifecycle.md, docs/architecture/use_cases.md:
    • Updated RPC comments and lifecycle documentation to specify which states are skipped and the unnamed Done: true Operation return contract.

Assumptions

  • Callers of StopWorkUnit and BatchStopWorkUnits do not inspect Operation.name or Operation.response when Operation.done is already true on initial RPC return — Existing callers (Angular UI, XManager SDK, and nightly verification scripts) treat an immediately done = true operation as a completed stop or fire-and-forget the RPC — If a caller polls GetOperation(op.name) unconditionally without checking op.done first, polling an empty operation name would fail with InvalidArgument.

Verification & Testing

Related Issues / Bugs

Bug: 564895979

TAG=agy
CONV=09465163-e7bc-4fe8-b3e8-8234cdf174d3

AntonTyb and others added 2 commits November 13, 2026 15:55
…atchStopWorkUnits

Filter out work units whose current execution is already in a terminal state (COMPLETED, FAILED, STOPPED) or STOPPING before creating a STOP operation, enqueuing a DeleteAction task to Redis, or updating execution status in BatchStopWorkUnits and StopWorkUnit. When no stoppable work units remain, return an already-completed Operation (Done: true) so stopping remaining trials does not rewrite or re-enqueue finished work units. Also add the singular stop_work_unit helper to the Python entities validator.

Source: http://b/564895979

Requested-by: grain
Approved-by: grain
@github-actions
github-actions Bot requested a review from janzak-dev November 13, 2026 15:56

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

Copy link
Copy Markdown

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 updates the BatchStopWorkUnits and StopWorkUnit RPCs to skip stopping work units that are already in a terminal or stopping state, returning a completed operation instead of enqueuing redundant stop tasks. It also adds comprehensive unit tests for these scenarios and updates the nightly verification tools. The feedback suggests adding defensive nil checks in isWorkUnitStoppable to safely handle cases where a work unit or its current execution is nil.

…toppable

Add defensive nil checks for workUnit and workUnit.GetCurrentWorkUnitExecution() in isWorkUnitStoppable and add table-driven unit tests in TestIsWorkUnitStoppable.

Comment link: #1692 (comment)

Requested-by: grain
Approved-by: grain
@AntonTyb
AntonTyb requested a review from VLukiantsev November 16, 2026 10:24
@VLukiantsev

Copy link
Copy Markdown
Collaborator

Review summary (outside the diff)

The main issue is posted inline on batchstopworkunits.go: skipping FAILED leaks Kueue-admitted JobSets, and skipping STOPPING removes the API retry path. Separately, a few things fall outside the diff:

  1. Docs and API comments describe the old stop contract (AGENTS.md Principle 5). Please update these to say which states are skipped and that the RPC returns a done Operation with no name when nothing is stoppable:
    • docs/flows/work_unit_lifecycle.md §3 (L142-158, including the BatchStop sentence)
    • docs/architecture/use_cases.md:136-138 ("STOPPED always arrives")
    • the StopWorkUnit / BatchStopWorkUnits comments in api/protos/experiment/api.proto (L186, L558-559)
  2. PR description:
    • Please add the mandatory ## Assumptions section and use the standard body template (.agents/rules/github_authoring_and_prs.md §3).
    • Please replace the http://b/ link with Bug: 564895979.
  3. Motivation and undisclosed contract changes. The body says the change prevents rewriting finished work units. That rewrite can't happen today: UpdateWorkUnitExecutions takes row locks, and ShouldUpdateWorkUnitExecutionState rejects any transition out of STOPPED/COMPLETED/FAILED. The real gain is fewer operation records and Delete tasks. Please also disclose the behavior changes:
    • (a) A WU stuck in STOPPING can no longer be re-stopped.
    • (b) A WU with no current execution now returns Done: true instead of InvalidArgument.
    • (c) StopWorkUnit can now return an unnamed Operation with no result.

Only skip COMPLETED and STOPPED work units (plus work units with no
current execution) in StopWorkUnit and BatchStopWorkUnits so that:
- FAILED work units still enqueue a DeleteAction to delete live
  Kueue-admitted JobSets left behind by startup diagnostics (e.g.
  ImagePullBackOff) while ShouldUpdateWorkUnitExecutionState preserves
  their FAILED state in Spanner;
- STOPPING work units remain re-stoppable to recover from lost or
  out-of-order DeleteAction tasks.

Also remove the unused stop_work_unit helper from
xmc_entities_validator.py, extract mustSetWorkUnitState into
utils_test.go, consolidate BatchStopWorkUnits state-filtering tests,
add a MockStorage test verifying AddWorkUnitOperation is skipped for
COMPLETED and STOPPED work units, and update api.proto and lifecycle
documentation to reflect the stop contract.

Bug: 564895979
Requested-by: grain
Approved-by: grain
@AntonTyb

Copy link
Copy Markdown
Collaborator Author

Addressed in 998a7bce and updated the PR summary details:

  1. Docs & Proto comments: Updated docs/flows/work_unit_lifecycle.md §3, docs/architecture/use_cases.md §1.6, and api/protos/experiment/api.proto (StopWorkUnit, BatchStopWorkUnits, and BatchStopWorkUnitsRequest.filter) to document that COMPLETED and STOPPED work units are skipped, FAILED and STOPPING still enqueue DeleteAction, and an unnamed Operation with done = true is returned when nothing is stoppable.
  2. PR description & template: Added the ## Assumptions section, formatted with the standard PR template, and replaced the http://b/ link with Bug: 564895979 below.
  3. Motivation & contract disclosures: Clarified that ShouldUpdateWorkUnitExecutionState already prevented state transitions out of terminal states while skipping COMPLETED/STOPPED avoids redundant Spanner STOP operations and Redis DeleteAction tasks, retained re-stopping for STOPPING and FAILED, and disclosed that missing current executions and skipped stops return an unnamed &lropb.Operation{Done: true} with unset response/error.

Summary

Skip work units already in COMPLETED or STOPPED state (or lacking a current execution) in StopWorkUnit and BatchStopWorkUnits so stopping an experiment or a finished trial does not persist redundant STOP operations in Spanner or enqueue redundant DeleteAction tasks in Redis.

While UpdateWorkUnitExecutions row locks and ShouldUpdateWorkUnitExecutionState already reject state transitions out of STOPPED, COMPLETED, and FAILED, filtering COMPLETED and STOPPED work units up front avoids unnecessary operation records and actuator DeleteAction tasks while preserving:

  • FAILED cleanup: Startup failures (such as ImagePullBackOff / ErrImagePull) mark the execution FAILED while the Kueue-admitted JobSet is still alive and holding ClusterQueue quota and node capacity. StopWorkUnit and BatchStopWorkUnits continue to enqueue a DeleteAction for FAILED work units while ShouldUpdateWorkUnitExecutionState preserves their FAILED state in Spanner.
  • STOPPING retry recovery: Re-issuing StopWorkUnit or BatchStopWorkUnits on a STOPPING work unit continues to enqueue an idempotent DeleteAction to recover from lost/DLQ'd or out-of-order DeleteAction tasks.

Contract & Behavior Disclosures

  • Work units in COMPLETED or STOPPED are skipped; STOPPING and FAILED remain stoppable.
  • A work unit with no current execution (GetCurrentWorkUnitExecution() == nil) now returns &lropb.Operation{Done: true} instead of failing downstream execution-name validation with InvalidArgument.
  • When StopWorkUnit targets a skipped work unit (or BatchStopWorkUnits matches no stoppable work units), the RPC returns &lropb.Operation{Done: true} with an empty name and unset response/error rather than persisting a new STOP operation.

Key Changes

  • cmd/experiment_state_server/server/batchstopworkunits.go & stopworkunit.go:
    • Introduced stopSkippedStates = {STOPPED, COMPLETED}, isWorkUnitStoppable, and filterStoppableWorkUnits to skip COMPLETED and STOPPED work units (and work units with nil current execution) before enqueueWorkUnitStop.
    • Returned &lropb.Operation{Done: true} in StopWorkUnit when !isWorkUnitStoppable(workUnit) and in BatchStopWorkUnits when filterStoppableWorkUnits leaves no stoppable work units.
  • cmd/experiment_state_server/server/utils_test.go, batchstopworkunits_test.go, stopworkunit_test.go:
    • Extracted mustSetWorkUnitState helper in utils_test.go and refactored state-transition test setup.
    • Added table-driven tests (TestBatchStopWorkUnits_StateFiltering, TestStopWorkUnit_ByCurrentState, TestStopWorkUnit_SkippedStateDoesNotAddOperation, TestIsWorkUnitStoppable) covering RUNNING, COMPLETED, FAILED, STOPPED, STOPPING, and nil executions.
  • api/protos/experiment/api.proto, docs/flows/work_unit_lifecycle.md, docs/architecture/use_cases.md:
    • Updated RPC comments and lifecycle documentation to specify which states are skipped and the unnamed Done: true Operation return contract.

Assumptions

  • Current callers of StopWorkUnit and BatchStopWorkUnits (such as the Angular UI and nightly verification cleanup) do not require a persisted operation name or populated response payload when done = true is returned immediately for an already COMPLETED or STOPPED work unit.

Test Validation

  • bazel test //cmd/experiment_state_server/server:server_test
  • ./.githooks/pre-commit

Bug: 564895979

…8-eka-batchstopworkunits-moves

# Conflicts:
#	cmd/experiment_state_server/server/utils_test.go
@AntonTyb
AntonTyb requested a review from VLukiantsev November 19, 2026 07:31
VLukiantsev
VLukiantsev previously approved these changes Nov 19, 2026

@VLukiantsev VLukiantsev 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.

Thanks. I verified all five inline fixes in 998a7bc: FAILED/STOPPING now re-enqueue the Delete, the dead helper is removed, the op shape and persistence are tested, the setup helper is in place, and the docs and proto comments are updated. All look good.

One thing left before merging: the PR title and description were never actually updated. The new text went into a comment instead. The title still says "skip terminal and stopping work units", and the body still lists FAILED/STOPPING as skipped, mentions the removed stop_work_unit helper, uses Source: http://b/…, and has no ## Assumptions. On squash-merge that becomes the commit message on main and contradicts the code. Please move the summary from your comment into the PR body (e.g. tools/pr_review/update_pr_description.py) and retitle it, for example fix(ess): skip COMPLETED and STOPPED work units in stop RPCs.

Approving on the assumption that the title and body get updated before merge.

Comment thread api/protos/experiment/api.proto Resolved
isWorkUnitStoppable returns false when a work unit is nil or has no current execution in addition to COMPLETED and STOPPED states. Update the filterStoppableWorkUnits doc comment and the skip log messages in BatchStopWorkUnits and StopWorkUnit to say "non-stoppable work unit" so missing current executions (state=WORK_UNIT_STATE_UNSPECIFIED) are not mislabeled as completed or stopped.

Comment link: #1692 (comment)
Comment link: #1692 (comment)

Requested-by: grain
Approved-by: grain

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤖 Incremental PR Approval

PR author is a code owner and changes since last approval (6 lines) are within the 50-line grace delta.

Preserving prior approval by @VLukiantsev on commit 84fff31.

@AntonTyb

Copy link
Copy Markdown
Collaborator Author

Updated the PR title to fix(ess): skip COMPLETED and STOPPED work units in stop RPCs and updated the PR description with the current summary, ## Assumptions, ## Verification, and Bug: 564895979 trailer.

@AntonTyb AntonTyb changed the title fix(ess): skip terminal and stopping work units in StopWorkUnit and BatchStopWorkUnits fix(ess): skip COMPLETED and STOPPED work units in stop RPCs Nov 19, 2026

This branch has not been deployed

No deployments
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.

3 participants