use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.
the class FullQueryLoggerTest method testBlocking.
/**
* Test that a thread will block if the FQL is over weight, and unblock once the backup is cleared
*/
@Test
public void testBlocking() throws Exception {
configureFQL();
// Prevent the bin log thread from making progress, causing the task queue to block
Semaphore blockBinLog = new Semaphore(0);
try {
// Find out when the bin log thread has been blocked, necessary to not run into batch task drain behavior
Semaphore binLogBlocked = new Semaphore(0);
FullQueryLogger.instance.binLog.put(new Query("foo1", QueryOptions.DEFAULT, queryState(), 1) {
public void writeMarshallablePayload(WireOut wire) {
// Notify that the bin log is blocking now
binLogBlocked.release();
try {
// Block the bin log thread so the task queue can be filled
blockBinLog.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
super.writeMarshallablePayload(wire);
}
public void release() {
super.release();
}
});
// Wait for the bin log thread to block so it can't batch drain tasks
Util.spinAssertEquals(true, binLogBlocked::tryAcquire, 60);
// Now fill the task queue
logQuery("foo2");
// Start a thread to block waiting on the bin log queue
Thread t = new Thread(() -> {
logQuery("foo3");
// Should be able to log another query without an issue
logQuery("foo4");
});
t.start();
Thread.sleep(500);
// If thread state is terminated then the thread started, finished, and didn't block on the full task queue
assertTrue(t.getState() != Thread.State.TERMINATED);
} finally {
// Unblock the binlog thread
blockBinLog.release();
}
Util.spinAssertEquals(true, () -> checkForQueries(Arrays.asList("foo1", "foo2", "foo3", "foo4")), 60);
}
use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.
the class FullQueryLoggerTest method testNonBlocking.
@Test
public void testNonBlocking() throws Exception {
FullQueryLogger.instance.enable(tempDir, "TEST_SECONDLY", false, 1, 1024 * 1024 * 256, StringUtils.EMPTY, 10);
// Prevent the bin log thread from making progress, causing the task queue to refuse tasks
Semaphore blockBinLog = new Semaphore(0);
try {
// Find out when the bin log thread has been blocked, necessary to not run into batch task drain behavior
Semaphore binLogBlocked = new Semaphore(0);
FullQueryLogger.instance.binLog.put(new Query("foo1", QueryOptions.DEFAULT, queryState(), 1) {
public void writeMarshallablePayload(WireOut wire) {
// Notify that the bin log is blocking now
binLogBlocked.release();
try {
// Block the bin log thread so the task queue can be filled
blockBinLog.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
super.writeMarshallablePayload(wire);
}
public void release() {
super.release();
}
});
// Wait for the bin log thread to block so it can't batch drain tasks
Util.spinAssertEquals(true, binLogBlocked::tryAcquire, 60);
// Now fill the task queue
logQuery("foo2");
// This sample should get dropped AKA released without being written
AtomicInteger releasedCount = new AtomicInteger(0);
AtomicInteger writtenCount = new AtomicInteger(0);
FullQueryLogger.instance.binLog.logRecord(new Query("foo3", QueryOptions.DEFAULT, queryState(), 1) {
public void writeMarshallablePayload(WireOut wire) {
writtenCount.incrementAndGet();
super.writeMarshallablePayload(wire);
}
public void release() {
releasedCount.incrementAndGet();
super.release();
}
});
Util.spinAssertEquals(1, releasedCount::get, 60);
assertEquals(0, writtenCount.get());
} finally {
blockBinLog.release();
}
// Wait for tasks to drain so there should be space in the queue
Util.spinAssertEquals(true, () -> checkForQueries(Arrays.asList("foo1", "foo2")), 60);
// Should be able to log again
logQuery("foo4");
Util.spinAssertEquals(true, () -> checkForQueries(Arrays.asList("foo1", "foo2", "foo4")), 60);
}
use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.
the class FQLReplayTest method testUnknownRecord.
@Test(expected = IORuntimeException.class)
public void testUnknownRecord() throws Exception {
FQLQueryReader reader = new FQLQueryReader();
File dir = Files.createTempDirectory("chronicle").toFile();
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(dir).build()) {
ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(new BinLog.ReleaseableWriteMarshallable() {
protected long version() {
return FullQueryLogger.CURRENT_VERSION;
}
protected String type() {
return "unknown-type";
}
public void writeMarshallablePayload(WireOut wire) {
wire.write("unknown-field").text("unknown_value");
}
public void release() {
}
});
ExcerptTailer tailer = queue.createTailer();
tailer.readDocument(reader);
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unsupported record type field"));
throw e;
}
}
use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.
the class FQLReplayTest method testFutureVersion.
@Test(expected = IORuntimeException.class)
public void testFutureVersion() throws Exception {
FQLQueryReader reader = new FQLQueryReader();
File dir = Files.createTempDirectory("chronicle").toFile();
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(dir).build()) {
ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(new BinLog.ReleaseableWriteMarshallable() {
protected long version() {
return 999;
}
protected String type() {
return FullQueryLogger.SINGLE_QUERY;
}
public void writeMarshallablePayload(WireOut wire) {
wire.write("future-field").text("future_value");
}
public void release() {
}
});
ExcerptTailer tailer = queue.createTailer();
tailer.readDocument(reader);
} catch (Exception e) {
assertTrue(e.getMessage().contains("Unsupported record version"));
throw e;
}
}
use of net.openhft.chronicle.wire.WireOut in project cassandra by apache.
the class BinLogTest method testBinLogStartStop.
/**
* Check that we can start and stop the bin log and that it releases resources held by any subsequent appended
* records
*/
@Test
public void testBinLogStartStop() throws Exception {
AtomicInteger releaseCount = new AtomicInteger();
CountDownLatch ready = new CountDownLatch(2);
Supplier<BinLog.ReleaseableWriteMarshallable> recordSupplier = () -> new BinLog.ReleaseableWriteMarshallable() {
public void release() {
releaseCount.incrementAndGet();
}
protected long version() {
return 0;
}
protected String type() {
return "test";
}
public void writeMarshallablePayload(WireOut wire) {
ready.countDown();
}
};
binLog.put(recordSupplier.get());
binLog.put(recordSupplier.get());
ready.await(1, TimeUnit.MINUTES);
Util.spinAssertEquals("Both records should be released", 2, releaseCount::get, 10, TimeUnit.SECONDS);
Thread t = new Thread(() -> {
try {
binLog.stop();
} catch (InterruptedException e) {
throw new AssertionError(e);
}
});
t.start();
t.join(60 * 1000);
assertEquals("BinLog should not take more than 1 minute to stop", t.getState(), Thread.State.TERMINATED);
Util.spinAssertEquals(2, releaseCount::get, 60);
Util.spinAssertEquals(Thread.State.TERMINATED, binLog.binLogThread::getState, 60);
}
Aggregations