-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathWorkflowClientOptions.java
278 lines (246 loc) · 9.35 KB
/
WorkflowClientOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Modifications Copyright (c) 2017-2020 Uber Technologies Inc.
* Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc.
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.uber.cadence.client;
import com.uber.cadence.QueryRejectCondition;
import com.uber.cadence.context.ContextPropagator;
import com.uber.cadence.converter.DataConverter;
import com.uber.cadence.converter.JsonDataConverter;
import com.uber.cadence.internal.metrics.MetricsTag;
import com.uber.cadence.internal.metrics.NoopScope;
import com.uber.m3.tally.Scope;
import com.uber.m3.util.ImmutableMap;
import java.lang.management.ManagementFactory;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/** Options for WorkflowClient configuration. */
public final class WorkflowClientOptions {
private static final String DEFAULT_DOMAIN = "default";
private static final WorkflowClientOptions DEFAULT_INSTANCE;
private static final WorkflowClientInterceptor[] EMPTY_INTERCEPTOR_ARRAY =
new WorkflowClientInterceptor[0];
private static final List<ContextPropagator> EMPTY_CONTEXT_PROPAGATORS = Arrays.asList();
private static final Duration DEFAULT_WORKER_SHUTDOWN_TIME = Duration.ofSeconds(10);
static {
DEFAULT_INSTANCE = new Builder().build();
}
public static WorkflowClientOptions defaultInstance() {
return DEFAULT_INSTANCE;
}
public static Builder newBuilder() {
return new Builder();
}
public static Builder newBuilder(WorkflowClientOptions options) {
return new Builder(options);
}
public static final class Builder {
private String domain = DEFAULT_DOMAIN;
private DataConverter dataConverter = JsonDataConverter.getInstance();
private WorkflowClientInterceptor[] interceptors = EMPTY_INTERCEPTOR_ARRAY;
private Scope metricsScope = NoopScope.getInstance();
private String identity = ManagementFactory.getRuntimeMXBean().getName();;
private List<ContextPropagator> contextPropagators = EMPTY_CONTEXT_PROPAGATORS;
private QueryRejectCondition queryRejectCondition;
private Duration timeForWorkerShutdown = DEFAULT_WORKER_SHUTDOWN_TIME;
private Builder() {}
private Builder(WorkflowClientOptions options) {
domain = options.getDomain();
dataConverter = options.getDataConverter();
interceptors = options.getInterceptors();
metricsScope = options.getMetricsScope();
identity = options.getIdentity();
queryRejectCondition = options.getQueryRejectCondition();
timeForWorkerShutdown = options.getTimeForWorkerShutdown();
}
public Builder setDomain(String domain) {
this.domain = domain;
return this;
}
/**
* Used to override default (JSON) data converter implementation.
*
* @param dataConverter data converter to serialize and deserialize arguments and return values.
* Not null.
*/
public Builder setDataConverter(DataConverter dataConverter) {
this.dataConverter = Objects.requireNonNull(dataConverter);
return this;
}
/**
* Interceptor used to intercept workflow client calls.
*
* @param interceptors not null
*/
public Builder setInterceptors(WorkflowClientInterceptor... interceptors) {
this.interceptors = Objects.requireNonNull(interceptors);
return this;
}
/**
* Sets the scope to be used for the workflow client for metrics reporting.
*
* @param metricsScope the scope to be used. Not null.
*/
public Builder setMetricsScope(Scope metricsScope) {
this.metricsScope = Objects.requireNonNull(metricsScope);
return this;
}
/**
* Override human readable identity of the worker. Identity is used to identify a worker and is
* recorded in the workflow history events. For example when a worker gets an activity task the
* correspondent ActivityTaskStarted event contains the worker identity as a field. Default is
* whatever <code>(ManagementFactory.getRuntimeMXBean().getName()</code> returns.
*/
public Builder setIdentity(String identity) {
this.identity = Objects.requireNonNull(identity);
return this;
}
public Builder setContextPropagators(List<ContextPropagator> contextPropagators) {
this.contextPropagators = contextPropagators;
return this;
}
/**
* QueryRejectCondition is an optional field used to reject queries based on workflow state.
* QueryRejectConditionNotOpen will reject queries to workflows that are not open.
* QueryRejectConditionNotCompletedCleanly will reject queries to workflows that are not
* completed successfully. (e.g. terminated, canceled timeout etc...)
*
* <p>Default is null, which means that queries will not be rejected based on workflow state.
*/
public Builder setQueryRejectCondition(QueryRejectCondition queryRejectCondition) {
this.queryRejectCondition = queryRejectCondition;
return this;
}
/**
* Time for worker shutdown is an optional field used as the amount of time alloted for handling
* worker shutdown after SIGTERM signal. Default is 10 seconds.
*
* @param timeForWorkerShutdown
*/
public Builder setTimeForWorkerShutdown(Duration timeForWorkerShutdown) {
this.timeForWorkerShutdown = timeForWorkerShutdown;
return this;
}
public WorkflowClientOptions build() {
metricsScope = metricsScope.tagged(ImmutableMap.of(MetricsTag.DOMAIN, domain));
return new WorkflowClientOptions(
domain,
dataConverter,
interceptors,
metricsScope,
identity,
contextPropagators,
queryRejectCondition,
timeForWorkerShutdown);
}
}
private final String domain;
private final DataConverter dataConverter;
private final WorkflowClientInterceptor[] interceptors;
private final Scope metricsScope;
private final String identity;
private final List<ContextPropagator> contextPropagators;
private final QueryRejectCondition queryRejectCondition;
private final Duration timeForWorkerShutdown;
private WorkflowClientOptions(
String domain,
DataConverter dataConverter,
WorkflowClientInterceptor[] interceptors,
Scope metricsScope,
String identity,
List<ContextPropagator> contextPropagators,
QueryRejectCondition queryRejectCondition,
Duration timeForWorkerShutdown) {
this.domain = domain;
this.dataConverter = dataConverter;
this.interceptors = interceptors;
this.metricsScope = metricsScope;
this.identity = identity;
this.contextPropagators = contextPropagators;
this.queryRejectCondition = queryRejectCondition;
this.timeForWorkerShutdown = timeForWorkerShutdown;
}
public String getDomain() {
return domain;
}
public DataConverter getDataConverter() {
return dataConverter;
}
public WorkflowClientInterceptor[] getInterceptors() {
return interceptors;
}
public Scope getMetricsScope() {
return metricsScope;
}
public String getIdentity() {
return identity;
}
public List<ContextPropagator> getContextPropagators() {
return contextPropagators;
}
public QueryRejectCondition getQueryRejectCondition() {
return queryRejectCondition;
}
public Duration getTimeForWorkerShutdown() {
return timeForWorkerShutdown;
}
@Override
public String toString() {
return "WorkflowClientOptions{"
+ "domain='"
+ domain
+ '\''
+ ", dataConverter="
+ dataConverter
+ ", interceptors="
+ Arrays.toString(interceptors)
+ ", identity='"
+ identity
+ '\''
+ ", contextPropagators="
+ contextPropagators
+ ", queryRejectCondition="
+ queryRejectCondition
+ ", timeForWorkerShutdown="
+ timeForWorkerShutdown
+ '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorkflowClientOptions that = (WorkflowClientOptions) o;
return com.google.common.base.Objects.equal(domain, that.domain)
&& com.google.common.base.Objects.equal(dataConverter, that.dataConverter)
&& Arrays.equals(interceptors, that.interceptors)
&& com.google.common.base.Objects.equal(identity, that.identity)
&& com.google.common.base.Objects.equal(contextPropagators, that.contextPropagators)
&& queryRejectCondition == that.queryRejectCondition
&& com.google.common.base.Objects.equal(timeForWorkerShutdown, that.timeForWorkerShutdown);
}
@Override
public int hashCode() {
return com.google.common.base.Objects.hashCode(
domain,
dataConverter,
Arrays.hashCode(interceptors),
identity,
contextPropagators,
queryRejectCondition,
timeForWorkerShutdown);
}
}