Skip to content

Commit 768b4bb

Browse files
Add attempt and firstAttemptTimestamp to request context (#119)
1 parent 95986e4 commit 768b4bb

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ See [samples](https://github.com/indeedeng/iwf-java-samples) for how to use this
99
## Gradle
1010
```gradle
1111
// https://mvnrepository.com/artifact/io.iworkflow/iwf-java-sdk
12-
implementation 'io.iworkflow:iwf-java-sdk:1.2.2'
12+
implementation 'io.iworkflow:iwf-java-sdk:1.2.3'
1313
```
1414
## Maven
1515
```
1616
<!-- https://mvnrepository.com/artifact/io.iworkflow/iwf-java-sdk -->
1717
<dependency>
1818
<groupId>io.iworkflow</groupId>
1919
<artifactId>iwf-java-sdk</artifactId>
20-
<version>1.2.2</version>
20+
<version>1.2.3</version>
2121
<type>pom</type>
2222
</dependency>
2323

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ signing {
150150
}
151151

152152
group = "io.iworkflow"
153-
version = "1.2.2"
153+
version = "1.2.3"
154154

155155
nexusPublishing {
156156
repositories {

iwf-idl

src/main/java/io/iworkflow/core/Context.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public abstract class Context {
1111
public abstract String getWorkflowRunId();
1212

1313
public abstract String getWorkflowId();
14+
15+
// this is the start time of the first attempt of the API call. It's from ScheduledTimestamp of Cadence/Temporal activity.GetInfo
16+
// require server version 1.2.2+, return -1 if server version is lower
17+
public abstract Long getFirstAttemptTimestampSeconds();
18+
19+
// Attempt starts from 1, and increased by 1 for every retry if retry policy is specified. It's from Attempt of Cadence/Temporal activity.GetInfo
20+
// require server version 1.2.2+, return -1 if server version is lower
21+
public abstract int getAttempt();
1422
}

src/main/java/io/iworkflow/core/WorkerService.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ public WorkflowStateStartResponse handleWorkflowStateStart(final WorkflowStateSt
4747
final Object input = workerOptions.getObjectEncoder().decode(stateInput, state.getWorkflowState().getInputType());
4848
final DataObjectsRWImpl dataObjectsRW =
4949
createDataObjectsRW(req.getWorkflowType(), req.getDataObjects());
50-
final Context context = ImmutableContext.builder()
51-
.workflowId(req.getContext().getWorkflowId())
52-
.workflowRunId(req.getContext().getWorkflowRunId())
53-
.workflowStartTimestampSeconds(req.getContext().getWorkflowStartedTimestamp())
54-
.stateExecutionId(req.getContext().getStateExecutionId())
55-
.build();
50+
final Context context = fromIdlContext(req.getContext());
5651
final StateLocalsImpl stateLocals = new StateLocalsImpl(toMap(null), workerOptions.getObjectEncoder());
5752
final Map<String, SearchAttributeValueType> saTypeMap = registry.getSearchAttributeKeyToTypeMap(req.getWorkflowType());
5853
final SearchAttributeRWImpl searchAttributeRW = new SearchAttributeRWImpl(saTypeMap, req.getSearchAttributes());
@@ -113,12 +108,7 @@ public WorkflowStateDecideResponse handleWorkflowStateDecide(final WorkflowState
113108
final DataObjectsRWImpl dataObjectsRW =
114109
createDataObjectsRW(req.getWorkflowType(), req.getDataObjects());
115110

116-
final Context context = ImmutableContext.builder()
117-
.workflowId(req.getContext().getWorkflowId())
118-
.workflowRunId(req.getContext().getWorkflowRunId())
119-
.workflowStartTimestampSeconds(req.getContext().getWorkflowStartedTimestamp())
120-
.stateExecutionId(req.getContext().getStateExecutionId())
121-
.build();
111+
final Context context = fromIdlContext(req.getContext());
122112
final StateLocalsImpl stateLocals = new StateLocalsImpl(toMap(req.getStateLocals()), workerOptions.getObjectEncoder());
123113
final Map<String, SearchAttributeValueType> saTypeMap = registry.getSearchAttributeKeyToTypeMap(req.getWorkflowType());
124114
final SearchAttributeRWImpl searchAttributeRW = new SearchAttributeRWImpl(saTypeMap, req.getSearchAttributes());
@@ -250,4 +240,25 @@ private List<SearchAttribute> createUpsertSearchAttributes(
250240
});
251241
return sas;
252242
}
243+
244+
private Context fromIdlContext(final io.iworkflow.gen.models.Context context) {
245+
int attempt = -1; //unsupported
246+
if (context.getAttempt() != null) {
247+
attempt = context.getAttempt();
248+
}
249+
long firstAttemptTimestamp = -1; //unsupported
250+
if (context.getFirstAttemptTimestamp() != null) {
251+
firstAttemptTimestamp = context.getFirstAttemptTimestamp();
252+
}
253+
254+
return ImmutableContext.builder()
255+
.workflowId(context.getWorkflowId())
256+
.workflowRunId(context.getWorkflowRunId())
257+
.workflowStartTimestampSeconds(context.getWorkflowStartedTimestamp())
258+
.stateExecutionId(context.getStateExecutionId())
259+
.attempt(attempt)
260+
.firstAttemptTimestampSeconds(firstAttemptTimestamp)
261+
.build();
262+
}
253263
}
264+

src/test/java/io/iworkflow/integ/basic/BasicWorkflowState1.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ public Class<Integer> getInputType() {
1717

1818
@Override
1919
public CommandRequest start(final Context context, final Integer input, Persistence persistence, final Communication communication) {
20+
if (context.getAttempt() <= 0) {
21+
throw new RuntimeException("attempt must be greater than zero");
22+
}
23+
if (context.getFirstAttemptTimestampSeconds() <= 0) {
24+
throw new RuntimeException("firstAttemptTimestampSeconds must be greater than zero");
25+
}
2026
return CommandRequest.empty;
2127
}
2228

2329
@Override
2430
public StateDecision decide(final Context context, final Integer input, final CommandResults commandResults, Persistence persistence, final Communication communication) {
31+
if (context.getAttempt() <= 0) {
32+
throw new RuntimeException("attempt must be greater than zero");
33+
}
34+
if (context.getFirstAttemptTimestampSeconds() <= 0) {
35+
throw new RuntimeException("firstAttemptTimestampSeconds must be greater than zero");
36+
}
37+
2538
final int output = input + 1;
2639
return StateDecision.singleNextState(BasicWorkflowState2.class, output);
2740
}

0 commit comments

Comments
 (0)