Search in sources :

Example 16 with DocumentContext

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

the class DocumentOrderingTest method codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty.

@Test
public void codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty() throws Exception {
    try (final SingleChronicleQueue queue = builder(DirectoryUtils.tempDir("document-ordering"), 3_000L).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final Future<RecordInfo> otherDocumentWriter;
        try (final DocumentContext documentContext = excerptAppender.writingDocument()) {
            // move time to beyond the next cycle
            clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
            otherDocumentWriter = attemptToWriteDocument(queue);
            // stall this thread, other thread should not be able to advance,
            // since this DocumentContext is still open
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2L));
            documentContext.wire().getValueOut().int32(counter.getAndIncrement());
        }
        assertEquals(1, otherDocumentWriter.get(5L, TimeUnit.SECONDS).counterValue);
        final ExcerptTailer tailer = queue.createTailer();
        expectValue(0, tailer);
        expectValue(1, tailer);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 17 with DocumentContext

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

the class DocumentOrderingTest method shouldRecoverFromUnfinishedFirstMessageInPreviousQueue.

@Test
public void shouldRecoverFromUnfinishedFirstMessageInPreviousQueue() throws Exception {
    // as below, but don't actually close the initial context
    try (final SingleChronicleQueue queue = builder(DirectoryUtils.tempDir("document-ordering"), 1_000L).progressOnContention(progressOnContention).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final Future<RecordInfo> otherDocumentWriter;
        // begin a record in the first cycle file
        final DocumentContext documentContext = excerptAppender.writingDocument();
        documentContext.wire().getValueOut().int32(counter.getAndIncrement());
        // move time to beyond the next cycle
        clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
        otherDocumentWriter = attemptToWriteDocument(queue);
        assertEquals(1, otherDocumentWriter.get(5L, TimeUnit.SECONDS).counterValue);
        final ExcerptTailer tailer = queue.createTailer();
        expectValue(1, tailer);
        assertThat(tailer.readingDocument().isPresent(), is(false));
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 18 with DocumentContext

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

the class DocumentOrderingTest method attemptToWriteDocument.

private Future<RecordInfo> attemptToWriteDocument(final SingleChronicleQueue queue) throws InterruptedException {
    final CountDownLatch startedLatch = new CountDownLatch(1);
    final Future<RecordInfo> future = executorService.submit(() -> {
        final int counterValue;
        startedLatch.countDown();
        try (final DocumentContext documentContext = queue.acquireAppender().writingDocument()) {
            counterValue = counter.getAndIncrement();
            documentContext.wire().getValueOut().int32(counterValue);
        }
        return new RecordInfo(counterValue);
    });
    assertTrue("Task did not start", startedLatch.await(1, TimeUnit.MINUTES));
    return future;
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 19 with DocumentContext

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

the class DuplicateMessageReadTest method write.

private static void write(final ExcerptAppender appender, final Data data) throws Exception {
    try (final DocumentContext dc = appender.writingDocument()) {
        final ObjectOutput out = dc.wire().objectOutput();
        out.writeInt(data.id);
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 20 with DocumentContext

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

the class EofMarkerOnEmptyQueueTest method shouldRecoverFromEmptyQueueOnRoll.

@Test
public void shouldRecoverFromEmptyQueueOnRoll() throws Exception {
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpFolder.newFolder()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(clock::get).timeoutMS(1_000).testBlockSize().build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final DocumentContext context = appender.writingDocument();
        // start to write a message, but don't close the context - simulates crashed writer
        final long expectedEofMarkerPosition = context.wire().bytes().writePosition() - Wires.SPB_HEADER_SIZE;
        context.wire().writeEventName("foo").int32(1);
        final int startCycle = queue.cycle();
        clock.addAndGet(TimeUnit.SECONDS.toMillis(1L));
        final int nextCycle = queue.cycle();
        // ensure that the cycle file will roll
        assertThat(startCycle, is(not(nextCycle)));
        Executors.newSingleThreadExecutor().submit(() -> {
            try (final DocumentContext nextCtx = queue.acquireAppender().writingDocument()) {
                nextCtx.wire().writeEventName("bar").int32(7);
            }
        }).get(3, TimeUnit.SECONDS);
        final WireStore firstCycleStore = queue.storeForCycle(startCycle, 0, false);
        final long firstCycleWritePosition = firstCycleStore.writePosition();
        // assert that no write was completed
        assertThat(firstCycleWritePosition, is(0L));
        final ExcerptTailer tailer = queue.createTailer();
        int recordCount = 0;
        int lastItem = -1;
        while (true) {
            try (final DocumentContext readCtx = tailer.readingDocument()) {
                if (!readCtx.isPresent()) {
                    break;
                }
                final StringBuilder name = new StringBuilder();
                final ValueIn field = readCtx.wire().readEventName(name);
                recordCount++;
                lastItem = field.int32();
            }
        }
        assertThat(firstCycleStore.bytes().readVolatileInt(expectedEofMarkerPosition), is(Wires.END_OF_DATA));
        assertThat(recordCount, is(1));
        assertThat(lastItem, is(7));
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) WireStore(net.openhft.chronicle.queue.impl.WireStore) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) 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