Search in sources :

Example 61 with DocumentContext

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

the class RawAccessJavaTest method Appender.

@Test
public void Appender() {
    if (!assert_from_cpp())
        return;
    String tmp = "/dev/shm/RawAccessJtoC";
    // so C++ knows this ran rather than skipped
    System.out.println(tmp);
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        ExcerptAppender appender = cq.acquireAppender();
        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = appender.writingDocument()) {
                Bytes bytes = dc.wire().bytes();
                // will contain the size of the blob
                long start = bytes.writePosition();
                bytes.writeSkip(RAW_SIZE_PREFIX);
                {
                    bytes.writeByte((byte) 0xab);
                    bytes.writeShort((short) 12);
                    bytes.writeInt(123);
                    bytes.writeLong(123456789L);
                    bytes.writeFloat(1.234f);
                    bytes.writeDouble(123.456);
                    bytes.writeChar('a');
                    bytes.write8bit("Hello World");
                }
                long end = bytes.writePosition();
                bytes.writeInt(start, (int) (end - start - RAW_SIZE_PREFIX));
            }
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 62 with DocumentContext

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

the class EntryCountNotBehindReadTest method runReader.

private void runReader(SingleChronicleQueue queue, CyclicBarrier startBarrier, LongConsumer onRead) {
    try (final ExcerptTailer tailer = queue.createTailer()) {
        waitOn(startBarrier);
        int count = 0;
        while (count < TOTAL_EVENTS) {
            try (DocumentContext entry = tailer.readingDocument()) {
                if (entry.isData() && !entry.isNotComplete()) {
                    onRead.accept(entry.index());
                    ++count;
                }
            }
        }
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 63 with DocumentContext

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

the class QueueMultiThreadedJLBHBenchmark method run.

@Override
public void run(long startTimeNS) {
    datum.ts = startTimeNS;
    try (DocumentContext dc = appender.writingDocument()) {
        datum.writeMarshallable(dc.wire().bytes());
    }
    writeProbe.sampleNanos(System.nanoTime() - startTimeNS);
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 64 with DocumentContext

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

the class PretoucherDontWriteTest method dontWrite.

@Test
public void dontWrite() {
    File dir = getTmpDir();
    try (final SingleChronicleQueue queue = PretoucherTest.createQueue(dir, clock::get);
        final SingleChronicleQueue pretoucherQueue = PretoucherTest.createQueue(dir, clock::get);
        final Pretoucher pretoucher = new Pretoucher(pretoucherQueue, chunkListener, capturedCycles::add, false, false)) {
        range(0, 10).forEach(i -> {
            try (final DocumentContext ctx = queue.acquireAppender().writingDocument()) {
                assertEquals(i + 0.5, capturedCycles.size(), 0.5);
                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(TimeUnit.SECONDS.toMillis(5L));
            try {
                pretoucher.execute();
            } catch (InvalidEventHandlerException e) {
                throw Jvm.rethrow(e);
            }
            assertEquals(i + 1.5, capturedCycles.size(), 0.5);
        });
        assertEquals(10.5, capturedCycles.size(), 0.5);
    }
}
Also used : InvalidEventHandlerException(net.openhft.chronicle.core.threads.InvalidEventHandlerException) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 65 with DocumentContext

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

the class AppenderLockOnlyAppliesToFileTest method concurrentLockItUp.

/*
    Failed tests:
  AppenderLockOnlyAppliesToFileTest.concurrentLockItUp:59 Writer thread completed before timeout

     */
@Ignore("fails too often")
@Test
public void concurrentLockItUp() throws InterruptedException {
    final AtomicReference<String> writerQueueFile = new AtomicReference<>();
    final File path = DirectoryUtils.tempDir(this.getClass().getSimpleName());
    final SingleChronicleQueueBuilder builder = ChronicleQueueBuilder.single(path).sourceId(1).rollCycle(ROLL_CYCLE).timeoutMS(TIMEOUT_MS);
    final String initialFile;
    final DocumentContext initialContext = builder.build().acquireAppender().writingDocument();
    initialContext.wire().writeText("abcd");
    initialFile = getFilename(initialContext);
    // don't close context
    final long afterInitialWrite = System.currentTimeMillis();
    final CountDownLatch writerStarted = new CountDownLatch(1);
    final CountDownLatch writerFinished = new CountDownLatch(1);
    Thread writerThread = new Thread(() -> {
        ExcerptAppender appender = builder.build().acquireAppender();
        writerStarted.countDown();
        // wait for less than timeout and more than roll
        Jvm.pause(WAIT_FOR_ROLL_MS);
        try (@NotNull DocumentContext context = appender.writingDocument()) {
            // should not have been held up by locking
            writerQueueFile.set(getFilename(context));
            Wire wire = context.wire();
            wire.writeText("hello");
            writerFinished.countDown();
        }
    });
    writerThread.start();
    assertTrue("Writer thread not started", writerStarted.await(1, TimeUnit.SECONDS));
    assertTrue("Writer thread completed before timeout", writerFinished.await(WAIT_FOR_ROLL_MS + 50, TimeUnit.MILLISECONDS));
    assertFalse("Threads wrote to different queue cycles, so no locking occurred", initialFile.equals(writerQueueFile.get()));
    assertTrue("We are within timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
    ExcerptTailer tailer = builder.build().createTailer();
    // this call to readingDocument waits for timeout and logs out ".... resetting header after timeout ...."
    try (DocumentContext rd = tailer.readingDocument()) {
        assertFalse("We are outside timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
        assertTrue("Something was written", rd.isPresent());
        String value = rd.wire().readText();
        assertEquals("the first (locked) write is lost", "hello", value);
    }
    try (DocumentContext rd = tailer.readingDocument()) {
        assertFalse("Should be only one message in the queue", rd.isPresent());
    }
    try {
        initialContext.close();
        fail("close should have thrown");
    } catch (IllegalStateException e) {
    // expected for close to throw
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Wire(net.openhft.chronicle.wire.Wire) NotNull(org.jetbrains.annotations.NotNull) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Ignore(org.junit.Ignore) 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