use of io.questdb.mp.SOUnboundedCountDownLatch in project questdb by bluestreak01.
the class LineTcpReceiverTest method testSomeWritersReleased.
@Test
public void testSomeWritersReleased() throws Exception {
runInContext((receiver) -> {
String lineData = "weather,location=us-midwest temperature=85 1465839830102300200\n" + "weather,location=us-eastcoast temperature=89 1465839830102400200\n" + "weather,location=us-westcost temperature=82 1465839830102500200\n";
int iterations = 8;
int threadCount = 8;
CharSequenceObjHashMap<SOUnboundedCountDownLatch> tableIndex = new CharSequenceObjHashMap<>();
tableIndex.put("weather", new SOUnboundedCountDownLatch());
for (int i = 1; i < threadCount; i++) {
tableIndex.put("weather" + i, new SOUnboundedCountDownLatch());
}
// One engine hook for all writers
engine.setPoolListener((factoryType, thread, name, event, segment, position) -> {
if (factoryType == PoolListener.SRC_WRITER && event == PoolListener.EV_RETURN) {
tableIndex.get(name).countDown();
}
});
try {
sendAndWait(receiver, lineData, tableIndex, 1);
SOCountDownLatch threadPushFinished = new SOCountDownLatch(threadCount - 1);
for (int i = 1; i < threadCount; i++) {
final String threadTable = "weather" + i;
final String lineDataThread = lineData.replace("weather", threadTable);
sendNoWait(receiver, threadTable, lineDataThread);
new Thread(() -> {
try {
for (int n = 0; n < iterations; n++) {
Os.sleep(minIdleMsBeforeWriterRelease - 50);
send(receiver, lineDataThread, threadTable, WAIT_NO_WAIT);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPushFinished.countDown();
}
}).start();
}
sendAndWait(receiver, lineData, tableIndex, 2);
try (TableWriter w = engine.getWriter(AllowAllCairoSecurityContext.INSTANCE, "weather", "testing")) {
w.truncate();
}
sendAndWait(receiver, lineData, tableIndex, 4);
String header = "location\ttemperature\ttimestamp\n";
String[] lines = { "us-midwest\t85.0\t2016-06-13T17:43:50.102300Z\n", "us-eastcoast\t89.0\t2016-06-13T17:43:50.102400Z\n", "us-westcost\t82.0\t2016-06-13T17:43:50.102500Z\n" };
String expected = header + lines[0] + lines[1] + lines[2];
assertTable(expected, "weather");
// Concatenate iterations + 1 of identical insert results
// to assert against weather 1-8 tables
StringBuilder expectedSB = new StringBuilder(header);
for (int l = 0; l < lines.length; l++) {
expectedSB.append(Chars.repeat(lines[l], iterations + 1));
}
// Wait async ILP send threads to finish.
threadPushFinished.await();
for (int i = 1; i < threadCount; i++) {
// Wait writer to be released and check.
String tableName = "weather" + i;
try {
try {
assertTable(expectedSB, tableName);
} catch (AssertionError e) {
int releasedCount = -tableIndex.get(tableName).getCount();
// Wait one more writer release before re-trying to compare
wait(tableIndex.get(tableName), releasedCount + 1, minIdleMsBeforeWriterRelease);
assertTable(expectedSB, tableName);
}
} catch (Throwable err) {
LOG.error().$("Error '").$(err.getMessage()).$("' comparing table: ").$(tableName).$();
throw err;
}
}
} finally {
// Clean engine hook
engine.setPoolListener((factoryType, thread, name, event, segment, position) -> {
});
}
});
}
Aggregations