You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Flink Agents' state access and event sending must be executed in the mailbox thread. This means:
Time-consuming operations in executeAsync can be executed in other threads
Code outside executeAsync (state access, sendEvent) must be executed in the mailbox thread
Challenge 2: JDK Version Compatibility
Flink Agents needs to support JDK 11+:
JDK 21+ users: Can use Continuation to implement true async execution
JDK < 21 users: Need to fall back to synchronous execution
API Design
Add executeAsync method to the RunnerContext interface:
publicinterfaceRunnerContext {
/** * Asynchronously executes the provided supplier function and returns the result. * * <p>On JDK 21+, this method uses Continuation to yield the current action execution, * submits the supplier to a thread pool, and resumes via mailbox when complete. * * <p>On JDK < 21, this method falls back to synchronous execution. * * <p><b>Note:</b> Access to memory and sendEvent are prohibited within the supplier. */
<T> TexecuteAsync(Supplier<T> supplier) throwsException;
voidexecuteAsync(Runnablerunnable) throwsException;
}
Usage Restrictions
Memory Access Restriction: State access is prohibited within the executeAsync supplier
Serial Execution: Multiple executeAsync calls are executed serially (consistent with Python behavior)
JDK < 21: Falls back to synchronous execution, blocking the mailbox thread
Design Approach
Continuation + Mailbox Scheduling
Use JDK 21's Continuation API to implement async execution:
Action Executes Within Continuation: Continuation wraps Action code, running in the mailbox thread
Explicit Yield on executeAsync: Pause the Continuation, submit the time-consuming task to a thread pool, return to the mailbox
Resume Continuation After Task Completion: Resume execution through mailbox scheduling, subsequent code remains in the mailbox thread
To achieve sequential code style while preserving the Mailbox model, we need the ability to explicitly pause and resume during method execution.
Preserving the Mailbox model is essential: Flink Agents' state access and event sending rely on the single-threaded model of the mailbox thread to ensure correctness. If Action code executes outside the mailbox thread, state access will have concurrency issues, leading to data inconsistency. Therefore, the virtual thread approach (Action executes in virtual threads) is not feasible.
Comparison of approaches to achieve this capability in Java:
Approach
Sequential Code Style
Preserves Mailbox Model
Disadvantages
Callback Pattern
❌ Requires callbacks
✅
Poor user experience, non-intuitive code
Virtual Threads
✅
❌ Not feasible
Action leaves mailbox thread, state access has concurrency issues
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Background
Python API already supports
execute_async, but the Java API does not yet support this feature. This design adds the same capability to the Java API.Design Goals
Technical Challenges
Challenge 1: Mailbox Thread Constraint
Flink Agents' state access and event sending must be executed in the mailbox thread. This means:
executeAsynccan be executed in other threadsexecuteAsync(state access, sendEvent) must be executed in the mailbox threadChallenge 2: JDK Version Compatibility
Flink Agents needs to support JDK 11+:
API Design
Add
executeAsyncmethod to theRunnerContextinterface:Usage Restrictions
executeAsyncsupplierexecuteAsynccalls are executed serially (consistent with Python behavior)Design Approach
Continuation + Mailbox Scheduling
Use JDK 21's Continuation API to implement async execution:
executeAsync: Pause the Continuation, submit the time-consuming task to a thread pool, return to the mailboxKey Advantages:
Note: JDK 21 environment requires adding JVM parameters:
Why Use Continuation API
To achieve sequential code style while preserving the Mailbox model, we need the ability to explicitly pause and resume during method execution.
Preserving the Mailbox model is essential: Flink Agents' state access and event sending rely on the single-threaded model of the mailbox thread to ensure correctness. If Action code executes outside the mailbox thread, state access will have concurrency issues, leading to data inconsistency. Therefore, the virtual thread approach (Action executes in virtual threads) is not feasible.
Comparison of approaches to achieve this capability in Java:
Continuation API is the only lightweight approach that can simultaneously satisfy "sequential code style" and "preserve Mailbox model".
Reference: JDK Continuation Source Code
jdk.internal.vm.Continuationis a JDK internal API with no stability guarantees, and may change in future versions.Execution Flow
JDK 21+ Environment
JavaActionTask.invoke()is called in the mailbox threadcont.run()ctx.executeAsync(supplier)is called:Continuation.yield()to pause, mailbox thread becomes idleJDK < 21 Environment
Falls back to synchronous execution, behavior is identical to current Java Action.
Core Component Design
ContinuationActionExecutor
Add new
ContinuationActionExecutorclass to encapsulate Continuation execution logic:JDK 11 Version: Execute action synchronously,
executeAsyncdirectly calls supplierJDK 21 Version:
executeAction(): Create Continuation wrapping action, callcont.run()to executeexecuteAsync():Continuation.yield()to pause current ContinuationmailboxExecutor.submit()JavaActionTask Modifications
Modify
JavaActionTask.invoke():ContinuationActionExecutorto execute actionWhen action yields at
executeAsync,invoke()returnsActionTaskResult(finished=false, generatedTask=this), waiting for subsequent resumption.Multi-release JAR Support
Adopt Multi-release JAR approach, JVM automatically selects the correct version:
Beta Was this translation helpful? Give feedback.
All reactions