Search in sources :

Example 61 with ChronicleQueue

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

the class ChronicleHistoryReaderTest method checkWithQueueHistoryRecordHistoryInitial.

private void checkWithQueueHistoryRecordHistoryInitial(Class<? extends DummyListener> dummyClass) {
    expectException("Overriding sourceId from existing metadata, was 0, overriding to");
    MessageHistory.set(null);
    int extraTiming = 1;
    File queuePath1 = IOTools.createTempFile("testWithQueueHistory1-");
    File queuePath2 = IOTools.createTempFile("testWithQueueHistory2-");
    File queuePath3 = IOTools.createTempFile("testWithQueueHistory3-");
    try {
        try (ChronicleQueue out = queue(queuePath1, 1)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).useMethodIds(true).get();
            // this will write the 1st timestamps
            writer.say("hello");
        }
        try (ChronicleQueue in = queue(queuePath1, 1);
            ChronicleQueue out = queue(queuePath2, 2)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
            final AtomicInteger numberRead = new AtomicInteger();
            // if this listener is a DummyListener then messages with methodId won't be routed to it
            DummyListenerId dummy = msg -> {
                numberRead.incrementAndGet();
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(1, history.sources());
                // written 1st then received by me
                Assert.assertEquals(1 + extraTiming, history.timings());
                // this writes 2 more timestamps
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertEquals("check routed to correct dest", 1, numberRead.get());
            assertFalse(reader.readOne());
        }
        try (ChronicleQueue in = queue(queuePath2, 2);
            ChronicleQueue out = queue(queuePath3, 3)) {
            DummyListener writer = out.acquireAppender().methodWriterBuilder(dummyClass).get();
            final AtomicInteger numberRead = new AtomicInteger();
            DummyListenerId dummy = msg -> {
                numberRead.incrementAndGet();
                MessageHistory history = MessageHistory.get();
                Assert.assertEquals(2, history.sources());
                Assert.assertEquals(3 + extraTiming, history.timings());
                // this writes 2 more timestamps
                writer.say(msg);
            };
            MethodReader reader = in.createTailer().methodReader(dummy);
            assertTrue(reader.readOne());
            assertEquals("check routed to correct dest", 1, numberRead.get());
            assertFalse(reader.readOne());
        }
        ChronicleHistoryReader chronicleHistoryReader = new ChronicleHistoryReader().withBasePath(queuePath3.toPath()).withTimeUnit(TimeUnit.MICROSECONDS).withMessageSink(System.out::println);
        Map<String, Histogram> histos = chronicleHistoryReader.readChronicle();
        chronicleHistoryReader.outputData();
        Assert.assertEquals(5, histos.size());
        Assert.assertEquals("[1, startTo1, 2, 1to2, endToEnd]", histos.keySet().toString());
    } finally {
        IOTools.deleteDirWithFiles(queuePath1.toString(), queuePath2.toString(), queuePath3.toString());
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Test(org.junit.Test) ChronicleHistoryReader(net.openhft.chronicle.queue.reader.ChronicleHistoryReader) File(java.io.File) MethodId(net.openhft.chronicle.bytes.MethodId) TimeUnit(java.util.concurrent.TimeUnit) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IOTools(net.openhft.chronicle.core.io.IOTools) Map(java.util.Map) QueueTestCommon(net.openhft.chronicle.queue.QueueTestCommon) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) Assert(org.junit.Assert) Histogram(net.openhft.chronicle.core.util.Histogram) ChronicleHistoryReader(net.openhft.chronicle.queue.reader.ChronicleHistoryReader) MessageHistory(net.openhft.chronicle.wire.MessageHistory) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader)

Example 62 with ChronicleQueue

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

the class Queue36Test method testTail.

@Test
public void testTail() {
    File basePath = getTmpDir();
    try (ChronicleQueue queue = ChronicleQueue.singleBuilder(basePath).testBlockSize().build()) {
        checkNoFiles(basePath);
        ExcerptTailer tailer = queue.createTailer();
        checkNoFiles(basePath);
        tailer.toStart();
        checkNoFiles(basePath);
        assertFalse(tailer.readDocument(d -> {
        }));
        checkNoFiles(basePath);
    }
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) ChronicleQueueTestBase(net.openhft.chronicle.queue.ChronicleQueueTestBase) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 63 with ChronicleQueue

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

