Search in sources :

Example 1 with TopicPartitions

use of org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions 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 2 with TopicPartitions

use of org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions 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 3 with TopicPartitions

use of org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions in project kafka by apache.

the class ElectLeadersRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    ApiError apiError = ApiError.fromThrowable(e);
    List<ReplicaElectionResult> electionResults = new ArrayList<>();
    if (data.topicPartitions() != null) {
        for (TopicPartitions topic : data.topicPartitions()) {
            ReplicaElectionResult electionResult = new ReplicaElectionResult();
            electionResult.setTopic(topic.topic());
            for (Integer partitionId : topic.partitions()) {
                PartitionResult partitionResult = new PartitionResult();
                partitionResult.setPartitionId(partitionId);
                partitionResult.setErrorCode(apiError.error().code());
                partitionResult.setErrorMessage(apiError.message());
                electionResult.partitionResult().add(partitionResult);
            }
            electionResults.add(electionResult);
        }
    }
    return new ElectLeadersResponse(throttleTimeMs, apiError.error().code(), electionResults, version());
}
Also used : ArrayList(java.util.ArrayList) ReplicaElectionResult(org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult) PartitionResult(org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult) TopicPartitions(org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)

Aggregations

TopicPartitions (org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitions)3 ArrayList (java.util.ArrayList)2 Uuid (org.apache.kafka.common.Uuid)2 ElectLeadersResponseData (org.apache.kafka.common.message.ElectLeadersResponseData)2 PartitionResult (org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult)2 ReplicaElectionResult (org.apache.kafka.common.message.ElectLeadersResponseData.ReplicaElectionResult)2 ApiError (org.apache.kafka.common.requests.ApiError)2 ApiMessageAndVersion (org.apache.kafka.server.common.ApiMessageAndVersion)2 ElectionType (org.apache.kafka.common.ElectionType)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)1 PartitionData (org.apache.kafka.common.message.AlterIsrRequestData.PartitionData)1 TopicData (org.apache.kafka.common.message.AlterIsrRequestData.TopicData)1 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)1 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)1 TopicPartitionsCollection (org.apache.kafka.common.message.ElectLeadersRequestData.TopicPartitionsCollection)1 PartitionChangeRecord (org.apache.kafka.common.metadata.PartitionChangeRecord)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1