Search in sources :

Example 6 with ElectLeadersResponseData

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

the class ReplicationControlManagerTest method testElectPreferredLeaders.

@Test
public void testElectPreferredLeaders() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3, 4);
    ctx.unfenceBrokers(2, 3, 4);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 2, 3, 4 }, new int[] { 0, 2, 1 } }).topicId();
    ElectLeadersRequestData request1 = new ElectLeadersRequestData().setElectionType(ElectionType.PREFERRED.value).setTopicPartitions(new TopicPartitionsCollection(asList(new TopicPartitions().setTopic("foo").setPartitions(asList(0, 1)), new TopicPartitions().setTopic("bar").setPartitions(asList(0, 1))).iterator()));
    ControllerResult<ElectLeadersResponseData> election1Result = replication.electLeaders(request1);
    ElectLeadersResponseData expectedResponse1 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), new ApiError(PREFERRED_LEADER_NOT_AVAILABLE)), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    assertElectLeadersResponse(expectedResponse1, election1Result.response());
    assertEquals(Collections.emptyList(), election1Result.records());
    ctx.unfenceBrokers(0, 1);
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(2).setBrokerEpoch(102).setTopics(asList(new AlterIsrRequestData.TopicData().setName("foo").setPartitions(asList(new AlterIsrRequestData.PartitionData().setPartitionIndex(0).setCurrentIsrVersion(0).setLeaderEpoch(0).setNewIsr(asList(1, 2, 3)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("foo").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(0).setLeaderId(2).setLeaderEpoch(0).setIsr(asList(1, 2, 3)).setCurrentIsrVersion(1).setErrorCode(NONE.code()))))), alterIsrResult.response());
    ElectLeadersResponseData expectedResponse2 = buildElectLeadersResponse(NONE, false, Utils.mkMap(Utils.mkEntry(new TopicPartition("foo", 0), ApiError.NONE), Utils.mkEntry(new TopicPartition("foo", 1), new ApiError(ELECTION_NOT_NEEDED)), Utils.mkEntry(new TopicPartition("bar", 0), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar")), Utils.mkEntry(new TopicPartition("bar", 1), new ApiError(UNKNOWN_TOPIC_OR_PARTITION, "No such topic as bar"))));
    ctx.replay(alterIsrResult.records());
    ControllerResult<ElectLeadersResponseData> election2Result = replication.electLeaders(request1);
    assertElectLeadersResponse(expectedResponse2, election2Result.response());
    assertEquals(asList(new ApiMessageAndVersion(new PartitionChangeRecord().setPartitionId(0).setTopicId(fooId).setLeader(1), (short) 0)), election2Result.records());
}
Also used : PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ApiError(org.apache.kafka.common.requests.ApiError) TopicPartitionsCollection(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitionsCollection) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 7 with ElectLeadersResponseData

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

the class ReplicationControlManager method electLeaders.

ControllerResult<ElectLeadersResponseData> electLeaders(ElectLeadersRequestData request) {
    ElectionType electionType = electionType(request.electionType());
    List<ApiMessageAndVersion> records = new ArrayList<>();
    ElectLeadersResponseData response = new ElectLeadersResponseData();
    if (request.topicPartitions() == null) {
        // compatibility with the old controller.
        for (Entry<String, Uuid> topicEntry : topicsByName.entrySet()) {
            String topicName = topicEntry.getKey();
            ReplicaElectionResult topicResults = new ReplicaElectionResult().setTopic(topicName);
            response.replicaElectionResults().add(topicResults);
            TopicControlInfo topic = topics.get(topicEntry.getValue());
            if (topic != null) {
                for (int partitionId : topic.parts.keySet()) {
                    ApiError error = electLeader(topicName, partitionId, electionType, records);
                    // partitions which already have the desired leader.
                    if (error.error() != Errors.ELECTION_NOT_NEEDED) {
                        topicResults.partitionResult().add(new PartitionResult().setPartitionId(partitionId).setErrorCode(error.error().code()).setErrorMessage(error.message()));
                    }
                }
            }
        }
    } else {
        for (TopicPartitions topic : request.topicPartitions()) {
            ReplicaElectionResult topicResults = new ReplicaElectionResult().setTopic(topic.topic());
            response.replicaElectionResults().add(topicResults);
            for (int partitionId : topic.partitions()) {
                ApiError error = electLeader(topic.topic(), partitionId, electionType, records);
                topicResults.partitionResult().add(new PartitionResult().setPartitionId(partitionId).setErrorCode(error.error().code()).setErrorMessage(error.message()));
            }
        }
    }
    return ControllerResult.of(records, response);
}
Also used : ArrayList(java.util.ArrayList) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) Uuid(org.apache.kafka.common.Uuid) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) ElectionType(org.apache.kafka.common.ElectionType) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) ApiError(org.apache.kafka.common.requests.ApiError) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)

Example 8 with ElectLeadersResponseData

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

the class ElectLeadersResponse method electLeadersResult.

public static Map<TopicPartition, Optional<Throwable>> electLeadersResult(ElectLeadersResponseData data) {
    Map<TopicPartition, Optional<Throwable>> map = new HashMap<>();
    for (ElectLeadersResponseData.ReplicaElectionResult topicResults : data.replicaElectionResults()) {
        for (ElectLeadersResponseData.PartitionResult partitionResult : topicResults.partitionResult()) {
            Optional<Throwable> value = Optional.empty();
            Errors error = Errors.forCode(partitionResult.errorCode());
            if (error != Errors.NONE) {
                value = Optional.of(error.exception(partitionResult.errorMessage()));
            }
            map.put(new TopicPartition(topicResults.topic(), partitionResult.partitionId()), value);
        }
    }
    return map;
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData)

Aggregations

ElectLeadersResponseData (org.apache.kafka.common.message.ElectLeadersResponseData)8 Uuid (org.apache.kafka.common.Uuid)7 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)6 TopicPartition (org.apache.kafka.common.TopicPartition)5 ApiError (org.apache.kafka.common.requests.ApiError)5 Test (org.junit.jupiter.api.Test)5 HashMap (java.util.HashMap)4 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 List (java.util.List)3 Map (java.util.Map)3 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)3 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)3 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)3 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)3 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)3 TopicIdPartition (org.apache.kafka.controller.BrokersToIsrs.TopicIdPartition)3 ArrayList (java.util.ArrayList)2 Optional (java.util.Optional)2 CountDownLatch (java.util.concurrent.CountDownLatch)2