Search in sources :

Example 6 with InternalAppender

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

the class InternalAppenderWriteBytesTest method appendToPreviousCycle.

@Test
public void appendToPreviousCycle() {
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    @NotNull Bytes<byte[]> test1 = Bytes.from("hello world again cycle1");
    @NotNull Bytes<byte[]> test2 = Bytes.from("hello world cycle2");
    Bytes result = Bytes.elasticHeapByteBuffer();
    SetTimeProvider timeProvider = new SetTimeProvider();
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(timeProvider).rollCycle(TEST_HOURLY).build()) {
        ExcerptAppender appender = q.acquireAppender();
        appender.writeBytes(test);
        long nextIndexInFirstCycle = appender.lastIndexAppended() + 1;
        int firstCycle = q.rollCycle().toCycle(nextIndexInFirstCycle);
        timeProvider.advanceMillis(TimeUnit.SECONDS.toMillis(65 * 60));
        appender.writeBytes(test2);
        // System.out.println(q.dump());
        Assert.assertTrue(hasEOF(q, firstCycle));
        // here we try and write to previous cycle file. We will overwrite the EOF in doing so
        ignoreException("Incomplete header found at pos: 33048: c0000000, overwriting");
        ((InternalAppender) appender).writeBytes(nextIndexInFirstCycle, test1);
        Assert.assertFalse(hasEOF(q, firstCycle));
        // we have to manually fix. This is done by CQE at the end of backfilling
        appender.normaliseEOFs();
        ExcerptTailer tailer = q.createTailer();
        tailer.readBytes(result);
        assertEquals(test, result);
        result.clear();
        tailer.readBytes(result);
        assertEquals(test1, result);
        result.clear();
        tailer.readBytes(result);
        assertEquals(test2, result);
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 7 with InternalAppender

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

the class InternalAppenderWriteBytesTest method dontOverwriteExistingDifferentQueueInstance.

@Test
public void dontOverwriteExistingDifferentQueueInstance() {
    expectException("Trying to overwrite index 0 which is before the end of the queue");
    expectException("Trying to overwrite index 1 which is before the end of the queue");
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    @NotNull Bytes<byte[]> test2 = Bytes.from("hello world2");
    Bytes result = Bytes.elasticHeapByteBuffer();
    long index;
    final File tmpDir = getTmpDir();
    final String expected = "" + "--- !!meta-data #binary\n" + "header: !STStore {\n" + "  wireType: !WireType BINARY_LIGHT,\n" + "  metadata: !SCQMeta {\n" + "    roll: !SCQSRoll { length: !int 86400000, format: yyyyMMdd'T4', epoch: 0 },\n" + "    deltaCheckpointInterval: 64,\n" + "    sourceId: 0\n" + "  }\n" + "}\n" + "# position: 176, header: 0\n" + "--- !!data #binary\n" + "listing.highestCycle: 0\n" + "# position: 216, header: 1\n" + "--- !!data #binary\n" + "listing.lowestCycle: 0\n" + "# position: 256, header: 2\n" + "--- !!data #binary\n" + "listing.modCount: 1\n" + "# position: 288, header: 3\n" + "--- !!data #binary\n" + "chronicle.write.lock: -9223372036854775808\n" + "# position: 328, header: 4\n" + "--- !!data #binary\n" + "chronicle.append.lock: -9223372036854775808\n" + "# position: 368, header: 5\n" + "--- !!data #binary\n" + "chronicle.lastIndexReplicated: -1\n" + "# position: 416, header: 6\n" + "--- !!data #binary\n" + "chronicle.lastAcknowledgedIndexReplicated: -1\n" + "...\n" + "# 130596 bytes remaining\n" + "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + "  writePosition: [\n" + "    792,\n" + "    3401614098433\n" + "  ],\n" + "  indexing: !SCQSIndexing {\n" + "    indexCount: 32,\n" + "    indexSpacing: 4,\n" + "    index2Index: 196,\n" + "    lastIndex: 4\n" + "  },\n" + "  dataFormat: 1\n" + "}\n" + "# position: 196, header: -1\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + "  # length: 32, used: 1\n" + "  488,\n" + "  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "# position: 488, header: -1\n" + "--- !!meta-data #binary\n" + "index: [\n" + "  # length: 32, used: 1\n" + "  776,\n" + "  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "# position: 776, header: 0\n" + "--- !!data\n" + "hello world\n" + "# position: 792, header: 1\n" + "--- !!data\n" + "hello world2\n" + "...\n" + "# 130260 bytes remaining\n";
    try (SingleChronicleQueue q = createQueue(tmpDir)) {
        ExcerptAppender appender = q.acquireAppender();
        appender.writeBytes(test);
        appender.writeBytes(test2);
        index = appender.lastIndexAppended();
    // assertEquals(expected, q.dump());
    }
    assertEquals(1, index);
    // has to be the same tmpDir
    try (SingleChronicleQueue q = createQueue(tmpDir)) {
        InternalAppender appender = (InternalAppender) q.acquireAppender();
        appender.writeBytes(0, Bytes.from("HELLO WORLD"));
        // assertEquals(expected, q.dump());
        appender.writeBytes(1, Bytes.from("HELLO WORLD"));
        // assertEquals(expected, q.dump());
        ExcerptTailer tailer = q.createTailer();
        tailer.readBytes(result);
        assertEquals(test, result);
        assertEquals(1, tailer.index());
    }
}
Also used : Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) File(java.io.File) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 8 with InternalAppender

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

