Workflow Versioning in Temporal

Hi Team,
I am quite confused with the versioning.

int version = Workflow.getVersion(“updateAdded”, Workflow.DEFAULT_VERSION, 1);
if(version ==1) {
activities.updateRequest(requestDto);
}else {
activities.validateRequest(requestDto);
}

I have few workflow executions which were started already and waiting. Once I add this change and restart the worker, what I observe is both the already started executions and newly executions are picking the new branch against the condition for version=1.
My requirement is already running execution should continue in older version of code and only newly created instances(after version change deployment) should pick up the new change. Is it possible to achieve this?

You can put the condition at the beginning of the workflow function and then use the same changeID in other places.

hi @maxim
Thanks for the response. I am a bit confused. But my question is I have a Workflow Definition as follows:

A—>B—>C

I have started a few executions already and they are currently at the execution point A. Now I made some changes in B with the versioning.
What I observe is after restarting the worker, my executions which were halting at A are also picking up the new change with the latest version that I made in B. What I expect here is only the workflow instances that are started after deploying this change should pick up the new change and already running executions (which are halting at A) should continue my older code itself.
Is it possible to achieve this behavior with temporal versioning concept?

Yes.

getVersion("changeId1", ...) ...
A
getVersion("changeId1", ...) ...
B 
C

In the above sample, both getVersion calls will return the same value. So if A executed the old version B will execute the old one as well.

hi @maxim
When does actually the version increment occurs in temporal?

int version = Workflow.getVersion(“updateAdded”, Workflow.DEFAULT_VERSION, 1);
if(version ==1) {
activities.updateRequest(requestDto);
}else {
activities.validateRequest(requestDto);
}

Upon adding the above change, the change Id is updated with version 1. How does that change to version 2? Does temporal automatically update the version if we increase the maxSupported version value?

The version is per workflow execution. So, each workflow can have a different value. The rule is that if getVersion is called during replay, it checks if there is a marker event with a version recorded. If recorded, then the version from the event is returned. If not recorded then the DEFAULT_VERSION is returned. If the getVersion is called during new code execution, the maximum version is returned, and the marker command with that version is emitted.

All getVersion calls with the same changeId return the same value. It also means that all getVersion calls with the same changeId must be added simultaneously.

Thanks for the help @maxim