Search in sources :

Example 6 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project aeron by real-logic.

the class LogInspector method main.

public static void main(final String[] args) {
    final PrintStream out = System.out;
    if (args.length < 1) {
        out.println("Usage: LogInspector <logFileName> [dump limit in bytes per message]");
        return;
    }
    final String logFileName = args[0];
    final int messageDumpLimit = args.length >= 2 ? Integer.parseInt(args[1]) : Integer.MAX_VALUE;
    try (LogBuffers logBuffers = new LogBuffers(logFileName)) {
        out.println("======================================================================");
        out.format("%s Inspection dump for %s%n", new Date(), logFileName);
        out.println("======================================================================");
        final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
        final UnsafeBuffer[] termBuffers = logBuffers.duplicateTermBuffers();
        final int termLength = logBuffers.termLength();
        final UnsafeBuffer metaDataBuffer = logBuffers.metaDataBuffer();
        final int initialTermId = initialTermId(metaDataBuffer);
        out.format("   Is Connected: %s%n", isConnected(metaDataBuffer));
        out.format("Initial term id: %d%n", initialTermId);
        out.format("     Term Count: %d%n", activeTermCount(metaDataBuffer));
        out.format("   Active index: %d%n", indexByTermCount(activeTermCount(metaDataBuffer)));
        out.format("    Term length: %d%n", termLength);
        out.format("     MTU length: %d%n", mtuLength(metaDataBuffer));
        out.format("      Page Size: %d%n", pageSize(metaDataBuffer));
        out.format("   EOS Position: %d%n%n", endOfStreamPosition(metaDataBuffer));
        if (!AERON_LOG_SKIP_DEFAULT_HEADER) {
            dataHeaderFlyweight.wrap(defaultFrameHeader(metaDataBuffer));
            out.format("default %s%n", dataHeaderFlyweight);
        }
        out.println();
        for (int i = 0; i < PARTITION_COUNT; i++) {
            final long rawTail = rawTailVolatile(metaDataBuffer, 0);
            final long termOffset = rawTail & 0xFFFF_FFFFL;
            final int termId = termId(rawTail);
            final int offset = (int) Math.min(termOffset, termLength);
            final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
            out.format("Index %d Term Meta Data termOffset=%d termId=%d rawTail=%d position=%d%n", i, termOffset, termId, rawTail, LogBufferDescriptor.computePosition(termId, offset, positionBitsToShift, initialTermId));
        }
        for (int i = 0; i < PARTITION_COUNT; i++) {
            out.println();
            out.println("======================================================================");
            out.format("Index %d Term Data%n%n", i);
            final UnsafeBuffer termBuffer = termBuffers[i];
            int offset = 0;
            do {
                dataHeaderFlyweight.wrap(termBuffer, offset, termLength - offset);
                out.println(offset + ": " + dataHeaderFlyweight.toString());
                final int frameLength = dataHeaderFlyweight.frameLength();
                if (frameLength < DataHeaderFlyweight.HEADER_LENGTH) {
                    if (0 == frameLength && AERON_LOG_SCAN_OVER_ZEROES) {
                        offset += FrameDescriptor.FRAME_ALIGNMENT;
                        continue;
                    }
                    try {
                        final int limit = min(termLength - (offset + HEADER_LENGTH), messageDumpLimit);
                        out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
                    } catch (final Exception ex) {
                        System.err.printf("frameLength=%d offset=%d%n", frameLength, offset);
                        ex.printStackTrace();
                    }
                    break;
                }
                final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
                out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
                offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
            } while (offset < termLength);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) LogBuffers(io.aeron.LogBuffers) Date(java.util.Date)

Example 7 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class ArchiveTool method createVerifyEntryProcessor.

private static CatalogEntryProcessor createVerifyEntryProcessor(final PrintStream out, final File archiveDir, final Set<VerifyOption> options, final Catalog catalog, final Checksum checksum, final EpochClock epochClock, final MutableInteger errorCount, final ActionConfirmation<File> truncateOnPageStraddle) {
    final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
    buffer.order(LITTLE_ENDIAN);
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);
    return (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(out, archiveDir, options, catalog, checksum, epochClock, errorCount, truncateOnPageStraddle, headerFlyweight, recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder);
}
Also used : CACHE_LINE_LENGTH(org.agrona.BitUtil.CACHE_LINE_LENGTH) MigrationUtils.fullVersionString(io.aeron.archive.MigrationUtils.fullVersionString) NATIVE_BYTE_ORDER(org.agrona.BufferUtil.NATIVE_BYTE_ORDER) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) org.agrona(org.agrona) Collectors.toMap(java.util.stream.Collectors.toMap) Path(java.nio.file.Path) HDR_TYPE_PAD(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_PAD) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) LogBufferDescriptor.positionBitsToShift(io.aeron.logbuffer.LogBufferDescriptor.positionBitsToShift) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) VERIFY_ALL_SEGMENT_FILES(io.aeron.archive.ArchiveTool.VerifyOption.VERIFY_ALL_SEGMENT_FILES) StandardOpenOption(java.nio.file.StandardOpenOption) Math.min(java.lang.Math.min) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) Stream(java.util.stream.Stream) LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN) EpochClock(org.agrona.concurrent.EpochClock) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ReplaySession.isInvalidHeader(io.aeron.archive.ReplaySession.isInvalidHeader) java.util(java.util) Checksums.newInstance(io.aeron.archive.checksum.Checksums.newInstance) Catalog(io.aeron.archive.Catalog) HeaderFlyweight(io.aeron.protocol.HeaderFlyweight) Configuration(io.aeron.driver.Configuration) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) io.aeron.archive.codecs(io.aeron.archive.codecs) MutableInteger(org.agrona.collections.MutableInteger) PrintStream(java.io.PrintStream) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Collections.emptySet(java.util.Collections.emptySet) Files(java.nio.file.Files) SESSION_ID_FIELD_OFFSET(io.aeron.protocol.DataHeaderFlyweight.SESSION_ID_FIELD_OFFSET) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) AeronException(io.aeron.exceptions.AeronException) File(java.io.File) INSTANCE(org.agrona.concurrent.SystemEpochClock.INSTANCE) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) APPLY_CHECKSUM(io.aeron.archive.ArchiveTool.VerifyOption.APPLY_CHECKSUM) CncFileDescriptor(io.aeron.CncFileDescriptor) MarkFileHeaderDecoder(io.aeron.archive.codecs.mark.MarkFileHeaderDecoder) HDR_TYPE_DATA(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_DATA) FileChannel(java.nio.channels.FileChannel) BitUtil.align(org.agrona.BitUtil.align) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) LogBufferDescriptor.computeTermIdFromPosition(io.aeron.logbuffer.LogBufferDescriptor.computeTermIdFromPosition) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 8 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class ReplaySessionTest method shouldReplayFromActiveRecording.