the class RollCycleMultiThreadTest method testRead1.

@Test
public void testRead1() throws ExecutionException, InterruptedException {
    File path = getTmpDir();
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
        ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            Assert.assertEquals(0, (int) scheduledExecutorService.submit(observer::call).get());
            // two days pass
            timeProvider.add(TimeUnit.DAYS.toMillis(2));
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 3 data");
            }
            Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer::call).get());
            assertEquals(1, observer.documentsRead);
        }
        scheduledExecutorService.shutdown();
        scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) NamedThreadFactory(net.openhft.chronicle.threads.NamedThreadFactory) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 64 with ChronicleQueue

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

the class RollCycleMultiThreadTest method testRead2.

@SuppressWarnings("unchecked")
@Test
public void testRead2() throws ExecutionException, InterruptedException {
    File path = getTmpDir();
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
        final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("test"));
        try (ChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DEFAULT).timeProvider(timeProvider).build()) {
            ExcerptAppender appender = queue.acquireAppender();
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 1 data");
            }
            Assert.assertEquals(1, (int) scheduledExecutorService.submit(observer).get());
            // two days pass
            timeProvider.add(TimeUnit.DAYS.toMillis(2));
            try (final DocumentContext dc = appender.writingDocument()) {
                dc.wire().write().text("Day 3 data");
            }
            Assert.assertEquals(2, (int) scheduledExecutorService.submit(observer).get());
            // System.out.println(queue.dump());
            assertEquals(2, observer.documentsRead);
        }
        scheduledExecutorService.shutdown();
        scheduledExecutorService.awaitTermination(1, TimeUnit.SECONDS);
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) NamedThreadFactory(net.openhft.chronicle.threads.NamedThreadFactory) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) Test(org.junit.Test)

Example 65 with ChronicleQueue

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

the class QueueReadJitterMain method run.

protected void run() {
    MappedFile.warmup();
    String path = "test-q-" + Time.uniqueId();
    // System.out.println("Writing to " + path);
    AtomicLong lastRead = new AtomicLong();
    Thread reader = new Thread(() -> {
        try (ChronicleQueue q = createQueue(path)) {
            ExcerptTailer tailer = q.createTailer().toEnd();
            long time = System.currentTimeMillis();
            while (running) {
                Jvm.safepoint();
                if (!tailer.peekDocument()) {
                    long now = System.currentTimeMillis();
                    if (now != time)
                        time = now;
                    else
                        continue;
                }
                try (DocumentContext dc = tailer.readingDocument(false)) {
                    if (!dc.isPresent()) {
                        Jvm.safepoint();
                        continue;
                    }
                    Jvm.safepoint();
                    Bytes<?> bytes = dc.wire().bytes();
                    long count = bytes.readLong();
                    while (bytes.readRemaining() > 7) bytes.readLong();
                    lastRead.set(count);
                    Jvm.safepoint();
                }
                Jvm.safepoint();
            }
        }
    });
    reader.setDaemon(true);
    reader.start();
    // give it time to start
    Jvm.pause(100);
    long count = 0;
    try (ChronicleQueue q = createQueue(path)) {
        ExcerptAppender appender = q.acquireAppender();
        long start0 = System.currentTimeMillis();
        do {
            try (DocumentContext dc = appender.writingDocument()) {
                Bytes<?> bytes = dc.wire().bytes();
                bytes.writeLong(++count);
                for (int i = 8; i < size; i += 8) bytes.writeLong(0);
            }
            long start1 = System.nanoTime();
            do {
                Thread.yield();
            } while (System.nanoTime() < start1 + sampleTime * 1000);
            long time1 = System.nanoTime() - start1;
            if (time1 > sampleTime * 1000 * 10) {
                System.out.println("Time paused = " + time1 / 1000 + " us");
            }
            if (lastRead.get() != count) {
                StackTraceElement[] stes = reader.getStackTrace();
                StringBuilder sb = new StringBuilder();
                sb.append(PROFILE_OF_THE_THREAD);
                Jvm.trimStackTrace(sb, stes);
                System.out.println(sb);
            }
        } while (System.currentTimeMillis() < start0 + runTime * 1_000);
    }
    running = false;
    IOTools.deleteDirWithFiles(path, 2);
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext)

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