How do the signal methods get invoked?
Based on the example here it looks like you don’t have to worry about thread safety.
Which thread handles the signals when there is an existing thread running the workflow code?
How do the signal methods get invoked?
Based on the example here it looks like you don’t have to worry about thread safety.
Which thread handles the signals when there is an existing thread running the workflow code?
Workflow code can be multithreaded. Every time you call Async.function
or Async.procedure
the function passed to it is executed in its own thread. The exception is when a reference to an activity or child workflow method is passed. These are executed fully asynchronously.
To support deterministic execution of multithreaded code Temporal uses cooperative multithreading. Threads are executed one by one and are not preempted until they cease control to the framework. Control is ceased when one of the Temporal SDK functions is called. The most frequently used are:
Workflow.sleep
Future.get
Workflow.await
Due to this model, all standard synchronization and thread creation, and communication features are prohibited. When used they can cause thread blocking on non-framework API causing deadlocks.
Every signal is invoked in its own workflow thread. So blocking (using supported Workflow primitives) this thread doesn’t stop other signals from being delivered. As I mentioned earlier no explicit thread safety is needed as SDK already provides it.
I personally recommend not doing much work in signal handlers themselves. But use them to queue up work for the main workflow thread to process. But each application is different, so no hard rule here.
Sorry, I got confused at this " Threads are executed one by one and are not preempted until they cease control to the framework."
Are you saying, all of Temporal is single threaded?
Or did you mean to say that all threads ( signals, main WF thread, search thread ) related to the same workflow will be scheduled in a single threaded fashion.
Assuming it’s the latter, how is this ( only one thread is active at any time ) achieved in java ?
You only have a single thread that is scheduled to do signals, main wf etc processing in succession ?
Or do you actually have multiple threads but only a single one is allowed to run at any point of time ?
A workflow can use multiple threads but only a single one is allowed to run at any point in time.
A workflow can use multiple threads but only a single one is allowed to run at any point in time.
So does it mean when a signal is invoked it’ll start a new thread, assuming workflow is already waiting, and after signal thread is done updating workflow state, workflow thread will be started/preempted?
So does it mean when a signal is invoked it’ll start a new thread, assuming workflow is already waiting, and after signal thread is done updating workflow state, workflow thread will be started/preempted?
Correct. The workflow thread will get a chance to wake up after the signal thread is blocked or completed.
Thanks @maxim for the clarification.