Java Code Examples for io.aeron.protocol.DataHeaderFlyweight#HEADER_LENGTH

The following examples show how to use io.aeron.protocol.DataHeaderFlyweight#HEADER_LENGTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SegmentInspector.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * 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.toString());

        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);
    }
}
 
Example 2
Source File: SenderEndPoint.java    From artio with Apache License 2.0 5 votes vote down vote up
private Action blockPosition(final long messagePosition, final int messageLength, final StreamTracker tracker)
{
    final int frameLength = DataHeaderFlyweight.HEADER_LENGTH + messageLength + HEADER_LENGTH;
    final int alignedLength = ArchiveDescriptor.alignTerm(frameLength);
    final long messageStartPosition = messagePosition - alignedLength;
    tracker.blockablePosition.blockPosition(messageStartPosition);
    tracker.skipPosition = messagePosition;
    return Action.CONTINUE;
}
 
Example 3
Source File: SequenceNumberIndexWriter.java    From artio with Apache License 2.0 5 votes vote down vote up
private void checkTermRoll(final DirectBuffer buffer, final int offset, final long endPosition, final int length)
{
    final long termBufferLength = buffer.capacity();
    if (nextRollPosition == UNINITIALISED)
    {
        final long startPosition = endPosition - (length + DataHeaderFlyweight.HEADER_LENGTH);
        nextRollPosition = startPosition + termBufferLength - offset;
    }
    else if (endPosition > nextRollPosition)
    {
        nextRollPosition += termBufferLength;
        updateFile();
    }
}
 
Example 4
Source File: MaxPositionPublicationTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldPublishFromExclusivePublication()
{
    final int initialTermId = -777;
    final int termLength = 64 * 1024;
    final long maxPosition = termLength * (Integer.MAX_VALUE + 1L);
    final long lastMessagePosition = maxPosition - (MESSAGE_LENGTH + DataHeaderFlyweight.HEADER_LENGTH);

    final String channelUri = new ChannelUriStringBuilder()
        .initialPosition(lastMessagePosition, initialTermId, termLength)
        .media("ipc")
        .validate()
        .build();

    try (Subscription subscription = aeron.addSubscription(channelUri, STREAM_ID);
        ExclusivePublication publication = aeron.addExclusivePublication(channelUri, STREAM_ID))
    {
        Tests.awaitConnected(subscription);

        assertEquals(lastMessagePosition, publication.position());
        assertEquals(lastMessagePosition, subscription.imageAtIndex(0).joinPosition());

        long resultingPosition = publication.offer(srcBuffer, 0, MESSAGE_LENGTH);
        while (resultingPosition < 0)
        {
            Tests.yield();
            resultingPosition = publication.offer(srcBuffer, 0, MESSAGE_LENGTH);
        }

        assertEquals(maxPosition, publication.maxPossiblePosition());
        assertEquals(publication.maxPossiblePosition(), resultingPosition);
        assertEquals(MAX_POSITION_EXCEEDED, publication.offer(srcBuffer, 0, MESSAGE_LENGTH));
        assertEquals(MAX_POSITION_EXCEEDED, publication.offer(srcBuffer, 0, MESSAGE_LENGTH));
    }
}
 
Example 5
Source File: LogPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean appendClusterAction(final long leadershipTermId, final long timestamp, final ClusterAction action)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + ClusterActionRequestEncoder.BLOCK_LENGTH;
    final int fragmentLength = DataHeaderFlyweight.HEADER_LENGTH +
        MessageHeaderEncoder.ENCODED_LENGTH +
        ClusterActionRequestEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long logPosition = publication.position() + BitUtil.align(fragmentLength, FRAME_ALIGNMENT);
        final long result = publication.tryClaim(length, bufferClaim);

        if (result > 0)
        {
            clusterActionRequestEncoder.wrapAndApplyHeader(
                bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .timestamp(timestamp)
                .action(action);

            bufferClaim.commit();
            return true;
        }

        checkResult(result);
    }
    while (--attempts > 0);

    return false;
}
 
