use of org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult in project kafka by apache.
the class KafkaAdminClientTest method testElectLeaders.
@Test
public void testElectLeaders() throws Exception {
TopicPartition topic1 = new TopicPartition("topic", 0);
TopicPartition topic2 = new TopicPartition("topic", 2);
try (AdminClientUnitTestEnv env = mockClientEnv()) {
for (ElectionType electionType : ElectionType.values()) {
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
// Test a call where one partition has an error.
ApiError value = ApiError.fromThrowable(new ClusterAuthorizationException(null));
List<ReplicaElectionResult> electionResults = new ArrayList<>();
ReplicaElectionResult electionResult = new ReplicaElectionResult();
electionResult.setTopic(topic1.topic());
// Add partition 1 result
PartitionResult partition1Result = new PartitionResult();
partition1Result.setPartitionId(topic1.partition());
partition1Result.setErrorCode(value.error().code());
partition1Result.setErrorMessage(value.message());
electionResult.partitionResult().add(partition1Result);
// Add partition 2 result
PartitionResult partition2Result = new PartitionResult();
partition2Result.setPartitionId(topic2.partition());
partition2Result.setErrorCode(value.error().code());
partition2Result.setErrorMessage(value.message());
electionResult.partitionResult().add(partition2Result);
electionResults.add(electionResult);
env.kafkaClient().prepareResponse(new ElectLeadersResponse(0, Errors.NONE.code(), electionResults, ApiKeys.ELECT_LEADERS.latestVersion()));
ElectLeadersResult results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)));
assertEquals(results.partitions().get().get(topic2).get().getClass(), ClusterAuthorizationException.class);
// Test a call where there are no errors. By mutating the internal of election results
partition1Result.setErrorCode(ApiError.NONE.error().code());
partition1Result.setErrorMessage(ApiError.NONE.message());
partition2Result.setErrorCode(ApiError.NONE.error().code());
partition2Result.setErrorMessage(ApiError.NONE.message());
env.kafkaClient().prepareResponse(new ElectLeadersResponse(0, Errors.NONE.code(), electionResults, ApiKeys.ELECT_LEADERS.latestVersion()));
results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)));
assertFalse(results.partitions().get().get(topic1).isPresent());
assertFalse(results.partitions().get().get(topic2).isPresent());
// Now try a timeout
results = env.adminClient().electLeaders(electionType, new HashSet<>(asList(topic1, topic2)), new ElectLeadersOptions().timeoutMs(100));
TestUtils.assertFutureError(results.partitions(), TimeoutException.class);
}
}
}
use of org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult in project kafka by apache.
the class ReplicationControlManagerTest method buildElectLeadersResponse.
private ElectLeadersResponseData buildElectLeadersResponse(Errors topLevelError, boolean electAllPartitions, Map<TopicPartition, ApiError> errors) {
Map<String, List<Map.Entry<TopicPartition, ApiError>>> errorsByTopic = errors.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getKey().topic()));
ElectLeadersResponseData response = new ElectLeadersResponseData().setErrorCode(topLevelError.code());
errorsByTopic.forEach((topic, partitionErrors) -> {
ReplicaElectionResult electionResult = new ReplicaElectionResult().setTopic(topic);
electionResult.setPartitionResult(partitionErrors.stream().filter(entry -> !electAllPartitions || entry.getValue().error() != ELECTION_NOT_NEEDED).map(entry -> {
TopicPartition topicPartition = entry.getKey();
ApiError error = entry.getValue();
return new PartitionResult().setPartitionId(topicPartition.partition()).setErrorCode(error.error().code()).setErrorMessage(error.message());
}).collect(Collectors.toList()));
response.replicaElectionResults().add(electionResult);
});
return response;
}
use of org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult in project kafka by apache.
the class RequestResponseTest method createElectLeadersResponse.
private ElectLeadersResponse createElectLeadersResponse() {
String topic = "myTopic";
List<ReplicaElectionResult> electionResults = new ArrayList<>();
ReplicaElectionResult electionResult = new ReplicaElectionResult();
electionResults.add(electionResult);
electionResult.setTopic(topic);
// Add partition 1 result
PartitionResult partitionResult = new PartitionResult();
partitionResult.setPartitionId(0);
partitionResult.setErrorCode(ApiError.NONE.error().code());
partitionResult.setErrorMessage(ApiError.NONE.message());
electionResult.partitionResult().add(partitionResult);
// Add partition 2 result
partitionResult = new PartitionResult();
partitionResult.setPartitionId(1);
partitionResult.setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code());
partitionResult.setErrorMessage(Errors.UNKNOWN_TOPIC_OR_PARTITION.message());
electionResult.partitionResult().add(partitionResult);
return new ElectLeadersResponse(200, Errors.NONE.code(), electionResults, ELECT_LEADERS.latestVersion());
}
use of org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult in project kafka by apache.
the class ReplicationControlManagerTest method collectElectLeadersErrors.
private Map<TopicPartition, PartitionResult> collectElectLeadersErrors(ElectLeadersResponseData response) {
Map<TopicPartition, PartitionResult> res = new HashMap<>();
response.replicaElectionResults().forEach(topicResult -> {
String topic = topicResult.topic();
topicResult.partitionResult().forEach(partitionResult -> {
TopicPartition topicPartition = new TopicPartition(topic, partitionResult.partitionId());
res.put(topicPartition, partitionResult);
});
});
return res;
}
use of org.apache.kafka.common.message.ElectLeadersResponseData.PartitionResult 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);
}
Aggregations