Search in sources :

Example 41 with ChronicleQueue

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

the class IndexTest method shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex.

@Test
public void shouldShortCircuitIndexLookupWhenNewIndexIsCloseToPreviousIndex() {
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).testBlockSize().wireType(this.wireType).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final int messageCount = INDEXING_LINEAR_SCAN_THRESHOLD + 5;
        final long[] indices = new long[messageCount];
        for (int i = 0; i < messageCount; i++) {
            try (final DocumentContext ctx = appender.writingDocument()) {
                ctx.wire().write("event").int32(i);
                indices[i] = ctx.index();
            }
        }
        final StoreTailer tailer = (StoreTailer) queue.createTailer();
        tailer.moveToIndex(indices[0]);
        assertEquals(indices[0], tailer.index());
        assertEquals(1, tailer.getIndexMoveCount());
        tailer.moveToIndex(indices[0]);
        assertEquals(indices[0], tailer.index());
        assertEquals(1, tailer.getIndexMoveCount());
        tailer.moveToIndex(indices[2]);
        assertEquals(indices[2], tailer.index());
        assertEquals(1, tailer.getIndexMoveCount());
        tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2]);
        assertEquals(indices[INDEXING_LINEAR_SCAN_THRESHOLD + 2], tailer.index());
        assertEquals(2, tailer.getIndexMoveCount());
        // document that moving backwards requires an index scan
        tailer.moveToIndex(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1]);
        assertEquals(indices[INDEXING_LINEAR_SCAN_THRESHOLD - 1], tailer.index());
        assertEquals(3, tailer.getIndexMoveCount());
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) Test(org.junit.Test)

Example 42 with ChronicleQueue

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

the class QueueExamples1 method main.

public static void main(String[] args) {
    ChronicleQueue queue = ChronicleQueue.single("./myQueueDir");
    Printer printer = queue.methodWriter(Printer.class);
    printer.print("hello world");
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue)

Example 43 with ChronicleQueue

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

the class ThroughputPerfMain method main.

public static void main(String[] args) {
    String base = path + "/delete-" + Time.uniqueId() + ".me";
    long start = System.nanoTime();
    long count = 0;
    nbs = BytesStore.nativeStoreWithFixedCapacity(size);
    long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
    try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
        ExcerptAppender appender = q.acquireAppender();
        long lastIndex = -1;
        do {
            int defaultIndexSpacing = q.rollCycle().defaultIndexSpacing();
            Wire wire = appender.wire();
            int writeCount = (int) (defaultIndexSpacing - (lastIndex & (defaultIndexSpacing - 1)) - 1);
            if (wire != null && writeCount > 0) {
                MappedBytes bytes = (MappedBytes) wire.bytes();
                long address = bytes.addressForWrite(bytes.writePosition());
                long bstart = bytes.start();
                long bcap = bytes.realCapacity();
                long canWrite = bcap - (bytes.writePosition() - bstart);
                long lengthCount = writeMessages(address, canWrite, writeCount);
                bytes.writeSkip((int) lengthCount);
                lastIndex += lengthCount >> 32;
                count += lengthCount >> 32;
            } else {
                try (DocumentContext dc = appender.writingDocument()) {
                    dc.wire().bytes().write(nbs);
                }
                lastIndex = appender.lastIndexAppended();
                count++;
            }
        } while (start + time * 1e9 > System.nanoTime());
    }
    nbs.releaseLast();
    long mid = System.nanoTime();
    long time1 = mid - start;
    Bytes bytes = Bytes.allocateElasticDirect(64);
    try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
        ExcerptTailer tailer = q.createTailer();
        for (long i = 0; i < count; i++) {
            try (DocumentContext dc = tailer.readingDocument()) {
                bytes.clear();
                bytes.write(dc.wire().bytes());
            }
        }
    }
    bytes.releaseLast();
    long end = System.nanoTime();
    long time2 = end - mid;
    System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time1 / 1e9, (long) (1e9 * count / time1));
    System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time2 / 1e9, (long) (1e9 * count / time2));
    // make sure its cleaned up for windows to delete.
    System.gc();
    IOTools.deleteDirWithFiles(base, 2);
}
Also used : MappedBytes(net.openhft.chronicle.bytes.MappedBytes) Bytes(net.openhft.chronicle.bytes.Bytes) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) Wire(net.openhft.chronicle.wire.Wire) DocumentContext(net.openhft.chronicle.wire.DocumentContext) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 44 with ChronicleQueue

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

the class CheckIndicesTest method test.

@Ignore("stress test to run manually")
@Test
public void test() throws ExecutionException, InterruptedException {
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).epoch(System.currentTimeMillis()).build()) {
        queue0 = queue;
        newSingleThreadScheduledExecutor().scheduleAtFixedRate(this::appendToQueue, 0, 1, TimeUnit.MICROSECONDS);
        Future f = newSingleThreadScheduledExecutor().submit(this::checkIndices);
        Future f2 = newSingleThreadScheduledExecutor().submit(this::checkIndices);
        for (; ; ) {
            if (f.isDone())
                f.get();
            if (f2.isDone())
                f2.get();
            Thread.sleep(500);
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Future(java.util.concurrent.Future) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 45 with ChronicleQueue

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

the class MessageHistoryTest method shouldAccessMessageHistory.

@Test
public void shouldAccessMessageHistory() {
    try (final ChronicleQueue inputQueue = createQueue(inputQueueDir, 1);
        final ChronicleQueue outputQueue = createQueue(outputQueueDir, 2)) {
        generateTestData(inputQueue, outputQueue);
        final ExcerptTailer tailer = outputQueue.createTailer(named ? "named" : null);
        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)

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