Search in sources :

Example 11 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class MessageHistoryTest method shouldAccessMessageHistoryWhenTailerIsMovedToEnd.

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

Example 12 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class NotCompleteTest method testInterruptedDuringSerialisation.

@Test
public void testInterruptedDuringSerialisation() throws InterruptedException {
    final File tmpDir = DirectoryUtils.tempDir("testInterruptedDuringSerialisation_" + (lazyIndexing ? "lazy" : "eager"));
    DirectoryUtils.deleteDir(tmpDir);
    tmpDir.mkdirs();
    final List<String> names = Collections.synchronizedList(new ArrayList<>());
    final Person person1 = new Person(40, "Terry");
    final Person interrupter = new Person(50, Person.INTERRUPT);
    final Person thrower = new Person(80, Person.THROW);
    final Person person2 = new Person(90, "Bert");
    try (final ChronicleQueue queueReader = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).timeoutMS(500).build();
        final ChronicleQueue queueWriter = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
        ExcerptTailer tailer = queueReader.createTailer();
        MethodReader reader = tailer.methodReader((PersonListener) person -> names.add(person.name));
        final StringBuilder queueDumpBeforeInterruptedWrite = new StringBuilder();
        // set up
        doWrite(queueWriter, (proxy, queue) -> {
            proxy.accept(person1);
            queueDumpBeforeInterruptedWrite.append(queue.dump());
        });
        String cleanedQueueDump = cleanQueueDump(queueDumpBeforeInterruptedWrite.toString());
        // start up writer thread
        Thread writerThread = new Thread(() -> doWrite(queueWriter, (proxy, queue) -> {
            // thread is interrupted during this
            proxy.accept(interrupter);
        }));
        writerThread.start();
        writerThread.join();
        try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
            String dump = cleanQueueDump(queue.dump());
            assertEquals("queue should be unchanged by the interrupted write", cleanedQueueDump, dump);
        }
        // check only 1 written
        assertTrue(reader.readOne());
        assertEquals(1, names.size());
        assertEquals(person1.name, names.get(0));
        assertFalse(reader.readOne());
        // do a write that throws an exception
        doWrite(queueWriter, (proxy, queue) -> {
            try {
                proxy.accept(thrower);
            } catch (NullPointerException npe) {
            // ignore
            }
        });
        try (final ChronicleQueue queue = binary(tmpDir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
            String dump = cleanQueueDump(queue.dump());
            if (lazyIndexing) {
                // reading the queue creates the index, thus changing it, so do a text comparison here
                cleanedQueueDump = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  wireType: !WireType BINARY_LIGHT,\n" + "  writePosition: [\n" + "    442,\n" + "    0\n" + "  ],\n" + "  roll: !SCQSRoll {\n" + "    length: !int 86400000,\n" + "    format: yyyyMMdd,\n" + "    epoch: 0\n" + "  },\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 8,\n" + "    indexSpacing: 1,\n" + "    index2Index: 475,\n" + "    lastIndex: 0\n" + "  },\n" + "  lastAcknowledgedIndexReplicated: -1,\n" + "  recovery: !TimedStoreRecovery {\n" + "    timeStamp: 0\n" + "  },\n" + "  deltaCheckpointInterval: 0,\n" + "  lastIndexReplicated: -1,\n" + "  sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "accept: {\n" + "  age: 40,\n" + "  name: Terry\n" + "}\n" + "# position: 475, header: 0\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + "  # length: 8, used: 0\n" + "  0, 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "...\n" + "\n";
            }
            assertEquals("queue should be unchanged by the failed write", cleanedQueueDump, dump);
        }
        // check nothing else written
        assertFalse(reader.readOne());
        // do an empty write
        ExcerptAppender appender = queueWriter.acquireAppender().lazyIndexing(lazyIndexing);
        DocumentContext wd = appender.writingDocument();
        wd.rollbackOnClose();
        wd.close();
        // check queue unchanged
        String dump = cleanQueueDump(queueWriter.dump());
        assertEquals("queue should be unchanged by the failed write", cleanedQueueDump, dump);
        // check nothing else written
        assertFalse(reader.readOne());
        // write another person to same queue in this thread
        doWrite(queueWriter, (proxy, queue) -> proxy.accept(person2));
        assertTrue(reader.readOne());
        assertEquals(2, names.size());
        assertEquals(person2.name, names.get(1));
        assertFalse(reader.readOne());
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) SingleChronicleQueueBuilder.binary(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder.binary) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) After(org.junit.After) BiConsumer(java.util.function.BiConsumer) DirectoryUtils(net.openhft.chronicle.queue.DirectoryUtils) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) BinaryLongReference(net.openhft.chronicle.bytes.ref.BinaryLongReference) Parameterized(org.junit.runners.Parameterized) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) DocumentContext(net.openhft.chronicle.wire.DocumentContext) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) BinaryLongArrayReference(net.openhft.chronicle.bytes.ref.BinaryLongArrayReference) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) WireOut(net.openhft.chronicle.wire.WireOut) List(java.util.List) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) RollCycles(net.openhft.chronicle.queue.RollCycles) NotNull(org.jetbrains.annotations.NotNull) Marshallable(net.openhft.chronicle.wire.Marshallable) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) 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) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 13 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class ChronicleHistoryReader method readChronicle.

