Search in sources :

Example 21 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class IndexTest method shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex.

@Test
public void shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex() throws Exception {
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).testBlockSize().wireType(this.wireType).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final int messageCount = INDEXING_LINEAR_SCAN_THRESHOLD + 5;
        final long[] indices = new long[messageCount];
        for (int i = 0; i < messageCount; i++) {
            try (final DocumentContext ctx = appender.writingDocument()) {
                ctx.wire().write("event").int32(i);
                indices[i] = ctx.index();
            }
        }
        final SingleChronicleQueueExcerpts.StoreTailer tailer = (SingleChronicleQueueExcerpts.StoreTailer) queue.createTailer();
        tailer.moveToIndex(indices[0]);
        assertThat(tailer.index(), is(indices[0]));
        assertThat(tailer.getIndexMoveCount(), is(1));
        tailer.moveToIndex(indices[0]);
        assertThat(tailer.index(), is(indices[0]));
        assertThat(tailer.getIndexMoveCount(), is(1));
        tailer.moveToIndex(indices[2]);
        assertThat(tailer.index(), is(indices[2]));
        assertThat(tailer.getIndexMoveCount(), is(1));
        tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2]);
        assertThat(tailer.index(), is(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2]));
        assertThat(tailer.getIndexMoveCount(), is(2));
        // document that moving backwards requires an index scan
        tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1]);
        assertThat(tailer.index(), is(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1]));
        assertThat(tailer.getIndexMoveCount(), is(3));
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 22 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 23 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 24 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext 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 25 with DocumentContext

use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.

the class PretoucherTest method shouldHandleCycleRoll.

@Test
public void shouldHandleCycleRoll() throws Exception {
    try (final SingleChronicleQueue queue = createQueue(path, clock::get)) {
        final Pretoucher pretoucher = new Pretoucher(queue, chunkListener, capturedCycles::add);
        range(0, 10).forEach(i -> {
            try (final DocumentContext ctx = queue.acquireAppender().writingDocument()) {
                ctx.wire().write().int32(i);
                pretoucher.execute();
                ctx.wire().write().bytes(new byte[1024]);
            }
            pretoucher.execute();
            clock.addAndGet(TimeUnit.SECONDS.toMillis(5L));
        });
        assertThat(capturedCycles.size(), is(10));
        assertThat(chunkListener.chunkMap.isEmpty(), is(false));
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)54 Test (org.junit.Test)41 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)28 File (java.io.File)22 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)22 MappedFile (net.openhft.chronicle.bytes.MappedFile)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)8 Wire (net.openhft.chronicle.wire.Wire)8 NotNull (org.jetbrains.annotations.NotNull)6 Ignore (org.junit.Ignore)6 Future (java.util.concurrent.Future)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Bytes (net.openhft.chronicle.bytes.Bytes)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)3