-
Notifications
You must be signed in to change notification settings - Fork 45
/
QueueSettings.java
388 lines (349 loc) · 12.8 KB
/
QueueSettings.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
package ru.yandex.money.common.dbqueue.settings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* Queue settings
*
* @author Oleg Kandaurov
* @since 10.07.2017
*/
public final class QueueSettings {
private static final Function<String, Duration> DURATION_CONVERTER =
Memoizer.memoize(Duration::parse);
private static final Duration DEFAULT_TIMEOUT_ON_FATAL_CRASH = Duration.ofSeconds(1L);
private final int threadCount;
@Nonnull
private final Duration noTaskTimeout;
@Nonnull
private final Duration betweenTaskTimeout;
@Nonnull
private final Duration fatalCrashTimeout;
@Nonnull
private final Duration retryInterval;
@Nonnull
private final TaskRetryType retryType;
@Nonnull
private final ReenqueueRetrySettings reenqueueRetrySettings;
@Nonnull
private final ProcessingMode processingMode;
@Nonnull
private final Map<String, String> additionalSettings;
private QueueSettings(@Nonnull Duration noTaskTimeout,
@Nonnull Duration betweenTaskTimeout,
@Nullable Duration fatalCrashTimeout,
@Nullable Integer threadCount,
@Nullable TaskRetryType retryType,
@Nullable Duration retryInterval,
@Nullable ReenqueueRetrySettings reenqueueRetrySettings,
@Nullable ProcessingMode processingMode,
@Nullable Map<String, String> additionalSettings) {
this.noTaskTimeout = Objects.requireNonNull(noTaskTimeout);
this.betweenTaskTimeout = Objects.requireNonNull(betweenTaskTimeout);
this.threadCount = threadCount == null ? 1 : threadCount;
this.fatalCrashTimeout = fatalCrashTimeout == null ? DEFAULT_TIMEOUT_ON_FATAL_CRASH : fatalCrashTimeout;
this.retryType = retryType == null ? TaskRetryType.GEOMETRIC_BACKOFF : retryType;
this.retryInterval = retryInterval == null ? Duration.ofMinutes(1) : retryInterval;
this.reenqueueRetrySettings = reenqueueRetrySettings == null
? ReenqueueRetrySettings.createDefault()
: reenqueueRetrySettings;
this.processingMode = processingMode == null ? ProcessingMode.SEPARATE_TRANSACTIONS : processingMode;
this.additionalSettings = additionalSettings == null ? Collections.emptyMap() :
Collections.unmodifiableMap(new HashMap<>(additionalSettings));
}
/**
* Get task execution retry strategy.
*
* @return Task execution retry strategy.
*/
@Nonnull
public TaskRetryType getRetryType() {
return retryType;
}
/**
* Get retry interval for task execution.
*
* @return Task retry interval.
* @see TaskRetryType
*/
@Nonnull
public Duration getRetryInterval() {
return retryInterval;
}
/**
* Settings for the task postponing strategy
* when the task should be brought back to the queue.
*
* @return Task postponing settings.
*/
@Nonnull
public ReenqueueRetrySettings getReenqueueRetrySettings() {
return reenqueueRetrySettings;
}
/**
* Get number of threads for processing tasks in the queue.
*
* @return Number of processing threads.
*/
public int getThreadCount() {
return threadCount;
}
/**
* Get delay duration between picking tasks from the queue if there are no task for processing.
*
* @return Delay when there are no tasks to process.
*/
@Nonnull
public Duration getNoTaskTimeout() {
return noTaskTimeout;
}
/**
* Get delay duration between picking tasks from the queue after the task was processed.
*
* @return Delay after next task was processed.
*/
@Nonnull
public Duration getBetweenTaskTimeout() {
return betweenTaskTimeout;
}
/**
* Get delay duration when task execution thread sleeps after unexpected error.
*
* @return Delay after unexpected error.
*/
@Nonnull
public Duration getFatalCrashTimeout() {
return fatalCrashTimeout;
}
/**
* Get task processing mode in the queue.
*
* @return Task processing mode.
*/
@Nonnull
public ProcessingMode getProcessingMode() {
return processingMode;
}
/**
* Get all additional properties for the queue.
*
* @return Additional queue properties.
*/
@Nonnull
public Map<String, String> getAdditionalSettings() {
return additionalSettings;
}
/**
* Get string value of additional queue property.
*
* @param settingName Name of the property.
* @return Property value.
*/
@Nonnull
public String getProperty(@Nonnull String settingName) {
Objects.requireNonNull(settingName);
return Objects.requireNonNull(additionalSettings.get(settingName),
String.format("null values are not allowed: settingName=%s", settingName));
}
/**
* Get {@linkplain Duration} value of additional queue property.
*
* @param settingName Name of the property.
* @return Property value.
*/
@Nonnull
public Duration getDurationProperty(@Nonnull String settingName) {
return DURATION_CONVERTER.apply(getProperty(settingName));
}
/**
* Create a new builder for queue settings.
*
* @return A new builder for queue settings.
*/
public static Builder builder() {
return new Builder();
}
@Override
public String toString() {
return '{' +
"threadCount=" + threadCount +
", betweenTaskTimeout=" + betweenTaskTimeout +
", noTaskTimeout=" + noTaskTimeout +
", processingMode=" + processingMode +
", retryType=" + retryType +
", retryInterval=" + retryInterval +
", reenqueueRetrySettings=" + reenqueueRetrySettings +
", fatalCrashTimeout=" + fatalCrashTimeout +
(additionalSettings.isEmpty() ? "" : ", additionalSettings=" + additionalSettings) +
'}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
QueueSettings that = (QueueSettings) obj;
return threadCount == that.threadCount &&
retryType == that.retryType &&
processingMode == that.processingMode &&
Objects.equals(reenqueueRetrySettings, that.reenqueueRetrySettings) &&
Objects.equals(noTaskTimeout, that.noTaskTimeout) &&
Objects.equals(betweenTaskTimeout, that.betweenTaskTimeout) &&
Objects.equals(fatalCrashTimeout, that.fatalCrashTimeout) &&
Objects.equals(retryInterval, that.retryInterval) &&
Objects.equals(additionalSettings, that.additionalSettings);
}
@Override
public int hashCode() {
return Objects.hash(threadCount, noTaskTimeout, betweenTaskTimeout, fatalCrashTimeout, retryType, reenqueueRetrySettings,
processingMode, retryInterval, additionalSettings);
}
/**
* A builder for queue settings.
*/
public static class Builder {
private Duration noTaskTimeout;
private Duration betweenTaskTimeout;
private Duration fatalCrashTimeout;
private Integer threadCount;
private TaskRetryType retryType;
private Duration retryInterval;
private ReenqueueRetrySettings reenqueueRetrySettings;
private ProcessingMode processingMode;
private final Map<String, String> additionalSettings = new HashMap<>();
private Builder() {
}
/**
* Set delay duration between picking tasks from the queue
* if there are no task for processing.
*
* @param noTaskTimeout Delay when there are no tasks to process.
* @return Reference to the same builder.
*/
public Builder withNoTaskTimeout(@Nonnull Duration noTaskTimeout) {
this.noTaskTimeout = Objects.requireNonNull(noTaskTimeout);
return this;
}
/**
* Set delay duration between picking tasks from the queue
* after the task was processed.
*
* @param betweenTaskTimeout Delay after next task was processed.
* @return Reference to the same builder.
*/
public Builder withBetweenTaskTimeout(@Nonnull Duration betweenTaskTimeout) {
this.betweenTaskTimeout = Objects.requireNonNull(betweenTaskTimeout);
return this;
}
/**
* Set delay duration after unexpected error.
*
* @param fatalCrashTimeout Delay after unexpected error.
* @return Reference to the same builder.
*/
public Builder withFatalCrashTimeout(@Nullable Duration fatalCrashTimeout) {
this.fatalCrashTimeout = fatalCrashTimeout;
return this;
}
/**
* Set number of threads for processing tasks in the queue.
*
* @param threadCount Number of processing threads.
* @return Reference to the same builder.
*/
public Builder withThreadCount(@Nullable Integer threadCount) {
this.threadCount = threadCount;
return this;
}
/**
* Set task execution retry strategy.
*
* @param taskRetryType Task execution retry strategy.
* @return Reference to the same builder.
*/
public Builder withRetryType(@Nullable TaskRetryType taskRetryType) {
this.retryType = taskRetryType;
return this;
}
/**
* Set retry interval for task execution.
*
* @param retryInterval Task retry interval.
* @return Reference to the same builder.
*/
public Builder withRetryInterval(@Nullable Duration retryInterval) {
this.retryInterval = retryInterval;
return this;
}
/**
* Set Settings for the task postponing strategy
* when the task should be brought back to the queue.
*
* @param reenqueueRetrySettings Task postponing settings.
* @return Reference to the same builder.
*/
public Builder withReenqueueRetrySettings(@Nullable ReenqueueRetrySettings reenqueueRetrySettings) {
this.reenqueueRetrySettings = reenqueueRetrySettings;
return this;
}
/**
* Set task processing mode in the queue.
*
* @param processingMode Task processing mode.
* @return Reference to the same builder.
*/
public Builder withProcessingMode(@Nullable ProcessingMode processingMode) {
this.processingMode = processingMode;
return this;
}
/**
* Set the map of additional properties for the queue.
*
* @param additionalSettings Additional properties for the queue.
* @return Reference to the same builder.
*/
public Builder withAdditionalSettings(@Nonnull Map<String, String> additionalSettings) {
Objects.requireNonNull(additionalSettings);
this.additionalSettings.putAll(additionalSettings);
return this;
}
/**
* Set additional property for the queue.
*
* @param settingName Property name.
* @param value Property value.
* @return Reference to the same builder.
*/
public Builder putSetting(String settingName, String value) {
additionalSettings.put(settingName, value);
return this;
}
/**
* Create new queue settings object.
*
* @return A new queue settings object.
*/
public QueueSettings build() {
return new QueueSettings(noTaskTimeout, betweenTaskTimeout, fatalCrashTimeout, threadCount,
retryType, retryInterval, reenqueueRetrySettings, processingMode, additionalSettings);
}
}
private static class Memoizer<T, U> {
private final Map<T, U> cache = new ConcurrentHashMap<>();
private Function<T, U> doMemoize(Function<T, U> function) {
return input -> cache.computeIfAbsent(input, function);
}
private static <T, U> Function<T, U> memoize(Function<T, U> function) {
return new Memoizer<T, U>().doMemoize(function);
}
}
}