@Test
public void shouldReplayFromActiveRecording() throws IOException {
    final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
    final int recordingId = RECORDING_ID + 1;
    recordingSummary.recordingId = recordingId;
    recordingSummary.stopPosition = NULL_POSITION;
    when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
    recordingPosition = START_POSITION;
    context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
    final RecordingWriter writer = new RecordingWriter(recordingId, START_POSITION, SEGMENT_LENGTH, mockImage, context);
    writer.init();
    final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
    final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
    final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
    header.buffer(buffer);
    recordFragment(writer, buffer, headerFwt, header, 0, FRAME_LENGTH, 0, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
    final long length = 5 * FRAME_LENGTH;
    final long correlationId = 1L;
    final int sessionId = 42;
    final int streamId = 21;
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession, recordingPositionCounter, null)) {
        when(mockReplayPub.isClosed()).thenReturn(false);
        when(mockReplayPub.isConnected()).thenReturn(false);
        when(mockReplayPub.sessionId()).thenReturn(sessionId);
        when(mockReplayPub.streamId()).thenReturn(streamId);
        replaySession.doWork();
        assertEquals(replaySession.state(), ReplaySession.State.INIT);
        when(mockReplayPub.isConnected()).thenReturn(true);
        mockPublication(mockReplayPub, termBuffer);
        assertNotEquals(0, replaySession.doWork());
        assertThat(messageCounter, is(1));
        validateFrame(termBuffer, 0, FRAME_LENGTH, 0, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, sessionId, streamId);
        assertEquals(0, replaySession.doWork());
        recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 2, FRAME_LENGTH, 2, END_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
        recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 3, FRAME_LENGTH, 3, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
        writer.close();
        when(recordingPositionCounter.isClosed()).thenReturn(true);
        when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
        assertNotEquals(0, replaySession.doWork());
        validateFrame(termBuffer, 2 * FRAME_LENGTH, FRAME_LENGTH, 2, END_FRAG_FLAG, sessionId, streamId);
        verify(mockReplayPub).appendPadding(FRAME_LENGTH - HEADER_LENGTH);
        assertTrue(replaySession.isDone());
    }
}
Also used : Header(io.aeron.logbuffer.Header) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Test(org.junit.jupiter.api.Test)

