-
Notifications
You must be signed in to change notification settings - Fork 804
Add TaskReadinessGate plugin extension point #7158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robsyme
wants to merge
24
commits into
adr/task-readiness-gate
Choose a base branch
from
feature/task-readiness-gate
base: adr/task-readiness-gate
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
08d9bab
Add TaskReadinessGate plugin extension point interface
robsyme faec578
Pin InterruptedException in TaskReadinessGate contract test
robsyme 6326896
Add executor.gateMaxWait config option
robsyme f2a4000
Wire TaskReadinessGate discovery and executor into TaskPollingMonitor
robsyme d61d9f0
Document GateState role in gateMaxWait accounting
robsyme 53e64b8
Submit TaskReadinessGate work on schedule()
robsyme e0e32be
Document gateExecutor non-blocking-submission contract
robsyme a2b7256
Check TaskReadinessGate futures in canSubmit()
robsyme 1066c40
Preserve cause exception type in TaskReadinessGate failure path
robsyme 3968af2
Test TaskReadinessGate exception propagation through canSubmit
robsyme 0199381
Preserve ProcessException identity through TaskReadinessGate failure …
robsyme 56f7ec4
Wrap non-ProcessException causes to preserve ProcessRetryableExceptio…
robsyme 5fdcfab
Enforce executor.gateMaxWait timeout in TaskReadinessGate flow
robsyme 7532936
Cancel in-flight TaskReadinessGate futures on task eviction
robsyme af4bc73
Test all-must-complete semantics for multiple TaskReadinessGates
robsyme e7f4913
Stub forks/ready in ParallelPollingMonitorTest array-size case after …
robsyme d77e0ab
Document TaskReadinessGate extension point and executor.gateMaxWait
robsyme d699108
Remove implementation plan from upstream docs
robsyme 898913d
Document retry-marker placement requirement in TaskReadinessGate
robsyme 48b2762
Drop executor.gateMaxWait; let plugins own timeout policy via hints
robsyme d8c05ec
Extract TaskGateManager from TaskPollingMonitor
robsyme 21c7d60
Revert TaskPollingMonitor constructor lock-init refactor
robsyme 34f8fed
Address re-review nits: stale javadoc, field-init comment, style, intent
robsyme c7445c2
Rename TaskGateManager.submit() to prepare() per PR review
robsyme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| (task-readiness-gate)= | ||
|
|
||
| # `TaskReadinessGate` | ||
|
|
||
| `TaskReadinessGate` is a plugin extension point that defers task submission until an external precondition is met — for example, restoring an S3 object from Glacier before an AWS Batch worker tries to stage it. It works uniformly with every executor that uses Nextflow's `TaskPollingMonitor` and removes the need for plugins to subclass an executor and its task handler purely to override `TaskHandler.isReady()`. | ||
|
|
||
| ## Interface | ||
|
|
||
| A gate implements one method: | ||
|
|
||
| ```groovy | ||
| package nextflow.processor | ||
|
|
||
| import org.pf4j.ExtensionPoint | ||
|
|
||
| interface TaskReadinessGate extends ExtensionPoint { | ||
| void prepare(TaskHandler handler) throws InterruptedException | ||
| } | ||
| ``` | ||
|
|
||
| The plugin registers an implementation via the standard PF4J `@Extension` mechanism, the same way `TraceObserverFactory`, `CacheFactory`, and other extension points are discovered. | ||
|
|
||
| ## Contract | ||
|
|
||
| - **Blocking is allowed.** `prepare` runs on a managed virtual-thread executor inside `TaskPollingMonitor`. Calling `Thread.sleep`, blocking I/O, or long-polling APIs is fine. The scheduler thread is never blocked. | ||
| - **Throwing fails the task.** Any exception marks the task as permanently failed and routes the cause through the task's `errorStrategy` directive. `ProcessException` (and subclasses) propagate identity-preserved. Other throwables are wrapped in a `ProcessException` with the original attached as `cause`, so retry markers like `ProcessRetryableException` reach `TaskProcessor.resumeOrDie` intact. | ||
| - **Retry markers** (`ProcessRetryableException`, `CloudSpotTerminationException`) are recognised by `resumeOrDie` on the *cause* of the thrown exception, not on the exception itself. If you want `errorStrategy 'retry'` to fire for a transient failure, throw the marker as-is (it will be wrapped) — do not pre-wrap it in a `ProcessException`, since the `ProcessException` would propagate identity-preserved and the marker would be lost. | ||
| - **Interrupts must be honored.** Task eviction, workflow abort, and the `executor.gateMaxWait` safety net cancel the in-flight `prepare` by interrupting its thread. Use interruptible primitives (`Thread.sleep`, blocking I/O on NIO channels, `Future.get`). | ||
| - **Multiple gates compose.** When several plugins register gates, all must complete successfully before the task is admitted. Gates run in parallel; order is unspecified. | ||
|
|
||
| ## Configuration | ||
|
|
||
| `executor.gateMaxWait` bounds the time a gate may spend preparing a single task (default `24h`). Tasks whose gates exceed this limit are cancelled and fail with a `ProcessException` that the task's `errorStrategy` can handle (including `retry`). | ||
|
|
||
| ## Per-process opt-out | ||
|
|
||
| Use the existing `hints` directive — no core change required: | ||
|
|
||
| ```nextflow | ||
| process FAST_PATH { | ||
| hints 'glacier/skip': true | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| Inside the gate, inspect `handler.task.config.hints` and short-circuit: | ||
|
|
||
| ```groovy | ||
| @Override | ||
| void prepare(TaskHandler handler) throws InterruptedException { | ||
| if( handler.task.config.hints['glacier/skip'] == true ) return | ||
| // ... real work | ||
| } | ||
| ``` | ||
|
|
||
| The `hints` directive uses dot-separated and prefix-separated keys; plugins should namespace their hint keys (e.g. `glacier/skip`, `mycorp.cold-storage/skip`) to avoid collisions. | ||
|
|
||
| ## Example | ||
|
|
||
| A minimal gate that issues an S3 Glacier restore request and waits for completion: | ||
|
|
||
| ```groovy | ||
| @CompileStatic | ||
| class GlacierReadinessGate implements TaskReadinessGate { | ||
|
|
||
| private final GlacierRestoreManager manager | ||
|
|
||
| GlacierReadinessGate() { | ||
| this.manager = new GlacierRestoreManager(/* config from session */) | ||
| } | ||
|
|
||
| @Override | ||
| void prepare(TaskHandler handler) throws InterruptedException { | ||
| if( handler.task.config.hints['glacier/skip'] == true ) return | ||
| for( S3Path path : extractS3Inputs(handler.task) ) { | ||
| manager.issueRestoreIfNeeded(path) // idempotent | ||
| while( !manager.isRestored(path) ) { | ||
| if( manager.isPermanentlyFailed(path) ) | ||
| throw new ProcessException("Glacier restore failed for ${path}") | ||
| Thread.sleep(60_000) // virtual thread parks | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## When `prepare` runs | ||
|
|
||
| `TaskPollingMonitor.schedule()` submits `prepare` to the managed executor the moment the task is enqueued — before any executor slot has freed up, before `canForkProcess()` is consulted. This means restore work for tasks queued behind a full executor begins immediately, not when slots free up. | ||
|
|
||
| `canSubmit()` then polls the resulting `Future` on every monitor tick. The task is admitted as soon as every gate's future has completed successfully and the standard `canForkProcess` / `isReady` / capacity checks pass. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
modules/nextflow/src/main/groovy/nextflow/processor/TaskReadinessGate.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2013-2026, Seqera Labs | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package nextflow.processor | ||
|
|
||
| import groovy.transform.CompileStatic | ||
| import org.pf4j.ExtensionPoint | ||
|
|
||
| /** | ||
| * Plugin extension point that defers task submission until an external precondition is met. | ||
| * | ||
| * <p>Implementations are invoked once per task by the scheduler, on a managed background | ||
| * thread (virtual thread when available). The task is admitted for submission when this | ||
| * method returns; throw to mark the task as permanently failed and route the cause through | ||
| * the task's {@code errorStrategy}. | ||
| * | ||
| * <p>Implementations may block freely — {@code Thread.sleep}, network calls, long polling. | ||
| * The scheduler thread is never blocked by this call. | ||
| * | ||
| * <p>Implementations <b>must</b> honor {@code Thread.interrupt()} so that task eviction, | ||
| * workflow abort, and the {@code executor.gateMaxWait} backstop can unblock {@code prepare} | ||
| * promptly. Use interruptible primitives ({@code Thread.sleep}, blocking I/O on NIO | ||
| * channels, {@code Future.get}) and propagate {@code InterruptedException}. | ||
| * | ||
| * <p>When multiple gates are registered, a task is admitted only when every gate's | ||
| * {@code prepare} method has returned successfully. Evaluation order across gates is | ||
| * unspecified; all gates start in parallel on the managed executor. | ||
| * | ||
| * <p>Per-process opt-out is available via the {@code hints} directive — gates that wish | ||
| * to support it should check a namespaced hint key (e.g. {@code 'glacier/skip': true}) and | ||
| * return immediately when set. No core mechanism is required. | ||
| */ | ||
| @CompileStatic | ||
| interface TaskReadinessGate extends ExtensionPoint { | ||
| void prepare(TaskHandler handler) throws InterruptedException | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add this extension point, I think we should move the logic in
handler.isReady()into a task gate. It can be a follow-up effortThat method is currently only used by Wave to make sure that the container is resolved before submitting the task, which seems like a clear use case for this extension point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. I'd keep into a follow-up effort to keep this PR self-contained