Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BatchProcessor exception handling so that scheduler is not cancelled after connection error #144

Merged
merged 2 commits into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/main/java/org/influxdb/impl/BatchProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
Expand All @@ -24,6 +26,8 @@
*
*/
public class BatchProcessor {

private static final Logger logger = Logger.getLogger(BatchProcessor.class.getName());
protected final BlockingQueue<BatchEntry> queue = new LinkedBlockingQueue<>();
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final InfluxDBImpl influxDB;
Expand Down Expand Up @@ -144,26 +148,31 @@ public void run() {
}

void write() {
if (this.queue.isEmpty()) {
return;
}

Map<String, BatchPoints> databaseToBatchPoints = Maps.newHashMap();
List<BatchEntry> batchEntries = new ArrayList<>(this.queue.size());
this.queue.drainTo(batchEntries);
try {
if (this.queue.isEmpty()) {
return;
}

for (BatchEntry batchEntry : batchEntries) {
String dbName = batchEntry.getDb();
if (!databaseToBatchPoints.containsKey(dbName)) {
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy(batchEntry.getRp()).build();
databaseToBatchPoints.put(dbName, batchPoints);
Map<String, BatchPoints> databaseToBatchPoints = Maps.newHashMap();
List<BatchEntry> batchEntries = new ArrayList<>(this.queue.size());
this.queue.drainTo(batchEntries);

for (BatchEntry batchEntry : batchEntries) {
String dbName = batchEntry.getDb();
if (!databaseToBatchPoints.containsKey(dbName)) {
BatchPoints batchPoints = BatchPoints.database(dbName).retentionPolicy(batchEntry.getRp()).build();
databaseToBatchPoints.put(dbName, batchPoints);
}
Point point = batchEntry.getPoint();
databaseToBatchPoints.get(dbName).point(point);
}
Point point = batchEntry.getPoint();
databaseToBatchPoints.get(dbName).point(point);
}

for (BatchPoints batchPoints : databaseToBatchPoints.values()) {
BatchProcessor.this.influxDB.write(batchPoints);
for (BatchPoints batchPoints : databaseToBatchPoints.values()) {
BatchProcessor.this.influxDB.write(batchPoints);
}
} catch (Throwable t) {
// any exception would stop the scheduler
logger.log(Level.SEVERE, "Batch could not be sent. Data will be lost", t);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/test/java/org/influxdb/impl/BatchProcessorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.influxdb.impl;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.concurrent.TimeUnit;

import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.testng.annotations.Test;

public class BatchProcessorTest {

@Test
public void testSchedulerExceptionHandling() throws InterruptedException {
InfluxDB mockInfluxDB = mock(InfluxDBImpl.class);
BatchProcessor batchProcessor = BatchProcessor.builder(mockInfluxDB).actions(Integer.MAX_VALUE)
.interval(1, TimeUnit.NANOSECONDS).build();

doThrow(new RuntimeException()).when(mockInfluxDB).write(any(BatchPoints.class));

Point point = Point.measurement("cpu").field("6", "").build();
BatchProcessor.BatchEntry batchEntry1 = new BatchProcessor.BatchEntry(point, "db1", "");
BatchProcessor.BatchEntry batchEntry2 = new BatchProcessor.BatchEntry(point, "db2", "");

batchProcessor.put(batchEntry1);
Thread.sleep(200); // wait for scheduler

// first try throws an exception
verify(mockInfluxDB, times(1)).write(any(BatchPoints.class));

batchProcessor.put(batchEntry2);
Thread.sleep(200); // wait for scheduler
// without try catch the 2nd time does not occur
verify(mockInfluxDB, times(2)).write(any(BatchPoints.class));
}
}