Search in sources :

Example 1 with VoteRequestData

use of org.apache.kafka.common.message.VoteRequestData in project kafka by apache.

the class KafkaRaftClient method handleVoteRequest.

/**
 * Handle a Vote request. This API may return the following errors:
 *
 * - {@link Errors#INCONSISTENT_CLUSTER_ID} if the cluster id is presented in request
 *      but different from this node
 * - {@link Errors#BROKER_NOT_AVAILABLE} if this node is currently shutting down
 * - {@link Errors#FENCED_LEADER_EPOCH} if the epoch is smaller than this node's epoch
 * - {@link Errors#INCONSISTENT_VOTER_SET} if the request suggests inconsistent voter membership (e.g.
 *      if this node or the sender is not one of the current known voters)
 * - {@link Errors#INVALID_REQUEST} if the last epoch or offset are invalid
 */
private VoteResponseData handleVoteRequest(RaftRequest.Inbound requestMetadata) {
    VoteRequestData request = (VoteRequestData) requestMetadata.data;
    if (!hasValidClusterId(request.clusterId())) {
        return new VoteResponseData().setErrorCode(Errors.INCONSISTENT_CLUSTER_ID.code());
    }
    if (!hasValidTopicPartition(request, log.topicPartition())) {
        // Until we support multi-raft, we treat individual topic partition mismatches as invalid requests
        return new VoteResponseData().setErrorCode(Errors.INVALID_REQUEST.code());
    }
    VoteRequestData.PartitionData partitionRequest = request.topics().get(0).partitions().get(0);
    int candidateId = partitionRequest.candidateId();
    int candidateEpoch = partitionRequest.candidateEpoch();
    int lastEpoch = partitionRequest.lastOffsetEpoch();
    long lastEpochEndOffset = partitionRequest.lastOffset();
    if (lastEpochEndOffset < 0 || lastEpoch < 0 || lastEpoch >= candidateEpoch) {
        return buildVoteResponse(Errors.INVALID_REQUEST, false);
    }
    Optional<Errors> errorOpt = validateVoterOnlyRequest(candidateId, candidateEpoch);
    if (errorOpt.isPresent()) {
        return buildVoteResponse(errorOpt.get(), false);
    }
    if (candidateEpoch > quorum.epoch()) {
        transitionToUnattached(candidateEpoch);
    }
    OffsetAndEpoch lastEpochEndOffsetAndEpoch = new OffsetAndEpoch(lastEpochEndOffset, lastEpoch);
    boolean voteGranted = quorum.canGrantVote(candidateId, lastEpochEndOffsetAndEpoch.compareTo(endOffset()) >= 0);
    if (voteGranted && quorum.isUnattached()) {
        transitionToVoted(candidateId, candidateEpoch);
    }
    logger.info("Vote request {} with epoch {} is {}", request, candidateEpoch, voteGranted ? "granted" : "rejected");
    return buildVoteResponse(Errors.NONE, voteGranted);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) VoteResponseData(org.apache.kafka.common.message.VoteResponseData) VoteRequestData(org.apache.kafka.common.message.VoteRequestData)

Example 2 with VoteRequestData

use of org.apache.kafka.common.message.VoteRequestData in project kafka by apache.

the class RaftClientTestContext method collectVoteRequests.

List<RaftRequest.Outbound> collectVoteRequests(int epoch, int lastEpoch, long lastEpochOffset) {
    List<RaftRequest.Outbound> voteRequests = new ArrayList<>();
    for (RaftMessage raftMessage : channel.drainSendQueue()) {
        if (raftMessage.data() instanceof VoteRequestData) {
            VoteRequestData request = (VoteRequestData) raftMessage.data();
            VoteRequestData.PartitionData partitionRequest = unwrap(request);
            assertEquals(epoch, partitionRequest.candidateEpoch());
            assertEquals(localIdOrThrow(), partitionRequest.candidateId());
            assertEquals(lastEpoch, partitionRequest.lastOffsetEpoch());
            assertEquals(lastEpochOffset, partitionRequest.lastOffset());
            voteRequests.add((RaftRequest.Outbound) raftMessage);
        }
    }
    return voteRequests;
}
Also used : ArrayList(java.util.ArrayList) VoteRequestData(org.apache.kafka.common.message.VoteRequestData)

Aggregations

VoteRequestData (org.apache.kafka.common.message.VoteRequestData)2 ArrayList (java.util.ArrayList)1 VoteResponseData (org.apache.kafka.common.message.VoteResponseData)1 Errors (org.apache.kafka.common.protocol.Errors)1