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());
}
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);
}
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());
}
Aggregations