Search in sources :

Example 1 with QUERY

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;
    }
}
Also used : Arrays(java.util.Arrays) BATCH_TYPE(org.apache.cassandra.fql.FullQueryLogger.BATCH_TYPE) File(org.apache.cassandra.io.util.File) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) GENERATED_NOW_IN_SECONDS(org.apache.cassandra.fql.FullQueryLogger.GENERATED_NOW_IN_SECONDS) Unpooled(io.netty.buffer.Unpooled) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SINGLE_QUERY(org.apache.cassandra.fql.FullQueryLogger.SINGLE_QUERY) After(org.junit.After) VERSION(org.apache.cassandra.fql.FullQueryLogger.VERSION) Path(java.nio.file.Path) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) QueryState(org.apache.cassandra.service.QueryState) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Util(org.apache.cassandra.Util) QUERY(org.apache.cassandra.fql.FullQueryLogger.QUERY) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) VALUES(org.apache.cassandra.fql.FullQueryLogger.VALUES) ObjectSizes(org.apache.cassandra.utils.ObjectSizes) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) ArrayList(java.util.ArrayList) BATCH(org.apache.cassandra.fql.FullQueryLogger.BATCH) PROTOCOL_VERSION(org.apache.cassandra.fql.FullQueryLogger.PROTOCOL_VERSION) Query(org.apache.cassandra.fql.FullQueryLogger.Query) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) QUERY_START_TIME(org.apache.cassandra.fql.FullQueryLogger.QUERY_START_TIME) Semaphore(java.util.concurrent.Semaphore) ClientState(org.apache.cassandra.service.ClientState) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TYPE(org.apache.cassandra.fql.FullQueryLogger.TYPE) Batch(org.apache.cassandra.fql.FullQueryLogger.Batch) GENERATED_TIMESTAMP(org.apache.cassandra.fql.FullQueryLogger.GENERATED_TIMESTAMP) WireOut(net.openhft.chronicle.wire.WireOut) ValueIn(net.openhft.chronicle.wire.ValueIn) RollCycles(net.openhft.chronicle.queue.RollCycles) Type(org.apache.cassandra.cql3.statements.BatchStatement.Type) QUERY_OPTIONS(org.apache.cassandra.fql.FullQueryLogger.QUERY_OPTIONS) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Assert.assertEquals(org.junit.Assert.assertEquals) QUERIES(org.apache.cassandra.fql.FullQueryLogger.QUERIES) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) LinkedList(java.util.LinkedList)

Example 2 with QUERY

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());
        }));
    }
}
Also used : Arrays(java.util.Arrays) BATCH_TYPE(org.apache.cassandra.fql.FullQueryLogger.BATCH_TYPE) File(org.apache.cassandra.io.util.File) StringUtils(org.apache.commons.lang3.StringUtils) ByteBuffer(java.nio.ByteBuffer) GENERATED_NOW_IN_SECONDS(org.apache.cassandra.fql.FullQueryLogger.GENERATED_NOW_IN_SECONDS) Unpooled(io.netty.buffer.Unpooled) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SINGLE_QUERY(org.apache.cassandra.fql.FullQueryLogger.SINGLE_QUERY) After(org.junit.After) VERSION(org.apache.cassandra.fql.FullQueryLogger.VERSION) Path(java.nio.file.Path) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) QueryState(org.apache.cassandra.service.QueryState) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Util(org.apache.cassandra.Util) QUERY(org.apache.cassandra.fql.FullQueryLogger.QUERY) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) FileUtils(org.apache.cassandra.io.util.FileUtils) Assert.assertFalse(org.junit.Assert.assertFalse) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) VALUES(org.apache.cassandra.fql.FullQueryLogger.VALUES) ObjectSizes(org.apache.cassandra.utils.ObjectSizes) BeforeClass(org.junit.BeforeClass) CQLTester(org.apache.cassandra.cql3.CQLTester) BatchStatement(org.apache.cassandra.cql3.statements.BatchStatement) ArrayList(java.util.ArrayList) BATCH(org.apache.cassandra.fql.FullQueryLogger.BATCH) PROTOCOL_VERSION(org.apache.cassandra.fql.FullQueryLogger.PROTOCOL_VERSION) Query(org.apache.cassandra.fql.FullQueryLogger.Query) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) LinkedList(java.util.LinkedList) Nullable(javax.annotation.Nullable) QUERY_START_TIME(org.apache.cassandra.fql.FullQueryLogger.QUERY_START_TIME) Semaphore(java.util.concurrent.Semaphore) ClientState(org.apache.cassandra.service.ClientState) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TYPE(org.apache.cassandra.fql.FullQueryLogger.TYPE) Batch(org.apache.cassandra.fql.FullQueryLogger.Batch) GENERATED_TIMESTAMP(org.apache.cassandra.fql.FullQueryLogger.GENERATED_TIMESTAMP) WireOut(net.openhft.chronicle.wire.WireOut) ValueIn(net.openhft.chronicle.wire.ValueIn) RollCycles(net.openhft.chronicle.queue.RollCycles) Type(org.apache.cassandra.cql3.statements.BatchStatement.Type) QUERY_OPTIONS(org.apache.cassandra.fql.FullQueryLogger.QUERY_OPTIONS) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Assert.assertEquals(org.junit.Assert.assertEquals) QUERIES(org.apache.cassandra.fql.FullQueryLogger.QUERIES) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 3 with QUERY

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);
}
Also used : Query(org.apache.cassandra.fql.FullQueryLogger.Query) WireOut(net.openhft.chronicle.wire.WireOut) Semaphore(java.util.concurrent.Semaphore) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Test(org.junit.Test)

Example 4 with QUERY

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);
}
Also used : Query(org.apache.cassandra.fql.FullQueryLogger.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WireOut(net.openhft.chronicle.wire.WireOut) Semaphore(java.util.concurrent.Semaphore) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Test(org.junit.Test)

Example 5 with QUERY

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());
}
Also used : Query(org.apache.cassandra.fql.FullQueryLogger.Query) QueryOptions(org.apache.cassandra.cql3.QueryOptions) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Test(org.junit.Test)

Aggregations

Query (org.apache.cassandra.fql.FullQueryLogger.Query)5 BinLogTest (org.apache.cassandra.utils.binlog.BinLogTest)5 Test (org.junit.Test)5 Semaphore (java.util.concurrent.Semaphore)4 WireOut (net.openhft.chronicle.wire.WireOut)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 QueryOptions (org.apache.cassandra.cql3.QueryOptions)3 Unpooled (io.netty.buffer.Unpooled)2 ByteBuffer (java.nio.ByteBuffer)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)2 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)2 RollCycles (net.openhft.chronicle.queue.RollCycles)2 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)2