Skip to content

Commit 78a766f

Browse files
Align root workflow execution with real server (#2477)
1 parent e3921b6 commit 78a766f

File tree

4 files changed

+84
-14
lines changed

4 files changed

+84
-14
lines changed

temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowInfoImpl.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,19 @@ public Optional<String> getParentRunId() {
127127
: Optional.of(parentWorkflowExecution.getRunId());
128128
}
129129

130-
public String getRootWorkflowId() {
130+
public Optional<String> getRootWorkflowId() {
131131
WorkflowExecution rootWorkflowExecution = context.getRootWorkflowExecution();
132-
return rootWorkflowExecution == null ? null : rootWorkflowExecution.getWorkflowId();
132+
return rootWorkflowExecution == null
133+
? Optional.empty()
134+
: Optional.of(rootWorkflowExecution.getWorkflowId());
133135
}
134136

135137
@Override
136-
public String getRootRunId() {
138+
public Optional<String> getRootRunId() {
137139
WorkflowExecution rootWorkflowExecution = context.getRootWorkflowExecution();
138-
return rootWorkflowExecution == null ? null : rootWorkflowExecution.getRunId();
140+
return rootWorkflowExecution == null
141+
? Optional.empty()
142+
: Optional.of(rootWorkflowExecution.getRunId());
139143
}
140144

141145
@Override

temporal-sdk/src/main/java/io/temporal/workflow/WorkflowInfo.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,17 @@ public interface WorkflowInfo {
140140

141141
/**
142142
* @return Workflow ID of the root Workflow
143-
* @apiNote On server versions prior to v1.27.0, this method will return null. Otherwise, it will
144-
* always return a non-null value.
143+
* @apiNote On server versions prior to v1.27.0, this method will be empty. Otherwise, it will be
144+
* empty if the workflow is its own root.
145145
*/
146-
@Nullable
147-
String getRootWorkflowId();
146+
Optional<String> getRootWorkflowId();
148147

149148
/**
150149
* @return Run ID of the root Workflow
151-
* @apiNote On server versions prior to v1.27.0, this method will return null. Otherwise, it will
152-
* always return a non-null value.
150+
* @apiNote On server versions prior to v1.27.0, this method will be empty. Otherwise, it will be
151+
* empty if the workflow is its own root.
153152
*/
154-
@Nullable
155-
String getRootRunId();
153+
Optional<String> getRootRunId();
156154

157155
/**
158156
* @return Workflow retry attempt handled by this Workflow code execution. Starts on "1".
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this material except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
package io.temporal.workflow;
22+
23+
import static org.junit.Assert.assertEquals;
24+
25+
import io.temporal.client.WorkflowStub;
26+
import io.temporal.testing.internal.SDKTestWorkflowRule;
27+
import io.temporal.workflow.shared.TestActivities;
28+
import io.temporal.workflow.shared.TestWorkflows;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
32+
public class GetRootWorkflowExecutionTest {
33+
private static final TestActivities.VariousTestActivities activitiesImpl =
34+
new TestActivities.TestActivitiesImpl();
35+
36+
@Rule
37+
public SDKTestWorkflowRule testWorkflowRule =
38+
SDKTestWorkflowRule.newBuilder()
39+
.setWorkflowTypes(TestWorkflowImpl.class)
40+
.setActivityImplementations(activitiesImpl)
41+
.build();
42+
43+
@Test
44+
public void getRootWorkflowExecution() {
45+
TestWorkflows.TestWorkflowReturnString workflowStub =
46+
testWorkflowRule.newWorkflowStubTimeoutOptions(
47+
TestWorkflows.TestWorkflowReturnString.class);
48+
String workflowId = workflowStub.execute();
49+
assertEquals(
50+
"empty " + WorkflowStub.fromTyped(workflowStub).getExecution().getWorkflowId(), workflowId);
51+
}
52+
53+
public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString {
54+
55+
@Override
56+
public String execute() {
57+
String result = Workflow.getInfo().getRootWorkflowId().orElse("empty");
58+
if (!Workflow.getInfo().getParentWorkflowId().isPresent()) {
59+
result += " ";
60+
result +=
61+
Workflow.newChildWorkflowStub(TestWorkflows.TestWorkflowReturnString.class).execute();
62+
}
63+
return result;
64+
}
65+
}
66+
}

temporal-test-server/src/main/java/io/temporal/internal/testservice/StateMachines.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,11 @@ private static void startWorkflow(
13511351
ExecutionId parentExecutionId = parent.get().getExecutionId();
13521352
a.setParentWorkflowNamespace(parentExecutionId.getNamespace());
13531353
a.setParentWorkflowExecution(parentExecutionId.getExecution());
1354+
// This mimics the real server behaviour where the root execution is only set in history
1355+
// if the workflow has a parent.
1356+
ExecutionId rootExecutionId = ctx.getWorkflowMutableState().getRoot().getExecutionId();
1357+
a.setRootWorkflowExecution(rootExecutionId.getExecution());
13541358
}
1355-
ExecutionId rootExecutionId = ctx.getWorkflowMutableState().getRoot().getExecutionId();
1356-
a.setRootWorkflowExecution(rootExecutionId.getExecution());
13571359
HistoryEvent.Builder event =
13581360
HistoryEvent.newBuilder()
13591361
.setEventType(EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED)

0 commit comments

Comments
 (0)