Skip to content

Commit 746d3c3

Browse files
Issue #2057: parsing workflow id from WorkflowExecutionStartedEventAttributes (#2542)
ssue #2057: parsing workflow id from WorkflowExecutionStartedEventAttributes Signed-off-by: Dmytro Danilenkov <[email protected]>
1 parent 402392c commit 746d3c3

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

temporal-sdk/src/main/java/io/temporal/common/WorkflowExecutionHistory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ public WorkflowExecutionHistory(History history, String workflowId) {
4040
* @return WorkflowExecutionHistory
4141
*/
4242
public static WorkflowExecutionHistory fromJson(String serialized) {
43-
return fromJson(serialized, DEFAULT_WORKFLOW_ID);
43+
String protoJson = HistoryJsonUtils.historyFormatJsonToProtoJson(serialized);
44+
45+
JsonFormat.Parser parser = JsonFormat.parser().ignoringUnknownFields();
46+
History.Builder historyBuilder = History.newBuilder();
47+
try {
48+
parser.merge(protoJson, historyBuilder);
49+
} catch (InvalidProtocolBufferException e) {
50+
throw new DataConverterException(e);
51+
}
52+
History history = historyBuilder.build();
53+
String workflowId =
54+
io.temporal.internal.common.WorkflowExecutionHistory.extractWorkflowId(history);
55+
return new WorkflowExecutionHistory(history, workflowId);
4456
}
4557

4658
/**

temporal-sdk/src/main/java/io/temporal/internal/common/WorkflowExecutionHistory.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class WorkflowExecutionHistory {
3535
private final String workflowId;
3636

3737
public WorkflowExecutionHistory(History history) {
38-
this(history, DEFAULT_WORKFLOW_ID);
38+
this(history, extractWorkflowId(history));
3939
}
4040

4141
public WorkflowExecutionHistory(History history, String workflowId) {
@@ -44,9 +44,17 @@ public WorkflowExecutionHistory(History history, String workflowId) {
4444
this.workflowId = workflowId;
4545
}
4646

47+
public static String extractWorkflowId(History history) {
48+
HistoryEvent startedEvent = history.getEvents(0);
49+
String id = startedEvent.getWorkflowExecutionStartedEventAttributes().getWorkflowId();
50+
return id.isEmpty() ? DEFAULT_WORKFLOW_ID : id;
51+
}
52+
4753
public static WorkflowExecutionHistory fromJson(String serialized) {
48-
return new WorkflowExecutionHistory(
49-
io.temporal.common.WorkflowExecutionHistory.fromJson(serialized).getHistory());
54+
io.temporal.common.WorkflowExecutionHistory parsed =
55+
io.temporal.common.WorkflowExecutionHistory.fromJson(serialized);
56+
History history = parsed.getHistory();
57+
return new WorkflowExecutionHistory(history, extractWorkflowId(history));
5058
}
5159

5260
private static void checkHistory(History history) {

temporal-sdk/src/test/java/io/temporal/internal/common/WorkflowExecutionHistoryTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public void deserializeAndSerializeBackComplexHistory() throws IOException {
4141
deserializeAndSerializeBack("complexHistory1.json");
4242
}
4343

44+
@Test
45+
public void workflowIdIsExtractedWhenPresent() throws IOException {
46+
WorkflowExecutionHistory history =
47+
WorkflowHistoryLoader.readHistoryFromResource("simpleHistory_withWorkflowId.json");
48+
assertEquals(
49+
"ff28c127-56ff-416f-8630-53fa4f4cf79a", history.getWorkflowExecution().getWorkflowId());
50+
}
51+
4452
public void deserializeAndSerializeBack(String resourceName) throws IOException {
4553
// Load legacy-format history
4654
ClassLoader classLoader = WorkflowExecutionUtils.class.getClassLoader();

temporal-sdk/src/test/java/io/temporal/workflow/activityTests/LocalActivityAfterCancelTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.temporal.workflow.activityTests;
22

3-
import static org.junit.Assert.assertThrows;
4-
53
import io.temporal.activity.LocalActivityOptions;
64
import io.temporal.api.enums.v1.EventType;
75
import io.temporal.api.enums.v1.ParentClosePolicy;
@@ -44,13 +42,10 @@ public void localActivityAfterChildWorkflowCanceled() {
4442
}
4543

4644
@Test
47-
public void testLocalActivityAfterChildWorkflowCanceledReplay() {
48-
assertThrows(
49-
RuntimeException.class,
50-
() ->
51-
WorkflowReplayer.replayWorkflowExecutionFromResource(
52-
"testLocalActivityAfterCancelTest.json",
53-
LocalActivityAfterCancelTest.TestLocalActivityRetry.class));
45+
public void testLocalActivityAfterChildWorkflowCanceledReplay() throws Exception {
46+
WorkflowReplayer.replayWorkflowExecutionFromResource(
47+
"testLocalActivityAfterCancelTest.json",
48+
LocalActivityAfterCancelTest.TestLocalActivityRetry.class);
5449
}
5550

5651
@WorkflowInterface
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"events": [
3+
{
4+
"eventId": "1",
5+
"eventTime": "2020-07-30T00:30:03.082421843Z",
6+
"eventType": "WorkflowExecutionStarted",
7+
"version": "-24",
8+
"taskId": "5242897",
9+
"workflowExecutionStartedEventAttributes": {
10+
"workflowType": {
11+
"name": "SomeName"
12+
},
13+
"taskQueue": {
14+
"name": "SomeQueueName",
15+
"kind": "Normal"
16+
},
17+
"workflowExecutionTimeout": "300s",
18+
"workflowTaskTimeout": "60s",
19+
"originalExecutionRunId": "1fd5d4c8-1590-4a0a-8027-535e8729de8e",
20+
"firstExecutionRunId": "1fd5d4c8-1590-4a0a-8027-535e8729de8e",
21+
"attempt": 1,
22+
"firstWorkflowTaskBackoff": "0s",
23+
"workflowId": "ff28c127-56ff-416f-8630-53fa4f4cf79a"
24+
}
25+
}
26+
]
27+
}

0 commit comments

Comments
 (0)