Skip to content

Commit 2c5c5a3

Browse files
committed
builder pattern + use latest protobuf commit
Signed-off-by: Cassandra Coyle <[email protected]>
1 parent 216be0c commit 2c5c5a3

File tree

13 files changed

+176
-62
lines changed

13 files changed

+176
-62
lines changed

client/build.gradle

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ dependencies {
4545

4646
// Netty dependencies for TLS
4747
implementation "io.grpc:grpc-netty-shaded:${grpcVersion}"
48-
implementation "io.netty:netty-handler:4.1.94.Final"
48+
implementation 'io.netty:netty-handler:4.1.119.Final'
4949
implementation "io.netty:netty-tcnative-boringssl-static:2.0.59.Final"
5050

5151
// Add Netty dependencies to test classpath
5252
testImplementation "io.grpc:grpc-netty-shaded:${grpcVersion}"
53-
testImplementation "io.netty:netty-handler:4.1.94.Final"
53+
testImplementation 'io.netty:netty-handler:4.1.119.Final'
5454
testImplementation "io.netty:netty-tcnative-boringssl-static:2.0.59.Final"
5555

56-
testImplementation 'org.bouncycastle:bcprov-jdk15on:1.70'
57-
testImplementation 'org.bouncycastle:bcpkix-jdk15on:1.70'
56+
testImplementation 'org.bouncycastle:bcprov-jdk15on:1.78'
5857
}
5958

