Skip to content

Commit ca347db

Browse files
authored
Merge pull request #282 from andyflury/feature-batchprocessor-capacity
Initial capacity on LinkedBlockingQueue inside BatchProcessor
2 parents a7fa63a + 1beafd3 commit ca347db

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/main/java/org/influxdb/impl/BatchProcessor.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
public class BatchProcessor {
3131

3232
private static final Logger LOG = Logger.getLogger(BatchProcessor.class.getName());
33-
protected final BlockingQueue<AbstractBatchEntry> queue = new LinkedBlockingQueue<>();
33+
protected final BlockingQueue<AbstractBatchEntry> queue;
3434
private final ScheduledExecutorService scheduler;
3535
final InfluxDBImpl influxDB;
3636
final int actions;
@@ -171,6 +171,11 @@ public static Builder builder(final InfluxDB influxDB) {
171171
this.flushIntervalUnit = flushIntervalUnit;
172172
this.flushInterval = flushInterval;
173173
this.scheduler = Executors.newSingleThreadScheduledExecutor(threadFactory);
174+
if (actions > 1 && actions < Integer.MAX_VALUE) {
175+
this.queue = new LinkedBlockingQueue<>(actions);
176+
} else {
177+
this.queue = new LinkedBlockingQueue<>();
178+
}
174179
// Flush at specified Rate
175180
this.scheduler.scheduleAtFixedRate(new Runnable() {
176181
@Override
@@ -238,7 +243,11 @@ void write() {
238243
* the batchEntry to write to the cache.
239244
*/
240245
void put(final AbstractBatchEntry batchEntry) {
241-
this.queue.add(batchEntry);
246+
try {
247+
this.queue.put(batchEntry);
248+
} catch (InterruptedException e) {
249+
throw new RuntimeException(e);
250+
}
242251
if (this.queue.size() >= this.actions) {
243252
this.scheduler.submit(new Runnable() {
244253
@Override

src/test/java/org/influxdb/InfluxDBTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ public void testAsyncWritePointThroughUDP() {
209209
this.influxDB.disableBatch();
210210
}
211211
}
212+
213+
214+
/**
215+
* Test the implementation of {@link InfluxDB#write(int, Point)}'s async support.
216+
*/
217+
@Test(expected = RuntimeException.class)
218+
public void testAsyncWritePointThroughUDPFail() {
219+
this.influxDB.enableBatch(1, 1, TimeUnit.SECONDS);
220+
try{
221+
Assert.assertTrue(this.influxDB.isBatchEnabled());
222+
String measurement = TestUtils.getRandomMeasurement();
223+
Point point = Point.measurement(measurement).tag("atag", "test").addField("used", 80L).addField("free", 1L).build();
224+
Thread.currentThread().interrupt();
225+
this.influxDB.write(UDP_PORT, point);
226+
}finally{
227+
this.influxDB.disableBatch();
228+
}
229+
}
212230

213231
/**
214232
* Test writing to the database using string protocol.

0 commit comments

Comments
 (0)