/
ChildWorkflowOptions.java
526 lines (449 loc) · 17.2 KB
/
ChildWorkflowOptions.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 io.temporal.workflow;
import static io.temporal.internal.common.OptionsUtils.roundUpToSeconds;
import com.google.common.base.Objects;
import io.temporal.api.enums.v1.ParentClosePolicy;
import io.temporal.api.enums.v1.WorkflowIdReusePolicy;
import io.temporal.common.CronSchedule;
import io.temporal.common.MethodRetry;
import io.temporal.common.RetryOptions;
import io.temporal.common.context.ContextPropagator;
import io.temporal.failure.CanceledFailure;
import io.temporal.failure.ChildWorkflowFailure;
import io.temporal.failure.TimeoutFailure;
import io.temporal.internal.common.OptionsUtils;
import java.time.Duration;
import java.util.List;
import java.util.Map;
public final class ChildWorkflowOptions {
public static Builder newBuilder() {
return new Builder();
}
public static Builder newBuilder(ChildWorkflowOptions options) {
return new Builder(options);
}
public static ChildWorkflowOptions getDefaultInstance() {
return DEFAULT_INSTANCE;
}
private static final ChildWorkflowOptions DEFAULT_INSTANCE;
static {
DEFAULT_INSTANCE = ChildWorkflowOptions.newBuilder().build();
}
public static final class Builder {
private String namespace;
private String workflowId;
private WorkflowIdReusePolicy workflowIdReusePolicy;
private Duration workflowRunTimeout;
private Duration workflowExecutionTimeout;
private Duration workflowTaskTimeout;
private String taskQueue;
private RetryOptions retryOptions;
private String cronSchedule;
private ParentClosePolicy parentClosePolicy;
private Map<String, Object> memo;
private Map<String, Object> searchAttributes;
private List<ContextPropagator> contextPropagators;
private ChildWorkflowCancellationType cancellationType;
private Builder() {}
private Builder(ChildWorkflowOptions options) {
if (options == null) {
return;
}
this.namespace = options.getNamespace();
this.workflowId = options.getWorkflowId();
this.workflowIdReusePolicy = options.getWorkflowIdReusePolicy();
this.workflowRunTimeout = options.getWorkflowRunTimeout();
this.workflowExecutionTimeout = options.getWorkflowExecutionTimeout();
this.workflowTaskTimeout = options.getWorkflowTaskTimeout();
this.taskQueue = options.getTaskQueue();
this.retryOptions = options.getRetryOptions();
this.cronSchedule = options.getCronSchedule();
this.parentClosePolicy = options.getParentClosePolicy();
this.memo = options.getMemo();
this.searchAttributes = options.getSearchAttributes();
this.contextPropagators = options.getContextPropagators();
this.cancellationType = options.getCancellationType();
}
/**
* Specify namespace in which workflow should be started.
*
* <p>TODO: Resolve conflict with WorkflowClient namespace.
*/
public Builder setNamespace(String namespace) {
this.namespace = namespace;
return this;
}
/**
* Workflow id to use when starting. If not specified a UUID is generated. Note that it is
* dangerous as in case of client side retries no deduplication will happen based on the
* generated id. So prefer assigning business meaningful ids if possible.
*/
public Builder setWorkflowId(String workflowId) {
this.workflowId = workflowId;
return this;
}
/**
* Specifies server behavior if a completed workflow with the same id exists. Note that under no
* conditions Temporal allows two workflows with the same namespace and workflow id run
* simultaneously.
* <li>
*
* <ul>
* AllowDuplicateFailedOnly is a default value. It means that workflow can start if
* previous run failed or was canceled or terminated.
* </ul>
*
* <ul>
* AllowDuplicate allows new run independently of the previous run closure status.
* </ul>
*
* <ul>
* RejectDuplicate doesn't allow new run independently of the previous run closure status.
* </ul>
*/
public Builder setWorkflowIdReusePolicy(WorkflowIdReusePolicy workflowIdReusePolicy) {
this.workflowIdReusePolicy = workflowIdReusePolicy;
return this;
}
/**
* The time after which child workflow run is automatically terminated by Temporal service with
* CHILD_WORKFLOW_EXECUTION_TIMED_OUT status. <br>
* Parent workflow receives {@link ChildWorkflowFailure} exception with {@link TimeoutFailure}
* cause from the child's {@link Promise} if this happens.
*
* <p>When a workflow reaches Workflow Run Timeout, it can't make any progress after that. Do
* not rely on this timeout in workflow implementation or business logic. This timeout is not
* designed to be handled in workflow code to perform any logic in case of timeout. Consider
* using workflow timers instead.
*
* <p>If you catch yourself setting this timeout to very small values, you're likely using it
* wrong.
*
* <p>Example: If Workflow Run Timeout is 30 seconds and the network was unavailable for 1
* minute, workflows that were scheduled before the network blip will never have a chance to
* make progress or react, and will be terminated. <br>
* A timer that is scheduled in the workflow code using {@link Workflow#newTimer(Duration)} will
* handle this situation gracefully. A workflow with such a timer will start after the network
* blip. If it started before the network blip and the timer fires during the network blip, it
* will get delivered after connectivity is restored and the workflow will be able to resume.
*/
public Builder setWorkflowRunTimeout(Duration workflowRunTimeout) {
this.workflowRunTimeout = workflowRunTimeout;
return this;
}
/**
* The time after which child workflow execution (which includes run retries and continue as
* new) is automatically terminated by Temporal service with WORKFLOW_EXECUTION_TIMED_OUT
* status. <br>
* Parent workflow receives {@link ChildWorkflowFailure} exception with {@link TimeoutFailure}
* cause from the child's {@link Promise} if this happens.
*
* <p>When a workflow reaches Workflow Execution Timeout, it can't make any progress after that.
* Do not rely on this timeout in workflow implementation or business logic. This timeout is not
* designed to be handled in workflow code to perform any logic in case of timeout. Consider
* using workflow timers instead.
*
* <p>If you catch yourself setting this timeout to very small values, you're likely using it
* wrong.
*
* <p>Example: If Workflow Execution Timeout is 30 seconds and the network was unavailable for 1
* minute, workflows that were scheduled before the network blip will never have a chance to
* make progress or react, and will be terminated. <br>
* A timer that is scheduled in the workflow code using {@link Workflow#newTimer(Duration)} will
* handle this situation gracefully. A workflow with such a timer will start after the network
* blip. If it started before the network blip and the timer fires during the network blip, it
* will get delivered after connectivity is restored and the workflow will be able to resume.
*/
public Builder setWorkflowExecutionTimeout(Duration workflowExecutionTimeout) {
this.workflowExecutionTimeout = workflowExecutionTimeout;
return this;
}
/**
* Maximum execution time of a single workflow task. Default is 10 seconds. Maximum accepted
* value is 60 seconds.
*/
public Builder setWorkflowTaskTimeout(Duration workflowTaskTimeout) {
if (roundUpToSeconds(workflowTaskTimeout) > 60) {
throw new IllegalArgumentException(
"WorkflowTaskTimeout over one minute: " + workflowTaskTimeout);
}
this.workflowTaskTimeout = workflowTaskTimeout;
return this;
}
/**
* Task queue to use for workflow tasks. It should match a task queue specified when creating a
* {@link io.temporal.worker.Worker} that hosts the workflow code.
*/
public Builder setTaskQueue(String taskQueue) {
this.taskQueue = taskQueue;
return this;
}
/**
* RetryOptions that define how child workflow is retried in case of failure. Default is null
* which is no reties.
*/
public Builder setRetryOptions(RetryOptions retryOptions) {
this.retryOptions = retryOptions;
return this;
}
public Builder setCronSchedule(String cronSchedule) {
this.cronSchedule = cronSchedule;
return this;
}
/** Specifies how this workflow reacts to the death of the parent workflow. */
public Builder setParentClosePolicy(ParentClosePolicy parentClosePolicy) {
this.parentClosePolicy = parentClosePolicy;
return this;
}
/** Specifies additional non-indexed information in result of list workflow. */
public Builder setMemo(Map<String, Object> memo) {
this.memo = memo;
return this;
}
/** Specifies additional indexed information in result of list workflow. */
public Builder setSearchAttributes(Map<String, Object> searchAttributes) {
this.searchAttributes = searchAttributes;
return this;
}
/** Specifies the list of context propagators to use during this workflow. */
public Builder setContextPropagators(List<ContextPropagator> contextPropagators) {
this.contextPropagators = contextPropagators;
return this;
}
/**
* In case of a child workflow cancellation it fails with a {@link CanceledFailure}. The type
* defines at which point the exception is thrown.
*/
public Builder setCancellationType(ChildWorkflowCancellationType cancellationType) {
this.cancellationType = cancellationType;
return this;
}
public Builder setMethodRetry(MethodRetry r) {
retryOptions = RetryOptions.merge(r, retryOptions);
return this;
}
public Builder setCronSchedule(CronSchedule c) {
String cronAnnotation = c == null ? "" : c.value();
cronSchedule = OptionsUtils.merge(cronAnnotation, cronSchedule, String.class);
return this;
}
public ChildWorkflowOptions build() {
return new ChildWorkflowOptions(
namespace,
workflowId,
workflowIdReusePolicy,
workflowRunTimeout,
workflowExecutionTimeout,
workflowTaskTimeout,
taskQueue,
retryOptions,
cronSchedule,
parentClosePolicy,
memo,
searchAttributes,
contextPropagators,
cancellationType);
}
public ChildWorkflowOptions validateAndBuildWithDefaults() {
return new ChildWorkflowOptions(
namespace,
workflowId,
workflowIdReusePolicy,
workflowRunTimeout,
workflowExecutionTimeout,
workflowTaskTimeout,
taskQueue,
retryOptions,
cronSchedule,
parentClosePolicy,
memo,
searchAttributes,
contextPropagators,
cancellationType == null
? ChildWorkflowCancellationType.WAIT_CANCELLATION_COMPLETED
: cancellationType);
}
}
private final String namespace;
private final String workflowId;
private final WorkflowIdReusePolicy workflowIdReusePolicy;
private final Duration workflowRunTimeout;
private final Duration workflowExecutionTimeout;
private final Duration workflowTaskTimeout;
private final String taskQueue;
private final RetryOptions retryOptions;
private final String cronSchedule;
private final ParentClosePolicy parentClosePolicy;
private final Map<String, Object> memo;
private final Map<String, Object> searchAttributes;
private final List<ContextPropagator> contextPropagators;
private final ChildWorkflowCancellationType cancellationType;
private ChildWorkflowOptions(
String namespace,
String workflowId,
WorkflowIdReusePolicy workflowIdReusePolicy,
Duration workflowRunTimeout,
Duration workflowExecutionTimeout,
Duration workflowTaskTimeout,
String taskQueue,
RetryOptions retryOptions,
String cronSchedule,
ParentClosePolicy parentClosePolicy,
Map<String, Object> memo,
Map<String, Object> searchAttributes,
List<ContextPropagator> contextPropagators,
ChildWorkflowCancellationType cancellationType) {
this.namespace = namespace;
this.workflowId = workflowId;
this.workflowIdReusePolicy = workflowIdReusePolicy;
this.workflowRunTimeout = workflowRunTimeout;
this.workflowExecutionTimeout = workflowExecutionTimeout;
this.workflowTaskTimeout = workflowTaskTimeout;
this.taskQueue = taskQueue;
this.retryOptions = retryOptions;
this.cronSchedule = cronSchedule;
this.parentClosePolicy = parentClosePolicy;
this.memo = memo;
this.searchAttributes = searchAttributes;
this.contextPropagators = contextPropagators;
this.cancellationType = cancellationType;
}
public String getNamespace() {
return namespace;
}
public String getWorkflowId() {
return workflowId;
}
public WorkflowIdReusePolicy getWorkflowIdReusePolicy() {
return workflowIdReusePolicy;
}
public Duration getWorkflowRunTimeout() {
return workflowRunTimeout;
}
public Duration getWorkflowExecutionTimeout() {
return workflowExecutionTimeout;
}
public Duration getWorkflowTaskTimeout() {
return workflowTaskTimeout;
}
public String getTaskQueue() {
return taskQueue;
}
public RetryOptions getRetryOptions() {
return retryOptions;
}
public String getCronSchedule() {
return cronSchedule;
}
public ParentClosePolicy getParentClosePolicy() {
return parentClosePolicy;
}
public Map<String, Object> getMemo() {
return memo;
}
public Map<String, Object> getSearchAttributes() {
return searchAttributes;
}
public List<ContextPropagator> getContextPropagators() {
return contextPropagators;
}
public ChildWorkflowCancellationType getCancellationType() {
return cancellationType;
}
public Builder toBuilder() {
return new Builder(this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChildWorkflowOptions that = (ChildWorkflowOptions) o;
return Objects.equal(namespace, that.namespace)
&& Objects.equal(workflowId, that.workflowId)
&& workflowIdReusePolicy == that.workflowIdReusePolicy
&& Objects.equal(workflowRunTimeout, that.workflowRunTimeout)
&& Objects.equal(workflowExecutionTimeout, that.workflowExecutionTimeout)
&& Objects.equal(workflowTaskTimeout, that.workflowTaskTimeout)
&& Objects.equal(taskQueue, that.taskQueue)
&& Objects.equal(retryOptions, that.retryOptions)
&& Objects.equal(cronSchedule, that.cronSchedule)
&& parentClosePolicy == that.parentClosePolicy
&& Objects.equal(memo, that.memo)
&& Objects.equal(searchAttributes, that.searchAttributes)
&& Objects.equal(contextPropagators, that.contextPropagators)
&& cancellationType == that.cancellationType;
}
@Override
public int hashCode() {
return Objects.hashCode(
namespace,
workflowId,
workflowIdReusePolicy,
workflowRunTimeout,
workflowExecutionTimeout,
workflowTaskTimeout,
taskQueue,
retryOptions,
cronSchedule,
parentClosePolicy,
memo,
searchAttributes,
contextPropagators,
cancellationType);
}
@Override
public String toString() {
return "ChildWorkflowOptions{"
+ "namespace='"
+ namespace
+ '\''
+ ", workflowId='"
+ workflowId
+ '\''
+ ", workflowIdReusePolicy="
+ workflowIdReusePolicy
+ ", workflowRunTimeout="
+ workflowRunTimeout
+ ", workflowExecutionTimeout="
+ workflowExecutionTimeout
+ ", workflowTaskTimeout="
+ workflowTaskTimeout
+ ", taskQueue='"
+ taskQueue
+ '\''
+ ", retryOptions="
+ retryOptions
+ ", cronSchedule='"
+ cronSchedule
+ '\''
+ ", parentClosePolicy="
+ parentClosePolicy
+ ", memo="
+ memo
+ ", searchAttributes="
+ searchAttributes
+ ", contextPropagators="
+ contextPropagators
+ ", cancellationType="
+ cancellationType
+ '}';
}
}