Repository navigation
feat(tasks): let task queues cancel their tasks when the warehouse is… - #2130
Conversation
… deleted A warehouse cannot be deleted while any task row exists for it, so a queue whose tasks reschedule themselves makes its warehouse undeletable. Queues now choose with `TaskConfig::on_warehouse_delete()`: `Block`, the default and today's behaviour, or `Cancel`, which cancels the queue's tasks in that warehouse, running ones included, inside the delete transaction. Built-in queues keep `Block`, so pending purges still block deletion. The cancel uses the existing `cancel_scheduled_tasks` and runs before `ensure_warehouse_spec_mutable` locks the warehouse row. A worker that records its outcome holds its task row and needs the warehouse row for the task_log foreign key, so cancelling under that lock would deadlock. No `CatalogStore` method is added and no lock is taken. Warehouse delete reports races as retryable 409s: - A task created after the unfinished-task check makes the DELETE fail on `task_warehouse_id_fk`. This is now reported as `WarehouseHasUnfinishedTasks`, not `WarehouseNotEmpty`. - When the DELETE removes no row because the warehouse changed concurrently (protected or deleted by another request, possible for bypass callers), the delete returns a `ConcurrentModification` conflict. It used to report success, so authz cleanup and the deletion event ran for a warehouse that still existed. Recording a task's outcome stops after one attempt when the task is gone (404), for example because its warehouse was deleted. A 409 still retries.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughWarehouse task queues now define whether warehouse deletion blocks or cancels their tasks. Deletion applies configured cancellation within its transaction and handles concurrent changes. Task recording recognizes closed attempts and preserves scheduled tasks when an older attempt records an outcome. Tests cover these behaviors and related deletion conditions. ChangesWarehouse task handling and deletion
Priority: ➖ Normal Estimated code review effort: 4 (Complex) | ~45 minutes Change: Feature Sequence Diagram(s)sequenceDiagram
participant WarehouseManagement
participant RegisteredTaskQueues
participant CatalogTaskOps
participant PostgresWarehouseStorage
WarehouseManagement->>RegisteredTaskQueues: get queues configured to cancel
WarehouseManagement->>CatalogTaskOps: cancel warehouse tasks in the transaction
WarehouseManagement->>PostgresWarehouseStorage: delete warehouse
PostgresWarehouseStorage-->>WarehouseManagement: deletion result or conflict
Merge Risk: ⚪ Minimal · up to
|
Extensions only return a variant from `TaskConfig::on_warehouse_delete`, which stays possible. Outside this crate a `match` needs a wildcard arm, so a later variant does not break downstream code.
- Recording the outcome of a closed attempt stops after one try, with an info log. An attempt is closed when its task is gone (404), or when it already has an outcome (409 `TaskAttemptAlreadyCompleted` or `TaskAttemptAlreadyFailed`, for example after a cancel or a timeout). A 409 `TransactionFailed` still retries. `is_task_attempt_closed` exposes this decision for code that calls `CatalogTaskOps::record_task_*` in its own transaction. - A late record of a closed attempt leaves a rescheduled task alone. Before, recording success after stop and "run now" deleted the rescheduled task when the caller committed (`record_success_in_transaction`). - Docs: a warehouse with namespaces cannot be deleted, so the tasks a delete cancels belong to deleted entities. Tasks of a queue that is not registered block the delete. - Test: a warehouse with a namespace keeps its tasks of `Cancel` queues.
- The `OnWarehouseDelete::Cancel` doc states the general rule: a transaction that writes a row referencing the warehouse or locks one of its tasks, and then waits on another task of the warehouse, can deadlock with the delete; one side then fails with a retryable conflict. - `queues_cancelled_on_warehouse_delete` matches on the variant, so a new variant needs a decision. Its sort keeps the order of cancelled queues the same across concurrent deletes. - The test for built-in queues asserts the exact queue names.
✅ PR Title Formatted CorrectlyThe title of this PR match the correct format. Thank you! |
Summary
Task queues can decide what deleting a warehouse does with their tasks.
TaskConfig::on_warehouse_delete()returnsOnWarehouseDelete::Block(default: the warehouse cannot be deleted while it has a task of this queue) orOnWarehouseDelete::Cancel(deleting the warehouse cancels the queue's tasks in that warehouse, running ones included; if the delete fails, the tasks stay). A warehouse with namespaces cannot be deleted, so the cancelled tasks belong to deleted tables, views and generic tables, or to the warehouse itself. The setting applies in every process that registers the queue; where the queue is not registered, its tasks block the delete.All built-in queues keep
Block. Without custom task queues, only the fixes below apply.Also:
409 WarehouseHasUnfinishedTasks(was409 WarehouseNotEmpty). Retry the request. Other deletes lock the warehouse first, so the task is created only after the delete and then fails.Related issue
None.
How was this tested?
The classification of record errors was tested against PostgreSQL 17 for an API cancel, a timed-out task that was picked again, a warehouse deleted during a run, an outcome recorded twice (every order), a real deadlock, a serialization failure, a terminated connection and a pool timeout. Only the closed-attempt cases stop; all others retry.
Checklist
.sqlx/folder committedaudit-format/unreleased/Breaking changes
None.
Downstream follow-up
lakekeeper::service::tasks::OnWarehouseDelete(Block,Cancel;#[non_exhaustive]),TaskConfig::on_warehouse_delete()with defaultBlockin bothopen-apivariants,is_task_attempt_closed, and the error-type constantsTASK_ATTEMPT_ALREADY_COMPLETED_ERROR_TYPEandTASK_ATTEMPT_ALREADY_FAILED_ERROR_TYPE. Nothing renamed or removed;QueueRegistrationand theCatalogStoretraits are unchanged. Plus compiles unchanged.record_success,record_failure,record_success_in_transactionand the deserialization-failure path stop after one attempt, with an info log, when the task is gone (404) or the attempt already has an outcome (409TaskAttemptAlreadyCompletedorTaskAttemptAlreadyFailed). Other errors, such as 409TransactionFailed, still retry.CatalogTaskOps::record_task_*directly can useis_task_attempt_closedto decide whether to retry.scheduledstate returns 404 or 409 and writes nothing.Cancelqueue must be registered wherever the management API runs (zero workers is fine). Otherwise its tasks still block the delete there.Release notes
Custom task queues can let warehouse deletion cancel their tasks (
TaskConfig::on_warehouse_delete). Built-in queues are unchanged.Bug Fixes
Summary by CodeRabbit