-
Notifications
You must be signed in to change notification settings - Fork 473
Expand file tree
/
Copy pathRetryCapableBatchWriter.java
More file actions
169 lines (149 loc) · 5.98 KB
/
RetryCapableBatchWriter.java
File metadata and controls
169 lines (149 loc) · 5.98 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
package org.influxdb.impl;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBException;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.function.BiConsumer;
/**
* Batch writer that tries to retry a write if it failed previously and
* the reason of the failure is not permanent.
*/
class RetryCapableBatchWriter implements BatchWriter {
private InfluxDB influxDB;
private BiConsumer<Iterable<Point>, Throwable> exceptionHandler;
private LinkedList<BatchPoints> batchQueue;
private int requestActionsLimit;
private int retryBufferCapacity;
private int usedRetryBufferCapacity;
RetryCapableBatchWriter(final InfluxDB influxDB, final BiConsumer<Iterable<Point>, Throwable> exceptionHandler,
final int retryBufferCapacity, final int requestActionsLimit) {
this.influxDB = influxDB;
this.exceptionHandler = exceptionHandler;
batchQueue = new LinkedList<>();
this.retryBufferCapacity = retryBufferCapacity;
this.requestActionsLimit = requestActionsLimit;
}
private enum WriteResultOutcome { WRITTEN, FAILED_RETRY_POSSIBLE, FAILED_RETRY_IMPOSSIBLE }
private static final class WriteResult {
static final WriteResult WRITTEN = new WriteResult(WriteResultOutcome.WRITTEN);
WriteResultOutcome outcome;
Throwable throwable;
private WriteResult(final WriteResultOutcome outcome) {
this.outcome = outcome;
}
private WriteResult(final WriteResultOutcome outcome, final Throwable throwable) {
this.outcome = outcome;
this.throwable = throwable;
}
private WriteResult(final InfluxDBException e) {
this.throwable = e;
if (e.isRetryWorth()) {
this.outcome = WriteResultOutcome.FAILED_RETRY_POSSIBLE;
} else {
this.outcome = WriteResultOutcome.FAILED_RETRY_IMPOSSIBLE;
}
}
}
/* This method is synchronized to avoid parallel execution when the user invokes flush/close
* of the client in the middle of scheduled write execution (buffer flush / action limit overrun) */
@Override
public synchronized void write(final Collection<BatchPoints> collection) {
// empty the cached data first
emptyCachedData(collection);
// write the last given batch last so that duplicate data points get overwritten correctly
writeLastBatch(collection);
}
private void emptyCachedData(final Collection<BatchPoints> collection){
ListIterator<BatchPoints> batchQueueIterator = batchQueue.listIterator();
while (batchQueueIterator.hasNext()) {
BatchPoints entry = batchQueueIterator.next();
WriteResult result = tryToWrite(entry);
if (result.outcome == WriteResultOutcome.WRITTEN
|| result.outcome == WriteResultOutcome.FAILED_RETRY_IMPOSSIBLE) {
batchQueueIterator.remove();
usedRetryBufferCapacity -= entry.getPoints().size();
// we are throwing out data, notify the client
if (result.outcome == WriteResultOutcome.FAILED_RETRY_IMPOSSIBLE) {
exceptionHandler.accept(entry.getPoints(), result.throwable);
}
} else {
// we cannot send more data otherwise we would write them in different
// order than in which were submitted
for (BatchPoints batchPoints : collection) {
addToBatchQueue(batchPoints);
}
return;
}
}
}
private void writeLastBatch(final Collection<BatchPoints> collection){
Iterator<BatchPoints> collectionIterator = collection.iterator();
while (collectionIterator.hasNext()) {
BatchPoints batchPoints = collectionIterator.next();
WriteResult result = tryToWrite(batchPoints);
switch (result.outcome) {
case FAILED_RETRY_POSSIBLE:
addToBatchQueue(batchPoints);
while (collectionIterator.hasNext()) {
addToBatchQueue(collectionIterator.next());
}
break;
case FAILED_RETRY_IMPOSSIBLE:
exceptionHandler.accept(batchPoints.getPoints(), result.throwable);
break;
default:
}
}
}
/* This method is synchronized to avoid parallel execution when the BatchProcessor scheduler
* has been shutdown but there are jobs still being executed (using RetryCapableBatchWriter.write).*/
@Override
public synchronized void close() {
// try to write everything queued / buffered
for (BatchPoints points : batchQueue) {
WriteResult result = tryToWrite(points);
if (result.outcome != WriteResultOutcome.WRITTEN) {
exceptionHandler.accept(points.getPoints(), result.throwable);
}
}
}
private WriteResult tryToWrite(final BatchPoints batchPoints) {
try {
influxDB.write(batchPoints);
return WriteResult.WRITTEN;
} catch (InfluxDBException e) {
return new WriteResult(e);
} catch (Exception e) {
return new WriteResult(WriteResultOutcome.FAILED_RETRY_POSSIBLE, e);
}
}
private void evictTooOldFailedWrites() {
while (usedRetryBufferCapacity > retryBufferCapacity && batchQueue.size() > 0) {
List<Point> points = batchQueue.removeFirst().getPoints();
usedRetryBufferCapacity -= points.size();
exceptionHandler.accept(points,
new InfluxDBException.RetryBufferOverrunException(
"Retry buffer overrun, current capacity: " + retryBufferCapacity));
}
}
private void addToBatchQueue(final BatchPoints batchPoints) {
boolean hasBeenMergedIn = false;
if (batchQueue.size() > 0) {
BatchPoints last = batchQueue.getLast();
if (last.getPoints().size() + batchPoints.getPoints().size() <= requestActionsLimit) {
hasBeenMergedIn = last.mergeIn(batchPoints);
}
}
if (!hasBeenMergedIn) {
batchQueue.add(batchPoints);
}
// recalculate local counter and evict old batches on merge as well
usedRetryBufferCapacity += batchPoints.getPoints().size();
evictTooOldFailedWrites();
}
}