reuse pdb writers
This commit is contained in:
@@ -158,11 +158,15 @@ public class TcpIngestor implements AutoCloseable {
|
||||
|
||||
serverThreadPool.submit(() -> {
|
||||
Thread.currentThread().setName("db-ingestion");
|
||||
try {
|
||||
db.put(new BlockingQueueIterator<>(queue, Entry.POISON));
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
|
||||
boolean finished = false;
|
||||
while (!finished) {
|
||||
try {
|
||||
db.put(new BlockingQueueIterator<>(queue, Entry.POISON));
|
||||
finished = true;
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
@@ -188,6 +192,8 @@ public class TcpIngestor implements AutoCloseable {
|
||||
} catch (final SocketTimeoutException e) {
|
||||
// expected every 100ms
|
||||
// needed to be able to stop the server
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.out.println("not accepting new connections. ");
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
@@ -52,8 +53,6 @@ public class TcpIngestorTest {
|
||||
|
||||
ingestor.start();
|
||||
|
||||
final SocketChannel channel = connect();
|
||||
|
||||
final Map<String, Object> entryA = new HashMap<>();
|
||||
entryA.put("duration", 1);
|
||||
entryA.put("@timestamp", dateA.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||
@@ -66,16 +65,7 @@ public class TcpIngestorTest {
|
||||
entryB.put("host", host);
|
||||
entryB.put("tags", Collections.emptyList());
|
||||
|
||||
final StringBuilder streamData = new StringBuilder();
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
streamData.append(mapper.writeValueAsString(entryA));
|
||||
streamData.append("\n");
|
||||
streamData.append(mapper.writeValueAsString(entryB));
|
||||
|
||||
final ByteBuffer src = ByteBuffer.wrap(streamData.toString().getBytes(StandardCharsets.UTF_8));
|
||||
channel.write(src);
|
||||
|
||||
channel.close();
|
||||
send(entryA, entryB);
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
@@ -93,6 +83,25 @@ public class TcpIngestorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
private final void send(final Map<String, Object>... entries) throws IOException {
|
||||
|
||||
final StringBuilder streamData = new StringBuilder();
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
for (final Map<String, Object> entry : entries) {
|
||||
|
||||
streamData.append(mapper.writeValueAsString(entry));
|
||||
streamData.append("\n");
|
||||
}
|
||||
|
||||
final SocketChannel channel = connect();
|
||||
final ByteBuffer src = ByteBuffer.wrap(streamData.toString().getBytes(StandardCharsets.UTF_8));
|
||||
channel.write(src);
|
||||
|
||||
channel.close();
|
||||
}
|
||||
|
||||
private SocketChannel connect() throws IOException {
|
||||
|
||||
SocketChannel result = null;
|
||||
@@ -110,4 +119,37 @@ public class TcpIngestorTest {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void testIngestionThreadDoesNotDieOnErrors() throws Exception {
|
||||
final OffsetDateTime invalidDate = OffsetDateTime.of(1969, 12, 31, 23, 59, 59, 999, ZoneOffset.UTC);
|
||||
final OffsetDateTime dateB = OffsetDateTime.now();
|
||||
final String host = "someHost";
|
||||
|
||||
try (TcpIngestor tcpIngestor = new TcpIngestor(dataDirectory)) {
|
||||
tcpIngestor.start();
|
||||
|
||||
// this entry will be skipped, because the date is invalid
|
||||
final Map<String, Object> entryA = new HashMap<>();
|
||||
entryA.put("duration", 1);
|
||||
entryA.put("@timestamp", invalidDate.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||
entryA.put("host", host);
|
||||
entryA.put("tags", Collections.emptyList());
|
||||
|
||||
final Map<String, Object> entryB = new HashMap<>();
|
||||
entryB.put("duration", 2);
|
||||
entryB.put("@timestamp", dateB.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
|
||||
entryB.put("host", host);
|
||||
entryB.put("tags", Collections.emptyList());
|
||||
|
||||
send(entryA, entryB);
|
||||
}
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
final List<Entry> result = db.get("host=" + host).singleGroup().asList();
|
||||
Assert.assertEquals(result.size(), 1);
|
||||
|
||||
Assert.assertEquals(result.get(0).getValue(), 2);
|
||||
Assert.assertEquals(result.get(0).getDate().toInstant(), dateB.toInstant());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user