Search in sources :

Example 6 with ChronicleQueue

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

the class NotCompleteTest method testUsingANotCompleteQueue.

/**
 * tests that when flags are set to not complete we are able to recover
 */
@Test
public void testUsingANotCompleteQueue() throws InterruptedException {
    BinaryLongReference.startCollecting();
    File tmpDir = DirectoryUtils.tempDir("testUsingANotCompleteQueue");
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
        try (DocumentContext dc = appender.writingDocument()) {
            dc.wire().write("some").text("data");
        }
        Thread.sleep(100);
        // System.out.println(queue.dump());
        // this is what will corrupt the queue
        BinaryLongReference.forceAllToNotCompleteState();
    }
    try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().timeoutMS(500).build()) {
        // System.out.println(queue.dump());
        ExcerptTailer tailer = queue.createTailer();
        try (DocumentContext dc = tailer.readingDocument()) {
            assertEquals("data", dc.wire().read(() -> "some").text());
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 7 with ChronicleQueue

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

the class RollCycleMultiThreadTest method testRead2.

@Test
public void testRead2() throws Exception {
    File path = DirectoryUtils.tempDir("testRead2");
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
        final ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).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);
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExecutorService(java.util.concurrent.ExecutorService) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 8 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 Exception {
    File path = DirectoryUtils.tempDir(getClass().getSimpleName());
    TestTimeProvider timeProvider = new TestTimeProvider();
    try (ChronicleQueue queue0 = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).timeProvider(timeProvider).build()) {
        ParallelQueueObserver observer = new ParallelQueueObserver(queue0);
        final ExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(DAILY).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);
        }
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ExecutorService(java.util.concurrent.ExecutorService) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) Test(org.junit.Test)

Example 9 with ChronicleQueue

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

the class QueueWireHandler method getQueue.

private ChronicleQueue getQueue(StringBuilder cspText) {
    ChronicleQueue queue;
    if (cid == 0) {
        // cid hasn't been passed in need to map it from csp
        cid = cspToCid.computeIfAbsent(cspText.toString(), s -> cidCounter.incrementAndGet());
        String[] parts = cspText.toString().split("/");
        String filename = "/tmp/" + parts[1] + "/" + parts[2] + ".q";
        queue = fileNameToChronicle.computeIfAbsent(filename, s -> {
            try {
                return new ChronicleQueueBuilder(filename).build();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        });
        cidToQueue.put(cid, queue);
    } else {
        // if the cid has been created then there must be a corresponding queue
        queue = cidToQueue.get(cid);
        assert queue != null;
    }
    return queue;
}
Also used : ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Logger(org.slf4j.Logger) StreamCorruptedException(java.io.StreamCorruptedException) EventGroup(net.openhft.chronicle.network.event.EventGroup) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder) LoggerFactory(org.slf4j.LoggerFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Consumer(java.util.function.Consumer) Bytes(net.openhft.chronicle.bytes.Bytes) EventId(net.openhft.chronicle.engine.client.internal.ClientWiredChronicleQueueStateless.EventId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) net.openhft.chronicle.wire(net.openhft.chronicle.wire) Map(java.util.Map) ClientWiredStatelessTcpConnectionHub(net.openhft.chronicle.engine.client.ClientWiredStatelessTcpConnectionHub) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) IOException(java.io.IOException) ChronicleQueueBuilder(net.openhft.chronicle.queue.ChronicleQueueBuilder)

Example 10 with ChronicleQueue

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

the class SingleChronicleQueueTest method testLastWrittenIndexPerAppender.

@Test
public void testLastWrittenIndexPerAppender() {
    final File file = createTempFile("testLastWrittenIndexPerAppender");
    try {
        final ChronicleQueue chronicle = createQueue(file);
        final ExcerptAppender appender = chronicle.acquireAppender();
        appender.writeDocument(wire -> wire.write(() -> "key").text("test"));
        Assert.assertEquals(0, appender.lastWrittenIndex());
    } finally {
        file.delete();
    }
}
Also used : ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) File(java.io.File) Test(org.junit.Test)

Aggregations

ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)21 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)20 Test (org.junit.Test)20 File (java.io.File)16 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)14 MappedFile (net.openhft.chronicle.bytes.MappedFile)10 DocumentContext (net.openhft.chronicle.wire.DocumentContext)9 IOException (java.io.IOException)4 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)4 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ChronicleQueueTestBase (net.openhft.chronicle.queue.ChronicleQueueTestBase)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)2 WireType (net.openhft.chronicle.wire.WireType)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2