Skip to content

Commit 4c63abd

Browse files
kaibocaiartursouza
andauthored
Add determinstic UUID generation (#947)
* add determinstic UUID generation Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> * add unit test to improve coverage Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> * update grpc version to 1.59.0 to be compatible updated durabletask-java Signed-off-by: kaibocai <kaibocai@microsoft.com> --------- Signed-off-by: kaibocai <89094811+kaibocai@users.noreply.github.com> Signed-off-by: kaibocai <kaibocai@microsoft.com> Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
1 parent 1bd2c22 commit 4c63abd

File tree

9 files changed

+133
-6
lines changed

9 files changed

+133
-6
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<properties>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<grpc.version>1.42.1</grpc.version>
17+
<grpc.version>1.59.0</grpc.version>
1818
<protobuf.version>3.17.3</protobuf.version>
1919
<dapr.proto.baseurl>https://raw.githubusercontent.com/dapr/dapr/v1.12.0-rc.2/dapr/proto</dapr.proto.baseurl>
2020
<dapr.sdk-workflows.version>0.11.0-SNAPSHOT</dapr.sdk-workflows.version>

sdk-actors/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<properties>
2020
<maven.deploy.skip>false</maven.deploy.skip>
21-
<grpc.version>1.42.1</grpc.version>
21+
<grpc.version>1.59.0</grpc.version>
2222
</properties>
2323

2424
<dependencies>

sdk-autogen/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<protobuf.output.directory>${project.build.directory}/generated-sources</protobuf.output.directory>
2121
<protobuf.input.directory>${project.build.directory}/proto</protobuf.input.directory>
2222
<maven.deploy.skip>false</maven.deploy.skip>
23-
<grpc.version>1.42.1</grpc.version>
23+
<grpc.version>1.59.0</grpc.version>
2424
</properties>
2525

2626
<dependencies>

sdk-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<dapr.sdk.version>1.11.0-SNAPSHOT</dapr.sdk.version>
1919
<protobuf.output.directory>${project.build.directory}/generated-sources</protobuf.output.directory>
2020
<protobuf.input.directory>${project.basedir}/proto</protobuf.input.directory>
21-
<grpc.version>1.42.1</grpc.version>
21+
<grpc.version>1.59.0</grpc.version>
2222
<protobuf.version>3.17.3</protobuf.version>
2323
<opentelemetry.version>0.14.0</opentelemetry.version>
2424
<spring-boot.version>2.7.8</spring-boot.version>

sdk-workflows/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<dependency>
5252
<groupId>com.microsoft</groupId>
5353
<artifactId>durabletask-client</artifactId>
54-
<version>1.1.1</version>
54+
<version>1.5.0</version>
5555
</dependency>
5656
<!--
5757
manually declare durabletask-client's jackson dependencies

sdk-workflows/src/main/java/io/dapr/workflows/DaprWorkflowContextImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.Duration;
2828
import java.time.Instant;
2929
import java.util.List;
30+
import java.util.UUID;
3031

3132
public class DaprWorkflowContextImpl implements WorkflowContext {
3233
private final TaskOrchestrationContext innerContext;
@@ -204,4 +205,12 @@ public void continueAsNew(Object input) {
204205
public void continueAsNew(Object input, boolean preserveUnprocessedEvents) {
205206
this.innerContext.continueAsNew(input, preserveUnprocessedEvents);
206207
}
208+
209+
/**
210+
* {@inheritDoc}
211+
*/
212+
@Override
213+
public UUID newUuid() {
214+
return this.innerContext.newUUID();
215+
}
207216
}

sdk-workflows/src/main/java/io/dapr/workflows/WorkflowContext.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.time.ZonedDateTime;
2828
import java.util.Arrays;
2929
import java.util.List;
30+
import java.util.UUID;
3031

3132
/**
3233
* Context object used by workflow implementations to perform actions such as scheduling activities,
@@ -514,4 +515,19 @@ default void continueAsNew(Object input) {
514515
* history, otherwise {@code false}
515516
*/
516517
void continueAsNew(Object input, boolean preserveUnprocessedEvents);
518+
519+
/**
520+
* Create a new UUID that is safe for replay within a workflow.
521+
*
522+
* <p>
523+
* The default implementation of this method creates a name-based UUID
524+
* using the algorithm from RFC 4122 §4.3. The name input used to generate
525+
* this value is a combination of the workflow instance ID and an
526+
* internally managed sequence number.
527+
*</p>
528+
* @return a deterministic UUID
529+
*/
530+
default UUID newUuid() {
531+
throw new RuntimeException("No implementation found.");
532+
}
517533
}

sdk-workflows/src/test/java/io/dapr/workflows/DaprWorkflowContextImplTest.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313

1414
package io.dapr.workflows;
1515

16+
import com.microsoft.durabletask.CompositeTaskFailedException;
1617
import com.microsoft.durabletask.RetryPolicy;
1718
import com.microsoft.durabletask.Task;
19+
import com.microsoft.durabletask.TaskCanceledException;
1820
import com.microsoft.durabletask.TaskOptions;
1921
import com.microsoft.durabletask.TaskOrchestrationContext;
2022

23+
import org.jetbrains.annotations.Nullable;
2124
import org.junit.jupiter.api.BeforeEach;
2225
import org.junit.jupiter.api.Test;
2326
import org.slf4j.Logger;
2427

2528
import java.time.Duration;
29+
import java.time.Instant;
2630
import java.time.ZonedDateTime;
2731
import java.util.Arrays;
2832
import java.util.List;
2933

34+
import static org.junit.jupiter.api.Assertions.assertEquals;
3035
import static org.mockito.ArgumentMatchers.any;
3136
import static org.mockito.Mockito.mock;
3237
import static org.mockito.Mockito.times;
@@ -37,11 +42,95 @@
3742
public class DaprWorkflowContextImplTest {
3843
private DaprWorkflowContextImpl context;
3944
private TaskOrchestrationContext mockInnerContext;
45+
private WorkflowContext testWorkflowContext;
4046

4147
@BeforeEach
4248
public void setUp() {
4349
mockInnerContext = mock(TaskOrchestrationContext.class);
4450
context = new DaprWorkflowContextImpl(mockInnerContext);
51+
testWorkflowContext = new WorkflowContext() {
52+
@Override
53+
public Logger getLogger() {
54+
return null;
55+
}
56+
57+
@Override
58+
public String getName() {
59+
return null;
60+
}
61+
62+
@Override
63+
public String getInstanceId() {
64+
return null;
65+
}
66+
67+
@Override
68+
public Instant getCurrentInstant() {
69+
return null;
70+
}
71+
72+
@Override
73+
public void complete(Object output) {
74+
75+
}
76+
77+
@Override
78+
public <V> Task<V> waitForExternalEvent(String name, Duration timeout, Class<V> dataType)
79+
throws TaskCanceledException {
80+
return null;
81+
}
82+
83+
@Override
84+
public <V> Task<Void> waitForExternalEvent(String name, Duration timeout) throws TaskCanceledException {
85+
return null;
86+
}
87+
88+
@Override
89+
public <V> Task<Void> waitForExternalEvent(String name) throws TaskCanceledException {
90+
return null;
91+
}
92+
93+
@Override
94+
public <V> Task<V> callActivity(String name, Object input, TaskOptions options, Class<V> returnType) {
95+
return null;
96+
}
97+
98+
@Override
99+
public boolean isReplaying() {
100+
return false;
101+
}
102+
103+
@Override
104+
public <V> Task<List<V>> allOf(List<Task<V>> tasks) throws CompositeTaskFailedException {
105+
return null;
106+
}
107+
108+
@Override
109+
public Task<Task<?>> anyOf(List<Task<?>> tasks) {
110+
return null;
111+
}
112+
113+
@Override
114+
public Task<Void> createTimer(Duration duration) {
115+
return null;
116+
}
117+
118+
@Override
119+
public <V> V getInput(Class<V> targetType) {
120+
return null;
121+
}
122+
123+
@Override
124+
public <V> Task<V> callSubWorkflow(String name, @Nullable Object input, @Nullable String instanceID,
125+
@Nullable TaskOptions options, Class<V> returnType) {
126+
return null;
127+
}
128+
129+
@Override
130+
public void continueAsNew(Object input, boolean preserveUnprocessedEvents) {
131+
132+
}
133+
};
45134
}
46135

47136
@Test
@@ -207,4 +296,17 @@ public void callSubWorkflow() {
207296
context.callSubWorkflow(expectedName, expectedInput, String.class);
208297
verify(mockInnerContext, times(1)).callSubOrchestrator(expectedName, expectedInput, null, null, String.class);
209298
}
299+
300+
@Test
301+
public void newUuidTest() {
302+
context.newUuid();
303+
verify(mockInnerContext, times(1)).newUUID();
304+
}
305+
306+
@Test
307+
public void newUuidTestNoImplementationExceptionTest() {
308+
RuntimeException runtimeException = assertThrows(RuntimeException.class, testWorkflowContext::newUuid);
309+
String expectedMessage = "No implementation found.";
310+
assertEquals(expectedMessage, runtimeException.getMessage());
311+
}
210312
}

sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<properties>
2020
<maven.deploy.skip>false</maven.deploy.skip>
21-
<grpc.version>1.42.1</grpc.version>
21+
<grpc.version>1.59.0</grpc.version>
2222
<argLine>
2323
--add-opens java.base/java.util=ALL-UNNAMED
2424
</argLine>

0 commit comments

Comments
 (0)