Java Code Examples for io.aeron.ExclusivePublication#tryClaim()

The following examples show how to use io.aeron.ExclusivePublication#tryClaim() . 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: ConsensusPublisher.java    From aeron with Apache License 2.0 6 votes vote down vote up
boolean stopCatchup(final ExclusivePublication publication, final long leadershipTermId, final int followerMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + StopCatchupEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            stopCatchupEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .followerMemberId(followerMemberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 2
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 6 votes vote down vote up
boolean joinCluster(final ExclusivePublication publication, final long leadershipTermId, final int memberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + JoinClusterEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            joinClusterEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .memberId(memberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 3
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 6 votes vote down vote up
boolean terminationPosition(final ExclusivePublication publication, final long logPosition)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + TerminationPositionEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            terminationPositionEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .logPosition(logPosition);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 4
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 6 votes vote down vote up
boolean terminationAck(final ExclusivePublication publication, final long logPosition, final int memberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + TerminationAckEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            terminationAckEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .logPosition(logPosition)
                .memberId(memberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 5
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static void pipeMessages(
    final Subscription subscription, final ExclusivePublication publication, final AtomicBoolean running)
{
    final IdleStrategy idleStrategy = idleStrategy();
    final BufferClaim bufferClaim = new BufferClaim();
    final FragmentHandler dataHandler =
        (buffer, offset, length, header) ->
        {
            long result;
            while ((result = publication.tryClaim(length, bufferClaim)) <= 0)
            {
                checkPublicationResult(result);
            }

            bufferClaim
                .flags(header.flags())
                .putBytes(buffer, offset, length)
                .commit();
        };

    final Image image = subscription.imageAtIndex(0);
    final int fragmentLimit = fragmentLimit();

    while (true)
    {
        final int fragmentsRead = image.poll(dataHandler, fragmentLimit);
        if (0 == fragmentsRead)
        {
            if (image.isClosed() || !running.get())
            {
                break;
            }
        }

        idleStrategy.idle(fragmentsRead);
    }
}
 
Example 6
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
void canvassPosition(
    final ExclusivePublication publication,
    final long logLeadershipTermId,
    final long logPosition,
    final int followerMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + CanvassPositionEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            canvassPositionEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .logLeadershipTermId(logLeadershipTermId)
                .logPosition(logPosition)
                .followerMemberId(followerMemberId);

            bufferClaim.commit();

            return;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example 7
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean requestVote(
    final ExclusivePublication publication,
    final long logLeadershipTermId,
    final long logPosition,
    final long candidateTermId,
    final int candidateMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + RequestVoteEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            requestVoteEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .logLeadershipTermId(logLeadershipTermId)
                .logPosition(logPosition)
                .candidateTermId(candidateTermId)
                .candidateMemberId(candidateMemberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 8
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
void placeVote(
    final ExclusivePublication publication,
    final long candidateTermId,
    final long logLeadershipTermId,
    final long logPosition,
    final int candidateMemberId,
    final int followerMemberId,
    final boolean vote)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + VoteEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            voteEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .candidateTermId(candidateTermId)
                .logLeadershipTermId(logLeadershipTermId)
                .logPosition(logPosition)
                .candidateMemberId(candidateMemberId)
                .followerMemberId(followerMemberId)
                .vote(vote ? BooleanType.TRUE : BooleanType.FALSE);

            bufferClaim.commit();

            return;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example 9
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
void newLeadershipTerm(
    final ExclusivePublication publication,
    final long logLeadershipTermId,
    final long logTruncatePosition,
    final long leadershipTermId,
    final long logPosition,
    final long timestamp,
    final int leaderMemberId,
    final int logSessionId,
    final boolean isStartup)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + NewLeadershipTermEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            newLeadershipTermEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .logLeadershipTermId(logLeadershipTermId)
                .logTruncatePosition(logTruncatePosition)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .timestamp(timestamp)
                .leaderMemberId(leaderMemberId)
                .logSessionId(logSessionId)
                .isStartup(isStartup ? BooleanType.TRUE : BooleanType.FALSE);

            bufferClaim.commit();

            return;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example 10
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean appendPosition(
    final ExclusivePublication publication,
    final long leadershipTermId,
    final long logPosition,
    final int followerMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + AppendPositionEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            appendPositionEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .followerMemberId(followerMemberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 11
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
void commitPosition(
    final ExclusivePublication publication,
    final long leadershipTermId,
    final long logPosition,
    final int leaderMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + CommitPositionEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            commitPositionEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .leaderMemberId(leaderMemberId);

            bufferClaim.commit();

            return;
        }

        checkResult(result);
    }
    while (--attempts > 0);
}
 
Example 12
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean catchupPosition(
    final ExclusivePublication publication,
    final long leadershipTermId,
    final long logPosition,
    final int followerMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + CatchupPositionEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            catchupPositionEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .leadershipTermId(leadershipTermId)
                .logPosition(logPosition)
                .followerMemberId(followerMemberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 13
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean addPassiveMember(
    final ExclusivePublication publication, final long correlationId, final String memberEndpoints)
{
    final int length =
        MessageHeaderEncoder.ENCODED_LENGTH +
        AddPassiveMemberEncoder.BLOCK_LENGTH +
        AddPassiveMemberEncoder.memberEndpointsHeaderLength() +
        memberEndpoints.length();

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            addPassiveMemberEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .correlationId(correlationId)
                .memberEndpoints(memberEndpoints);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 14
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean clusterMemberChange(
    final ExclusivePublication publication,
    final long correlationId,
    final int leaderMemberId,
    final String activeMembers,
    final String passiveMembers)
{
    final int length =
        MessageHeaderEncoder.ENCODED_LENGTH +
        ClusterMembersChangeEncoder.BLOCK_LENGTH +
        ClusterMembersChangeEncoder.activeMembersHeaderLength() +
        activeMembers.length() +
        ClusterMembersChangeEncoder.passiveMembersHeaderLength() +
        passiveMembers.length();

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            clusterMembersChangeEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .correlationId(correlationId)
                .leaderMemberId(leaderMemberId)
                .activeMembers(activeMembers)
                .passiveMembers(passiveMembers);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 15
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean snapshotRecordingQuery(
    final ExclusivePublication publication, final long correlationId, final int requestMemberId)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + SnapshotRecordingQueryEncoder.BLOCK_LENGTH;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            snapshotRecordingQueryEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .correlationId(correlationId)
                .requestMemberId(requestMemberId);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}
 
Example 16
Source File: ConsensusPublisher.java    From aeron with Apache License 2.0 5 votes vote down vote up
boolean backupQuery(
    final ExclusivePublication publication,
    final long correlationId,
    final int responseStreamId,
    final int version,
    final String responseChannel,
    final byte[] encodedCredentials)
{
    final int length = MessageHeaderEncoder.ENCODED_LENGTH + BackupQueryEncoder.BLOCK_LENGTH +
        BackupQueryEncoder.responseChannelHeaderLength() +
        responseChannel.length() +
        BackupQueryEncoder.encodedCredentialsHeaderLength() +
        encodedCredentials.length;

    int attempts = SEND_ATTEMPTS;
    do
    {
        final long result = publication.tryClaim(length, bufferClaim);
        if (result > 0)
        {
            backupQueryEncoder
                .wrapAndApplyHeader(bufferClaim.buffer(), bufferClaim.offset(), messageHeaderEncoder)
                .correlationId(correlationId)
                .responseStreamId(responseStreamId)
                .version(version)
                .responseChannel(responseChannel)
                .putEncodedCredentials(encodedCredentials, 0, encodedCredentials.length);

            bufferClaim.commit();

            return true;
        }

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

    return false;
}