Search in sources :

Example 11 with DocumentContext

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

the class RareAppenderLatencyTest method testRareAppenderLatency.

@Test
public void testRareAppenderLatency() throws IOException, InterruptedException, ExecutionException {
    System.setProperty("ignoreHeaderCountIfNumberOfExcerptsBehindExceeds", "" + (1 << 12));
    if (Jvm.isDebug())
        // this is a performance test so should not be run in debug mode
        return;
    assert (isAssertionsOn = true);
    if (isAssertionsOn)
        // this is a performance test so should not be run with assertions turned on
        return;
    System.out.println("starting test");
    String pathname = OS.getTarget() + "/testRareAppenderLatency-" + System.nanoTime();
    new File(pathname).deleteOnExit();
    // Shared queue between two threads appending. One appends very rarely, another heavily.
    ChronicleQueue queue = new SingleChronicleQueueBuilder(pathname).rollCycle(RollCycles.HOURLY).build();
    String text = getText();
    // Write a some messages with an appender from Main thread.
    ExcerptAppender rareAppender = queue.acquireAppender();
    for (int i = 0; i < RARE_MSGS; i++) {
        try (DocumentContext ctx = rareAppender.writingDocument()) {
            ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
        }
    }
    // Write a bunch of messages from another thread.
    Future f = appenderES.submit(() -> {
        ExcerptAppender appender = queue.acquireAppender();
        long start = System.currentTimeMillis();
        for (int i = 0; i < HEAVY_MSGS; i++) {
            try (DocumentContext ctx = appender.writingDocument()) {
                ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
            }
            if (appenderES.isShutdown())
                return;
        }
        System.out.println("Wrote heavy " + HEAVY_MSGS + " msgs in " + (System.currentTimeMillis() - start) + " ms");
    });
    f.get();
    // Write a message from the Main thread again (this will have unacceptable latency!)
    rareAppender = queue.acquireAppender();
    long now = System.currentTimeMillis();
    try (DocumentContext ctx = rareAppender.writingDocument()) {
        ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
    }
    long l = System.currentTimeMillis() - now;
    // Write another message from the Main thread (this will be fast since we are caught up)
    now = System.currentTimeMillis();
    try (DocumentContext ctx = rareAppender.writingDocument()) {
        ctx.wire().write(() -> "ts").int64(System.currentTimeMillis()).write(() -> "msg").text(text);
    }
    System.out.println("Wrote first rare one in " + l + " ms");
    System.out.println("Wrote another rare one in " + (System.currentTimeMillis() - now) + " ms");
    assertFalse("Appending from rare thread latency too high!", l > 150);
}
Also used : SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) Future(java.util.concurrent.Future) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 12 with DocumentContext

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

the class ReadWriteTest method testReadFromReadOnlyChronicle.

@Test
public void testReadFromReadOnlyChronicle() {
    try (SingleChronicleQueue out = SingleChronicleQueueBuilder.binary(chroniclePath).testBlockSize().readOnly(!OS.isWindows()).build()) {
        // check dump
        assertTrue(out.dump().length() > 1);
        // and tailer
        ExcerptTailer tailer = out.createTailer();
        assertEquals(STR1, tailer.readText());
        try (DocumentContext dc = tailer.readingDocument()) {
            assertEquals(STR2, dc.wire().bytes().readUtf8());
        // even though this is read-only we can still call dc.wire().bytes().write... which causes java.lang.InternalError
        // Fixing this in a type-safe manner would require on Read/WriteDocumentContext to return WireIn/WireOut
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 13 with DocumentContext

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

the class ReadWriteTest method setup.

@Before
public void setup() {
    chroniclePath = new File(OS.TARGET, "read_only");
    try (SingleChronicleQueue readWrite = SingleChronicleQueueBuilder.binary(chroniclePath).readOnly(false).testBlockSize().build()) {
        final ExcerptAppender appender = readWrite.acquireAppender();
        appender.writeText(STR1);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().bytes().writeUtf8(STR2);
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Before(org.junit.Before)

Example 14 with DocumentContext

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

the class TailerDirectionTest method uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument.

@Test
public void uninitialisedTailerCreatedBeforeFirstAppendWithDirectionNoneShouldNotFindDocument() {
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
    final ChronicleQueue queue = ChronicleQueueBuilder.single(path).timeProvider(clock::get).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build();
    final ExcerptTailer tailer = queue.createTailer();
    tailer.direction(TailerDirection.NONE);
    final ExcerptAppender excerptAppender = queue.acquireAppender();
    for (int i = 0; i < 10; i++) {
        excerptAppender.writeDocument(i, (out, value) -> {
            out.int32(value);
        });
    }
    DocumentContext document = tailer.readingDocument();
    assertFalse(document.isPresent());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 15 with DocumentContext

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

the class AppenderFileHandleLeakTest method tailerShouldReleaseFileHandlesAsQueueRolls.

@Test
public void tailerShouldReleaseFileHandlesAsQueueRolls() throws Exception {
    System.gc();
    Thread.sleep(100);
    assumeThat(OS.isLinux(), is(true));
    final int messagesPerThread = 10;
    try (SingleChronicleQueue queue = createQueue(currentTime::get)) {
        final long openFileHandleCount = countFileHandlesOfCurrentProcess();
        final List<Path> fileHandlesAtStart = new ArrayList<>(lastFileHandles);
        final List<Future<Boolean>> futures = new LinkedList<>();
        for (int i = 0; i < THREAD_COUNT; i++) {
            futures.add(threadPool.submit(() -> {
                for (int j = 0; j < messagesPerThread; j++) {
                    writeMessage(j, queue);
                    currentTime.addAndGet(100);
                }
                return Boolean.TRUE;
            }));
        }
        for (Future<Boolean> future : futures) {
            assertThat(future.get(1, TimeUnit.MINUTES), is(true));
        }
        waitForFileHandleCountToDrop(openFileHandleCount, fileHandlesAtStart);
        fileHandlesAtStart.clear();
        final long tailerOpenFileHandleCount = countFileHandlesOfCurrentProcess();
        final ExcerptTailer tailer = queue.createTailer();
        tailer.toStart();
        final int expectedMessageCount = THREAD_COUNT * messagesPerThread;
        int messageCount = 0;
        storeFileListener.reset();
        while (true) {
            try (final DocumentContext ctx = tailer.readingDocument()) {
                if (!ctx.isPresent()) {
                    break;
                }
                messageCount++;
            }
        }
        assertThat(messageCount, is(expectedMessageCount));
        assertThat(storeFileListener.toString(), storeFileListener.releasedCount, is(withinDelta(storeFileListener.acquiredCount, 3)));
        waitForFileHandleCountToDrop(tailerOpenFileHandleCount, fileHandlesAtStart);
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Future(java.util.concurrent.Future) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Aggregations

DocumentContext (net.openhft.chronicle.wire.DocumentContext)54 Test (org.junit.Test)41 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)28 File (java.io.File)22 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)22 MappedFile (net.openhft.chronicle.bytes.MappedFile)12 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)8 Wire (net.openhft.chronicle.wire.Wire)8 NotNull (org.jetbrains.annotations.NotNull)6 Ignore (org.junit.Ignore)6 Future (java.util.concurrent.Future)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)4 Bytes (net.openhft.chronicle.bytes.Bytes)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)4 ValueOut (net.openhft.chronicle.wire.ValueOut)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)3