-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkerHost.java
291 lines (257 loc) · 10.8 KB
/
WorkerHost.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
/*
* Copyright 2023 Swift Software Group, Inc.
* (Code and content before December 13, 2023, Copyright Netflix, Inc.)
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.swiftconductor.conductor.client.automation;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.discovery.EurekaClient;
import com.swiftconductor.conductor.client.exception.ClientException;
import com.swiftconductor.conductor.client.http.TaskClient;
import com.swiftconductor.conductor.client.worker.AbstractWorker;
/**
* Configures automated polling of tasks and execution via the registered
* {@link AbstractWorker}s.
*/
public class WorkerHost {
static final Logger LOGGER = LoggerFactory.getLogger(WorkerHost.class);
private static final String INVALID_THREAD_COUNT = "Invalid worker thread count specified, use either shared thread pool or config thread count per task";
private ScheduledExecutorService scheduledExecutorService;
private final EurekaClient eurekaClient;
private final TaskClient taskClient;
private final List<AbstractWorker> workers = new LinkedList<>();
private final int sleepWhenRetry;
private final int updateRetryCount;
@Deprecated
private final int threadCount;
private final int shutdownGracePeriodSeconds;
private final String workerNamePrefix;
private final Map<String /* taskType */, String /* domain */> taskToDomain;
private final Map<String /* taskType */, Integer /* threadCount */> taskThreadCount;
private WorkerProcess taskPollExecutor;
/**
* @see WorkerHost.Builder
* @see WorkerHost#init()
*/
private WorkerHost(Builder builder) {
// WorkerHosthared thread pool or per task thread pool
if (builder.threadCount != -1 && !builder.taskThreadCount.isEmpty()) {
LOGGER.error(INVALID_THREAD_COUNT);
throw new ClientException(INVALID_THREAD_COUNT);
} else if (!builder.taskThreadCount.isEmpty()) {
for (AbstractWorker worker : builder.workers) {
if (!builder.taskThreadCount.containsKey(worker.getTaskDefName())) {
LOGGER.info("No thread count specified for task type {}, default to 1 thread", worker.getTaskDefName());
builder.taskThreadCount.put(worker.getTaskDefName(), 1);
}
workers.add(worker);
}
this.taskThreadCount = builder.taskThreadCount;
this.threadCount = -1;
} else {
Set<String> taskTypes = new HashSet<>();
for (AbstractWorker worker : builder.workers) {
taskTypes.add(worker.getTaskDefName());
workers.add(worker);
}
this.threadCount = (builder.threadCount == -1) ? workers.size() : builder.threadCount;
// shared thread pool will be evenly split between task types
int splitThreadCount = threadCount / taskTypes.size();
this.taskThreadCount = taskTypes.stream().collect(Collectors.toMap(v -> v, v -> splitThreadCount));
}
this.eurekaClient = builder.eurekaClient;
this.taskClient = builder.taskClient;
this.sleepWhenRetry = builder.sleepWhenRetry;
this.updateRetryCount = builder.updateRetryCount;
this.workerNamePrefix = builder.workerNamePrefix;
this.taskToDomain = builder.taskToDomain;
this.shutdownGracePeriodSeconds = builder.shutdownGracePeriodSeconds;
}
/** Builder used to create the instances of WorkerHost */
public static class Builder {
private String workerNamePrefix = "workflow-worker-%d";
private int sleepWhenRetry = 500;
private int updateRetryCount = 3;
@Deprecated
private int threadCount = -1;
private int shutdownGracePeriodSeconds = 10;
private final Iterable<AbstractWorker> workers;
private EurekaClient eurekaClient;
private final TaskClient taskClient;
private Map<String /* taskType */, String /* domain */> taskToDomain = new HashMap<>();
private Map<String /* taskType */, Integer /* threadCount */> taskThreadCount = new HashMap<>();
public Builder(TaskClient taskClient, Iterable<AbstractWorker> workers) {
Validate.notNull(taskClient, "TaskClient cannot be null");
Validate.notNull(workers, "Workers cannot be null");
this.taskClient = taskClient;
this.workers = workers;
}
/**
* @param workerNamePrefix
* prefix to be used for worker names, defaults to workflow-worker-
* if not supplied.
* @return Returns the current instance.
*/
public Builder withWorkerNamePrefix(String workerNamePrefix) {
this.workerNamePrefix = workerNamePrefix;
return this;
}
/**
* @param sleepWhenRetry
* time in milliseconds, for which the thread should sleep when task
* update call fails, before retrying the operation.
* @return Returns the current instance.
*/
public Builder withSleepWhenRetry(int sleepWhenRetry) {
this.sleepWhenRetry = sleepWhenRetry;
return this;
}
/**
* @param updateRetryCount
* number of times to retry the failed updateTask operation
* @return Builder instance
* @see #withSleepWhenRetry(int)
*/
public Builder withUpdateRetryCount(int updateRetryCount) {
this.updateRetryCount = updateRetryCount;
return this;
}
/**
* @param threadCount
* # of threads assigned to the workers. Should be at-least the size
* of taskWorkers to avoid starvation in a busy system.
* @return Builder instance
* @deprecated Use {@link WorkerHost.Builder#withTaskThreadCount(Map)} instead.
*/
@Deprecated
public Builder withThreadCount(int threadCount) {
if (threadCount < 1) {
throw new IllegalArgumentException("No. of threads cannot be less than 1");
}
this.threadCount = threadCount;
return this;
}
/**
* @param shutdownGracePeriodSeconds
* waiting seconds before forcing shutdown of your worker
* @return Builder instance
*/
public Builder withShutdownGracePeriodSeconds(int shutdownGracePeriodSeconds) {
if (shutdownGracePeriodSeconds < 1) {
throw new IllegalArgumentException("Seconds of shutdownGracePeriod cannot be less than 1");
}
this.shutdownGracePeriodSeconds = shutdownGracePeriodSeconds;
return this;
}
/**
* @param eurekaClient
* Eureka client - used to identify if the server is in discovery or
* not. When the server goes out of discovery, the polling is
* terminated. If passed null, discovery check is not done.
* @return Builder instance
*/
public Builder withEurekaClient(EurekaClient eurekaClient) {
this.eurekaClient = eurekaClient;
return this;
}
public Builder withTaskToDomain(Map<String, String> taskToDomain) {
this.taskToDomain = taskToDomain;
return this;
}
public Builder withTaskThreadCount(Map<String, Integer> taskThreadCount) {
this.taskThreadCount = taskThreadCount;
return this;
}
/**
* Builds an instance of the WorkerHost.
*
* <p>
* Please see {@link WorkerHost#init()} method. The method must be called after
* this constructor for the polling to start.
*/
public WorkerHost build() {
return new WorkerHost(this);
}
}
/**
* @return Thread Count for the shared executor pool
*/
@Deprecated
public int getThreadCount() {
return threadCount;
}
/**
* @return Thread Count for individual task type
*/
public Map<String, Integer> getTaskThreadCount() {
return taskThreadCount;
}
/**
* @return seconds before forcing shutdown of worker
*/
public int getShutdownGracePeriodSeconds() {
return shutdownGracePeriodSeconds;
}
/**
* @return sleep time in millisecond before task update retry is done when
* receiving error from the Conductor server
*/
public int getSleepWhenRetry() {
return sleepWhenRetry;
}
/**
* @return Number of times updateTask should be retried when receiving error
* from Conductor server
*/
public int getUpdateRetryCount() {
return updateRetryCount;
}
/**
* @return prefix used for worker names
*/
public String getWorkerNamePrefix() {
return workerNamePrefix;
}
/**
* Starts the polling. Must be called after {@link WorkerHost.Builder#build()}
* method.
*/
public synchronized void init() {
this.taskPollExecutor = new WorkerProcess(
eurekaClient, taskClient, updateRetryCount,
taskToDomain, workerNamePrefix, taskThreadCount);
this.scheduledExecutorService = Executors.newScheduledThreadPool(workers.size());
workers.forEach(worker -> scheduledExecutorService.scheduleWithFixedDelay(() -> taskPollExecutor.pollAndExecute(worker),
worker.getPollingInterval(),
worker.getPollingInterval(),
TimeUnit.MILLISECONDS));
}
/**
* Invoke this method within a PreDestroy block within your application to
* facilitate a graceful shutdown of your worker, during process termination.
*/
public void shutdown() {
taskPollExecutor.shutdownAndAwaitTermination(scheduledExecutorService, shutdownGracePeriodSeconds);
taskPollExecutor.shutdown(shutdownGracePeriodSeconds);
}
}