Search in sources :

Example 16 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class NotCompleteTest method testUsingANotCompleteArrayQueue.

@Test
public void testUsingANotCompleteArrayQueue() throws InterruptedException {
    BinaryLongArrayReference.startCollecting();
    File tmpDir = DirectoryUtils.tempDir("testUsingANotCompleteArrayQueue");
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write("some").text("data");
        }
        Thread.sleep(100);
        // System.out.println(queue.dump());
        // this is what will corrupt the queue
        BinaryLongArrayReference.forceAllToNotCompleteState();
    }
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().timeoutMS(500).build()) {
        // System.out.println(queue.dump());
        ExcerptTailer tailer = queue.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            assertEquals("data", dc.wire().read(() -> "some").text());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 17 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class NotCompleteTest method testSkipSafeLengthOverBlock.

@Ignore("store.writePosition() not set after we recover, but not trivial to fix. Problem only occurs rarely")
@Test
public void testSkipSafeLengthOverBlock() {
    File tmpDir = DirectoryUtils.tempDir("testSkipSafeLengthOverBlock");
    // 3rd time will do it
    for (int i = 0; i < 8; i++) {
        try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeoutMS(1).build()) {
            ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
            // start a message which won't be completed.
            DocumentContext dc = appender.writingDocument();
            // 2nd and subsequent times we call this will invoke recovery
            dc.wire().write("some").text("data");
        // don't call dc.close();
        }
    }
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().build()) {
        ExcerptTailer tailer = queue.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            assertFalse(dc.isPresent());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class NotCompleteTest method testUsingANotCompleteQueue.

/**
 * tests that when flags are set to not complete we are able to recover
 */
@Test
public void testUsingANotCompleteQueue() throws InterruptedException {
    BinaryLongReference.startCollecting();
    File tmpDir = DirectoryUtils.tempDir("testUsingANotCompleteQueue");
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write("some").text("data");
        }
        Thread.sleep(100);
        // System.out.println(queue.dump());
        // this is what will corrupt the queue
        BinaryLongReference.forceAllToNotCompleteState();
    }
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().timeoutMS(500).build()) {
        // System.out.println(queue.dump());
        ExcerptTailer tailer = queue.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            assertEquals("data", dc.wire().read(() -> "some").text());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 19 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class QueueWireHandler method onEvent.

@SuppressWarnings("UnusedReturnValue")
void onEvent() throws IOException {
    // Be careful not to use Wires.acquireStringBuilder() as we
    // need to store the value
    inWire.readDocument(w -> {
        w.read(CoreFields.csp).text(cspText).read(CoreFields.tid).int64(x -> tid = x).read(CoreFields.cid).int64(x -> cid = x);
        queue = getQueue(cspText);
    }, dataWireIn -> {
        ValueIn vin = inWire.readEventName(eventName);
        try {
            // writes out the tid
            outWire.writeDocument(true, wire -> outWire.write(CoreFields.tid).int64(tid));
            if (EventId.lastWrittenIndex.contentEquals(eventName)) {
                writeData(wireOut -> wireOut.write(CoreFields.reply).int64(queue.lastWrittenIndex()));
            } else if (EventId.createAppender.contentEquals(eventName)) {
                // only need one appender per queue
                queueToAppender.computeIfAbsent(queue, s -> {
                    try {
                        return queue.createAppender();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                });
                outWire.writeDocument(false, wireOut -> {
                    QueueAppenderResponse qar = new QueueAppenderResponse();
                    qar.setCid(cid);
                    qar.setCsp(cspText);
                    wireOut.write(CoreFields.reply).typedMarshallable(qar);
                });
            } else if (EventId.submit.contentEquals(eventName)) {
                ExcerptAppender appender = queueToAppender.get(queue);
                appender.writeDocument(wo -> wo.bytes().write(vin.bytes()));
                outWire.writeDocument(false, wire -> wire.write(EventId.index).int64(appender.lastWrittenIndex()));
            } else if (EventId.createTailer.contentEquals(eventName)) {
                // only need one appender per queue
                queueToTailer.computeIfAbsent(queue, s -> {
                    try {
                        return queue.createTailer();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                });
                outWire.writeDocument(false, wireOut -> {
                    QueueTailerResponse qar = new QueueTailerResponse();
                    qar.setCid(cid);
                    qar.setCsp(cspText);
                    wireOut.write(CoreFields.reply).typedMarshallable(qar);
                });
            } else if (EventId.hasNext.contentEquals(eventName)) {
                ExcerptTailer tailer = queueToTailer.get(queue);
                vin.marshallable((ReadMarshallable) rm -> {
                    long index = rm.read(() -> "index").int64();
                    sendBackMessage(tailer, index);
                });
            }
        } finally {
            if (EventGroup.IS_DEBUG) {
                long len = outWire.bytes().position() - SIZE_OF_SIZE;
                if (len == 0) {
                    System.out.println("--------------------------------------------\n" + "server writes:\n\n<EMPTY>");
                } else {
                    System.out.println("--------------------------------------------\n" + "server writes:\n\n" + Wires.fromSizePrefixedBlobs(outWire.bytes(), SIZE_OF_SIZE, len));
                }
            }
        }
    });
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Logger(org.slf4j.Logger) StreamCorruptedException(java.io.StreamCorruptedException) EventGroup(net.openhft.chronicle.network.event.EventGroup) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Bytes(net.openhft.chronicle.bytes.Bytes) EventId(net.openhft.chronicle.engine.client.internal.ClientWiredChronicleQueueStateless.EventId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) net.openhft.chronicle.wire(net.openhft.chronicle.wire) Map(java.util.Map) ClientWiredStatelessTcpConnectionHub(net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) IOException(java.io.IOException) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 20 with ExcerptTailer

use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.

the class Indexer method index.

/**
 * Scans through every excerpts and records every 64th addressForRead in the index2index'
 *
 * @
 */
public synchronized void index() {
    final ExcerptTailer tailer = chronicle.createTailer();
    for (long i = 0; i <= chronicle.lastIndex(); i++) {
        final long index = i;
        tailer.readDocument(wireIn -> {
            long address = wireIn.bytes().position() - 4;
            recordAddress(index, address);
            wireIn.bytes().skip(wireIn.bytes().remaining());
        });
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Aggregations

ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)45 Test (org.junit.Test)39 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)34 File (java.io.File)24 DocumentContext (net.openhft.chronicle.wire.DocumentContext)22 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)15 MappedFile (net.openhft.chronicle.bytes.MappedFile)11 ArrayList (java.util.ArrayList)5 MethodReader (net.openhft.chronicle.bytes.MethodReader)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 IOException (java.io.IOException)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Bytes (net.openhft.chronicle.bytes.Bytes)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)3 Wire (net.openhft.chronicle.wire.Wire)3 ByteBuffer (java.nio.ByteBuffer)2 Future (java.util.concurrent.Future)2