Search in sources :

Example 86 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 ExecutionException, InterruptedException {
    File path = getTmpDir();
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
        ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).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);
        }
        scheduledExecutorService.shutdown();
        scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) NamedThreadFactory(net.openhft.chronicle.threads.NamedThreadFactory) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 87 with DocumentContext

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

the class RollCycleMultiThreadTest method testRead2.

@SuppressWarnings("unchecked")
@Test
public void testRead2() throws ExecutionException, InterruptedException {
    File path = getTmpDir();
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
        final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).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);
        }
        scheduledExecutorService.shutdown();
        scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) NamedThreadFactory(net.openhft.chronicle.threads.NamedThreadFactory) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 88 with DocumentContext

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

the class QueueLargeMessageJLBHBenchmark method run.

@Override
public void run(long startTimeNS) {
    if (MSG_DIRECT)
        bytesArr2.readLimit(MSG_LENGTH);
    try (DocumentContext dc = appender.writingDocument()) {
        Bytes<?> bytes = dc.wire().bytes();
        bytes.writeLong(startTimeNS);
        bytes.writeInt(bytes.length());
        if (MSG_DIRECT)
            bytes.write(bytesArr2);
        else
            bytes.write(bytesArr);
    }
    try (DocumentContext dc = tailer.readingDocument()) {
        if (dc.wire() != null) {
            Bytes<?> bytes = dc.wire().bytes();
            long start = bytes.readLong();
            int length = bytes.readInt();
            assert length == MSG_LENGTH;
            if (MSG_DIRECT)
                bytes.read(bytesArr2.clear(), length);
            else
                bytes.read(bytesArr);
            jlbh.sample(System.nanoTime() - start);
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 89 with DocumentContext

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

the class InternalAppenderWriteBytesTest method testJumpingAMessageThrowsAIllegalStateException.

@Test(expected = IllegalStateException.class)
public void testJumpingAMessageThrowsAIllegalStateException() {
    try (SingleChronicleQueue q = binary(tempDir("q")).rollCycle(MINUTELY).timeProvider(() -> 0).build();
        ExcerptAppender appender = q.acquireAppender()) {
        appender.writeText("hello");
        appender.writeText("hello2");
        try (final DocumentContext dc = appender.writingDocument()) {
            dc.wire().bytes().writeLong(1);
        }
        final long l = appender.lastIndexAppended();
        final RollCycle rollCycle = q.rollCycle();
        final int currentCycle = rollCycle.toCycle(l);
        // try to write to next roll cycle and write at seqnum 1 (but miss the 0th seqnum of that roll cycle)
        final long index = rollCycle.toIndex(currentCycle + 1, 1);
        ((InternalAppender) appender).writeBytes(index, Bytes.from("text"));
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 90 with DocumentContext

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

the class LastAcknowledgedTest method testLastAcknowledge.

@Test
public void testLastAcknowledge() {
    String name = OS.getTarget() + "/testLastAcknowledge-" + Time.uniqueId();
    long lastIndexAppended;
    try (ChronicleQueue q = SingleChronicleQueueBuilder.single(name).testBlockSize().build()) {
        ExcerptAppender excerptAppender = q.acquireAppender();
        excerptAppender.writeText("Hello World");
        lastIndexAppended = excerptAppender.lastIndexAppended();
        ExcerptTailer tailer = q.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            assertTrue(dc.isData());
            assertEquals(lastIndexAppended, tailer.index());
        }
        ExcerptTailer tailer2 = q.createTailer();
        tailer2.readAfterReplicaAcknowledged(true);
        try (DocumentContext dc = tailer2.readingDocument()) {
            assertFalse(dc.isPresent());
        }
    }
    try (ChronicleQueue q = SingleChronicleQueueBuilder.single(name).testBlockSize().build()) {
        assertEquals(-1, q.lastAcknowledgedIndexReplicated());
        q.lastAcknowledgedIndexReplicated(lastIndexAppended - 1);
        ExcerptTailer tailer2 = q.createTailer();
        tailer2.readAfterReplicaAcknowledged(true);
        try (DocumentContext dc = tailer2.readingDocument()) {
            assertFalse(dc.isPresent());
        }
        q.lastAcknowledgedIndexReplicated(lastIndexAppended);
        try (DocumentContext dc = tailer2.readingDocument()) {
            assertTrue(dc.isData());
            assertEquals(lastIndexAppended, tailer2.index());
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)127 Test (org.junit.Test)76 File (java.io.File)46 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)32 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)27 Wire (net.openhft.chronicle.wire.Wire)23 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)22 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)13 NotNull (org.jetbrains.annotations.NotNull)12 Bytes (net.openhft.chronicle.bytes.Bytes)11 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)11 MappedFile (net.openhft.chronicle.bytes.MappedFile)10 Ignore (org.junit.Ignore)7 ArrayList (java.util.ArrayList)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 ValueOut (net.openhft.chronicle.wire.ValueOut)6 Histogram (net.openhft.chronicle.core.util.Histogram)5 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)5 NamedThreadFactory (net.openhft.chronicle.threads.NamedThreadFactory)5 IOException (java.io.IOException)4