Search in sources :

Example 56 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class MappedMemoryUnmappingTest method shouldUnmapMemoryAsCycleRolls.

@Test
public void shouldUnmapMemoryAsCycleRolls() throws IOException {
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    long initialQueueMappedMemory = 0L;
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmp.newFolder()).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).timeProvider(clock::get).build()) {
        for (int i = 0; i < 100; i++) {
            queue.acquireAppender().writeDocument(System.nanoTime(), (d, t) -> d.int64(t));
            clock.addAndGet(TimeUnit.SECONDS.toMillis(1L));
            if (initialQueueMappedMemory == 0L) {
                initialQueueMappedMemory = OS.memoryMapped();
            }
        }
    }
    GcControls.waitForGcCycle();
    final long timeoutAt = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10L);
    while (System.currentTimeMillis() < timeoutAt) {
        if (OS.memoryMapped() < 2 * initialQueueMappedMemory) {
            return;
        }
    }
    fail(String.format("Mapped memory (%dB) did not fall below threshold (%dB)", OS.memoryMapped(), 2 * initialQueueMappedMemory));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Test(org.junit.Test)

Example 57 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class MicroToucherTest method touchPage.

public void touchPage(Consumer<SingleChronicleQueueBuilder> configure, int pagesExpected) {
    long start = System.nanoTime();
    String path = OS.getTarget() + "/touchPage-" + System.nanoTime();
    int pages = 0;
    final SingleChronicleQueueBuilder builder = ChronicleQueue.singleBuilder(path);
    configure.accept(builder);
    try (ChronicleQueue q = builder.build();
        final StoreAppender appender = (StoreAppender) q.acquireAppender()) {
        Thread msync = new Thread(() -> {
            try {
                while (true) {
                    appender.bgMicroTouch();
                    Jvm.pause(25);
                }
            } catch (ClosedIllegalStateException expected) {
            }
        });
        msync.setDaemon(true);
        msync.start();
        long lastPage = 0;
        for (int i = 0; i < (1 << 20); i++) {
            try (DocumentContext dc = appender.writingDocument()) {
                dc.wire().bytes().writeSkip(256);
            }
            long page = (appender.lastPosition + 0xFFF) & ~0xFFF;
            boolean touch = page != lastPage && appender.wire().bytes().bytesStore().inside(page, 8);
            lastPage = page;
            if (touch != appender.microTouch())
                assertEquals("i: " + i, touch, appender.microTouch());
            if (touch)
                pages++;
        }
    }
    System.out.println("pages = " + pages);
    // assertEquals(pagesExpected, pages);
    System.out.println("Time = " + (System.nanoTime() - start) / 1000000 / 1e3);
    IOTools.deleteDirWithFiles(path);
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ClosedIllegalStateException(net.openhft.chronicle.core.io.ClosedIllegalStateException) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

Example 58 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class QueueExamples2 method main.

public static void main(String[] args) {
    ChronicleQueue queue = ChronicleQueue.single("./myQueueDir");
    final MethodReader methodReader = queue.createTailer().methodReader((Printer) System.out::println);
    for (; ; ) {
        final boolean successIfMessageRead = methodReader.readOne();
        Thread.yield();
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader)

Example 59 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class MessageHistoryTest method shouldAccessMessageHistoryWhenTailerIsMovedToEnd.

@Test
public void shouldAccessMessageHistoryWhenTailerIsMovedToEnd() {
    try (final ChronicleQueue inputQueue = createQueue(inputQueueDir, 1);
        final ChronicleQueue outputQueue = createQueue(outputQueueDir, 2)) {
        generateTestData(inputQueue, outputQueue);
        final ExcerptTailer tailer = outputQueue.createTailer(named ? "named" : null);
        tailer.direction(TailerDirection.BACKWARD).toEnd();
        final ValidatingSecond validatingSecond = new ValidatingSecond();
        final MethodReader validator = tailer.methodReader(validatingSecond);
        assertTrue(validator.readOne());
        assertTrue(validatingSecond.messageHistoryPresent());
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) MethodReader(net.openhft.chronicle.bytes.MethodReader) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 60 with ChronicleQueue

use of net.openhft.chronicle.queue.ChronicleQueue in project Chronicle-Queue by OpenHFT.

the class MoveToIndexTest method shouldMoveToPreviousIndexAfterDocumentIsConsumed.

@Test
public void shouldMoveToPreviousIndexAfterDocumentIsConsumed() throws IOException {
    File queuePath = tmpFolder.newFolder("cq");
    try (ChronicleQueue queue = ChronicleQueue.singleBuilder(queuePath).build()) {
        ExcerptAppender appender = queue.acquireAppender();
        for (int i = 1; i < 10; ++i) {
            appender.writeText("id" + i);
        }
        ExcerptTailer tailer = queue.createTailer();
        assertNext(tailer, "id1");
        long index = tailer.index();
        assertNext(tailer, "id2");
        tailer.moveToIndex(index);
        assertNext(tailer, "id2");
        tailer.moveToIndex(index);
        assertNext(tailer, "id2");
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Aggregations

ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)80 Test (org.junit.Test)59 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)37 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)37 File (java.io.File)36 DocumentContext (net.openhft.chronicle.wire.DocumentContext)21 ArrayList (java.util.ArrayList)16 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)14 MethodReader (net.openhft.chronicle.bytes.MethodReader)12 List (java.util.List)11 IOException (java.io.IOException)9 MappedFile (net.openhft.chronicle.bytes.MappedFile)8 Path (java.nio.file.Path)7 Bytes (net.openhft.chronicle.bytes.Bytes)7 Collections (java.util.Collections)6 RollCycles (net.openhft.chronicle.queue.RollCycles)6 NotNull (org.jetbrains.annotations.NotNull)6 ByteBuffer (java.nio.ByteBuffer)5 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)5 Histogram (net.openhft.chronicle.core.util.Histogram)5