Skip to content

Commit 3caeb97

Browse files
authored
Refactor options and worker dependencies (#551)
This is the first part of a general options and worker dependencies cleanup. After this refactoring, user should follow the sequence below to setup worker and its dependencies: - IWorkflowService is a client stub for communicating to Cadence Server - WorkflowClient wraps IWorkflowService and provides higher level API to start, signal, query and wait for completion of workflows. - WorkerFactory wraps WorkflowClient and can be used to create instances of Workers. - Worker listens on a single task list and is created by a WorkerFactory.
1 parent 47c9097 commit 3caeb97

24 files changed

+720
-785
lines changed

src/main/java/com/uber/cadence/client/WorkflowClient.java

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -107,77 +107,29 @@ public interface WorkflowClient {
107107
/** Replays workflow to the current state and returns empty result or error if replay failed. */
108108
String QUERY_TYPE_REPLAY_ONLY = "__replay_only";
109109

110-
/**
111-
* Creates worker that connects to the local instance of the Cadence Service that listens on a
112-
* default port (7933).
113-
*
114-
* @param domain domain that worker uses to poll.
115-
*/
116-
static WorkflowClient newInstance(String domain) {
117-
return WorkflowClientInternal.newInstance(domain);
118-
}
119-
120-
/**
121-
* Creates worker that connects to the local instance of the Cadence Service that listens on a
122-
* default port (7933).
123-
*
124-
* @param domain domain that worker uses to poll.
125-
* @param options Options (like {@link com.uber.cadence.converter.DataConverter}er override) for
126-
* configuring client.
127-
*/
128-
static WorkflowClient newInstance(String domain, WorkflowClientOptions options) {
129-
return WorkflowClientInternal.newInstance(domain, options);
130-
}
131-
132-
/**
133-
* Creates client that connects to an instance of the Cadence Service.
134-
*
135-
* @param host of the Cadence Service endpoint
136-
* @param port of the Cadence Service endpoint
137-
* @param domain domain that worker uses to poll.
138-
*/
139-
static WorkflowClient newInstance(String host, int port, String domain) {
140-
return WorkflowClientInternal.newInstance(host, port, domain);
141-
}
142-
143-
/**
144-
* Creates client that connects to an instance of the Cadence Service.
145-
*
146-
* @param host of the Cadence Service endpoint
147-
* @param port of the Cadence Service endpoint
148-
* @param domain domain that worker uses to poll.
149-
* @param options Options (like {@link com.uber.cadence.converter.DataConverter}er override) for
150-
* configuring client.
151-
*/
152-
static WorkflowClient newInstance(
153-
String host, int port, String domain, WorkflowClientOptions options) {
154-
return WorkflowClientInternal.newInstance(host, port, domain, options);
155-
}
156-
157110
/**
158111
* Creates client that connects to an instance of the Cadence Service.
159112
*
160113
* @param service client to the Cadence Service endpoint.
161-
* @param domain domain that worker uses to poll.
162114
*/
163-
static WorkflowClient newInstance(IWorkflowService service, String domain) {
164-
return WorkflowClientInternal.newInstance(service, domain);
115+
static WorkflowClient newInstance(IWorkflowService service) {
116+
return WorkflowClientInternal.newInstance(service, WorkflowClientOptions.defaultInstance());
165117
}
166118

167119
/**
168120
* Creates client that connects to an instance of the Cadence Service.
169121
*
170122
* @param service client to the Cadence Service endpoint.
171-
* @param domain domain that worker uses to poll.
172123
* @param options Options (like {@link com.uber.cadence.converter.DataConverter}er override) for
173124
* configuring client.
174125
*/
175-
static WorkflowClient newInstance(
176-
IWorkflowService service, String domain, WorkflowClientOptions options) {
177-
return WorkflowClientInternal.newInstance(service, domain, options);
126+
static WorkflowClient newInstance(IWorkflowService service, WorkflowClientOptions options) {
127+
return WorkflowClientInternal.newInstance(service, options);
178128
}
179129

180-
String getDomain();
130+
WorkflowClientOptions getOptions();
131+
132+
IWorkflowService getService();
181133

182134
/**
183135
* Creates workflow client stub that can be used to start a single workflow execution. The first
@@ -728,7 +680,4 @@ static <A1, A2, A3, A4, A5, A6, R> CompletableFuture<R> execute(
728680
A6 arg6) {
729681
return WorkflowClientInternal.execute(workflow, arg1, arg2, arg3, arg4, arg5, arg6);
730682
}
731-
732-
/** Closes the workflow client and the underlying IWorkflowService when this method is called. */
733-
void close();
734683
}

src/main/java/com/uber/cadence/client/WorkflowClientOptions.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,47 @@
2525

2626
/** Options for WorkflowClient configuration. */
2727
public final class WorkflowClientOptions {
28+
private static final String DEFAULT_DOMAIN = "default";
29+
private static final WorkflowClientOptions DEFAULT_INSTANCE;
30+
private static final WorkflowClientInterceptor[] EMPTY_INTERCEPTOR_ARRAY =
31+
new WorkflowClientInterceptor[0];
2832

29-
public static final class Builder {
33+
static {
34+
DEFAULT_INSTANCE = new Builder().build();
35+
}
36+
37+
public static WorkflowClientOptions defaultInstance() {
38+
return DEFAULT_INSTANCE;
39+
}
40+
41+
public static Builder newBuilder() {
42+
return new Builder();
43+
}
44+
45+
public static Builder newBuilder(WorkflowClientOptions options) {
46+
return new Builder(options);
47+
}
3048

49+
public static final class Builder {
50+
private String domain = DEFAULT_DOMAIN;
3151
private DataConverter dataConverter = JsonDataConverter.getInstance();
3252
private WorkflowClientInterceptor[] interceptors = EMPTY_INTERCEPTOR_ARRAY;
33-
private Scope metricsScope;
53+
private Scope metricsScope = NoopScope.getInstance();
3454

35-
public Builder() {}
55+
private Builder() {}
3656

37-
public Builder(WorkflowClientOptions options) {
57+
private Builder(WorkflowClientOptions options) {
58+
domain = options.getDomain();
3859
dataConverter = options.getDataConverter();
3960
interceptors = options.getInterceptors();
4061
metricsScope = options.getMetricsScope();
4162
}
4263

64+
public Builder setDomain(String domain) {
65+
this.domain = domain;
66+
return this;
67+
}
68+
4369
/**
4470
* Used to override default (JSON) data converter implementation.
4571
*
@@ -72,28 +98,30 @@ public Builder setMetricsScope(Scope metricsScope) {
7298
}
7399

74100
public WorkflowClientOptions build() {
75-
if (metricsScope == null) {
76-
metricsScope = NoopScope.getInstance();
77-
}
78-
return new WorkflowClientOptions(dataConverter, interceptors, metricsScope);
101+
return new WorkflowClientOptions(domain, dataConverter, interceptors, metricsScope);
79102
}
80103
}
81104

82-
private static final WorkflowClientInterceptor[] EMPTY_INTERCEPTOR_ARRAY =
83-
new WorkflowClientInterceptor[0];
105+
private final String domain;
84106
private final DataConverter dataConverter;
85-
86107
private final WorkflowClientInterceptor[] interceptors;
87-
88108
private final Scope metricsScope;
89109

90110
private WorkflowClientOptions(
91-
DataConverter dataConverter, WorkflowClientInterceptor[] interceptors, Scope metricsScope) {
111+
String domain,
112+
DataConverter dataConverter,
113+
WorkflowClientInterceptor[] interceptors,
114+
Scope metricsScope) {
115+
this.domain = domain;
92116
this.dataConverter = dataConverter;
93117
this.interceptors = interceptors;
94118
this.metricsScope = metricsScope;
95119
}
96120

121+
public String getDomain() {
122+
return domain;
123+
}
124+
97125
public DataConverter getDataConverter() {
98126
return dataConverter;
99127
}

src/main/java/com/uber/cadence/internal/external/ManualActivityCompletionClientFactoryImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.uber.m3.tally.Scope;
2525
import com.uber.m3.util.ImmutableMap;
2626
import java.util.Map;
27+
import java.util.Objects;
2728

2829
public class ManualActivityCompletionClientFactoryImpl
2930
extends ManualActivityCompletionClientFactory {
@@ -35,9 +36,9 @@ public class ManualActivityCompletionClientFactoryImpl
3536

3637
public ManualActivityCompletionClientFactoryImpl(
3738
IWorkflowService service, String domain, DataConverter dataConverter, Scope metricsScope) {
38-
this.service = service;
39-
this.domain = domain;
40-
this.dataConverter = dataConverter;
39+
this.service = Objects.requireNonNull(service);
40+
this.domain = Objects.requireNonNull(domain);
41+
this.dataConverter = Objects.requireNonNull(dataConverter);
4142

4243
Map<String, String> tags =
4344
new ImmutableMap.Builder<String, String>(1).put(MetricsTag.DOMAIN, domain).build();
@@ -54,16 +55,11 @@ public DataConverter getDataConverter() {
5455

5556
@Override
5657
public ManualActivityCompletionClient getClient(byte[] taskToken) {
57-
if (service == null) {
58-
throw new IllegalStateException("required property service is null");
59-
}
60-
if (dataConverter == null) {
61-
throw new IllegalStateException("required property dataConverter is null");
62-
}
6358
if (taskToken == null || taskToken.length == 0) {
6459
throw new IllegalArgumentException("null or empty task token");
6560
}
66-
return new ManualActivityCompletionClientImpl(service, taskToken, dataConverter, metricsScope);
61+
return new ManualActivityCompletionClientImpl(
62+
service, domain, taskToken, dataConverter, metricsScope);
6763
}
6864

6965
@Override

src/main/java/com/uber/cadence/internal/external/ManualActivityCompletionClientImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ class ManualActivityCompletionClientImpl extends ManualActivityCompletionClient
5757
private final Scope metricsScope;
5858

5959
ManualActivityCompletionClientImpl(
60-
IWorkflowService service, byte[] taskToken, DataConverter dataConverter, Scope metricsScope) {
60+
IWorkflowService service,
61+
String domain,
62+
byte[] taskToken,
63+
DataConverter dataConverter,
64+
Scope metricsScope) {
6165
this.service = service;
6266
this.taskToken = taskToken;
6367
this.dataConverter = dataConverter;
64-
this.domain = null;
68+
this.domain = domain;
6569
this.execution = null;
6670
this.activityId = null;
6771
this.metricsScope = metricsScope;

src/main/java/com/uber/cadence/internal/sync/DeterministicRunnerImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ public void runUntilAllBlocked() throws Throwable {
273273
if (nextWakeUpTime < currentTimeMillis() || nextWakeUpTime == Long.MAX_VALUE) {
274274
nextWakeUpTime = 0;
275275
}
276+
276277
} finally {
277278
inRunUntilAllBlocked = false;
278279
// Close was requested while running

src/main/java/com/uber/cadence/internal/sync/TestActivityEnvironmentInternal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public TestActivityEnvironmentInternal(TestEnvironmentOptions options) {
7474
activityTaskHandler =
7575
new POJOActivityTaskHandler(
7676
new WorkflowServiceWrapper(workflowService),
77-
testEnvironmentOptions.getDomain(),
77+
testEnvironmentOptions.getWorkflowClientOptions().getDomain(),
7878
testEnvironmentOptions.getDataConverter(),
7979
heartbeatExecutor);
8080
}

0 commit comments

Comments
 (0)