public Map<String, Histogram> readChronicle() {
    final SingleChronicleQueue q = createQueue();
    final ExcerptTailer tailer = q.createTailer();
    final WireParselet parselet = parselet();
    final MethodReader mr = new VanillaMethodReader(tailer, true, parselet, null, parselet);
    MessageHistory.set(new VanillaMessageHistory());
    while (!Thread.currentThread().isInterrupted() && mr.readOne()) {
        ++counter;
        if (this.progress && counter % 1_000_000L == 0) {
            System.out.println("Progress: " + counter);
        }
    }
    return histos;
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) WireParselet(net.openhft.chronicle.wire.WireParselet) VanillaMethodReader(net.openhft.chronicle.wire.VanillaMethodReader) VanillaMessageHistory(net.openhft.chronicle.wire.VanillaMessageHistory) MethodReader(net.openhft.chronicle.bytes.MethodReader) VanillaMethodReader(net.openhft.chronicle.wire.VanillaMethodReader) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer)

Example 14 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class LastAppendedTest method testLastWritten.

@Test
public void testLastWritten() {
    SetTimeProvider timeProvider = new SetTimeProvider();
    try (SingleChronicleQueue outQueue = single(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).sourceId(1).timeProvider(timeProvider).build()) {
        try (SingleChronicleQueue inQueue = single(getTmpDir()).rollCycle(RollCycles.TEST_SECONDLY).sourceId(2).timeProvider(timeProvider).build()) {
            // write some initial data to the inqueue
            final Msg msg = inQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
            msg.msg("somedata-0");
            timeProvider.advanceMillis(1000);
            // write data into the inQueue
            msg.msg("somedata-1");
            // read a message on the in queue and write it to the out queue
            {
                Msg out = outQueue.acquireAppender().methodWriterBuilder(Msg.class).recordHistory(true).build();
                MethodReader methodReader = inQueue.createTailer().methodReader((Msg) out::msg);
                // reads the somedata-0
                methodReader.readOne();
                // reads the somedata-1
                methodReader.readOne();
            }
            // write data into the inQueue
            msg.msg("somedata-2");
            timeProvider.advanceMillis(2000);
            msg.msg("somedata-3");
            msg.msg("somedata-4");
            System.out.println(inQueue.dump());
            AtomicReference<String> actualValue = new AtomicReference<>();
            // check that we are able to pick up from where we left off, in other words the next read should be somedata-2
            {
                ExcerptTailer excerptTailer = inQueue.createTailer().afterLastWritten(outQueue);
                MethodReader methodReader = excerptTailer.methodReader((Msg) actualValue::set);
                methodReader.readOne();
                Assert.assertEquals("somedata-2", actualValue.get());
                methodReader.readOne();
                Assert.assertEquals("somedata-3", actualValue.get());
                methodReader.readOne();
                Assert.assertEquals("somedata-4", actualValue.get());
            }
        }
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) AtomicReference(java.util.concurrent.atomic.AtomicReference) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Example 15 with MethodReader

use of net.openhft.chronicle.bytes.MethodReader in project Chronicle-Queue by OpenHFT.

the class MessageReaderWriterTest method testWriteWhileReading.

@Test
public void testWriteWhileReading() {
    ClassAliasPool.CLASS_ALIASES.addAlias(Message1.class);
    ClassAliasPool.CLASS_ALIASES.addAlias(Message2.class);
    File path1 = DirectoryUtils.tempDir("testWriteWhileReading1");
    File path2 = DirectoryUtils.tempDir("testWriteWhileReading2");
    try (SingleChronicleQueue queue1 = SingleChronicleQueueBuilder.binary(path1).testBlockSize().build();
        SingleChronicleQueue queue2 = SingleChronicleQueueBuilder.binary(path2).testBlockSize().build()) {
        MethodReader reader2 = queue1.createTailer().methodReader(ObjectUtils.printAll(MessageListener.class));
        MessageListener writer2 = queue2.acquireAppender().methodWriter(MessageListener.class);
        MessageListener processor = new MessageProcessor(writer2);
        MethodReader reader1 = queue1.createTailer().methodReader(processor);
        MessageListener writer1 = queue1.acquireAppender().methodWriter(MessageListener.class);
        for (int i = 0; i < 3; i++) {
            // write a message
            writer1.method1(new Message1("hello"));
            writer1.method2(new Message2(234));
            // read those messages
            assertTrue(reader1.readOne());
            assertTrue(reader1.readOne());
            assertFalse(reader1.readOne());
            // read the produced messages
            assertTrue(reader2.readOne());
            assertTrue(reader2.readOne());
            assertFalse(reader2.readOne());
        }
    // System.out.println(queue1.dump());
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) File(java.io.File) MethodReader(net.openhft.chronicle.bytes.MethodReader) Test(org.junit.Test)

Aggregations

MethodReader (net.openhft.chronicle.bytes.MethodReader)22 Test (org.junit.Test)13 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)10 File (java.io.File)9 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)6 IOException (java.io.IOException)3 OS (net.openhft.chronicle.core.OS)3 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)3 SingleChronicleQueueBuilder (net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder)3 ConfigParser (net.openhft.load.config.ConfigParser)3 StageConfig (net.openhft.load.config.StageConfig)3 FileWriter (java.io.FileWriter)2 Writer (java.io.Writer)2 List (java.util.List)2 TimeUnit (java.util.concurrent.TimeUnit)2 IOTools (net.openhft.chronicle.core.io.IOTools)2 RollCycles (net.openhft.chronicle.queue.RollCycles)2 MessageHistory (net.openhft.chronicle.wire.MessageHistory)2 Assert (org.junit.Assert)2 Assert.assertEquals (org.junit.Assert.assertEquals)2