Search in sources :

Example 81 with DocumentContext

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

the class TailerTest method drainTailer.

private List<String> drainTailer(ExcerptTailer tailer) {
    final List<String> result = new ArrayList<>();
    for (; ; ) {
        try (DocumentContext dc = tailer.readingDocument()) {
            if (!dc.isPresent())
                break;
            final String text = dc.wire().readText();
            System.out.format("Tailed   \"%s\" at %d%n", text, dc.index());
            result.add(text);
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 82 with DocumentContext

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

the class TailerTest method reproduce.

@Test
public void reproduce() {
    IOTools.deleteDirWithFiles(QUEUE_PATH.toFile());
    long firstOutputIndex = Long.MAX_VALUE;
    long lastOutputIndex = Long.MIN_VALUE;
    try (ChronicleQueue q = createQueue();
        ExcerptAppender appender = q.acquireAppender();
        ExcerptTailer tailer = q.createTailer()) {
        for (int i = 0; i < 5; i++) {
            final String text = "Hello World " + i;
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().writeText(text);
            }
            System.out.format("Appended \"%s\" at %d%n", text, appender.lastIndexAppended());
            firstOutputIndex = Math.min(firstOutputIndex, appender.lastIndexAppended());
            lastOutputIndex = Math.max(lastOutputIndex, appender.lastIndexAppended());
        }
        System.out.format("firstOutputIndex = %d%n", firstOutputIndex);
        System.out.format("lastOutputIndex = %d%n", lastOutputIndex);
        System.out.println("Reading initial");
        drainTailer(tailer);
    }
    try (ChronicleQueue q = createQueue();
        ExcerptTailer tailer = q.createTailer()) {
        initRecovery(tailer, firstOutputIndex + OFFSET);
        final List<String> messages = drainTailer(tailer);
        assertEquals("Hello World " + OFFSET, messages.get(0));
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 83 with DocumentContext

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

the class PretoucherTest method cycleRollByPretoucher.

private void cycleRollByPretoucher(int earlyMillis) {
    File dir = getTmpDir();
    clock.set(100);
    try (final SingleChronicleQueue queue = createQueue(dir, clock::get);
        final SingleChronicleQueue queue2 = createQueue(dir, clock::get);
        final Pretoucher pretoucher = new Pretoucher(queue2, chunkListener, capturedCycles::add, true, true)) {
        range(0, 10).forEach(i -> {
            try (final DocumentContext ctx = queue.acquireAppender().writingDocument()) {
                assertEquals(i == 0 ? 0 : i + 1, capturedCycles.size());
                ctx.wire().write().int32(i);
                ctx.wire().write().bytes(new byte[1024]);
            }
            try {
                pretoucher.execute();
            } catch (InvalidEventHandlerException e) {
                throw Jvm.rethrow(e);
            }
            assertEquals(i + 1, capturedCycles.size());
            clock.addAndGet(950 - earlyMillis);
            try {
                pretoucher.execute();
            } catch (InvalidEventHandlerException e) {
                throw Jvm.rethrow(e);
            }
            clock.addAndGet(50 + earlyMillis);
            BackgroundResourceReleaser.releasePendingResources();
            assertEquals(i + 2, capturedCycles.size());
        });
        assertEquals(11, capturedCycles.size());
    // TODO FIX
    // assertFalse(chunkListener.chunkMap.isEmpty());
    }
}
Also used : InvalidEventHandlerException(net.openhft.chronicle.core.threads.InvalidEventHandlerException) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File)

Example 84 with DocumentContext

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

the class RollAtEndOfCycleTest method shouldAppendToExistingQueueFile.

@Test
public void shouldAppendToExistingQueueFile() throws IOException {
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 1);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
    }
}
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 85 with DocumentContext

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

the class RollAtEndOfCycleTest method shouldRollAndAppendToNewFile.

@Test
public void shouldRollAndAppendToNewFile() throws IOException {
    assumeFalse(Jvm.isArm());
    try (final SingleChronicleQueue queue = createQueue()) {
        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(1, (w, i) -> {
            w.int32(i);
        });
        final ExcerptTailer tailer = queue.createTailer();
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        assertQueueFileCount(queue.path.toPath(), 1);
        clock.addAndGet(TimeUnit.SECONDS.toMillis(2));
        assertFalse(tailer.readingDocument().isPresent());
        appender.writeDocument(2, (w, i) -> {
            w.int32(i);
        });
        assertQueueFileCount(queue.path.toPath(), 2);
        try (final DocumentContext context = tailer.readingDocument()) {
            assertTrue(context.isPresent());
        }
        final ExcerptTailer newTailer = queue.createTailer();
        int totalCount = 0;
        while (true) {
            final DocumentContext context = newTailer.readingDocument();
            if (context.isPresent() && context.isData()) {
                assertNotEquals(0, context.wire().read().int32());
                totalCount++;
            } else if (!context.isPresent()) {
                break;
            }
        }
        assertEquals(2, totalCount);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) 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