Search in sources :

Example 26 with DocumentContext

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

the class RollCycleMultiThreadTest method testRead2.

@Test
public void testRead2() throws Exception {
    File path = DirectoryUtils.tempDir("testRead2");
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
        final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 1 data");
            }
            Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer).get());
            // two days pass
            timeProvider.add(TimeUnit.DAYS.toMillis(2));
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 3 data");
            }
            Assert.assertEquals(2, (int) scheduledExecutorService.submit(observer).get());
            System.out.println(queue.dump());
            assertEquals(2, observer.documentsRead);
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExecutorService(java.util.concurrent.ExecutorService) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 27 with DocumentContext

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

the class RollCycleMultiThreadTest method testRead1.

@Test
public void testRead1() throws Exception {
    File path = DirectoryUtils.tempDir(getClass().getSimpleName());
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
        ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            Assert.assertEquals(0, (int) scheduledExecutorService.submit(observer::call).get());
            // two days pass
            timeProvider.add(TimeUnit.DAYS.toMillis(2));
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 3 data");
            }
            Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer::call).get());
            assertEquals(1, observer.documentsRead);
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExecutorService(java.util.concurrent.ExecutorService) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 28 with DocumentContext

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

the class SingleChronicleQueueStoreTest method writeMessagesStoreIndices.

private static long[] writeMessagesStoreIndices(final ExcerptAppender appender, final ExcerptTailer tailer) {
    final long[] indices = new long[RECORD_COUNT];
    for (int i = 0; i < RECORD_COUNT; i++) {
        try (final DocumentContext ctx = appender.writingDocument()) {
            ctx.wire().getValueOut().int32(i);
        }
    }
    for (int i = 0; i < RECORD_COUNT; i++) {
        try (final DocumentContext ctx = tailer.readingDocument()) {
            assertThat("Expected record at index " + i, ctx.isPresent(), is(true));
            indices[i] = tailer.index();
        }
    }
    return indices;
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 29 with DocumentContext

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

the class GarbageFreeMethodPublisher method onEightyByteMessage.

@Override
public void onEightyByteMessage(final EightyByteMessage message) {
    final ExcerptAppender appender = outputSupplier.get();
    // DebugTimestamps.operationStart(DebugTimestamps.Operation.GET_WRITING_DOCUMENT);
    @NotNull DocumentContext context = appender.writingDocument();
    // DebugTimestamps.operationEnd(DebugTimestamps.Operation.GET_WRITING_DOCUMENT);
    try {
        Wire wire = context.wire();
        // DebugTimestamps.operationStart(DebugTimestamps.Operation.WRITE_EVENT);
        try {
            wire.write(MethodReader.HISTORY).marshallable(MessageHistory.get());
            final ValueOut valueOut = wire.writeEventName("onEightyByteMessage");
            valueOut.object(EightyByteMessage.class, message);
            wire.padToCacheAlign();
        } finally {
        // DebugTimestamps.operationEnd(DebugTimestamps.Operation.WRITE_EVENT);
        }
    } finally {
        // DebugTimestamps.operationStart(DebugTimestamps.Operation.CLOSE_CONTEXT);
        try {
            context.close();
        } finally {
        // DebugTimestamps.operationEnd(DebugTimestamps.Operation.CLOSE_CONTEXT);
        }
    }
}
Also used : ValueOut(net.openhft.chronicle.wire.ValueOut) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with DocumentContext

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

the class VisibilityOfMessagesBetweenTailorsAndAppenderTest method test.

/**
 * check if a message is written with an appender its visible to the tailor, without locks etc.
 *
 * @throws InterruptedException
 * @throws ExecutionException
 */
@Test
public void test() throws InterruptedException, ExecutionException {
    SingleChronicleQueue x = SingleChronicleQueueBuilder.binary(getTmpDir()).build();
    ExecutorService e1 = Executors.newSingleThreadExecutor();
    e1.submit(() -> {
        ExcerptAppender excerptAppender = x.acquireAppender();
        for (long i = 0; i < 1_000_000; i++) {
            try (DocumentContext dc = excerptAppender.writingDocument()) {
                dc.wire().getValueOut().int64(i);
            }
            lastWrittenIndex = excerptAppender.lastIndexAppended();
        }
    });
    ExecutorService e2 = Executors.newSingleThreadExecutor();
    Future f2 = e2.submit(() -> {
        ExcerptTailer tailer = x.createTailer();
        for (; ; ) {
            long i = lastWrittenIndex;
            if (i != Long.MIN_VALUE)
                if (!tailer.moveToIndex(i))
                    throw new ExecutionException("non atomic, index=" + Long.toHexString(i), null);
        }
    });
    try {
        f2.get(5, TimeUnit.SECONDS);
    } catch (TimeoutException ignore) {
    }
    e2.shutdownNow();
    e1.shutdownNow();
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) 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