Search in sources :

Example 1 with WireStore

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

the class QueueInspector method getWritingThreadId.

public int getWritingThreadId() {
    final WireStore wireStore = queue.storeForCycle(queue.cycle(), queue.epoch(), false);
    if (wireStore != null) {
        final long position = wireStore.writePosition();
        final int header = wireStore.bytes().readVolatileInt(position);
        if (Wires.isReady(header)) {
            final long nextHeaderPosition = position + Wires.lengthOf(header) + Wires.SPB_HEADER_SIZE;
            final int unfinishedHeader = wireStore.bytes().readVolatileInt(nextHeaderPosition);
            if (Wires.isNotComplete(unfinishedHeader) && unfinishedHeader != 0) {
                return Wires.extractTidFromHeader(unfinishedHeader);
            }
        }
    }
    return NO_CURRENT_WRITER;
}
Also used : WireStore(net.openhft.chronicle.queue.impl.WireStore)

Example 2 with WireStore

use of net.openhft.chronicle.queue.impl.WireStore 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)

Aggregations

WireStore (net.openhft.chronicle.queue.impl.WireStore)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ExcerptAppender (net.openhft.chronicle.queue.ExcerptAppender)1 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)1 DocumentContext (net.openhft.chronicle.wire.DocumentContext)1 ValueIn (net.openhft.chronicle.wire.ValueIn)1 Test (org.junit.Test)1