the class InternalAppenderWriteBytesTest method cantAppendPastTheEnd.

@Test(expected = java.lang.IllegalStateException.class)
public void cantAppendPastTheEnd() {
    @NotNull Bytes<byte[]> test = Bytes.from("hello world");
    try (SingleChronicleQueue q = SingleChronicleQueueBuilder.binary(getTmpDir()).timeProvider(() -> 0).build()) {
        ExcerptAppender appender = q.acquireAppender();
        appender.writeBytes(test);
        // this will throw because it is not in sequence
        ((InternalAppender) appender).writeBytes(2, test);
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) NotNull(org.jetbrains.annotations.NotNull) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 9 with InternalAppender

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

the class InternalAppenderWriteBytesTest method testJumpingAMessageThrowsAIllegalStateException.

@Test(expected = IllegalStateException.class)
public void testJumpingAMessageThrowsAIllegalStateException() {
    try (SingleChronicleQueue q = binary(tempDir("q")).rollCycle(MINUTELY).timeProvider(() -> 0).build();
        ExcerptAppender appender = q.acquireAppender()) {
        appender.writeText("hello");
        appender.writeText("hello2");
        try (final DocumentContext dc = appender.writingDocument()) {
            dc.wire().bytes().writeLong(1);
        }
        final long l = appender.lastIndexAppended();
        final RollCycle rollCycle = q.rollCycle();
        final int currentCycle = rollCycle.toCycle(l);
        // try to write to next roll cycle and write at seqnum 1 (but miss the 0th seqnum of that roll cycle)
        final long index = rollCycle.toIndex(currentCycle + 1, 1);
        ((InternalAppender) appender).writeBytes(index, Bytes.from("text"));
    }
}
Also used : SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) DocumentContext(net.openhft.chronicle.wire.DocumentContext) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender) Test(org.junit.Test)

Example 10 with InternalAppender

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

the class ChronicleQueueIndexTest method checkTheEOFisWrittenToPreQueueFileInner.

private void checkTheEOFisWrittenToPreQueueFileInner(Consumer<InternalAppender> writer1, BiConsumer<SetTimeProvider, RollCycle> tpConsumer, Consumer<InternalAppender> writer2) {
    SetTimeProvider tp = new SetTimeProvider(1_000_000_000);
    File file1 = getTmpDir();
    RollCycles rollCycle = RollCycles.DEFAULT;
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(file1).rollCycle(rollCycle).timeProvider(tp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        writer1.accept(appender);
        Assert.assertFalse(hasEOFAtEndOfFile(file1));
    }
    tpConsumer.accept(tp, rollCycle);
    try (ChronicleQueue queue = SingleChronicleQueueBuilder.builder().path(file1).rollCycle(rollCycle).timeProvider(tp).testBlockSize().build()) {
        InternalAppender appender = (InternalAppender) queue.acquireAppender();
        // assertFalse(hasEOFAtEndOfFile(file1));
        writer2.accept(appender);
        // Simulate the end of the day i.e the queue closes the day rolls
        // (note the change of index from 18264 to 18265)
        assertTrue(hasEOFAtEndOfFile(file1));
    }
}
Also used : SetTimeProvider(net.openhft.chronicle.core.time.SetTimeProvider) File(java.io.File) InternalAppender(net.openhft.chronicle.queue.impl.single.InternalAppender)

Aggregations

InternalAppender (net.openhft.chronicle.queue.impl.single.InternalAppender)10 Test (org.junit.Test)9 SingleChronicleQueue (net.openhft.chronicle.queue.impl.single.SingleChronicleQueue)7 Bytes (net.openhft.chronicle.bytes.Bytes)6 NotNull (org.jetbrains.annotations.NotNull)6 File (java.io.File)4 SetTimeProvider (net.openhft.chronicle.core.time.SetTimeProvider)2 DocumentContext (net.openhft.chronicle.wire.DocumentContext)2 ArrayList (java.util.ArrayList)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1