Skip to content

Commit ccc9dea

Browse files
emrahsmeiliang86
andauthored
Add parent workflow ID and run ID to WorkflowInfo (#442) (#462)
* Add parent workflow ID and run ID to WorkflowInfo (#442) Prior to this change, child workflows didn't have a way to retrieve the workflow ID and run ID of the parent workflow. This change adds getters to the WorkflowInfo interface to expose the information that exists in the WorkflowExecutionStartedEvent. Resolves: #442 * Fix getParentRunId to actually return runId Co-authored-by: Liang Mei <[email protected]>
1 parent 365724a commit ccc9dea

File tree

10 files changed

+89
-8
lines changed

10 files changed

+89
-8
lines changed

src/main/java/com/uber/cadence/internal/replay/DecisionContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public interface DecisionContext extends ReplayAware {
4343

4444
WorkflowExecution getWorkflowExecution();
4545

46-
// TODO: Add to Cadence
47-
// com.uber.cadence.WorkflowExecution getParentWorkflowExecution();
46+
WorkflowExecution getParentWorkflowExecution();
4847

4948
WorkflowType getWorkflowType();
5049

src/main/java/com/uber/cadence/internal/replay/DecisionContextImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public WorkflowExecution getWorkflowExecution() {
106106
return workflowContext.getWorkflowExecution();
107107
}
108108

109+
@Override
110+
public WorkflowExecution getParentWorkflowExecution() {
111+
return workflowContext.getParentWorkflowExecution();
112+
}
113+
109114
@Override
110115
public WorkflowType getWorkflowType() {
111116
return workflowContext.getWorkflowType();

src/main/java/com/uber/cadence/internal/replay/WorkflowContext.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ void setContinueAsNewOnCompletion(ContinueAsNewWorkflowExecutionParameters conti
9090
}
9191

9292
// TODO: Implement as soon as WorkflowExecutionStartedEventAttributes have these fields added.
93-
//// WorkflowExecution getParentWorkflowExecution() {
94-
// WorkflowExecutionStartedEventAttributes attributes =
95-
// getWorkflowStartedEventAttributes();
96-
// return attributes.getParentWorkflowExecution();
97-
// }
98-
9993
//// com.uber.cadence.ChildPolicy getChildPolicy() {
10094
// WorkflowExecutionStartedEventAttributes attributes =
10195
// getWorkflowStartedEventAttributes();
@@ -108,6 +102,11 @@ void setContinueAsNewOnCompletion(ContinueAsNewWorkflowExecutionParameters conti
108102
// return attributes.getContinuedExecutionRunId();
109103
// }
110104

105+
WorkflowExecution getParentWorkflowExecution() {
106+
WorkflowExecutionStartedEventAttributes attributes = getWorkflowStartedEventAttributes();
107+
return attributes.getParentWorkflowExecution();
108+
}
109+
111110
int getExecutionStartToCloseTimeoutSeconds() {
112111
WorkflowExecutionStartedEventAttributes attributes = getWorkflowStartedEventAttributes();
113112
return attributes.getExecutionStartToCloseTimeoutSeconds();

src/main/java/com/uber/cadence/internal/sync/DeterministicRunnerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ public WorkflowExecution getWorkflowExecution() {
524524
throw new UnsupportedOperationException("not implemented");
525525
}
526526

527+
@Override
528+
public WorkflowExecution getParentWorkflowExecution() {
529+
throw new UnsupportedOperationException("not implemented");
530+
}
531+
527532
@Override
528533
public WorkflowType getWorkflowType() {
529534
return new WorkflowType().setName("dummy-workflow");

src/main/java/com/uber/cadence/internal/sync/WorkflowInfoImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.internal.sync;
1919

2020
import com.uber.cadence.SearchAttributes;
21+
import com.uber.cadence.WorkflowExecution;
2122
import com.uber.cadence.internal.replay.DecisionContext;
2223
import com.uber.cadence.workflow.WorkflowInfo;
2324
import java.time.Duration;
@@ -64,4 +65,16 @@ public Duration getExecutionStartToCloseTimeout() {
6465
public SearchAttributes getSearchAttributes() {
6566
return context.getSearchAttributes();
6667
}
68+
69+
@Override
70+
public String getParentWorkflowId() {
71+
WorkflowExecution parentWorkflowExecution = context.getParentWorkflowExecution();
72+
return parentWorkflowExecution == null ? null : parentWorkflowExecution.getWorkflowId();
73+
}
74+
75+
@Override
76+
public String getParentRunId() {
77+
WorkflowExecution parentWorkflowExecution = context.getParentWorkflowExecution();
78+
return parentWorkflowExecution == null ? null : parentWorkflowExecution.getRunId();
79+
}
6780
}

src/main/java/com/uber/cadence/internal/testservice/StateMachines.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ private static void startWorkflow(
553553
a.setMemo(request.getMemo());
554554
a.setSearchAttributes((request.getSearchAttributes()));
555555
a.setHeader(request.getHeader());
556+
Optional<TestWorkflowMutableState> parent = ctx.getWorkflowMutableState().getParent();
557+
if (parent.isPresent()) {
558+
ExecutionId parentExecutionId = parent.get().getExecutionId();
559+
a.setParentWorkflowDomain(parentExecutionId.getDomain());
560+
a.setParentWorkflowExecution(parentExecutionId.getExecution());
561+
}
556562
HistoryEvent event =
557563
new HistoryEvent()
558564
.setEventType(EventType.WorkflowExecutionStarted)

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowMutableState.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,6 @@ void completeQuery(QueryId queryId, RespondQueryTaskCompletedRequest completeReq
139139
throws EntityNotExistsError;
140140

141141
StickyExecutionAttributes getStickyExecutionAttributes();
142+
143+
Optional<TestWorkflowMutableState> getParent();
142144
}

src/main/java/com/uber/cadence/internal/testservice/TestWorkflowMutableStateImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ public StickyExecutionAttributes getStickyExecutionAttributes() {
280280
return stickyExecutionAttributes;
281281
}
282282

283+
@Override
284+
public Optional<TestWorkflowMutableState> getParent() {
285+
return parent;
286+
}
287+
283288
@Override
284289
public void startDecisionTask(
285290
PollForDecisionTaskResponse task, PollForDecisionTaskRequest pollRequest)

src/main/java/com/uber/cadence/workflow/WorkflowInfo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public interface WorkflowInfo {
3535
Duration getExecutionStartToCloseTimeout();
3636

3737
SearchAttributes getSearchAttributes();
38+
39+
String getParentWorkflowId();
40+
41+
String getParentRunId();
3842
}

src/test/java/com/uber/cadence/workflow/WorkflowTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5387,6 +5387,49 @@ public void testUpsertSearchAttributes() {
53875387
tracer.setExpected("upsertSearchAttributes", "executeActivity TestActivities::activity");
53885388
}
53895389

5390+
public static class TestMultiargsWorkflowsFuncChild implements TestMultiargsWorkflowsFunc2 {
5391+
@Override
5392+
public String func2(String s, int i) {
5393+
WorkflowInfo wi = Workflow.getWorkflowInfo();
5394+
String parentId = wi.getParentWorkflowId();
5395+
return parentId;
5396+
}
5397+
}
5398+
5399+
public static class TestMultiargsWorkflowsFuncParent implements TestMultiargsWorkflowsFunc {
5400+
@Override
5401+
public String func() {
5402+
ChildWorkflowOptions workflowOptions =
5403+
new ChildWorkflowOptions.Builder()
5404+
.setExecutionStartToCloseTimeout(Duration.ofSeconds(100))
5405+
.setTaskStartToCloseTimeout(Duration.ofSeconds(60))
5406+
.build();
5407+
TestMultiargsWorkflowsFunc2 child =
5408+
Workflow.newChildWorkflowStub(TestMultiargsWorkflowsFunc2.class, workflowOptions);
5409+
5410+
String parentWorkflowId = Workflow.getWorkflowInfo().getParentWorkflowId();
5411+
String childsParentWorkflowId = child.func2(null, 0);
5412+
5413+
String result = String.format("%s - %s", parentWorkflowId, childsParentWorkflowId);
5414+
return result;
5415+
}
5416+
}
5417+
5418+
@Test
5419+
public void testParentWorkflowInfoInChildWorkflows() {
5420+
startWorkerFor(TestMultiargsWorkflowsFuncParent.class, TestMultiargsWorkflowsFuncChild.class);
5421+
5422+
String workflowId = "testParentWorkflowInfoInChildWorkflows";
5423+
WorkflowOptions workflowOptions =
5424+
newWorkflowOptionsBuilder(taskList).setWorkflowId(workflowId).build();
5425+
TestMultiargsWorkflowsFunc parent =
5426+
workflowClient.newWorkflowStub(TestMultiargsWorkflowsFunc.class, workflowOptions);
5427+
5428+
String result = parent.func();
5429+
String expected = String.format("%s - %s", null, workflowId);
5430+
assertEquals(expected, result);
5431+
}
5432+
53905433
private static class TracingWorkflowInterceptor implements WorkflowInterceptor {
53915434

53925435
private final FilteredTrace trace;

0 commit comments

Comments
 (0)