Skip to content

Commit 91e8b8a

Browse files
authored
Adding attempt to workflow info (#210)
* Adding attempt to workflow info * Set attempt to 1 for start workflow event in test service * Check attempt number upon retry
1 parent 27ba155 commit 91e8b8a

File tree

8 files changed

+54
-1
lines changed

8 files changed

+54
-1
lines changed

src/main/java/io/temporal/internal/replay/ReplayWorkflowContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,6 @@ void getVersion(
262262

263263
/** Updates or inserts search attributes used to index workflows. */
264264
void upsertSearchAttributes(SearchAttributes searchAttributes);
265+
266+
int getAttempt();
265267
}

src/main/java/io/temporal/internal/replay/ReplayWorkflowContextImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,9 @@ public void upsertSearchAttributes(SearchAttributes searchAttributes) {
339339
workflowStateMachines.upsertSearchAttributes(searchAttributes);
340340
workflowContext.mergeSearchAttributes(searchAttributes);
341341
}
342+
343+
@Override
344+
public int getAttempt() {
345+
return workflowContext.getAttempt();
346+
}
342347
}

src/main/java/io/temporal/internal/replay/WorkflowContext.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ SearchAttributes getSearchAttributes() {
151151
: searchAttributes.build();
152152
}
153153

154+
int getAttempt() {
155+
return startedAttributes.getAttempt();
156+
}
157+
154158
public List<ContextPropagator> getContextPropagators() {
155159
return contextPropagators;
156160
}

src/main/java/io/temporal/internal/sync/DeterministicRunnerImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,5 +738,10 @@ public UUID randomUUID() {
738738
public void upsertSearchAttributes(SearchAttributes searchAttributes) {
739739
throw new UnsupportedOperationException("not implemented");
740740
}
741+
742+
@Override
743+
public int getAttempt() {
744+
return 1;
745+
}
741746
}
742747
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,9 @@ public Optional<String> getParentRunId() {
9999
? Optional.empty()
100100
: Optional.of(parentWorkflowExecution.getRunId());
101101
}
102+
103+
@Override
104+
public int getAttempt() {
105+
return context.getAttempt();
106+
}
102107
}

src/main/java/io/temporal/internal/testservice/StateMachines.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,8 @@ private static void startWorkflow(
818818
.setWorkflowExecutionTimeout(request.getWorkflowExecutionTimeout())
819819
.setIdentity(request.getIdentity())
820820
.setInput(request.getInput())
821-
.setTaskQueue(request.getTaskQueue());
821+
.setTaskQueue(request.getTaskQueue())
822+
.setAttempt(1);
822823
if (data.retryState.isPresent()) {
823824
a.setAttempt(data.retryState.get().getAttempt());
824825
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ public interface WorkflowInfo {
5252
Optional<String> getParentWorkflowId();
5353

5454
Optional<String> getParentRunId();
55+
56+
int getAttempt();
5557
}

src/test/java/io/temporal/workflow/WorkflowTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,6 +3996,8 @@ public String execute(String testName) {
39963996
count = new AtomicInteger();
39973997
retryCount.put(testName, count);
39983998
}
3999+
int attempt = Workflow.getInfo().getAttempt();
4000+
assertEquals(count.get() + 1, attempt);
39994001
throw ApplicationFailure.newFailure("simulated " + count.incrementAndGet(), "test");
40004002
}
40014003
}
@@ -4671,6 +4673,13 @@ public interface TestMultiargsWorkflowsProc6 extends ProcInvocationQueryable {
46714673
void proc6(String a1, int a2, int a3, int a4, int a5, int a6);
46724674
}
46734675

4676+
@WorkflowInterface
4677+
public interface TestGetAttemptWorkflowsFunc {
4678+
4679+
@WorkflowMethod
4680+
int func();
4681+
}
4682+
46744683
public static class TestMultiargsWorkflowsImpl
46754684
implements TestMultiargsWorkflowsFunc,
46764685
TestMultiargsWorkflowsFunc1,
@@ -6495,6 +6504,14 @@ public String func2(String s, int i) {
64956504
}
64966505
}
64976506

6507+
public static class TestAttemptReturningWorkflowFunc implements TestGetAttemptWorkflowsFunc {
6508+
@Override
6509+
public int func() {
6510+
WorkflowInfo wi = Workflow.getInfo();
6511+
return wi.getAttempt();
6512+
}
6513+
}
6514+
64986515
public static class TestMultiargsWorkflowsFuncParent implements TestMultiargsWorkflowsFunc {
64996516
@Override
65006517
public String func() {
@@ -6530,6 +6547,18 @@ public void testParentWorkflowInfoInChildWorkflows() {
65306547
assertEquals(expected, result);
65316548
}
65326549

6550+
@Test
6551+
public void testGetAttemptFromWorkflowInfo() {
6552+
startWorkerFor(TestMultiargsWorkflowsFuncParent.class, TestAttemptReturningWorkflowFunc.class);
6553+
String workflowId = "testGetAttemptWorkflow";
6554+
WorkflowOptions workflowOptions =
6555+
newWorkflowOptionsBuilder(taskQueue).setWorkflowId(workflowId).build();
6556+
TestGetAttemptWorkflowsFunc workflow =
6557+
workflowClient.newWorkflowStub(TestGetAttemptWorkflowsFunc.class, workflowOptions);
6558+
int attempt = workflow.func();
6559+
assertEquals(1, attempt);
6560+
}
6561+
65336562
public interface WorkflowBase {
65346563
@WorkflowMethod
65356564
String execute(String arg);

0 commit comments

Comments
 (0)