Newbie to Temporal. This is my workflow method code using the java sdk.
Map<String, String> activityResult = customActivity.fetchCustomMap();
Workflow.sleep(Duration.ofSeconds(5));
Map<String, String> activityResult1 = customActivity.fetchCustomMap();
System.out.println(Thread.currentThread() + "activity result " + activityResult);
System.out.println(Thread.currentThread() + "activity result1 " + activityResult1);
Workflow.sleep(Duration.ofSeconds(2));
System.out.println(Thread.currentThread() + "workflow execution ends");
Workflow.sleep(Duration.ofSeconds(2));
Just wanted to understand when an activity method is called, it appears as if the thread is blocked (works like a sync request/response).
How does it scale? IF this is all synchronous, this means a workflow is essentially pinned to one worker instance?
How does the activity look if it has to be async and needed to be distributed to different workers?
If that happens, how does the caching work in case of replays?
A different worker needs to know what all the previous activities have been executed?
[Edit - Added more info]
In the above code snippet, when a thread T1 is executing this line:
Map<String, String> activityResult = customActivity.fetchCustomMap();
My understanding (please correct if something is incorrect) is - it is intercepted by the SDK and makes a remote call (via events) to the Temporal Server to schedule the activity execution.
The temporal server then emits an activity event for worker(s) to consume and execute.
It is then picked up by a worker (it could be another worker too if multiple worker instances are running) to execute the activity.
Once the activity execution is finished, then the SDK intercepts and makes a remote call to mark the completion and record the state in the history.
Then, another workflow event is scheduled and emitted and consumed by the first worker which initiated the activity call (from the workflow) and it resumes from there on.
The question I was having is, is the thread T1 (where the activity call was initially forked) is blocked until this round-trip finishes?
Workflow is not pinned to the worker instance. Otherwise, a workflow would not be able to continue if this worker instance dies. Workflow is cached on a worker instance as a performance optimization. So if it is blocked for a long time it is usually pushed out of the cache and is recovered on another worker when the time to continue comes. This way you can have many more blocked workflows than a single worker can keep in memory or has threads to process.
Activity invocation is internally always async and by default is delivered to workers through a task queue. So it can end up on another worker any time it is executed or retried.
I don't understand the question. The activity worker is fully stateless. Each activity execution is completely independent.