Search in sources :

Example 76 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project cassandra by apache.

the class Dump method dump.

public static void dump(List<String> arguments, String rollCycle, boolean follow) {
    StringBuilder sb = new StringBuilder();
    ReadMarshallable reader = wireIn -> {
        sb.setLength(0);
        int version = wireIn.read(BinLog.VERSION).int16();
        if (version > FullQueryLogger.CURRENT_VERSION) {
            throw new IORuntimeException("Unsupported record version [" + version + "] - highest supported version is [" + FullQueryLogger.CURRENT_VERSION + ']');
        }
        String type = wireIn.read(BinLog.TYPE).text();
        if (!FullQueryLogger.SINGLE_QUERY.equals((type)) && !FullQueryLogger.BATCH.equals((type))) {
            throw new IORuntimeException("Unsupported record type field [" + type + "] - supported record types are [" + FullQueryLogger.SINGLE_QUERY + ", " + FullQueryLogger.BATCH + ']');
        }
        sb.append("Type: ").append(type).append(System.lineSeparator());
        long queryStartTime = wireIn.read(FullQueryLogger.QUERY_START_TIME).int64();
        sb.append("Query start time: ").append(queryStartTime).append(System.lineSeparator());
        int protocolVersion = wireIn.read(FullQueryLogger.PROTOCOL_VERSION).int32();
        sb.append("Protocol version: ").append(protocolVersion).append(System.lineSeparator());
        QueryOptions options = QueryOptions.codec.decode(Unpooled.wrappedBuffer(wireIn.read(FullQueryLogger.QUERY_OPTIONS).bytes()), ProtocolVersion.decode(protocolVersion, true));
        long generatedTimestamp = wireIn.read(FullQueryLogger.GENERATED_TIMESTAMP).int64();
        sb.append("Generated timestamp:").append(generatedTimestamp).append(System.lineSeparator());
        int generatedNowInSeconds = wireIn.read(FullQueryLogger.GENERATED_NOW_IN_SECONDS).int32();
        sb.append("Generated nowInSeconds:").append(generatedNowInSeconds).append(System.lineSeparator());
        switch(type) {
            case (FullQueryLogger.SINGLE_QUERY):
                dumpQuery(options, wireIn, sb);
                break;
            case (FullQueryLogger.BATCH):
                dumpBatch(options, wireIn, sb);
                break;
            default:
                throw new IORuntimeException("Log entry of unsupported type " + type);
        }
        System.out.print(sb.toString());
        System.out.flush();
    };
    // Backoff strategy for spinning on the queue, not aggressive at all as this doesn't need to be low latency
    Pauser pauser = Pauser.millis(100);
    List<ChronicleQueue> queues = arguments.stream().distinct().map(path -> SingleChronicleQueueBuilder.single(new File(path)).readOnly(true).rollCycle(RollCycles.valueOf(rollCycle)).build()).collect(Collectors.toList());
    List<ExcerptTailer> tailers = queues.stream().map(ChronicleQueue::createTailer).collect(Collectors.toList());
    boolean hadWork = true;
    while (hadWork) {
        hadWork = false;
        for (ExcerptTailer tailer : tailers) {
            while (tailer.readDocument(reader)) {
                hadWork = true;
            }
        }
        if (follow) {
            if (!hadWork) {
                // Chronicle queue doesn't support blocking so use this backoff strategy
                pauser.pause();
            }
            // Don't terminate the loop even if there wasn't work
            hadWork = true;
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) Unpooled(io.netty.buffer.Unpooled) Bytes(net.openhft.chronicle.bytes.Bytes) Option(io.airlift.airline.Option) ReadMarshallable(net.openhft.chronicle.wire.ReadMarshallable) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Pauser(net.openhft.chronicle.threads.Pauser) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) BinLog(org.apache.cassandra.utils.binlog.BinLog) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Collectors(java.util.stream.Collectors) File(java.io.File) BufferUnderflowException(java.nio.BufferUnderflowException) WireIn(net.openhft.chronicle.wire.WireIn) Command(io.airlift.airline.Command) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) List(java.util.List) ValueIn(net.openhft.chronicle.wire.ValueIn) FullQueryLogger(org.apache.cassandra.fql.FullQueryLogger) RollCycles(net.openhft.chronicle.queue.RollCycles) Arguments(io.airlift.airline.Arguments) Collections(java.util.Collections) QueryOptions(org.apache.cassandra.cql3.QueryOptions) QueryOptions(org.apache.cassandra.cql3.QueryOptions) Pauser(net.openhft.chronicle.threads.Pauser) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) ReadMarshallable(net.openhft.chronicle.wire.ReadMarshallable) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) File(java.io.File)

