Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,19 @@ public Optional<String> getParentRunId() {
: Optional.of(parentWorkflowExecution.getRunId());
}

public String getRootWorkflowId() {
public Optional<String> getRootWorkflowId() {
WorkflowExecution rootWorkflowExecution = context.getRootWorkflowExecution();
return rootWorkflowExecution == null ? null : rootWorkflowExecution.getWorkflowId();
return rootWorkflowExecution == null
? Optional.empty()
: Optional.of(rootWorkflowExecution.getWorkflowId());
}

@Override
public String getRootRunId() {
public Optional<String> getRootRunId() {
WorkflowExecution rootWorkflowExecution = context.getRootWorkflowExecution();
return rootWorkflowExecution == null ? null : rootWorkflowExecution.getRunId();
return rootWorkflowExecution == null
? Optional.empty()
: Optional.of(rootWorkflowExecution.getRunId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,17 @@ public interface WorkflowInfo {

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

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

/**
* @return Workflow retry attempt handled by this Workflow code execution. Starts on "1".
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.workflow;

import static org.junit.Assert.assertEquals;

import io.temporal.client.WorkflowStub;
import io.temporal.testing.internal.SDKTestWorkflowRule;
import io.temporal.workflow.shared.TestActivities;
import io.temporal.workflow.shared.TestWorkflows;
import org.junit.Rule;
import org.junit.Test;

public class GetRootWorkflowExecutionTest {
private static final TestActivities.VariousTestActivities activitiesImpl =
new TestActivities.TestActivitiesImpl();

@Rule
public SDKTestWorkflowRule testWorkflowRule =
SDKTestWorkflowRule.newBuilder()
.setWorkflowTypes(TestWorkflowImpl.class)
.setActivityImplementations(activitiesImpl)
.build();

@Test
public void getRootWorkflowExecution() {
TestWorkflows.TestWorkflowReturnString workflowStub =
testWorkflowRule.newWorkflowStubTimeoutOptions(
TestWorkflows.TestWorkflowReturnString.class);
String workflowId = workflowStub.execute();
assertEquals(
"empty " + WorkflowStub.fromTyped(workflowStub).getExecution().getWorkflowId(), workflowId);
}

public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString {

@Override
public String execute() {
String result = Workflow.getInfo().getRootWorkflowId().orElse("empty");
if (!Workflow.getInfo().getParentWorkflowId().isPresent()) {
result += " ";
result +=
Workflow.newChildWorkflowStub(TestWorkflows.TestWorkflowReturnString.class).execute();
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1351,9 +1351,11 @@ private static void startWorkflow(
ExecutionId parentExecutionId = parent.get().getExecutionId();
a.setParentWorkflowNamespace(parentExecutionId.getNamespace());
a.setParentWorkflowExecution(parentExecutionId.getExecution());
// This mimics the real server behaviour where the root execution is only set in history
// if the workflow has a parent.
ExecutionId rootExecutionId = ctx.getWorkflowMutableState().getRoot().getExecutionId();
a.setRootWorkflowExecution(rootExecutionId.getExecution());
}
ExecutionId rootExecutionId = ctx.getWorkflowMutableState().getRoot().getExecutionId();
a.setRootWorkflowExecution(rootExecutionId.getExecution());
HistoryEvent.Builder event =
HistoryEvent.newBuilder()
.setEventType(EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED)
Expand Down
Loading