Search in sources :

Example 11 with DataHeaderFlyweight

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

the class ReplaySessionTest method shouldDoCrcForEachDataFrame.

@Test
public void shouldDoCrcForEachDataFrame() throws IOException {
    context.recordChecksum(crc32());
    context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
    final RecordingWriter writer = new RecordingWriter(RECORDING_ID, 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, 10, UNFRAGMENTED, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 10));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH, FRAME_LENGTH, 20, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 20));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 2, FRAME_LENGTH, 30, END_FRAG_FLAG, HDR_TYPE_DATA, checksum(FRAME_LENGTH, 30));
    recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 3, FRAME_LENGTH, 40, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
    writer.close();
    final long length = 4 * FRAME_LENGTH;
    final long correlationId = 1L;
    final int sessionId = Integer.MIN_VALUE;
    final int streamId = Integer.MAX_VALUE;
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession, null, context.recordChecksum())) {
        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);
        final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
        mockPublication(mockReplayPub, termBuffer);
        assertNotEquals(0, replaySession.doWork());
        assertThat(messageCounter, is(2));
        validateFrame(termBuffer, 0, FRAME_LENGTH, 10, UNFRAGMENTED, sessionId, streamId);
        validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 20, BEGIN_FRAG_FLAG, sessionId, streamId);
        validateFrame(termBuffer, 2 * FRAME_LENGTH, FRAME_LENGTH, 30, 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 12 with DataHeaderFlyweight

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

the class LogInspector method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
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, i);
            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);
                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 13 with DataHeaderFlyweight

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

the class SegmentInspector method dumpSegment.

/**
 * Dump the contents of a segment file to a {@link PrintStream}.
 *
 * @param out              for the dumped contents.
 * @param messageDumpLimit for the number of bytes per message fragment to dump.
 * @param buffer           the wraps the segment file.
 */
public static void dumpSegment(final PrintStream out, final int messageDumpLimit, final UnsafeBuffer buffer) {
    final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
    final int length = buffer.capacity();
    int offset = 0;
    while (offset < length) {
        dataHeaderFlyweight.wrap(buffer, offset, length - offset);
        out.println(offset + ": " + dataHeaderFlyweight);
        final int frameLength = dataHeaderFlyweight.frameLength();
        if (frameLength < DataHeaderFlyweight.HEADER_LENGTH) {
            break;
        }
        final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
        out.println(LogInspector.formatBytes(buffer, offset + HEADER_LENGTH, limit));
        offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
    }
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight)

Example 14 with DataHeaderFlyweight

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

the class ImageArchivingSessionTest method setupMockTermBuff.

@Before
public void setupMockTermBuff() throws IOException {
    termFile = File.createTempFile("archiver.test", "source");
    logBufferRandomAccessFile = new RandomAccessFile(termFile, "rw");
    // size this file as a mock term buffer
    mockLogBufferChannel = logBufferRandomAccessFile.getChannel();
    mockLogBufferChannel.position(termBufferLength - 1);
    mockLogBufferChannel.write(ByteBuffer.wrap(new byte[1]));
    // write some data at term offset
    final ByteBuffer bb = ByteBuffer.allocate(100);
    mockLogBufferChannel.position(termOffset);
    mockLogBufferChannel.write(bb);
    mockLogBufferMapped = new UnsafeBuffer(mockLogBufferChannel.map(FileChannel.MapMode.READ_WRITE, 0, termBufferLength));
    // prep a single message in the log buffer
    final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight();
    headerFlyweight.wrap(mockLogBufferMapped);
    headerFlyweight.headerType(DataHeaderFlyweight.HDR_TYPE_DATA).frameLength(100);
}
Also used : DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ByteBuffer(java.nio.ByteBuffer)

Example 15 with DataHeaderFlyweight

use of io.aeron.protocol.DataHeaderFlyweight in project nd4j by deeplearning4j.

the class LogInspector method main.

public static void main(final String[] args) throws Exception {
    final PrintStream out = System.out;
    if (args.length < 1) {
        out.println("Usage: LogInspector <logFileName> [message dump limit]");
        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, READ_ONLY)) {
        out.println("======================================================================");
        out.format("%s Inspection dump for %s%n", new Date(), logFileName);
        out.println("======================================================================");
        final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
        final UnsafeBuffer[] termBuffers = logBuffers.termBuffers();
        final int termLength = logBuffers.termLength();
        final UnsafeBuffer metaDataBuffer = logBuffers.metaDataBuffer();
        out.format("Time of last SM: %s%n", new Date(timeOfLastStatusMessage(metaDataBuffer)));
        out.format("Initial term id: %d%n", initialTermId(metaDataBuffer));
        out.format("   Active index: %d%n", activePartitionIndex(metaDataBuffer));
        out.format("    Term length: %d%n", termLength);
        out.format("     MTU length: %d%n%n", mtuLength(metaDataBuffer));
        if (!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;
            out.format("Index %d Term Meta Data termOffset=%d termId=%d rawTail=%d%n", i, termOffset, termId(rawTail), rawTail);
        }
        for (int i = 0; i < PARTITION_COUNT; i++) {
            out.println("%n======================================================================");
            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 && 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.out.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)

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