Search in sources :

Example 56 with DocumentContext

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

the class CheckIndicesTest method appendToQueue.

private void appendToQueue() {
    ExcerptAppender appender = queue0.acquireAppender();
    try {
        for (int i = 0; i < BATCH_SIZE; i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                long seq = appender.queue().rollCycle().toSequenceNumber(dc.index());
                // System.out.println("write=" + Long.toHexString(dc.index()));
                dc.wire().write("value").writeLong(seq);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExecutionException(java.util.concurrent.ExecutionException)

Example 57 with DocumentContext

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

the class DocumentOrderingTest method codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty.

@Test
public void codeWithinPriorDocumentMustExecuteBeforeSubsequentDocumentWhenQueueIsEmpty() throws InterruptedException, TimeoutException, ExecutionException {
    try (final ChronicleQueue queue = builder(getTmpDir(), 3_000L).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final Future<RecordInfo> otherDocumentWriter;
        try (final DocumentContext documentContext = excerptAppender.writingDocument()) {
            // move time to beyond the next cycle
            clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
            otherDocumentWriter = attemptToWriteDocument(queue);
            // stall this thread, other thread should not be able to advance,
            // since this DocumentContext is still open
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2L));
            documentContext.wire().getValueOut().int32(counter.getAndIncrement());
        }
        expectCounterVaueOne(otherDocumentWriter);
        final ExcerptTailer tailer = queue.createTailer();
        expectValue(0, tailer);
        expectValue(1, tailer);
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 58 with DocumentContext

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

the class DocumentOrderingTest method shouldRecoverFromUnfinishedFirstMessageInPreviousQueue.

@Test
public void shouldRecoverFromUnfinishedFirstMessageInPreviousQueue() throws InterruptedException, TimeoutException, ExecutionException {
    expectException("Couldn't acquire write lock");
    expectException("Forced unlock for the lock");
    // as below, but don't actually close the initial context
    try (final ChronicleQueue queue = builder(getTmpDir(), 1_000L).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        final Future<RecordInfo> otherDocumentWriter;
        // begin a record in the first cycle file
        final DocumentContext documentContext = excerptAppender.writingDocument();
        documentContext.wire().getValueOut().int32(counter.getAndIncrement());
        // move time to beyond the next cycle
        clock.addAndGet(TimeUnit.SECONDS.toMillis(2L));
        otherDocumentWriter = attemptToWriteDocument(queue);
        expectCounterVaueOne(otherDocumentWriter);
        final ExcerptTailer tailer = queue.createTailer();
        expectValue(1, tailer);
        assertFalse(tailer.readingDocument().isPresent());
    }
}
Also used : DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 59 with DocumentContext

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

the class QueueAppendAfterRollReplayedIssueTest method test.

@Test
public void test() {
    int messages = 10;
    String path = OS.getTarget() + "/" + getClass().getSimpleName() + "-" + Time.uniqueId();
    SetTimeProvider timeProvider = new SetTimeProvider();
    try (final ChronicleQueue writeQueue = ChronicleQueue.singleBuilder(path).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        for (int i = 0; i < messages; i++) {
            timeProvider.advanceMillis(i * 100);
            ExcerptAppender appender = writeQueue.acquireAppender();
            Map<String, Object> map = new HashMap<>();
            map.put("key", i);
            appender.writeMap(map);
        }
    }
    timeProvider.advanceMillis(1000);
    try (final ChronicleQueue queue = ChronicleQueue.singleBuilder(path).testBlockSize().timeProvider(timeProvider).rollCycle(RollCycles.TEST_SECONDLY).build()) {
        final ExcerptAppender excerptAppender = queue.acquireAppender();
        try (final DocumentContext documentContext = excerptAppender.acquireWritingDocument(false)) {
            assertNotNull(documentContext.wire());
        }
    } finally {
        try {
            IOTools.deleteDirWithFiles(path, 2);
        } catch (IORuntimeException todoFixOnWindows) {
        }
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) HashMap(java.util.HashMap) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) Test(org.junit.Test)

Example 60 with DocumentContext

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

the class RawAccessJavaTest method Tailer.

@Test
public void Tailer() {
    if (!assert_from_cpp())
        return;
    String tmp = "/dev/shm/RawAccessCtoJ";
    // so C++ knows this ran rather than skipped
    System.out.println(tmp);
    try (ChronicleQueue cq = SingleChronicleQueueBuilder.binary(tmp).build()) {
        ExcerptTailer tailer = cq.createTailer();
        for (int i = 0; i < COUNT; ++i) {
            try (DocumentContext dc = tailer.readingDocument()) {
                Bytes bytes = dc.wire().bytes();
                bytes.readSkip(-QUEUE_HEADER_SIZE);
                int header = bytes.readInt();
                // document length, inc 4-byte length
                int length = Wires.lengthOf(header);
                // actual length of data
                int data_length = bytes.readInt();
                assertEquals(bytes.readByte(), (byte) 0xab);
                assertEquals(bytes.readShort(), (short) 12);
                assertEquals(bytes.readInt(), 123);
                assertEquals(bytes.readLong(), 123456789L);
                assertEquals(bytes.readFloat(), 1.234f, 1.0e-7);
                assertEquals(bytes.readDouble(), 123.456, 1.0e-7);
                assertEquals(bytes.readChar(), 'a');
                StringBuilder sb = new StringBuilder();
                bytes.read8bit(sb);
                assertEquals(sb.toString(), "Hello World");
            }
        }
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) 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