Search in sources :

Example 6 with RollingChronicleQueue

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

the class IndexTest method test.

@Test
public void test() throws IOException {
    try (final RollingChronicleQueue queue = SingleChronicleQueueBuilder.binary(getTmpDir()).testBlockSize().wireType(this.wireType).build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        for (int i = 0; i < 5; i++) {
            final int n = i;
            appender.writeDocument(w -> w.write(TestKey.test).int32(n));
            final int cycle = queue.lastCycle();
            long index0 = queue.rollCycle().toIndex(cycle, n);
            long indexA = appender.lastIndexAppended();
            accessHexEquals(index0, indexA);
        }
    }
}
Also used : ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) Test(org.junit.Test)

Example 7 with RollingChronicleQueue

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

the class MultiThreadedRollTest method test.

@Test(timeout = 10000)
public void test() throws ExecutionException, InterruptedException {
    final SetTimeProvider timeProvider = new SetTimeProvider();
    timeProvider.currentTimeMillis(1000);
    final File path = DirectoryUtils.tempDir("MultiThreadedRollTest");
    try (final RollingChronicleQueue wqueue = binary(path).testBlockSize().timeProvider(timeProvider).rollCycle(TEST_SECONDLY).build()) {
        wqueue.acquireAppender().writeText("hello world");
        try (final RollingChronicleQueue rqueue = binary(path).testBlockSize().timeProvider(timeProvider).rollCycle(TEST_SECONDLY).build()) {
            ExcerptTailer tailer = rqueue.createTailer();
            Future f = reader.submit(() -> {
                long index;
                do {
                    try (DocumentContext documentContext = tailer.readingDocument()) {
                        System.out.println("tailer.state: " + tailer.state());
                        // index is only meaningful if present.
                        index = documentContext.index();
                        // if (documentContext.isPresent())
                        final boolean present = documentContext.isPresent();
                        System.out.println("documentContext.isPresent=" + present + (present ? ",index=" + Long.toHexString(index) : ", no index"));
                        Jvm.pause(50);
                    }
                } while (index != 0x200000000L && !reader.isShutdown());
            });
            timeProvider.currentTimeMillis(2000);
            ((SingleChronicleQueueExcerpts.StoreAppender) wqueue.acquireAppender()).writeEndOfCycleIfRequired();
            Jvm.pause(200);
            wqueue.acquireAppender().writeText("hello world");
            f.get();
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) Future(java.util.concurrent.Future) DocumentContext(net.openhft.chronicle.wire.DocumentContext) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File) MappedFile(net.openhft.chronicle.bytes.MappedFile) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 8 with RollingChronicleQueue

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

the class QueueEpochTest method shouldRollQueueFilesAccordingToUtcOffset.

@Test
public void shouldRollQueueFilesAccordingToUtcOffset() {
    logDebug("UTC offset is %dms%n", UTC_OFFSET);
    final File queueDir = getTmpDir();
    final CapturingStoreFileListener fileListener = new CapturingStoreFileListener();
    setCurrentTime(MIDNIGHT_UTC_BASE_TIME);
    try (final RollingChronicleQueue queue = ChronicleQueue.singleBuilder(queueDir).rollTime(LocalTime.of(21, 15), // epoch is deprecated in favour of rollTime
    ZoneOffset.UTC).timeProvider(// override the clock used by the queue to detect roll-over
    this::getCurrentTime).storeFileListener(// capture file-roll events
    fileListener).rollCycle(DAILY_ROLL).build()) {
        logDebug("Queue epoch offset is %d%n", queue.epoch());
        final ExcerptAppender appender = queue.acquireAppender();
        final TestEvent eventWriter = appender.methodWriter(TestEvent.class);
        setCurrentTime(TEN_MINUTES_BEFORE_ROLL_TIME);
        eventWriter.setOrGetEvent(Long.toString(TEN_MINUTES_BEFORE_ROLL_TIME));
        setCurrentTime(FIVE_MINUTES_BEFORE_ROLL_TIME);
        eventWriter.setOrGetEvent(Long.toString(FIVE_MINUTES_BEFORE_ROLL_TIME));
        setCurrentTime(ONE_SECOND_BEFORE_ROLL_TIME);
        eventWriter.setOrGetEvent(Long.toString(ONE_SECOND_BEFORE_ROLL_TIME));
        assertEquals(0, fileListener.numberOfRollEvents());
        setCurrentTime(ONE_SECOND_AFTER_ROLL_TIME);
        eventWriter.setOrGetEvent(Long.toString(ONE_SECOND_AFTER_ROLL_TIME));
        assertEquals(1, fileListener.numberOfRollEvents());
        setCurrentTime(ONE_SECOND_BEFORE_ROLL_TIME + ONE_DAY);
        eventWriter.setOrGetEvent(Long.toString(ONE_SECOND_BEFORE_ROLL_TIME + ONE_DAY));
        assertEquals(1, fileListener.numberOfRollEvents());
        setCurrentTime(ONE_SECOND_AFTER_ROLL_TIME + ONE_DAY);
        eventWriter.setOrGetEvent(Long.toString(ONE_SECOND_AFTER_ROLL_TIME + ONE_DAY));
        assertEquals(2, fileListener.numberOfRollEvents());
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) File(java.io.File) Test(org.junit.Test)

Example 9 with RollingChronicleQueue

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

the class SingleCQFormat2Test method testWritingThreeMessages.

@Test
public void testWritingThreeMessages() throws FileNotFoundException {
    for (int m = 0; m <= 2; m++) {
        appendMode = m;
        @NotNull File dir = getTmpDir();
        dir.mkdir();
        try (@NotNull RollingChronicleQueue queue = binary(dir).blockSize(QueueUtil.testBlockSize()).indexCount(8).indexSpacing(1).build()) {
            long start = RollCycles.DEFAULT.toIndex(queue.cycle(), 0);
            appendMessage(queue, start, "Hello World");
            @NotNull String expectedEager = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    392,\n" + "    1683627180032\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 8,\n" + "    indexSpacing: 1,\n" + "    index2Index: 196,\n" + "    lastIndex: 1\n" + "  },\n" + "  dataFormat: 1\n" + "}\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + "  # length: 8, used: 1\n" + "  296,\n" + "  0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "--- !!meta-data #binary\n" + "index: [\n" + "  # length: 8, used: 1\n" + "  392,\n" + "  0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "--- !!data #binary\n" + "msg: Hello World\n" + "...\n";
            checkFileContents(getFirstQueueFile(dir), expectedEager);
            appendMessage(queue, start + 1, "Another Hello World");
            @NotNull String expectedEager2 = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    412,\n" + "    1769526525953\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 8,\n" + "    indexSpacing: 1,\n" + "    index2Index: 196,\n" + "    lastIndex: 2\n" + "  },\n" + "  dataFormat: 1\n" + "}\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + "  # length: 8, used: 1\n" + "  296,\n" + "  0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "--- !!meta-data #binary\n" + "index: [\n" + "  # length: 8, used: 2\n" + "  392,\n" + "  412,\n" + "  0, 0, 0, 0, 0, 0\n" + "]\n" + "--- !!data #binary\n" + "msg: Hello World\n" + "--- !!data #binary\n" + "msg: Another Hello World\n" + "...\n";
            checkFileContents(getFirstQueueFile(dir), expectedEager2);
            appendMessage(queue, start + 2, "Bye for now");
            @NotNull String expectedEager3 = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    440,\n" + "    1889785610242\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 8,\n" + "    indexSpacing: 1,\n" + "    index2Index: 196,\n" + "    lastIndex: 3\n" + "  },\n" + "  dataFormat: 1\n" + "}\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + "  # length: 8, used: 1\n" + "  296,\n" + "  0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "--- !!meta-data #binary\n" + "index: [\n" + "  # length: 8, used: 3\n" + "  392,\n" + "  412,\n" + "  440,\n" + "  0, 0, 0, 0, 0\n" + "]\n" + "--- !!data #binary\n" + "msg: Hello World\n" + "--- !!data #binary\n" + "msg: Another Hello World\n" + "--- !!data #binary\n" + "msg: Bye for now\n" + "...\n";
            checkFileContents(getFirstQueueFile(dir), expectedEager3);
        }
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) Test(org.junit.Test)

