Skip to content

feat(gtf): cancel orphaned warehouse queries + dedicated reaper beat job - #43658

Merged
villebro merged 1 commit into
gaq-to-gtffrom
villebro/gtf-orphan-query-cancel
Aug 28, 2026
Merged

villebro merged 1 commit into
gaq-to-gtffrom
villebro/gtf-orphan-query-cancel

Conversation

@villebro

Copy link
Copy Markdown
Member

SUMMARY

Follow-up to #43627 (merged), closing the one gap it explicitly deferred and tightening the reaper's schedule.

1. Cancel the orphaned warehouse query. #43627 made async chart-data query tasks cancellable while the worker is alive (an on_abort handler runs db_engine_spec.cancel_query over a fresh connection). But when a worker dies, there's no live thread to run that handler, so the reaper only marked the Task FAILURE and revoked the Celery job — the abandoned warehouse query kept running until the warehouse's own statement/idle timeout. Now the reaper cancels it out-of-band.

2. Dedicated reaper beat job. Reaping was folded into prune_tasks. Reaping wants a short cadence (detect a dead worker — and cancel its query — within ~a minute); retention pruning is a heavy, infrequent bulk DELETE. One shared cadence can't serve both, so reaping now runs on its own reap_orphaned_tasks beat schedule.

How the orphan-cancel works

The reaper runs in a different process from the dead worker, so the engine cancel-id must be on the Task row:

  • Persist at capture — when the capture sink obtains a cancel-id, TaskContext.set_cancellation(database_id, cancel_id) merges {cancel_database_id, cancel_query_id} into the property cache. It does not write: the task registers its abort handler immediately after, and that is_abortable write already flushes the whole cache — so the handle is persisted in an existing write, no extra DB write on the hot path. Only engines that expose a cancel-id before execution (Postgres/MySQL/Snowflake/Redshift, as in the live path) persist a handle.
  • Reaper cancels — ReapOrphanedTasksCommand._reap reads the handle, loads the Database, and reuses the existing cancel_chart_query(database, cancel_id) helper (fresh connection, id validation, gtf.query.cancel / gtf.query.cancel_failed metrics) before the revoke + CAS→FAILURE. Best-effort: no handle, a missing database, or a cancel failure never blocks the FAILURE transition. A dead worker holds no lock on the row, so this is race-free.

No new helper, config key, or migration — cancel_chart_query and the reaper already exist; this reuses them. The live abort path is unchanged (it still uses the in-memory closure).

Scheduling change

  • reap_orphaned_tasks is a new Celery beat task; prune_tasks reverts to retention-only. CELERY_CONFIG.beat_schedule gains a commented reap_orphaned_tasks template on a short interval (e.g. every minute), separate from the prune_tasks retention template. Docs/UPDATING updated.

TESTING INSTRUCTIONS

  • Unit (tests/unit_tests/tasks/): TaskContext.set_cancellation merges the handle into the cache (persisted by the ensuing is_abortable write, existing keys preserved); the capture sink calls set_cancellation before on_abort (and neither without a cancel-id); _reap calls cancel_chart_query with the persisted (database, cancel_id) when present, skips it when absent or the database is gone, and reaps regardless; reap_orphaned_tasks delegates to the command.
  • Integration (tests/integration_tests/tasks/commands/test_reap.py): an orphan with a persisted handle → the reaper invokes cancel_chart_query with the loaded Database and still ends the row FAILURE.
  • Manual (Celery worker + reap_orphaned_tasks beat + DISTRIBUTED_COORDINATION_CONFIG, a cancellable engine like Postgres): run an async chart query, kill -9 the worker mid-query → within GTF_ORPHAN_TASK_TIMEOUT the task is FAILURE and the backend query is gone from pg_stat_activity.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags: GLOBAL_TASK_FRAMEWORK (async chart data also needs GLOBAL_ASYNC_QUERIES)
  • Changes UI
  • Includes DB Migration (the cancel_* handle lives in the existing properties JSON blob — no schema change)
  • Introduces new feature or API
  • Removes existing feature or API

Two related follow-ups to #43627:

- Split reaping onto its own `reap_orphaned_tasks` beat schedule (short cadence)
  instead of riding `prune_tasks` (a heavy, infrequent retention delete); the
  two cadences conflicted.
- Cancel the abandoned warehouse query of an orphaned task out-of-band. Live
  cancellation already runs an on_abort handler in the worker, but a dead worker
  can't; so the cancel handle (database id + engine cancel id) is now persisted
  on the task at capture — merged into the property cache so the existing
  is_abortable write flushes it (no extra write) — and the reaper reuses
  `cancel_chart_query` to kill the query before marking the task FAILURE.
  Best-effort and engine-gated, matching the live path.
@dosubot dosubot Bot added change:backend Requires changing the backend global:async-query Related to Async Queries feature labels Aug 28, 2026
@bito-code-review

bito-code-review Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Bito Automatic Review Skipped - Branch Excluded

Bito didn't auto-review because the source or target branch is excluded from automatic reviews.
No action is needed if you didn't intend for the agent to review it. Otherwise, to manually trigger a review, type /review in a comment and save.
You can change the branch exclusion settings here, or contact your Bito workspace admin at [email protected].

@github-actions github-actions Bot added the doc Namespace | Anything related to documentation label Aug 28, 2026
@netlify

netlify Bot commented Aug 28, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit c4fcf97
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a91fc8310368200075fa3a2
😎 Deploy Preview https://deploy-preview-43658--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Aug 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (gaq-to-gtf@e8a1a84). Learn more about missing BASE report.

Files with missing lines Patch % Lines
superset/tasks/scheduler.py 62.50% 3 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##             gaq-to-gtf   #43658   +/-   ##
=============================================
  Coverage              ?   79.27%           
=============================================
  Files                 ?     2890           
  Lines                 ?   166418           
  Branches              ?    38456           
=============================================
  Hits                  ?   131935           
  Misses                ?    32004           
  Partials              ?     2479           
Flag Coverage Δ
hive 37.99% <16.66%> (?)
mysql 57.62% <54.16%> (?)
postgres 57.66% <54.16%> (?)
presto 39.90% <16.66%> (?)
python 83.86% <87.50%> (?)
sqlite 57.35% <54.16%> (?)
unit 74.26% <87.50%> (?)

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.

@villebro
villebro merged commit 96ca860 into gaq-to-gtf Aug 28, 2026
73 checks passed
@villebro
villebro deleted the villebro/gtf-orphan-query-cancel branch August 28, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:backend Requires changing the backend doc Namespace | Anything related to documentation global:async-query Related to Async Queries feature size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant