use of org.apache.cassandra.fql.FullQueryLogger.Query in project cassandra by apache.
the class FullQueryLoggerTest method checkForQueries.
private boolean checkForQueries(List<String> queries) {
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
ExcerptTailer tailer = queue.createTailer();
List<String> expectedQueries = new LinkedList<>(queries);
while (!expectedQueries.isEmpty()) {
if (!tailer.readDocument(wire -> {
assertEquals(expectedQueries.get(0), wire.read("query").text());
expectedQueries.remove(0);
})) {
return false;
}
}
assertFalse(tailer.readDocument(wire -> {
}));
return true;
}
}
use of org.apache.cassandra.fql.FullQueryLogger.Query in project cassandra by apache.
the class FullQueryLoggerTest method assertRoundTripQuery.
private void assertRoundTripQuery(@Nullable String keyspace) {
try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
ExcerptTailer tailer = queue.createTailer();
assertTrue(tailer.readDocument(wire -> {
assertEquals(0, wire.read(VERSION).int16());
assertEquals(SINGLE_QUERY, wire.read(TYPE).text());
assertEquals(1L, wire.read(QUERY_START_TIME).int64());
ProtocolVersion protocolVersion = ProtocolVersion.decode(wire.read(PROTOCOL_VERSION).int32(), true);
assertEquals(ProtocolVersion.CURRENT, protocolVersion);
QueryOptions queryOptions = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wire.read(QUERY_OPTIONS).bytes()), protocolVersion);
compareQueryOptions(QueryOptions.DEFAULT, queryOptions);
String wireKeyspace = wire.read(FullQueryLogger.KEYSPACE).text();
assertEquals(keyspace, wireKeyspace);
assertEquals("foo", wire.read(QUERY).text());
}));
}
}
use of org.apache.cassandra.fql.FullQueryLogger.Query 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 org.apache.cassandra.fql.FullQueryLogger.Query 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 org.apache.cassandra.fql.FullQueryLogger.Query in project cassandra by apache.
the class FullQueryLoggerTest method testQueryWeight.
@Test
public void testQueryWeight() {
// Empty query should have some weight
Query query = new Query("", QueryOptions.DEFAULT, queryState(), 1);
assertTrue(query.weight() >= 95);
StringBuilder sb = new StringBuilder();
for (int ii = 0; ii < 1024 * 1024; ii++) {
sb.append('a');
}
query = new Query(sb.toString(), QueryOptions.DEFAULT, queryState(), 1);
// A large query should be reflected in the size, * 2 since characters are still two bytes
assertTrue(query.weight() > ObjectSizes.measureDeep(sb.toString()));
// Large query options should be reflected
QueryOptions largeOptions = QueryOptions.forInternalCalls(Arrays.asList(ByteBuffer.allocate(1024 * 1024)));
query = new Query("", largeOptions, queryState(), 1);
assertTrue(query.weight() > 1024 * 1024);
System.out.printf("weight %d%n", query.weight());
}
Aggregations