forked from influxdata/influxdb-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteOptions.java
More file actions
469 lines (425 loc) · 16.3 KB
/
WriteOptions.java
File metadata and controls
469 lines (425 loc) · 16.3 KB
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
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.client;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.concurrent.ThreadSafe;
import com.influxdb.utils.Arguments;
import io.reactivex.rxjava3.core.BackpressureOverflowStrategy;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.schedulers.Schedulers;
/**
* WriteOptions are used to configure writes the data point into InfluxDB 2.x.
*
* <p>
* The default setting use the batching configured to (consistent with Telegraf):
* <ul>
* <li>batchSize = 1000</li>
* <li>flushInterval = 1000 ms</li>
* <li>retryInterval = 5000 ms</li>
* <li>jitterInterval = 0</li>
* <li>bufferLimit = 10_000</li>
* <li>concatMapPrefetch = 2</li>
* <li>captureBackpressureData = false</li>
* </ul>
* <p>
* The default backpressure strategy is {@link BackpressureOverflowStrategy#DROP_OLDEST}.
* <p>
*
* @author Jakub Bednar (bednar@github) (21/09/2018 10:11)
*/
@ThreadSafe
public final class WriteOptions implements WriteApi.RetryOptions {
public static final int DEFAULT_BATCH_SIZE = 1000;
public static final int DEFAULT_FLUSH_INTERVAL = 1000;
public static final int DEFAULT_JITTER_INTERVAL = 0;
public static final int DEFAULT_RETRY_INTERVAL = 5000;
public static final int DEFAULT_MAX_RETRIES = 5;
public static final int DEFAULT_MAX_RETRY_DELAY = 125_000;
public static final int DEFAULT_MAX_RETRY_TIME = 180_000;
public static final int DEFAULT_EXPONENTIAL_BASE = 2;
public static final int DEFAULT_BUFFER_LIMIT = 10000;
public static final int DEFAULT_CONCAT_MAP_PREFETCH = 2;
public static final boolean DEFAULT_CAPTURE_BACKPRESSURE_DATA = false;
/**
* Default configuration with values that are consistent with Telegraf.
*/
public static final WriteOptions DEFAULTS = WriteOptions.builder().build();
private final int batchSize;
private final int flushInterval;
private final int jitterInterval;
private final int retryInterval;
private final int maxRetries;
private final int maxRetryDelay;
private final int maxRetryTime;
private final int exponentialBase;
private final int bufferLimit;
private final int concatMapPrefetch;
private final Scheduler writeScheduler;
private final BackpressureOverflowStrategy backpressureStrategy;
private final boolean captureBackpressureData;
/**
* @return the number of data point to collect in batch
* @see WriteOptions.Builder#batchSize(int)
*/
public int getBatchSize() {
return batchSize;
}
/**
* @return the time to wait at most (milliseconds)
* @see WriteOptions.Builder#flushInterval(int) (int)
*/
public int getFlushInterval() {
return flushInterval;
}
/**
* @return batch flush jitter interval value (milliseconds)
* @see WriteOptions.Builder#jitterInterval(int)
*/
@Override
public int getJitterInterval() {
return jitterInterval;
}
/**
* The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
* <br>
* Retry-After: A non-negative decimal integer indicating the seconds to delay after the response is received.
*
* @return the time to wait before retry unsuccessful write (milliseconds)
* @see WriteOptions.Builder#retryInterval(int)
*/
@Override
public int getRetryInterval() {
return retryInterval;
}
/**
* The number of max retries when write fails.
*
* @return number of max retries
* @see WriteOptions.Builder#maxRetries(int)
*/
@Override
public int getMaxRetries() {
return maxRetries;
}
/**
* The maximum delay between each retry attempt in milliseconds.
*
* @return maximum delay
* @see WriteOptions.Builder#maxRetryDelay(int)
*/
@Override
public int getMaxRetryDelay() {
return maxRetryDelay;
}
/**
* The maximum total retry timeout in milliseconds.
*
* @return maximum delay
* @see WriteOptions.Builder#maxRetryTime(int)
*/
public int getMaxRetryTime() {
return maxRetryTime;
}
/**
* The base for the exponential retry delay.
*
* The next delay is computed as: retryInterval * exponentialBase^(attempts-1) + random(jitterInterval)
*
* @return exponential base
* @see WriteOptions.Builder#exponentialBase(int)
*/
@Override
public int getExponentialBase() {
return exponentialBase;
}
/**
* @return Maximum number of points stored in the retry buffer.
* @see WriteOptions.Builder#bufferLimit(int)
*/
public int getBufferLimit() {
return bufferLimit;
}
/**
* The number of upstream items to prefetch so that fresh items are ready to be mapped when a previous
* MaybeSource terminates.
*
* @return the prefetch value for concatMapMaybe operator
* @see WriteOptions.Builder#concatMapPrefetch(int)
*/
public int getConcatMapPrefetch() {
return concatMapPrefetch;
}
/**
* @return The scheduler which is used for write data points.
* @see WriteOptions.Builder#writeScheduler(Scheduler)
*/
@Nonnull
public Scheduler getWriteScheduler() {
return writeScheduler;
}
/**
* @return the strategy to deal with buffer overflow when using onBackpressureBuffer
* @see WriteOptions.Builder#backpressureStrategy(BackpressureOverflowStrategy)
*/
@Nonnull
public BackpressureOverflowStrategy getBackpressureStrategy() {
return backpressureStrategy;
}
/**
* @return whether to capture affected data points in backpressure events
* @see WriteOptions.Builder#captureBackpressureData(boolean)
*/
public boolean getCaptureBackpressureData() {
return captureBackpressureData;
}
private WriteOptions(@Nonnull final Builder builder) {
Arguments.checkNotNull(builder, "WriteOptions.Builder");
batchSize = builder.batchSize;
flushInterval = builder.flushInterval;
jitterInterval = builder.jitterInterval;
retryInterval = builder.retryInterval;
maxRetries = builder.maxRetries;
maxRetryDelay = builder.maxRetryDelay;
maxRetryTime = builder.maxRetryTime;
exponentialBase = builder.exponentialBase;
bufferLimit = builder.bufferLimit;
concatMapPrefetch = builder.concatMapPrefetch;
writeScheduler = builder.writeScheduler;
backpressureStrategy = builder.backpressureStrategy;
captureBackpressureData = builder.captureBackpressureData;
}
/**
* Creates a builder instance.
*
* @return a builder
*/
@Nonnull
public static WriteOptions.Builder builder() {
return new WriteOptions.Builder();
}
/**
* A builder for {@code WriteOptions}.
*/
@NotThreadSafe
public static class Builder {
private int batchSize = DEFAULT_BATCH_SIZE;
private int flushInterval = DEFAULT_FLUSH_INTERVAL;
private int jitterInterval = DEFAULT_JITTER_INTERVAL;
private int retryInterval = DEFAULT_RETRY_INTERVAL;
private int maxRetries = DEFAULT_MAX_RETRIES;
private int maxRetryDelay = DEFAULT_MAX_RETRY_DELAY;
private int maxRetryTime = DEFAULT_MAX_RETRY_TIME;
private int exponentialBase = DEFAULT_EXPONENTIAL_BASE;
private int bufferLimit = DEFAULT_BUFFER_LIMIT;
private int concatMapPrefetch = DEFAULT_CONCAT_MAP_PREFETCH;
private Scheduler writeScheduler = Schedulers.newThread();
private BackpressureOverflowStrategy backpressureStrategy = BackpressureOverflowStrategy.DROP_OLDEST;
private boolean captureBackpressureData = DEFAULT_CAPTURE_BACKPRESSURE_DATA;
/**
* Set the number of data point to collect in batch.
*
* @param batchSize the number of data point to collect in batch
* @return {@code this}
*/
@Nonnull
public Builder batchSize(final int batchSize) {
Arguments.checkPositiveNumber(batchSize, "batchSize");
this.batchSize = batchSize;
return this;
}
/**
* Set the time to wait at most (milliseconds).
*
* @param flushInterval the time to wait at most (milliseconds).
* @return {@code this}
*/
@Nonnull
public Builder flushInterval(final int flushInterval) {
Arguments.checkPositiveNumber(flushInterval, "flushInterval");
this.flushInterval = flushInterval;
return this;
}
/**
* Jitters the batch flush interval by a random amount. This is primarily to avoid
* large write spikes for users running a large number of client instances.
* ie, a jitter of 5s and flush duration 10s means flushes will happen every 10-15s.
*
* @param jitterInterval (milliseconds)
* @return {@code this}
*/
@Nonnull
public Builder jitterInterval(final int jitterInterval) {
Arguments.checkNotNegativeNumber(jitterInterval, "jitterInterval");
this.jitterInterval = jitterInterval;
return this;
}
/**
* Set the the time to wait before retry unsuccessful write (milliseconds).
* <br><br>
* The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
* <br>
* Retry-After: A non-negative decimal integer indicating the seconds to delay after the response is received.
*
* @param retryInterval the time to wait before retry unsuccessful write
* @return {@code this}
*/
@Nonnull
public Builder retryInterval(final int retryInterval) {
Arguments.checkPositiveNumber(retryInterval, "retryInterval");
this.retryInterval = retryInterval;
return this;
}
/**
* The number of max retries when write fails.
*
* @param maxRetries number of max retries
* @return {@code this}
*/
@Nonnull
public Builder maxRetries(final int maxRetries) {
Arguments.checkPositiveNumber(maxRetries, "maxRetries");
this.maxRetries = maxRetries;
return this;
}
/**
* The maximum delay between each retry attempt in milliseconds.
*
* @param maxRetryDelay maximum delay
* @return {@code this}
*/
@Nonnull
public Builder maxRetryDelay(final int maxRetryDelay) {
Arguments.checkPositiveNumber(maxRetryDelay, "maxRetryDelay");
this.maxRetryDelay = maxRetryDelay;
return this;
}
/**
* The maximum total retry timeout in milliseconds.
*
* @param maxRetryTime maximum timout
* @return {@code this}
*/
@Nonnull
public Builder maxRetryTime(final int maxRetryTime) {
Arguments.checkPositiveNumber(maxRetryTime, "maxRetryTime");
this.maxRetryTime = maxRetryTime;
return this;
}
/**
* The base for the exponential retry delay.
*
* @param exponentialBase exponential base
* @return {@code this}
*/
@Nonnull
public Builder exponentialBase(final int exponentialBase) {
if (exponentialBase < 2) {
throw new IllegalArgumentException("Expecting a number >= 2 for exponentialBase");
}
this.exponentialBase = exponentialBase;
return this;
}
/**
* The client maintains a buffer for failed writes so that the writes will be retried later on. This may
* help to overcome temporary network problems or InfluxDB load spikes.
* When the buffer is full and new points are written, oldest entries in the buffer are lost.
*
* @param bufferLimit maximum number of points stored in the retry buffer
* @return {@code this}
*/
@Nonnull
public Builder bufferLimit(final int bufferLimit) {
Arguments.checkPositiveNumber(bufferLimit, "bufferLimit");
this.bufferLimit = bufferLimit;
return this;
}
/**
* Set the prefetch value for the concatMapMaybe operator that processes write batches.
*
* The number of upstream items to prefetch so that fresh items are ready to be mapped when a previous
* MaybeSource terminates.
*
* @param concatMapPrefetch the prefetch value for concatMapMaybe operator (must be positive)
* @return {@code this}
*/
@Nonnull
public Builder concatMapPrefetch(final int concatMapPrefetch) {
Arguments.checkPositiveNumber(concatMapPrefetch, "concatMapPrefetch");
this.concatMapPrefetch = concatMapPrefetch;
return this;
}
/**
* Set the scheduler which is used for write data points. It is useful for disabling batch writes or
* for tuning the performance. Default value is {@link Schedulers#newThread()}.
*
* @param writeScheduler the scheduler which is used for write data points.
* @return {@code this}
*/
@Nonnull
public Builder writeScheduler(@Nonnull final Scheduler writeScheduler) {
Arguments.checkNotNull(writeScheduler, "Write scheduler");
this.writeScheduler = writeScheduler;
return this;
}
/**
* Set the strategy to deal with buffer overflow when using onBackpressureBuffer.
*
* @param backpressureStrategy the strategy to deal with buffer overflow when using onBackpressureBuffer.
* Default {@link BackpressureOverflowStrategy#DROP_OLDEST};
* @return {@code this}
*/
@Nonnull
public Builder backpressureStrategy(@Nonnull final BackpressureOverflowStrategy backpressureStrategy) {
Arguments.checkNotNull(backpressureStrategy, "Backpressure Overflow Strategy");
this.backpressureStrategy = backpressureStrategy;
return this;
}
/**
* Set whether to capture affected data points in backpressure events.
*
* When enabled, BackpressureEvent will include the specific line protocol points
* that are affected by the backpressure condition:
* - For DROP_OLDEST strategy: points that will be dropped
* - For DROP_LATEST strategy: newest points being added
*
* Disabling this can improve performance when backpressure data capture is not needed.
*
* @param captureBackpressureData whether to capture affected data points. Default is false.
* @return {@code this}
*/
@Nonnull
public Builder captureBackpressureData(final boolean captureBackpressureData) {
this.captureBackpressureData = captureBackpressureData;
return this;
}
/**
* Build an instance of WriteOptions.
*
* @return {@code WriteOptions}
*/
@Nonnull
public WriteOptions build() {
return new WriteOptions(this);
}
}
}