Search in sources :

Example 1 with ValueIn

use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.

the class EofMarkerOnEmptyQueueTest method shouldRecoverFromEmptyQueueOnRoll.

@Test
public void shouldRecoverFromEmptyQueueOnRoll() throws Exception {
    final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
    try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpFolder.newFolder()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(clock::get).timeoutMS(1_000).testBlockSize().build()) {
        final ExcerptAppender appender = queue.acquireAppender();
        final DocumentContext context = appender.writingDocument();
        // start to write a message, but don't close the context - simulates crashed writer
        final long expectedEofMarkerPosition = context.wire().bytes().writePosition() - Wires.SPB_HEADER_SIZE;
        context.wire().writeEventName("foo").int32(1);
        final int startCycle = queue.cycle();
        clock.addAndGet(TimeUnit.SECONDS.toMillis(1L));
        final int nextCycle = queue.cycle();
        // ensure that the cycle file will roll
        assertThat(startCycle, is(not(nextCycle)));
        Executors.newSingleThreadExecutor().submit(() -> {
            try (final DocumentContext nextCtx = queue.acquireAppender().writingDocument()) {
                nextCtx.wire().writeEventName("bar").int32(7);
            }
        }).get(3, TimeUnit.SECONDS);
        final WireStore firstCycleStore = queue.storeForCycle(startCycle, 0, false);
        final long firstCycleWritePosition = firstCycleStore.writePosition();
        // assert that no write was completed
        assertThat(firstCycleWritePosition, is(0L));
        final ExcerptTailer tailer = queue.createTailer();
        int recordCount = 0;
        int lastItem = -1;
        while (true) {
            try (final DocumentContext readCtx = tailer.readingDocument()) {
                if (!readCtx.isPresent()) {
                    break;
                }
                final StringBuilder name = new StringBuilder();
                final ValueIn field = readCtx.wire().readEventName(name);
                recordCount++;
                lastItem = field.int32();
            }
        }
        assertThat(firstCycleStore.bytes().readVolatileInt(expectedEofMarkerPosition), is(Wires.END_OF_DATA));
        assertThat(recordCount, is(1));
        assertThat(lastItem, is(7));
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) WireStore(net.openhft.chronicle.queue.impl.WireStore) DocumentContext(net.openhft.chronicle.wire.DocumentContext) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 2 with ValueIn

use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.

the class ExcerptsSkippedWhenTailerDirectionNoneTest method shouldNotSkipMessageAtStartOfQueue.

@Test
public void shouldNotSkipMessageAtStartOfQueue() throws Exception {
    final File tmpDir = DirectoryUtils.tempDir(ExcerptsSkippedWhenTailerDirectionNoneTest.class.getSimpleName());
    try (final ChronicleQueue writeQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
        final ExcerptAppender excerptAppender = writeQueue.acquireAppender();
        try (final DocumentContext ctx = excerptAppender.writingDocument()) {
            ctx.wire().getValueOut().object("first");
        }
        try (final DocumentContext ctx = excerptAppender.writingDocument()) {
            ctx.wire().getValueOut().object("second");
        }
    }
    try (final SingleChronicleQueue readQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
        final ExcerptTailer tailer = readQueue.createTailer();
        final RollCycle rollCycle = readQueue.rollCycle();
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
        try (final DocumentContext ctx = tailer.direction(TailerDirection.NONE).readingDocument()) {
        // access the first document without incrementing sequence number
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
        String value;
        try (DocumentContext dc = tailer.direction(TailerDirection.FORWARD).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("first"));
        try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("second"));
        try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
            ValueIn valueIn = dc.wire().getValueIn();
            value = (String) valueIn.object();
        }
        assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
        assertThat(value, is("second"));
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) ExcerptAppender(net.openhft.chronicle.queue.ExcerptAppender) RollCycle(net.openhft.chronicle.queue.RollCycle) DocumentContext(net.openhft.chronicle.wire.DocumentContext) File(java.io.File) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Test(org.junit.Test)

Example 3 with ValueIn

use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.

the class SingleChronicleQueueStore method loadWritePosition.

private LongValue loadWritePosition(@NotNull WireIn wire) {
    final ValueIn read = wire.read(MetaDataField.writePosition);
    final int code;
    final long start = wire.bytes().readPosition();
    try {
        wire.consumePadding();
        code = wire.bytes().uncheckedReadUnsignedByte();
    } finally {
        wire.bytes().readPosition(start);
    }
    if (code == BinaryWireCode.I64_ARRAY) {
        TwoLongValue result = wire.newTwoLongReference();
        // when the write position is and array it also encodes the sequence number in the write position as the second long value
        read.int128(result);
        return result;
    }
    final LongValue result = wire.newLongReference();
    read.int64(result);
    return result;
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) TwoLongValue(net.openhft.chronicle.core.values.TwoLongValue) LongValue(net.openhft.chronicle.core.values.LongValue) TwoLongValue(net.openhft.chronicle.core.values.TwoLongValue)

Example 4 with ValueIn

use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.

the class SingleTableBuilder method build.

// *************************************************************************
// 
// *************************************************************************
@NotNull
public TableStore build() {
    if (readOnly && !file.exists()) {
        throw new IORuntimeException("File not found in readOnly mode");
    }
    try {
        MappedBytes bytes = MappedBytes.mappedBytes(file, 64 << 10, 0, readOnly);
        Wire wire = wireType.apply(bytes);
        StoreRecovery recovery = recoverySupplier.apply(wireType);
        try {
            TableStore tableStore;
            if ((!readOnly) && wire.writeFirstHeader()) {
                tableStore = writeTableStore(bytes, wire, recovery);
            } else {
                wire.readFirstHeader(timeoutMS, TimeUnit.MILLISECONDS);
                StringBuilder name = Wires.acquireStringBuilder();
                ValueIn valueIn = wire.readEventName(name);
                if (StringUtils.isEqual(name, MetaDataKeys.header.name())) {
                    tableStore = valueIn.typedMarshallable();
                } else {
                    // noinspection unchecked
                    throw new StreamCorruptedException("The first message should be the header, was " + name);
                }
            }
            return tableStore;
        } catch (TimeoutException e) {
            recovery.recoverAndWriteHeader(wire, 10_000, null, null);
            return writeTableStore(bytes, wire, recovery);
        }
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : ValueIn(net.openhft.chronicle.wire.ValueIn) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimedStoreRecovery(net.openhft.chronicle.queue.impl.single.TimedStoreRecovery) StoreRecovery(net.openhft.chronicle.queue.impl.single.StoreRecovery) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TableStore(net.openhft.chronicle.queue.impl.TableStore) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ValueIn (net.openhft.chronicle.wire.ValueIn)4 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)2 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)2 DocumentContext (net.openhft.chronicle.wire.DocumentContext)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 LongValue (net.openhft.chronicle.core.values.LongValue)1 TwoLongValue (net.openhft.chronicle.core.values.TwoLongValue)1 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)1 RollCycle (net.openhft.chronicle.queue.RollCycle)1 TableStore (net.openhft.chronicle.queue.impl.TableStore)1 WireStore (net.openhft.chronicle.queue.impl.WireStore)1 StoreRecovery (net.openhft.chronicle.queue.impl.single.StoreRecovery)1 TimedStoreRecovery (net.openhft.chronicle.queue.impl.single.TimedStoreRecovery)1