Search in sources :

Example 1 with DocumentContext

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

the class TestWriteWhenCurrentCycleGotEOF method createQueueWithOnlyHeaderFile.

private void createQueueWithOnlyHeaderFile(File dir) {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build();
    queue.storeForCycle(queue.cycle(), queue.epoch(), true);
    ExcerptTailer tailer = queue.acquireTailer();
    try (DocumentContext dc = tailer.readingDocument()) {
        assertFalse(dc.isPresent());
    }
    Wire wire;
    ExcerptAppender excerptAppender = queue.acquireAppender();
    try (DocumentContext dc = excerptAppender.writingDocument()) {
        wire = dc.wire();
    }
    // overwrite last record with EOF
    Bytes<?> bytes = wire.bytes();
    bytes.writeVolatileInt(bytes.writePosition() - 5, Wires.END_OF_DATA);
    bytes.writeVolatileInt(bytes.writePosition() - 1, 0);
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 2 with DocumentContext

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

the class FsFullReadTest method testFullReadFs.

@Ignore("broken test")
@Test
public void testFullReadFs() throws Exception {
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptTailer tailer = queue.createTailer();
    DocumentContext dc = tailer.readingDocument();
    boolean doExit = false;
    int entries = 0;
    while (!doExit) {
        try {
            if (dc.isPresent()) {
                entries++;
                Wire w = dc.wire();
                LocalDateTime dt = w.read().dateTime();
                assertNotNull(dt);
                byte[] b = w.read().bytes();
                assertEquals(1024, b.length);
            } else {
                System.out.println("Exiting");
                doExit = true;
            }
        } finally {
            dc.close();
        }
    }
    System.out.println(String.format("Read %d entries.", entries));
    CommonStore commonStore = queue.storeForCycle(queue.cycle(), 0, false);
    File file = commonStore.file();
    queue.close();
    int dumpEntries = 0;
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 4 << 20);
        bytes.readLimit(bytes.realCapacity());
        WireDumper dumper = WireDumper.of(bytes);
        Bytes<ByteBuffer> buffer = Bytes.elasticByteBuffer();
        while (bytes.readRemaining() >= 4) {
            StringBuilder sb = new StringBuilder();
            boolean last = dumper.dumpOne(sb, buffer);
            assertTrue(sb.length() > 0);
            if (last)
                break;
            dumpEntries++;
        }
    } catch (IOException ioe) {
        err.println("Failed to read " + file + " " + ioe);
    }
    assertEquals(dumpEntries, entries);
}
Also used : LocalDateTime(java.time.LocalDateTime) WireDumper(net.openhft.chronicle.wire.WireDumper) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) ByteBuffer(java.nio.ByteBuffer) CommonStore(net.openhft.chronicle.queue.impl.CommonStore) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with DocumentContext

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

the class FsFullWriteTest method testAppenderFullFs.

@Ignore("flaky test")
@Test
public void testAppenderFullFs() throws Exception {
    ChronicleQueue queue = SingleChronicleQueueBuilder.binary(basePath).blockSize(256 << 1000).rollCycle(RollCycles.DAILY).build();
    ExcerptAppender appender = queue.acquireAppender();
    byte[] payload = new byte[1024];
    Random r = new Random();
    r.nextBytes(payload);
    final LocalDateTime now = LocalDateTime.now(Clock.systemUTC());
    for (int i = 0; i < 1024 * 200; i++) {
        DocumentContext dc = appender.writingDocument();
        try {
            Wire w = dc.wire();
            w.write().dateTime(now);
            w.write().bytes(payload);
        } finally {
            dc.close();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Random(java.util.Random) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Wire(net.openhft.chronicle.wire.Wire) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 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 5 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)

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