Example 10 with RollingChronicleQueue

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

the class SingleCQFormatTest method testCompleteHeader2.

@Test
public void testCompleteHeader2() throws FileNotFoundException {
    final File dir = new File(OS.getTarget(), getClass().getSimpleName() + "-" + Time.uniqueId());
    dir.mkdir();
    final MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101-02" + SingleChronicleQueue.SUFFIX), QueueUtil.testBlockSize() * 2L);
    final Wire wire = new BinaryWire(bytes);
    wire.usePadding(true);
    try (final SingleChronicleQueueStore store = new SingleChronicleQueueStore(RollCycles.HOURLY, WireType.BINARY, bytes, 4 << 10, 4)) {
        try (DocumentContext dc = wire.writingDocument(true)) {
            dc.wire().write("header").typedMarshallable(store);
        }
        assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    0,\n" + "    0\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: !short 4096,\n" + "    indexSpacing: 4,\n" + "    index2Index: 0,\n" + "    lastIndex: 0\n" + "  },\n" + "  dataFormat: 1\n" + "}\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
    }
    try (RollingChronicleQueue queue = binary(dir).testBlockSize().rollCycle(RollCycles.HOURLY).build()) {
        testQueue(queue);
        assertEquals(2, queue.firstCycle());
    }
    try {
        IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : RollingChronicleQueue(net.openhft.chronicle.queue.impl.RollingChronicleQueue) File(java.io.File) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Aggregations

RollingChronicleQueue (net.openhft.chronicle.queue.impl.RollingChronicleQueue)13 Test (org.junit.Test)12 File (java.io.File)8 DocumentContext (net.openhft.chronicle.wire.DocumentContext)4 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)2 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)2 NotNull (org.jetbrains.annotations.NotNull)2 Path (java.nio.file.Path)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MappedFile (net.openhft.chronicle.bytes.MappedFile)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1 NamedThreadFactory (net.openhft.chronicle.threads.NamedThreadFactory)1 UnrecoverableTimeoutException (net.openhft.chronicle.wire.UnrecoverableTimeoutException)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1