6059
compileJava {

client/src/main/java/io/dapr/durabletask/TaskOptions.java

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,49 +10,53 @@ public final class TaskOptions {
1010
private final RetryHandler retryHandler;
1111
private final String appID;
1212

13-
public TaskOptions(RetryPolicy retryPolicy, RetryHandler retryHandler, String appID) {
14-
this.retryPolicy = retryPolicy;
15-
this.retryHandler = retryHandler;
16-
this.appID = appID;
13+
private TaskOptions(Builder builder) {
14+
this.retryPolicy = builder.retryPolicy;
15+
this.retryHandler = builder.retryHandler;
16+
this.appID = builder.appID;
1717
}
1818

19-
public TaskOptions(RetryPolicy retryPolicy, RetryHandler retryHandler) {
20-
this.retryPolicy = retryPolicy;
21-
this.retryHandler = retryHandler;
22-
this.appID = null;
19+
/**
20+
* Creates a new builder for {@code TaskOptions}.
21+
* @return a new builder instance
22+
*/
23+
public static Builder builder() {
24+
return new Builder();
25+
}
26+
27+
/**
28+
* Creates a new {@code TaskOptions} object with default values.
29+
* @return a new TaskOptions instance with no configuration
30+
*/
31+
public static TaskOptions create() {
32+
return new Builder().build();
2333
}
2434

2535
/**
2636
* Creates a new {@code TaskOptions} object from a {@link RetryPolicy}.
2737
* @param retryPolicy the retry policy to use in the new {@code TaskOptions} object.
38+
* @return a new TaskOptions instance with the specified retry policy
2839
*/
29-
public TaskOptions(RetryPolicy retryPolicy) {
30-
this(retryPolicy, null, null);
40+
public static TaskOptions withRetryPolicy(RetryPolicy retryPolicy) {
41+
return new Builder().retryPolicy(retryPolicy).build();
3142
}
3243

3344
/**
3445
* Creates a new {@code TaskOptions} object from a {@link RetryHandler}.
3546
* @param retryHandler the retry handler to use in the new {@code TaskOptions} object.
47+
* @return a new TaskOptions instance with the specified retry handler
3648
*/
37-
public TaskOptions(RetryHandler retryHandler) {
38-
this(null, retryHandler, null);
49+
public static TaskOptions withRetryHandler(RetryHandler retryHandler) {
50+
return new Builder().retryHandler(retryHandler).build();
3951
}
4052

4153
/**
4254
* Creates a new {@code TaskOptions} object with the specified app ID.
4355
* @param appID the app ID to use for cross-app workflow routing
56+
* @return a new TaskOptions instance with the specified app ID
4457
*/
45-
public TaskOptions(String appID) {
46-
this(null, null, appID);
47-
}
48-
49-
/**
50-
* Creates a new {@code TaskOptions} object with the specified retry policy and app ID.
51-
* @param retryPolicy the retry policy to use
52-
* @param appID the app ID to use for cross-app workflow routing
53-
*/
54-
public TaskOptions(RetryPolicy retryPolicy, String appID) {
55-
this(retryPolicy, null, appID);
58+
public static TaskOptions withAppID(String appID) {
59+
return new Builder().appID(appID).build();
5660
}
5761

5862
boolean hasRetryPolicy() {
@@ -90,4 +94,55 @@ public String getAppID() {
9094
boolean hasAppID() {
9195
return this.appID != null && !this.appID.isEmpty();
9296
}
97+
98+
/**
99+
* Builder for creating {@code TaskOptions} instances.
100+
*/
101+
public static final class Builder {
102+
private RetryPolicy retryPolicy;
103+
private RetryHandler retryHandler;
104+
private String appID;
105+
106+
private Builder() {
107+
// Private constructor -enforces using TaskOptions.builder()
108+
}
109+
110+
/**
111+
* Sets the retry policy for the task options.
112+
* @param retryPolicy the retry policy to use
113+
* @return this builder instance for method chaining
114+
*/
115+
public Builder retryPolicy(RetryPolicy retryPolicy) {
116+
this.retryPolicy = retryPolicy;
117+
return this;
118+
}
119+
120+
/**
121+
* Sets the retry handler for the task options.
122+
* @param retryHandler the retry handler to use
123+
* @return this builder instance for method chaining
124+
*/
125+
public Builder retryHandler(RetryHandler retryHandler) {
126+
this.retryHandler = retryHandler;
127+
return this;
128+
}
129+
130+
/**
131+
* Sets the app ID for cross-app workflow routing.
132+
* @param appID the app ID to use
133+
* @return this builder instance for method chaining
134+
*/
135+
public Builder appID(String appID) {
136+
this.appID = appID;
137+
return this;
138+
}
139+
140+
/**
141+
* Builds a new {@code TaskOptions} instance with the configured values.
142+
* @return a new TaskOptions instance
143+
*/
144+
public TaskOptions build() {
145+
return new TaskOptions(this);
146+
}
147+
}
93148
}

client/src/main/java/io/dapr/durabletask/TaskOrchestrationExecutor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,12 @@ public <V> Task<V> callActivity(
283283
// Add router information for cross-app routing
284284
// Router always has a source app ID from EXECUTIONSTARTED event
285285
TaskRouter.Builder routerBuilder = TaskRouter.newBuilder()
286-
.setSource(this.appId);
286+
.setSourceAppID(this.appId);
287287

288288
// Add target app ID if specified in options
289289
if (options != null && options.hasAppID()) {
290290
String targetAppId = options.getAppID();
291-
routerBuilder.setTarget(targetAppId);
291+
routerBuilder.setTargetAppID(targetAppId);
292292
this.logger.fine(() -> String.format(
293293
"cross app routing detected: source=%s, target=%s",
294294
this.appId, targetAppId));
@@ -303,10 +303,10 @@ public <V> Task<V> callActivity(
303303
.setId(id)
304304
.setScheduleTask(scheduleTaskBuilder);
305305
TaskRouter.Builder actionRouterBuilder = TaskRouter.newBuilder()
306-
.setSource(this.appId);
306+
.setSourceAppID(this.appId);
307307
if (options != null && options.hasAppID()) {
308308
String targetAppId = options.getAppID();
309-
actionRouterBuilder.setTarget(targetAppId);
309+
actionRouterBuilder.setTargetAppID(targetAppId);
310310
}
311311

312312
actionBuilder.setRouter(actionRouterBuilder.build());
@@ -874,7 +874,7 @@ private void processEvent(HistoryEvent e) {
874874
this.setInput(executionStarted.getInput().getValue());
875875
this.setInstanceId(executionStarted.getOrchestrationInstance().getInstanceId());
876876
this.logger.fine(() -> this.instanceId + ": Workflow execution started");
877-
this.appId = e.getRouter().getSource();
877+
this.appId = e.getRouter().getSourceAppID();
878878

879879
// Create and invoke the workflow orchestrator
880880
TaskOrchestrationFactory factory = TaskOrchestrationExecutor.this.orchestrationFactories.get(executionStarted.getName());

client/src/test/java/io/dapr/durabletask/ErrorHandlingIntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void retryActivityFailures(int maxNumberOfAttempts) throws TimeoutExcepti
112112
ctx.callActivity(
113113
"BustedActivity",
114114
null,
115-
new TaskOptions(retryPolicy)).await();
115+
TaskOptions.withRetryPolicy(retryPolicy)).await();
116116
});
117117
}
118118

@@ -125,7 +125,7 @@ public void retryActivityFailuresWithCustomLogic(int maxNumberOfAttempts) throws
125125
// Run the test and get back the details of the last failure
126126
this.retryOnFailuresCoreTest(maxNumberOfAttempts, maxNumberOfAttempts, ctx -> {
127127
RetryHandler retryHandler = getCommonRetryHandler(retryHandlerCalls, maxNumberOfAttempts);
128-
TaskOptions options = new TaskOptions(retryHandler);
128+
TaskOptions options = TaskOptions.withRetryHandler(retryHandler);
129129
ctx.callActivity("BustedActivity", null, options).await();
130130
});
131131

@@ -194,7 +194,7 @@ public void retrySubOrchestratorFailures(int maxNumberOfAttempts) throws Timeout
194194
"BustedSubOrchestrator",
195195
null,
196196
null,
197-
new TaskOptions(retryPolicy)).await();
197+
TaskOptions.withRetryPolicy(retryPolicy)).await();
198198
});
199199
}
200200

@@ -207,7 +207,7 @@ public void retrySubOrchestrationFailuresWithCustomLogic(int maxNumberOfAttempts
207207
// Run the test and get back the details of the last failure
208208
this.retryOnFailuresCoreTest(maxNumberOfAttempts, maxNumberOfAttempts, ctx -> {
209209
RetryHandler retryHandler = getCommonRetryHandler(retryHandlerCalls, maxNumberOfAttempts);
210-
TaskOptions options = new TaskOptions(retryHandler);
210+
TaskOptions options = TaskOptions.withRetryHandler(retryHandler);
211211
ctx.callSubOrchestrator("BustedSubOrchestrator", null, null, options).await();
212212
});
213213

client/src/test/java/io/dapr/durabletask/IntegrationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ void activityAllOf() throws IOException, TimeoutException {
13701370
final int activityCount = 10;
13711371
final AtomicBoolean throwException = new AtomicBoolean(true);
13721372
final RetryPolicy retryPolicy = new RetryPolicy(2, Duration.ofSeconds(5));
1373-
final TaskOptions taskOptions = new TaskOptions(retryPolicy);
1373+
final TaskOptions taskOptions = TaskOptions.withRetryPolicy(retryPolicy);
13741374

13751375
DurableTaskGrpcWorker worker = this.createWorkerBuilder()
13761376
.addOrchestrator(orchestratorName, ctx -> {
@@ -1428,7 +1428,7 @@ void activityAllOfException() throws IOException, TimeoutException {
14281428
final String result = "test fail";
14291429
final int activityMiddle = 5;
14301430
final RetryPolicy retryPolicy = new RetryPolicy(2, Duration.ofSeconds(5));
1431-
final TaskOptions taskOptions = new TaskOptions(retryPolicy);
1431+
final TaskOptions taskOptions = TaskOptions.withRetryPolicy(retryPolicy);
14321432

14331433
DurableTaskGrpcWorker worker = this.createWorkerBuilder()
14341434
.addOrchestrator(orchestratorName, ctx -> {
@@ -1491,7 +1491,7 @@ void activityAnyOf() throws IOException, TimeoutException {
14911491
final int activityCount = 10;
14921492
final AtomicBoolean throwException = new AtomicBoolean(true);
14931493
final RetryPolicy retryPolicy = new RetryPolicy(2, Duration.ofSeconds(5));
1494-
final TaskOptions taskOptions = new TaskOptions(retryPolicy);
1494+
final TaskOptions taskOptions = TaskOptions.withRetryPolicy(retryPolicy);
14951495

14961496
DurableTaskGrpcWorker worker = this.createWorkerBuilder()
14971497
.addOrchestrator(orchestratorName, ctx -> {
@@ -1585,7 +1585,7 @@ public void taskExecutionIdTest() {
15851585
var orchestratorName = "test-task-execution-id";
15861586
var retryActivityName = "RetryN";
15871587
final RetryPolicy retryPolicy = new RetryPolicy(4, Duration.ofSeconds(3));
1588-
final TaskOptions taskOptions = new TaskOptions(retryPolicy);
1588+
final TaskOptions taskOptions = TaskOptions.withRetryPolicy(retryPolicy);
15891589

15901590
var execMap = new HashMap<String, Integer>();
15911591

client/src/test/java/io/dapr/durabletask/TaskOptionsTest.java

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class TaskOptionsTest {
2525

2626
@Test
2727
void taskOptionsWithAppID() {
28-
TaskOptions options = new TaskOptions("app1");
28+
TaskOptions options = TaskOptions.withAppID("app1");
2929

3030
assertTrue(options.hasAppID());
3131
assertEquals("app1", options.getAppID());
@@ -36,7 +36,10 @@ void taskOptionsWithAppID() {
3636
@Test
3737
void taskOptionsWithRetryPolicyAndAppID() {
3838
RetryPolicy retryPolicy = new RetryPolicy(3, Duration.ofSeconds(1));
39-
TaskOptions options = new TaskOptions(retryPolicy, null, "app2");
39+
TaskOptions options = TaskOptions.builder()
40+
.retryPolicy(retryPolicy)
41+
.appID("app2")
42+
.build();
4043

4144
assertTrue(options.hasAppID());
4245
assertEquals("app2", options.getAppID());
@@ -53,7 +56,10 @@ public boolean handle(RetryContext context) {
5356
return context.getLastAttemptNumber() < 2;
5457
}
5558
};
56-
TaskOptions options = new TaskOptions(null, retryHandler, "app3");
59+
TaskOptions options = TaskOptions.builder()
60+
.retryHandler(retryHandler)
61+
.appID("app3")
62+
.build();
5763

5864
assertTrue(options.hasAppID());
5965
assertEquals("app3", options.getAppID());
@@ -64,25 +70,76 @@ public boolean handle(RetryContext context) {
6470

6571
@Test
6672
void taskOptionsWithoutAppID() {
67-
TaskOptions options = new TaskOptions(null, null, null);
73+
TaskOptions options = TaskOptions.create();
6874

6975
assertFalse(options.hasAppID());
7076
assertNull(options.getAppID());
7177
}
7278

7379
@Test
7480
void taskOptionsWithEmptyAppID() {
75-
TaskOptions options = new TaskOptions("");
81+
TaskOptions options = TaskOptions.withAppID("");
7682

7783
assertFalse(options.hasAppID());
7884
assertEquals("", options.getAppID());
7985
}
8086

8187
@Test
8288
void taskOptionsWithNullAppID() {
83-
TaskOptions options = new TaskOptions(null, null, null);
89+
TaskOptions options = TaskOptions.builder().appID(null).build();
8490

8591
assertFalse(options.hasAppID());
8692
assertNull(options.getAppID());
8793
}
94+
95+
@Test
96+
void taskOptionsWithRetryPolicy() {
97+
RetryPolicy retryPolicy = new RetryPolicy(5, Duration.ofMinutes(1));
98+
TaskOptions options = TaskOptions.withRetryPolicy(retryPolicy);
99+
100+
assertTrue(options.hasRetryPolicy());
101+
assertEquals(retryPolicy, options.getRetryPolicy());
102+
assertFalse(options.hasRetryHandler());
103+
assertFalse(options.hasAppID());
104+
}
105+
106+
@Test
107+
void taskOptionsWithRetryHandler() {
108+
RetryHandler retryHandler = new RetryHandler() {
109+
@Override
110+
public boolean handle(RetryContext context) {
111+
return context.getLastAttemptNumber() < 3;
112+
}
113+
};
114+
TaskOptions options = TaskOptions.withRetryHandler(retryHandler);
115+
116+
assertTrue(options.hasRetryHandler());
117+
assertEquals(retryHandler, options.getRetryHandler());
118+
assertFalse(options.hasRetryPolicy());
119+
assertFalse(options.hasAppID());
120+
}
121+
122+
@Test
123+
void taskOptionsWithBuilderChaining() {
124+
RetryPolicy retryPolicy = new RetryPolicy(2, Duration.ofSeconds(30));
125+
RetryHandler retryHandler = new RetryHandler() {
126+
@Override
127+
public boolean handle(RetryContext context) {
128+
return context.getLastAttemptNumber() < 1;
129+
}
130+
};
131+
132+
TaskOptions options = TaskOptions.builder()
133+
.retryPolicy(retryPolicy)
134+
.retryHandler(retryHandler)
135+
.appID("test-app")
136+
.build();
137+
138+
assertTrue(options.hasRetryPolicy());
139+
assertEquals(retryPolicy, options.getRetryPolicy());
140+
assertTrue(options.hasRetryHandler());
141+
assertEquals(retryHandler, options.getRetryHandler());
142+
assertTrue(options.hasAppID());
143+
assertEquals("test-app", options.getAppID());
144+
}
88145
}

0 commit comments

Comments
 (0)