Skip to content

Increase TestDataflowRunner state timeout and retry FailedPrecondition on cancel while non-terminal - #41627

Merged
damccorm merged 3 commits into
masterfrom
fix/python-dataflow-wait-in-state-and-cancel-retry
Dec 3, 2026
Merged

damccorm merged 3 commits into
masterfrom
fix/python-dataflow-wait-in-state-and-cancel-retry

Conversation

@damccorm

@damccorm damccorm commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Note: this PR was auto-generated by tooling. Before moving it out of draft, the author has done a full review of the PR, specifically evaluating it on the following criteria:

  • The PR is an appropriate fix for the issue (e.g. we're not changing a test to avoid a bug)
  • The PR is a simple fix to the issue (e.g. we're not adding infra when we could just modify a test)
  • The PR is not covering for other problems (e.g. if we're bumping timeouts, it is not because we're covering for a performance problem)

Full details of the methodology of generation and review can be found here - https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing

If you have concerns with the PR or with the underlying methodology used to generate this, please comment below - they will be addressed by the author not the tooling :)

Summary

Increases WAIT_IN_STATE_TIMEOUT in TestDataflowRunner from 10 minutes to 20 minutes and retries Dataflow 400 FailedPrecondition errors in DataflowApplicationClient.modify_job_state when canceling a job that is still in a non-terminal JobState.

Production Impact

This changes production code in DataflowApplicationClient.modify_job_state (sdks/python/apache_beam/runners/dataflow/internal/apiclient.py), which backs DataflowPipelineResult.cancel(). Previously, calling cancel() while a newly created Dataflow job was still initializing (e.g. 'Job is not yet ready for canceling') or during a brief control-plane backend reassignment window (e.g. 'Job has no backend assigned') immediately failed with an unretried google.api_core.exceptions.FailedPrecondition. With this change, FailedPrecondition errors on JOB_STATE_CANCELLED are retried with bounded exponential backoff whenever get_job() reports a non-terminal state (independent of error-message wording), while already-terminal / already-cancelling / resource-cleaning-up jobs and non-cancel transitions (e.g. draining a batch job) fail immediately without retrying.

Root Cause

  1. Under concurrent Dataflow job submissions in apache-beam-testing, streaming Dataflow jobs can spend slightly more than 10 minutes in JOB_STATE_PENDING before transitioning to JOB_STATE_RUNNING, tripping TestDataflowRunner.WAIT_IN_STATE_TIMEOUT = 10 * 60.
  2. When TestDataflowRunner (or any user code) cancels a job that is still initializing or briefly unassigned to a workflow backend, the Dataflow update_job API returns 400 FAILED_PRECONDITION. modify_job_state previously used the default retry.retry_on_server_errors_filter, which does not retry HTTP 400 / FailedPrecondition.

Fix

  • Bump WAIT_IN_STATE_TIMEOUT in sdks/python/apache_beam/runners/dataflow/test_dataflow_runner.py from 10 * 60 to 20 * 60.
  • Update DataflowApplicationClient.modify_job_state in sdks/python/apache_beam/runners/dataflow/internal/apiclient.py to wrap update_job in bounded exponential backoff (num_retries=10, initial_delay_secs=5.0, max_delay_secs=30.0, stop_after_secs=180.0) that retries server errors and, for JOB_STATE_CANCELLED + FailedPrecondition, queries self.get_job(job_id) and retries only if current_state is not in _NON_RETRYABLE_CANCEL_STATES (DONE, FAILED, CANCELLED, CANCELLING, UPDATED, DRAINED, RESOURCE_CLEANING_UP). If get_job() fails or the requested transition is not JOB_STATE_CANCELLED, the original FailedPrecondition is raised immediately.
  • Add unit test UtilTest.test_modify_job_state_retries_failed_precondition_by_job_state in sdks/python/apache_beam/runners/dataflow/internal/apiclient_test.py covering retryable states (PENDING, RUNNING, UNKNOWN), non-retryable states (DONE, FAILED, CANCELLED, CANCELLING, RESOURCE_CLEANING_UP), non-cancel transitions (DRAINING), and get_job() error propagation.

Verification

  • Ran yapf --style setup.cfg --diff across modified files (clean).
  • Ran pytest apache_beam/runners/dataflow/internal/apiclient_test.py apache_beam/runners/dataflow/dataflow_runner_test.py locally (101 passed, 2 skipped).
  • Triggered PostCommit Python ValidatesRunner Dataflow: https://github.com/apache/beam/actions/runs/45688455700

Recent Occurrences

4 failing master workflow runs in the past 3 weeks:

@damccorm damccorm added the test-failure-cleanup Automated test health and flake cleanup PRs label Nov 24, 2026
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@github-actions github-actions 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.

📋 Review Summary

This PR increases WAIT_IN_STATE_TIMEOUT in TestDataflowRunner from 10 minutes to 20 minutes to accommodate longer worker startup latencies under concurrent load, and adds retry logic in DataflowApplicationClient.modify_job_state for transient FailedPrecondition errors during job cancellation or draining. The changes are well-scoped, properly preserve fast failure on terminal preconditions, and include solid unit test coverage.

🔍 General Feedback

  • The _retry_on_modify_job_state_error filter cleanly delegates server errors to retry_on_server_errors_filter while specifically matching the transient control-plane messages ('not yet ready for canceling' and 'no backend assigned').
  • Unit tests in apiclient_test.py verify both transient retry behavior and non-retryable terminal FailedPrecondition behavior.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.19%. Comparing base (0da0d89) to head (f17fa51).
⚠️ Report is 206 commits behind head on master.

Files with missing lines Patch % Lines
...apache_beam/runners/dataflow/internal/apiclient.py 90.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #41627      +/-   ##
============================================
+ Coverage     58.04%   58.19%   +0.15%     
  Complexity     2844     2844              
============================================
  Files          1210     1211       +1     
  Lines        173261   173793     +532     
  Branches       2329     2329              
============================================
+ Hits         100561   101145     +584     
+ Misses        69987    69935      -52     
  Partials       2713     2713              
Flag Coverage Δ
python 80.85% <90.47%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions github-actions Bot added python runners size/s S: 10-49 lines changed labels Nov 24, 2026
@damccorm damccorm added the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Nov 24, 2026
@damccorm
damccorm marked this pull request as ready for review November 25, 2026 19:34
@damccorm
damccorm marked this pull request as draft November 25, 2026 19:34
@damccorm

Copy link
Copy Markdown
Contributor Author

/gemini review

@github-actions github-actions Bot added size/m M: 50-249 lines changed and removed size/s S: 10-49 lines changed labels Nov 25, 2026
@damccorm damccorm changed the title Increase TestDataflowRunner state timeout and retry transient FailedPrecondition on cancel Increase TestDataflowRunner state timeout and retry FailedPrecondition on cancel while non-terminal Nov 25, 2026
@damccorm damccorm removed the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Nov 25, 2026

@github-actions github-actions 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.

📋 Review Summary

This PR resolves flaky Dataflow E2E tests by increasing WAIT_IN_STATE_TIMEOUT in TestDataflowRunner from 10 to 20 minutes and retrying FailedPrecondition errors in DataflowApplicationClient.modify_job_state when canceling a job that is still in a non-terminal state. The changes are well-scoped, properly bounded with exponential backoff, and accompanied by comprehensive unit tests.

🔍 General Feedback

  • Gating FailedPrecondition retries on JobState via get_job() rather than matching error message strings is a clean, resilient design.
  • One edge case worth considering in DataflowPipelineResult.cancel() / modify_job_state: if a job transitions into a terminal or cancelling state concurrently with the update_job call (resulting in a FailedPrecondition with current_state in JOB_STATE_CANCELLED or JOB_STATE_CANCELLING), modify_job_state will re-raise FailedPrecondition even though the cancellation goal has effectively been met.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@damccorm

Copy link
Copy Markdown
Contributor Author

This should help us avoid failures, but maybe more importantly will avoid us failing to cancelled completed jobs in some cases

@damccorm
damccorm marked this pull request as ready for review November 25, 2026 20:44
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.
Backup reviewer: claudevdm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

return (Job, (self.options, ))


_NON_RETRYABLE_CANCEL_STATES = frozenset({

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.

_NON_RETRYABLE_CANCEL_STATES is confusing to me since the elements in the set don't seem to be cancel states.

@tvalentyn tvalentyn Dec 2, 2026 •

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.

perhaps: _NON_RETRYABLE_FOR_CANCEL_STATES is slightly better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed to _NON_RETRYABLE_FOR_CANCEL_STATES in f17fa51 — thanks!

@damccorm
damccorm merged commit e3c4ff6 into master Dec 3, 2026
88 of 89 checks passed
@damccorm
damccorm deleted the fix/python-dataflow-wait-in-state-and-cancel-retry branch December 3, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Next Action: Reviewers python runners size/m M: 50-249 lines changed test-failure-cleanup Automated test health and flake cleanup PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants