Search code examples
apache-flinkflink-statefun

Flink Stateful Functions 2.0 Multiple Calls During Asynchronous Wait


Flink Stateful Functions 2.0 has the ability to make asychronous calls, for example to an external API: [https://ci.apache.org/projects/flink/flink-statefun-docs-release-2.0/sdk/java.html#completing-async-requests][1].

Function execution is then paused until the call completed with Success, Failure, or Unknown. Unknown is:

The stateful function was restarted, possibly on a different machine, before the CompletableFuture was completed, therefore it is unknown what is the status of the asynchronous operation.

What happens when there is a second call with the same ID to the paused/waiting function?

  1. Does the callee then wait on the called function's processing of its async result so that this second call executes with a clean, non-shared post-async state?
  2. Or does the second call execute on a normal schedule, and thus on top of the state that was current as of the async call, and then when the async call completes it continues processing using the state that was updated while the async call was pending?
  3. Or maybe the call counts as a "restart" of the called function - in which case what is the order of execution: the "restart" runs and then the async returns with "restart" to execute from the now updated state, or this order is reversed?
  4. Or something else?

Solution

  • Function execution does not pause while an async request is completing. The instance for that id will continue to process messages until the request completes. This means the state can change while the future is running.

    Think of your future as an ad-hoc function that you message and that then messages you back when it has a result. Functions can spawn multiple asynchronous requests without issue. Whichever future completes first will be processed first by the function instance, not necessarily the order in which they were spawned.