Example 9 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class CatalogTest method shouldNotThrowWhenOldRecordingLogsAreDeleted.

@Test
void shouldNotThrowWhenOldRecordingLogsAreDeleted() throws IOException {
    final File segmentFile = new File(archiveDir, segmentFileName(recordingThreeId, SEGMENT_LENGTH * 2));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(256);
        log.write(bb);
    }
    final Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer);
    catalog.close();
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.

the class CatalogTest method shouldFixTimestampAndPositionAfterFailureFullSegment.

@Test
void shouldFixTimestampAndPositionAfterFailureFullSegment() throws Exception {
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(SEGMENT_LENGTH - 128);
        log.write(bb);
        bb.clear();
        flyweight.frameLength(128);
        log.write(bb, SEGMENT_LENGTH - 128);
        log.truncate(SEGMENT_LENGTH);
    }
    try (Catalog catalog = new Catalog(archiveDir, clock)) {
        assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
            assertThat(descriptorDecoder.stopTimestamp(), is(NULL_TIMESTAMP));
            assertThat(descriptorDecoder.stopPosition(), is(NULL_POSITION));
        }));
    }
    currentTimeMs = 42L;
    try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer)) {
        assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
            assertThat(descriptorDecoder.stopTimestamp(), is(42L));
            assertThat(descriptorDecoder.stopPosition(), is((long) SEGMENT_LENGTH));
        }));
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Catalog(io.aeron.archive.Catalog) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IoUtil(org.agrona.IoUtil) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) CatalogHeaderEncoder(io.aeron.archive.codecs.CatalogHeaderEncoder) ByteBuffer.allocate(java.nio.ByteBuffer.allocate) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) MethodSource(org.junit.jupiter.params.provider.MethodSource) ValueSource(org.junit.jupiter.params.provider.ValueSource) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) RecordingDescriptorHeaderDecoder(io.aeron.archive.codecs.RecordingDescriptorHeaderDecoder) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) BufferUtil(org.agrona.BufferUtil) Checksum(io.aeron.archive.checksum.Checksum) Arguments(org.junit.jupiter.params.provider.Arguments) ArrayUtil(org.agrona.collections.ArrayUtil) File(java.io.File) NULL_TIMESTAMP(io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP) RecordingDescriptorDecoder(io.aeron.archive.codecs.RecordingDescriptorDecoder) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) ArchiveException(io.aeron.archive.client.ArchiveException) Assertions(org.junit.jupiter.api.Assertions) EpochClock(org.agrona.concurrent.EpochClock) Matchers.is(org.hamcrest.Matchers.is) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) Checksums.crc32(io.aeron.archive.checksum.Checksums.crc32) Archive.segmentFileName(io.aeron.archive.Archive.segmentFileName) FileChannel(java.nio.channels.FileChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)25 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)16 ByteBuffer (java.nio.ByteBuffer)11 FileChannel (java.nio.channels.FileChannel)10 File (java.io.File)9 Test (org.junit.jupiter.api.Test)8 Catalog (io.aeron.archive.Catalog)6 Header (io.aeron.logbuffer.Header)6 MappedByteBuffer (java.nio.MappedByteBuffer)6 NULL_POSITION (io.aeron.archive.client.AeronArchive.NULL_POSITION)5 HEADER_LENGTH (io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH)5 StandardOpenOption (java.nio.file.StandardOpenOption)5 EpochClock (org.agrona.concurrent.EpochClock)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 CATALOG_FILE_NAME (io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME)4 FILE_IO_MAX_LENGTH_DEFAULT (io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT)4 Archive.segmentFileName (io.aeron.archive.Archive.segmentFileName)4 Checksum (io.aeron.archive.checksum.Checksum)4 NULL_TIMESTAMP (io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP)4 ArchiveException (io.aeron.archive.client.ArchiveException)4