Example 6
Source File: Configuration.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that the MTU is an appropriate length. MTU lengths must be a multiple of
 * {@link FrameDescriptor#FRAME_ALIGNMENT}.
 *
 * @param mtuLength to be validated.
 * @throws ConfigurationException if the MTU length is not valid.
 */
public static void validateMtuLength(final int mtuLength)
{
    if (mtuLength <= DataHeaderFlyweight.HEADER_LENGTH || mtuLength > MAX_UDP_PAYLOAD_LENGTH)
    {
        throw new ConfigurationException(
            "mtuLength must be a > HEADER_LENGTH and <= MAX_UDP_PAYLOAD_LENGTH: " + mtuLength);
    }

    if ((mtuLength & (FrameDescriptor.FRAME_ALIGNMENT - 1)) != 0)
    {
        throw new ConfigurationException("mtuLength must be a multiple of FRAME_ALIGNMENT: " + mtuLength);
    }
}
 
Example 7
Source File: NetworkPublication.java    From aeron with Apache License 2.0 5 votes vote down vote up
private int heartbeatMessageCheck(
    final long nowNs, final int activeTermId, final int termOffset, final boolean signalEos)
{
    int bytesSent = 0;

    if ((timeOfLastSendOrHeartbeatNs + PUBLICATION_HEARTBEAT_TIMEOUT_NS) - nowNs < 0)
    {
        heartbeatBuffer.clear();
        heartbeatDataHeader
            .sessionId(sessionId)
            .streamId(streamId)
            .termId(activeTermId)
            .termOffset(termOffset)
            .flags((byte)(signalEos ? BEGIN_END_AND_EOS_FLAGS : BEGIN_AND_END_FLAGS));

        bytesSent = channelEndpoint.send(heartbeatBuffer);
        if (DataHeaderFlyweight.HEADER_LENGTH != bytesSent)
        {
            shortSends.increment();
        }

        timeOfLastSendOrHeartbeatNs = nowNs;
        heartbeatsSent.incrementOrdered();
    }

    return bytesSent;
}
 
Example 8
Source File: RecordingReader.java    From aeron with Apache License 2.0 4 votes vote down vote up
int poll(final SimpleFragmentHandler fragmentHandler, final int fragmentLimit)
{
    int fragments = 0;

    while (replayPosition < replayLimit && fragments < fragmentLimit)
    {
        if (termOffset == termLength)
        {
            nextTerm();
        }

        final int frameOffset = termOffset;
        final UnsafeBuffer termBuffer = this.termBuffer;
        final int frameLength = FrameDescriptor.frameLength(termBuffer, frameOffset);
        if (frameLength <= 0)
        {
            isDone = true;
            closeRecordingSegment();
            break;
        }

        final int frameType = FrameDescriptor.frameType(termBuffer, frameOffset);
        final byte flags = FrameDescriptor.frameFlags(termBuffer, frameOffset);
        final long reservedValue = termBuffer.getLong(frameOffset + RESERVED_VALUE_OFFSET, LITTLE_ENDIAN);

        final int alignedLength = BitUtil.align(frameLength, FRAME_ALIGNMENT);
        final int dataOffset = frameOffset + DataHeaderFlyweight.HEADER_LENGTH;
        final int dataLength = frameLength - DataHeaderFlyweight.HEADER_LENGTH;

        fragmentHandler.onFragment(termBuffer, dataOffset, dataLength, frameType, flags, reservedValue);

        replayPosition += alignedLength;
        termOffset += alignedLength;
        fragments++;

        if (replayPosition >= replayLimit)
        {
            isDone = true;
            closeRecordingSegment();
            break;
        }
    }

    return fragments;
}
 
Example 9
Source File: ArchiveTest.java    From aeron with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldRecoverRecordingWithNonZeroStartPosition()
{
    final MediaDriver.Context driverCtx = new MediaDriver.Context()
        .dirDeleteOnStart(true)
        .threadingMode(ThreadingMode.SHARED);
    final Context archiveCtx = new Context().threadingMode(SHARED);

    long resultingPosition;
    final int initialPosition = DataHeaderFlyweight.HEADER_LENGTH * 9;
    final long recordingId;

    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtx.clone(), archiveCtx.clone());
        AeronArchive archive = AeronArchive.connect())
    {
        final int termLength = 128 * 1024;
        final int initialTermId = 29;

        final String channel = new ChannelUriStringBuilder()
            .media(CommonContext.IPC_MEDIA)
            .initialPosition(initialPosition, initialTermId, termLength)
            .build();

        final Publication publication = archive.addRecordedExclusivePublication(channel, 1);
        final DirectBuffer buffer = new UnsafeBuffer("Hello World".getBytes(StandardCharsets.US_ASCII));

        while ((resultingPosition = publication.offer(buffer)) <= 0)
        {
            Tests.yield();
        }

        final Aeron aeron = archive.context().aeron();

        int counterId;
        final int sessionId = publication.sessionId();
        final CountersReader countersReader = aeron.countersReader();
        while (Aeron.NULL_VALUE == (counterId = RecordingPos.findCounterIdBySession(countersReader, sessionId)))
        {
            Tests.yield();
        }

        recordingId = RecordingPos.getRecordingId(countersReader, counterId);

        while (countersReader.getCounterValue(counterId) < resultingPosition)
        {
            Tests.yield();
        }
    }

    try (Catalog catalog = openCatalog(archiveCtx))
    {
        final Catalog.CatalogEntryProcessor catalogEntryProcessor =
            (headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) ->
            descriptorEncoder.stopPosition(Aeron.NULL_VALUE);

        assertTrue(catalog.forEntry(recordingId, catalogEntryProcessor));
    }

    final Context archiveCtxClone = archiveCtx.clone();
    final MediaDriver.Context driverCtxClone = driverCtx.clone();
    try (ArchivingMediaDriver ignore = ArchivingMediaDriver.launch(driverCtxClone, archiveCtxClone);
        AeronArchive archive = AeronArchive.connect())
    {
        assertEquals(initialPosition, archive.getStartPosition(recordingId));
        assertEquals(resultingPosition, archive.getStopPosition(recordingId));
    }
    finally
    {
        archiveCtxClone.deleteDirectory();
        driverCtxClone.deleteDirectory();
    }
}
 
Example 10
Source File: LogPublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
boolean appendNewLeadershipTermEvent(
    final long leadershipTermId,
    final long timestamp,
    final long termBaseLogPosition,
    final int leaderMemberId,
    final int logSessionId,
    final TimeUnit timeUnit,
    final int appVersion)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + NewLeadershipTermEventEncoder.BLOCK_LENGTH;
    final int fragmentLength = DataHeaderFlyweight.HEADER_LENGTH +
        MessageHeaderEncoder.ENCODED_LENGTH +
        NewLeadershipTermEventEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long logPosition = publication.position() + BitUtil.align(fragmentLength, FRAME_ALIGNMENT);
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            newLeadershipTermEventEncoder.wrapAndApplyHeader(
                bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .timestamp(timestamp)
                .termBaseLogPosition(termBaseLogPosition)
                .leaderMemberId(leaderMemberId)
                .logSessionId(logSessionId)
                .timeUnit(ClusterClock.map(timeUnit))
                .appVersion(appVersion);

            bufferClaim.commit();
            return true;
        }

        checkResult(result);
    }
    while (--attempts > 0);

    return false;
}