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 @@ -262,4 +262,6 @@ void getVersion(

/** Updates or inserts search attributes used to index workflows. */
void upsertSearchAttributes(SearchAttributes searchAttributes);

int getAttempt();
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,9 @@ public void upsertSearchAttributes(SearchAttributes searchAttributes) {
workflowStateMachines.upsertSearchAttributes(searchAttributes);
workflowContext.mergeSearchAttributes(searchAttributes);
}

@Override
public int getAttempt() {
return workflowContext.getAttempt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ SearchAttributes getSearchAttributes() {
: searchAttributes.build();
}

int getAttempt() {
return startedAttributes.getAttempt();
}

public List<ContextPropagator> getContextPropagators() {
return contextPropagators;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,5 +738,10 @@ public UUID randomUUID() {
public void upsertSearchAttributes(SearchAttributes searchAttributes) {
throw new UnsupportedOperationException("not implemented");
}

@Override
public int getAttempt() {
return 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ public Optional<String> getParentRunId() {
? Optional.empty()
: Optional.of(parentWorkflowExecution.getRunId());
}

@Override
public int getAttempt() {
return context.getAttempt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,8 @@ private static void startWorkflow(
.setWorkflowExecutionTimeout(request.getWorkflowExecutionTimeout())
.setIdentity(request.getIdentity())
.setInput(request.getInput())
.setTaskQueue(request.getTaskQueue());
.setTaskQueue(request.getTaskQueue())
.setAttempt(1);
if (data.retryState.isPresent()) {
a.setAttempt(data.retryState.get().getAttempt());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/temporal/workflow/WorkflowInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ public interface WorkflowInfo {
Optional<String> getParentWorkflowId();

Optional<String> getParentRunId();

int getAttempt();
}
29 changes: 29 additions & 0 deletions src/test/java/io/temporal/workflow/WorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3996,6 +3996,8 @@ public String execute(String testName) {
count = new AtomicInteger();
retryCount.put(testName, count);
}
int attempt = Workflow.getInfo().getAttempt();
assertEquals(count.get() + 1, attempt);
throw ApplicationFailure.newFailure("simulated " + count.incrementAndGet(), "test");
}
}
Expand Down Expand Up @@ -4671,6 +4673,13 @@ public interface TestMultiargsWorkflowsProc6 extends ProcInvocationQueryable {
void proc6(String a1, int a2, int a3, int a4, int a5, int a6);
}

@WorkflowInterface
public interface TestGetAttemptWorkflowsFunc {

@WorkflowMethod
int func();
}

public static class TestMultiargsWorkflowsImpl
implements TestMultiargsWorkflowsFunc,
TestMultiargsWorkflowsFunc1,
Expand Down Expand Up @@ -6495,6 +6504,14 @@ public String func2(String s, int i) {
}
}

public static class TestAttemptReturningWorkflowFunc implements TestGetAttemptWorkflowsFunc {
@Override
public int func() {
WorkflowInfo wi = Workflow.getInfo();
return wi.getAttempt();
}
}

public static class TestMultiargsWorkflowsFuncParent implements TestMultiargsWorkflowsFunc {
@Override
public String func() {
Expand Down Expand Up @@ -6530,6 +6547,18 @@ public void testParentWorkflowInfoInChildWorkflows() {
assertEquals(expected, result);
}

@Test
public void testGetAttemptFromWorkflowInfo() {
startWorkerFor(TestMultiargsWorkflowsFuncParent.class, TestAttemptReturningWorkflowFunc.class);
String workflowId = "testGetAttemptWorkflow";
WorkflowOptions workflowOptions =
newWorkflowOptionsBuilder(taskQueue).setWorkflowId(workflowId).build();
TestGetAttemptWorkflowsFunc workflow =
workflowClient.newWorkflowStub(TestGetAttemptWorkflowsFunc.class, workflowOptions);
int attempt = workflow.func();
assertEquals(1, attempt);
}

public interface WorkflowBase {
@WorkflowMethod
String execute(String arg);
Expand Down