/
TemporalUtils.java
272 lines (231 loc) · 12.5 KB
/
TemporalUtils.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
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.workers.temporal;
import static java.util.stream.Collectors.toSet;
import io.airbyte.commons.lang.Exceptions;
import io.airbyte.config.Configs;
import io.airbyte.config.EnvConfigs;
import io.airbyte.scheduler.models.JobRunConfig;
import io.temporal.activity.ActivityExecutionContext;
import io.temporal.api.common.v1.WorkflowExecution;
import io.temporal.api.namespace.v1.NamespaceConfig;
import io.temporal.api.namespace.v1.NamespaceInfo;
import io.temporal.api.workflowservice.v1.DescribeNamespaceRequest;
import io.temporal.api.workflowservice.v1.DescribeNamespaceResponse;
import io.temporal.api.workflowservice.v1.ListNamespacesRequest;
import io.temporal.api.workflowservice.v1.UpdateNamespaceRequest;
import io.temporal.client.ActivityCompletionException;
import io.temporal.client.WorkflowClient;
import io.temporal.client.WorkflowOptions;
import io.temporal.client.WorkflowStub;
import io.temporal.common.RetryOptions;
import io.temporal.serviceclient.WorkflowServiceStubs;
import io.temporal.serviceclient.WorkflowServiceStubsOptions;
import io.temporal.workflow.Functions;
import java.io.Serializable;
import java.time.Duration;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TemporalUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(TemporalUtils.class);
public static final Duration SEND_HEARTBEAT_INTERVAL = Duration.ofSeconds(10);
public static final Duration HEARTBEAT_TIMEOUT = Duration.ofSeconds(30);
public static WorkflowServiceStubs createTemporalService(final String temporalHost) {
final WorkflowServiceStubsOptions options = WorkflowServiceStubsOptions.newBuilder()
.setTarget(temporalHost) // todo: move to EnvConfigs
.build();
return getTemporalClientWhenConnected(
Duration.ofSeconds(2),
Duration.ofMinutes(2),
Duration.ofSeconds(5),
() -> WorkflowServiceStubs.newInstance(options));
}
public static final RetryOptions NO_RETRY = RetryOptions.newBuilder().setMaximumAttempts(1).build();
private static final Configs configs = new EnvConfigs();
public static final RetryOptions RETRY = RetryOptions.newBuilder()
.setMaximumAttempts(configs.getActivityNumberOfAttempt())
.setInitialInterval(Duration.ofSeconds(configs.getDelayBetweenActivityAttempts()))
.build();
public static final String DEFAULT_NAMESPACE = "default";
private static final Duration WORKFLOW_EXECUTION_TTL = Duration.ofDays(configs.getTemporalRetentionInDays());
private static final String HUMAN_READABLE_WORKFLOW_EXECUTION_TTL =
DurationFormatUtils.formatDurationWords(WORKFLOW_EXECUTION_TTL.toMillis(), true, true);
public static void configureTemporalNamespace(final WorkflowServiceStubs temporalService) {
final var client = temporalService.blockingStub();
final var describeNamespaceRequest = DescribeNamespaceRequest.newBuilder().setNamespace(DEFAULT_NAMESPACE).build();
final var currentRetentionGrpcDuration = client.describeNamespace(describeNamespaceRequest).getConfig().getWorkflowExecutionRetentionTtl();
final var currentRetention = Duration.ofSeconds(currentRetentionGrpcDuration.getSeconds());
if (currentRetention.equals(WORKFLOW_EXECUTION_TTL)) {
LOGGER.info("Workflow execution TTL already set for namespace " + DEFAULT_NAMESPACE + ". Remains unchanged as: "
+ HUMAN_READABLE_WORKFLOW_EXECUTION_TTL);
} else {
final var newGrpcDuration = com.google.protobuf.Duration.newBuilder().setSeconds(WORKFLOW_EXECUTION_TTL.getSeconds()).build();
final var humanReadableCurrentRetention = DurationFormatUtils.formatDurationWords(currentRetention.toMillis(), true, true);
final var namespaceConfig = NamespaceConfig.newBuilder().setWorkflowExecutionRetentionTtl(newGrpcDuration).build();
final var updateNamespaceRequest = UpdateNamespaceRequest.newBuilder().setNamespace(DEFAULT_NAMESPACE).setConfig(namespaceConfig).build();
LOGGER.info("Workflow execution TTL differs for namespace " + DEFAULT_NAMESPACE + ". Changing from (" + humanReadableCurrentRetention + ") to ("
+ HUMAN_READABLE_WORKFLOW_EXECUTION_TTL + "). ");
client.updateNamespace(updateNamespaceRequest);
}
}
@FunctionalInterface
public interface TemporalJobCreator<T extends Serializable> {
UUID create(WorkflowClient workflowClient, long jobId, int attempt, T config);
}
public static WorkflowOptions getWorkflowOptionsWithWorkflowId(final TemporalJobType jobType, final String workflowId) {
return WorkflowOptions.newBuilder()
.setWorkflowId(workflowId)
.setRetryOptions(NO_RETRY)
.setTaskQueue(jobType.name())
.build();
}
public static WorkflowOptions getWorkflowOptions(final TemporalJobType jobType) {
return WorkflowOptions.newBuilder()
.setTaskQueue(jobType.name())
// todo (cgardens) we do not leverage Temporal retries.
.setRetryOptions(RetryOptions.newBuilder().setMaximumAttempts(1).build())
.build();
}
public static JobRunConfig createJobRunConfig(final UUID jobId, final int attemptId) {
return createJobRunConfig(String.valueOf(jobId), attemptId);
}
public static JobRunConfig createJobRunConfig(final long jobId, final int attemptId) {
return createJobRunConfig(String.valueOf(jobId), attemptId);
}
public static JobRunConfig createJobRunConfig(final String jobId, final int attemptId) {
return new JobRunConfig()
.withJobId(jobId)
.withAttemptId((long) attemptId);
}
/**
* Allows running a given temporal workflow stub asynchronously. This method only works for
* workflows that take one argument. Because of the iface that Temporal supplies, in order to handle
* other method signatures, if we need to support them, we will need to add another helper with that
* number of args. For a reference on how Temporal recommends to do this see their docs:
* https://docs.temporal.io/docs/java/workflows#asynchronous-start
*
* @param workflowStub - workflow stub to be executed
* @param function - function on the workflow stub to be executed
* @param arg1 - argument to be supplied to the workflow function
* @param outputType - class of the output type of the workflow function
* @param <STUB> - type of the workflow stub
* @param <A1> - type of the argument of the workflow stub
* @param <R> - type of the return of the workflow stub
* @return pair of the workflow execution (contains metadata on the asynchronously running job) and
* future that can be used to await the result of the workflow stub's function
*/
public static <STUB, A1, R> ImmutablePair<WorkflowExecution, CompletableFuture<R>> asyncExecute(final STUB workflowStub,
final Functions.Func1<A1, R> function,
final A1 arg1,
final Class<R> outputType) {
final WorkflowStub untyped = WorkflowStub.fromTyped(workflowStub);
final WorkflowExecution workflowExecution = WorkflowClient.start(function, arg1);
final CompletableFuture<R> resultAsync = untyped.getResultAsync(outputType);
return ImmutablePair.of(workflowExecution, resultAsync);
}
/**
* Loops and waits for the Temporal service to become available and returns a client.
*
* This function uses a supplier as input since the creation of a WorkflowServiceStubs can result in
* connection exceptions as well.
*/
public static WorkflowServiceStubs getTemporalClientWhenConnected(
final Duration waitInterval,
final Duration maxTimeToConnect,
final Duration waitAfterConnection,
final Supplier<WorkflowServiceStubs> temporalServiceSupplier) {
LOGGER.info("Waiting for temporal server...");
boolean temporalStatus = false;
WorkflowServiceStubs temporalService = null;
long millisWaited = 0;
while (!temporalStatus) {
if (millisWaited >= maxTimeToConnect.toMillis()) {
throw new RuntimeException("Could not create Temporal client within max timeout!");
}
LOGGER.warn("Waiting for default namespace to be initialized in temporal...");
Exceptions.toRuntime(() -> Thread.sleep(waitInterval.toMillis()));
millisWaited = millisWaited + waitInterval.toMillis();
try {
temporalService = temporalServiceSupplier.get();
temporalStatus = getNamespaces(temporalService).contains("default");
} catch (final Exception e) {
// Ignore the exception because this likely means that the Temporal service is still initializing.
LOGGER.warn("Ignoring exception while trying to request Temporal namespaces:", e);
}
}
// sometimes it takes a few additional seconds for workflow queue listening to be available
Exceptions.toRuntime(() -> Thread.sleep(waitAfterConnection.toMillis()));
LOGGER.info("Found temporal default namespace!");
return temporalService;
}
protected static Set<String> getNamespaces(final WorkflowServiceStubs temporalService) {
return temporalService.blockingStub()
.listNamespaces(ListNamespacesRequest.newBuilder().build())
.getNamespacesList()
.stream()
.map(DescribeNamespaceResponse::getNamespaceInfo)
.map(NamespaceInfo::getName)
.collect(toSet());
}
/**
* Runs the code within the supplier while heartbeating in the backgroud. Also makes sure to shut
* down the heartbeat server after the fact.
*/
public static <T> T withBackgroundHeartbeat(final Callable<T> callable,
final Supplier<ActivityExecutionContext> activityContext) {
final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
try {
scheduledExecutor.scheduleAtFixedRate(
() -> new CancellationHandler.TemporalCancellationHandler(activityContext.get()).checkAndHandleCancellation(() -> {}),
0, SEND_HEARTBEAT_INTERVAL.toSeconds(), TimeUnit.SECONDS);
return callable.call();
} catch (final ActivityCompletionException e) {
LOGGER.warn("Job either timed out or was cancelled.");
throw new RuntimeException(e);
} catch (final Exception e) {
throw new RuntimeException(e);
} finally {
LOGGER.info("Stopping temporal heartbeating...");
scheduledExecutor.shutdown();
}
}
public static <T> T withBackgroundHeartbeat(final AtomicReference<Runnable> cancellationCallbackRef,
final Callable<T> callable,
final Supplier<ActivityExecutionContext> activityContext) {
final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
try {
scheduledExecutor.scheduleAtFixedRate(() -> {
final CancellationHandler cancellationHandler = new CancellationHandler.TemporalCancellationHandler(activityContext.get());
cancellationHandler.checkAndHandleCancellation(() -> {
if (cancellationCallbackRef != null) {
final Runnable cancellationCallback = cancellationCallbackRef.get();
if (cancellationCallback != null) {
cancellationCallback.run();
}
}
});
}, 0, SEND_HEARTBEAT_INTERVAL.toSeconds(), TimeUnit.SECONDS);
return callable.call();
} catch (final ActivityCompletionException e) {
LOGGER.warn("Job either timed out or was cancelled.");
throw new RuntimeException(e);
} catch (final Exception e) {
throw new RuntimeException(e);
} finally {
LOGGER.info("Stopping temporal heartbeating...");
scheduledExecutor.shutdown();
}
}
}