Example 77 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project cassandra by apache.

the class FullQueryLoggerTest method testRoundTripBatchWithKeyspace.

@Test
public void testRoundTripBatchWithKeyspace() throws Exception {
    configureFQL();
    logBatch(Type.UNLOGGED, Arrays.asList("foo1", "foo2"), Arrays.asList(Arrays.asList(ByteBuffer.allocate(1), ByteBuffer.allocateDirect(2)), Collections.emptyList()), QueryOptions.DEFAULT, queryState("abcdefgh"), 1);
    Util.spinAssertEquals(true, () -> {
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
            return queue.createTailer().readingDocument().isPresent();
        }
    }, 60);
    assertRoundTripBatch("abcdefgh");
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Test(org.junit.Test)

Example 78 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project cassandra by apache.

the class FullQueryLoggerTest method testRoundTripBatchWithKeyspaceNull.

@Test
public void testRoundTripBatchWithKeyspaceNull() throws Exception {
    configureFQL();
    logBatch(Type.UNLOGGED, Arrays.asList("foo1", "foo2"), Arrays.asList(Arrays.asList(ByteBuffer.allocate(1), ByteBuffer.allocateDirect(2)), Collections.emptyList()), QueryOptions.DEFAULT, queryState(), 1);
    Util.spinAssertEquals(true, () -> {
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(tempDir.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
            return queue.createTailer().readingDocument().isPresent();
        }
    }, 60);
    assertRoundTripBatch(null);
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) BinLogTest(org.apache.cassandra.utils.binlog.BinLogTest) Test(org.junit.Test)

Example 79 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project cassandra by apache.

the class AuditLogViewerTest method testIgnoreFutureVersionRecord.

@Test
public void testIgnoreFutureVersionRecord() {
    List<String> records = new ArrayList<>();
    records.add("Test foo bar 1");
    records.add("Test foo bar 2");
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(path.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        // Write future record
        appender.writeDocument(createFutureRecord());
        // Write bunch of current records
        records.forEach(s -> appender.writeDocument(new BinAuditLogger.Message(s)));
        // Read those written records
        List<String> actualRecords = new ArrayList<>();
        AuditLogViewer.dump(ImmutableList.of(path.toString()), RollCycles.TEST_SECONDLY.toString(), false, true, actualRecords::add);
        // Assert all current records are present
        assertRecordsMatch(records, actualRecords);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 80 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project cassandra by apache.

the class AuditLogViewerTest method testIgnoreUnknownTypeRecord.

@Test
public void testIgnoreUnknownTypeRecord() {
    List<String> records = new ArrayList<>();
    records.add("Test foo bar 1");
    records.add("Test foo bar 2");
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.single(path.toFile()).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        // Write unrecognized type record
        appender.writeDocument(createUnknownTypeRecord());
        // Write bunch of supported records
        records.forEach(s -> appender.writeDocument(new BinAuditLogger.Message(s)));
        // Read those written records
        List<String> actualRecords = new ArrayList<>();
        AuditLogViewer.dump(ImmutableList.of(path.toString()), RollCycles.TEST_SECONDLY.toString(), false, true, actualRecords::add);
        // Assert all supported records are present
        assertRecordsMatch(records, actualRecords);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)80 Test (org.junit.Test)59 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)37 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)37 File (java.io.File)36 DocumentContext (net.openhft.chronicle.wire.DocumentContext)21 ArrayList (java.util.ArrayList)16 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)14 MethodReader (net.openhft.chronicle.bytes.MethodReader)12 List (java.util.List)11 IOException (java.io.IOException)9 MappedFile (net.openhft.chronicle.bytes.MappedFile)8 Path (java.nio.file.Path)7 Bytes (net.openhft.chronicle.bytes.Bytes)7 Collections (java.util.Collections)6 RollCycles (net.openhft.chronicle.queue.RollCycles)6 NotNull (org.jetbrains.annotations.NotNull)6 ByteBuffer (java.nio.ByteBuffer)5 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)5 Histogram (net.openhft.chronicle.core.util.Histogram)5