-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathDecisionContextImpl.java
381 lines (317 loc) · 11.7 KB
/
DecisionContextImpl.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
/*
* Copyright 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 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.internal.replay;
import com.uber.cadence.ChildPolicy;
import com.uber.cadence.DecisionTaskFailedCause;
import com.uber.cadence.DecisionTaskFailedEventAttributes;
import com.uber.cadence.HistoryEvent;
import com.uber.cadence.PollForDecisionTaskResponse;
import com.uber.cadence.TimerFiredEventAttributes;
import com.uber.cadence.WorkflowExecution;
import com.uber.cadence.WorkflowExecutionStartedEventAttributes;
import com.uber.cadence.WorkflowType;
import com.uber.cadence.converter.DataConverter;
import com.uber.cadence.internal.metrics.ReplayAwareScope;
import com.uber.cadence.internal.worker.LocalActivityWorker;
import com.uber.cadence.internal.worker.SingleWorkerOptions;
import com.uber.cadence.workflow.Functions.Func;
import com.uber.cadence.workflow.Functions.Func1;
import com.uber.cadence.workflow.Promise;
import com.uber.cadence.workflow.Workflow;
import com.uber.m3.tally.Scope;
import java.time.Duration;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
final class DecisionContextImpl implements DecisionContext, HistoryEventHandler {
private static final Logger log = LoggerFactory.getLogger(DecisionContextImpl.class);
private final ActivityDecisionContext activityClient;
private final WorkflowDecisionContext workflowClient;
private final ClockDecisionContext workflowClock;
private final WorkflowContext workflowContext;
private final Scope metricsScope;
private final boolean enableLoggingInReplay;
DecisionContextImpl(
DecisionsHelper decisionsHelper,
String domain,
PollForDecisionTaskResponse decisionTask,
WorkflowExecutionStartedEventAttributes startedAttributes,
SingleWorkerOptions options,
BiFunction<LocalActivityWorker.Task, Duration, Boolean> laTaskPoller,
ReplayDecider replayDecider) {
this.activityClient = new ActivityDecisionContext(decisionsHelper);
this.workflowContext = new WorkflowContext(domain, decisionTask, startedAttributes);
this.workflowClient = new WorkflowDecisionContext(decisionsHelper, workflowContext);
this.workflowClock =
new ClockDecisionContext(
decisionsHelper, laTaskPoller, replayDecider, options.getDataConverter());
this.enableLoggingInReplay = options.getEnableLoggingInReplay();
this.metricsScope =
new ReplayAwareScope(options.getMetricsScope(), this, workflowClock::currentTimeMillis);
}
@Override
public boolean getEnableLoggingInReplay() {
return enableLoggingInReplay;
}
@Override
public UUID randomUUID() {
return workflowClient.randomUUID();
}
@Override
public long getScheduleTimeMillis() {
return workflowClient.getScheduleTimeMillis();
}
@Override
public long getExecutionTimeMillis() {
return workflowClient.getExecutionTimeMillis();
}
@Override
public Random newRandom() {
return workflowClient.newRandom();
}
@Override
public Scope getMetricsScope() {
return metricsScope;
}
@Override
public WorkflowExecution getWorkflowExecution() {
return workflowContext.getWorkflowExecution();
}
@Override
public WorkflowType getWorkflowType() {
return workflowContext.getWorkflowType();
}
@Override
public boolean isCancelRequested() {
return workflowContext.isCancelRequested();
}
void setCancelRequested(boolean flag) {
workflowContext.setCancelRequested(flag);
}
@Override
public ContinueAsNewWorkflowExecutionParameters getContinueAsNewOnCompletion() {
return workflowContext.getContinueAsNewOnCompletion();
}
@Override
public void setContinueAsNewOnCompletion(
ContinueAsNewWorkflowExecutionParameters continueParameters) {
workflowContext.setContinueAsNewOnCompletion(continueParameters);
}
@Override
public int getExecutionStartToCloseTimeoutSeconds() {
return workflowContext.getExecutionStartToCloseTimeoutSeconds();
}
@Override
public Duration getDecisionTaskTimeout() {
return Duration.ofSeconds(workflowContext.getDecisionTaskTimeoutSeconds());
}
@Override
public ChildPolicy getChildPolicy() {
return workflowContext.getChildPolicy();
}
@Override
public String getTaskList() {
return workflowContext.getTaskList();
}
@Override
public String getDomain() {
return workflowContext.getDomain();
}
@Override
public String getWorkflowId() {
return workflowContext.getWorkflowExecution().getWorkflowId();
}
@Override
public String getRunId() {
return workflowContext.getWorkflowExecution().getRunId();
}
@Override
public Duration getExecutionStartToCloseTimeout() {
return Duration.ofSeconds(workflowContext.getExecutionStartToCloseTimeoutSeconds());
}
@Override
public Consumer<Exception> scheduleActivityTask(
ExecuteActivityParameters parameters, BiConsumer<byte[], Exception> callback) {
return activityClient.scheduleActivityTask(parameters, callback);
}
@Override
public Consumer<Exception> scheduleLocalActivityTask(
ExecuteLocalActivityParameters parameters, BiConsumer<byte[], Exception> callback) {
return workflowClock.scheduleLocalActivityTask(parameters, callback);
}
@Override
public Consumer<Exception> startChildWorkflow(
StartChildWorkflowExecutionParameters parameters,
Consumer<WorkflowExecution> executionCallback,
BiConsumer<byte[], Exception> callback) {
return workflowClient.startChildWorkflow(parameters, executionCallback, callback);
}
@Override
public boolean isServerSideChildWorkflowRetry() {
return workflowClient.isChildWorkflowExecutionStartedWithRetryOptions();
}
@Override
public boolean isServerSideActivityRetry() {
return activityClient.isActivityScheduledWithRetryOptions();
}
@Override
public Consumer<Exception> signalWorkflowExecution(
SignalExternalWorkflowParameters signalParameters, BiConsumer<Void, Exception> callback) {
return workflowClient.signalWorkflowExecution(signalParameters, callback);
}
@Override
public Promise<Void> requestCancelWorkflowExecution(WorkflowExecution execution) {
workflowClient.requestCancelWorkflowExecution(execution);
// TODO: Make promise return success or failure of the cancellation request.
return Workflow.newPromise(null);
}
@Override
public void continueAsNewOnCompletion(ContinueAsNewWorkflowExecutionParameters parameters) {
workflowClient.continueAsNewOnCompletion(parameters);
}
void setReplayCurrentTimeMilliseconds(long replayCurrentTimeMilliseconds) {
if (replayCurrentTimeMilliseconds < workflowClock.currentTimeMillis()) {
if (log.isWarnEnabled()) {
log.warn(
"Trying to set workflow clock back from "
+ workflowClock.currentTimeMillis()
+ " to "
+ replayCurrentTimeMilliseconds
+ ". This will be a no-op.");
}
return;
}
workflowClock.setReplayCurrentTimeMilliseconds(replayCurrentTimeMilliseconds);
}
long getReplayCurrentTimeMilliseconds() {
return workflowClock.currentTimeMillis();
}
@Override
public boolean isReplaying() {
return workflowClock.isReplaying();
}
@Override
public Consumer<Exception> createTimer(long delaySeconds, Consumer<Exception> callback) {
return workflowClock.createTimer(delaySeconds, callback);
}
@Override
public byte[] sideEffect(Func<byte[]> func) {
return workflowClock.sideEffect(func);
}
@Override
public Optional<byte[]> mutableSideEffect(
String id, DataConverter converter, Func1<Optional<byte[]>, Optional<byte[]>> func) {
return workflowClock.mutableSideEffect(id, converter, func);
}
@Override
public int getVersion(
String changeID, DataConverter converter, int minSupported, int maxSupported) {
return workflowClock.getVersion(changeID, converter, minSupported, maxSupported);
}
@Override
public long currentTimeMillis() {
return workflowClock.currentTimeMillis();
}
void setReplaying(boolean replaying) {
workflowClock.setReplaying(replaying);
}
@Override
public void handleActivityTaskCanceled(HistoryEvent event) {
activityClient.handleActivityTaskCanceled(event);
}
@Override
public void handleActivityTaskCompleted(HistoryEvent event) {
activityClient.handleActivityTaskCompleted(event);
}
@Override
public void handleActivityTaskFailed(HistoryEvent event) {
activityClient.handleActivityTaskFailed(event);
}
@Override
public void handleActivityTaskTimedOut(HistoryEvent event) {
activityClient.handleActivityTaskTimedOut(event);
}
@Override
public void handleChildWorkflowExecutionCancelRequested(HistoryEvent event) {}
@Override
public void handleChildWorkflowExecutionCanceled(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionCanceled(event);
}
@Override
public void handleChildWorkflowExecutionStarted(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionStarted(event);
}
@Override
public void handleChildWorkflowExecutionTimedOut(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionTimedOut(event);
}
@Override
public void handleChildWorkflowExecutionTerminated(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionTerminated(event);
}
@Override
public void handleStartChildWorkflowExecutionFailed(HistoryEvent event) {
workflowClient.handleStartChildWorkflowExecutionFailed(event);
}
@Override
public void handleChildWorkflowExecutionFailed(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionFailed(event);
}
@Override
public void handleChildWorkflowExecutionCompleted(HistoryEvent event) {
workflowClient.handleChildWorkflowExecutionCompleted(event);
}
@Override
public void handleTimerFired(TimerFiredEventAttributes attributes) {
workflowClock.handleTimerFired(attributes);
}
@Override
public void handleTimerCanceled(HistoryEvent event) {
workflowClock.handleTimerCanceled(event);
}
void handleSignalExternalWorkflowExecutionFailed(HistoryEvent event) {
workflowClient.handleSignalExternalWorkflowExecutionFailed(event);
}
@Override
public void handleExternalWorkflowExecutionSignaled(HistoryEvent event) {
workflowClient.handleExternalWorkflowExecutionSignaled(event);
}
@Override
public void handleMarkerRecorded(HistoryEvent event) {
workflowClock.handleMarkerRecorded(event);
}
public void handleDecisionTaskFailed(HistoryEvent event) {
DecisionTaskFailedEventAttributes attr = event.getDecisionTaskFailedEventAttributes();
if (attr != null && attr.getCause() == DecisionTaskFailedCause.RESET_WORKFLOW) {
workflowContext.setCurrentRunId(attr.getNewRunId());
}
}
boolean startUnstartedLaTasks(Duration maxWaitAllowed) {
return workflowClock.startUnstartedLaTasks(maxWaitAllowed);
}
int numPendingLaTasks() {
return workflowClock.numPendingLaTasks();
}
void awaitTaskCompletion(Duration duration) throws InterruptedException {
workflowClock.awaitTaskCompletion(duration);
}
}