Search in sources :

Example 6 with OngoingPartitionReassignment

use of org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment in project kafka by apache.

the class KafkaAdminClient method listPartitionReassignments.

@Override
public ListPartitionReassignmentsResult listPartitionReassignments(Optional<Set<TopicPartition>> partitions, ListPartitionReassignmentsOptions options) {
    final KafkaFutureImpl<Map<TopicPartition, PartitionReassignment>> partitionReassignmentsFuture = new KafkaFutureImpl<>();
    if (partitions.isPresent()) {
        for (TopicPartition tp : partitions.get()) {
            String topic = tp.topic();
            int partition = tp.partition();
            if (topicNameIsUnrepresentable(topic)) {
                partitionReassignmentsFuture.completeExceptionally(new InvalidTopicException("The given topic name '" + topic + "' cannot be represented in a request."));
            } else if (partition < 0) {
                partitionReassignmentsFuture.completeExceptionally(new InvalidTopicException("The given partition index " + partition + " is not valid."));
            }
            if (partitionReassignmentsFuture.isCompletedExceptionally())
                return new ListPartitionReassignmentsResult(partitionReassignmentsFuture);
        }
    }
    final long now = time.milliseconds();
    runnable.call(new Call("listPartitionReassignments", calcDeadlineMs(now, options.timeoutMs()), new ControllerNodeProvider()) {

        @Override
        ListPartitionReassignmentsRequest.Builder createRequest(int timeoutMs) {
            ListPartitionReassignmentsRequestData listData = new ListPartitionReassignmentsRequestData();
            listData.setTimeoutMs(timeoutMs);
            if (partitions.isPresent()) {
                Map<String, ListPartitionReassignmentsTopics> reassignmentTopicByTopicName = new HashMap<>();
                for (TopicPartition tp : partitions.get()) {
                    if (!reassignmentTopicByTopicName.containsKey(tp.topic()))
                        reassignmentTopicByTopicName.put(tp.topic(), new ListPartitionReassignmentsTopics().setName(tp.topic()));
                    reassignmentTopicByTopicName.get(tp.topic()).partitionIndexes().add(tp.partition());
                }
                listData.setTopics(new ArrayList<>(reassignmentTopicByTopicName.values()));
            }
            return new ListPartitionReassignmentsRequest.Builder(listData);
        }

        @Override
        void handleResponse(AbstractResponse abstractResponse) {
            ListPartitionReassignmentsResponse response = (ListPartitionReassignmentsResponse) abstractResponse;
            Errors error = Errors.forCode(response.data().errorCode());
            switch(error) {
                case NONE:
                    break;
                case NOT_CONTROLLER:
                    handleNotControllerError(error);
                    break;
                default:
                    partitionReassignmentsFuture.completeExceptionally(new ApiError(error, response.data().errorMessage()).exception());
                    break;
            }
            Map<TopicPartition, PartitionReassignment> reassignmentMap = new HashMap<>();
            for (OngoingTopicReassignment topicReassignment : response.data().topics()) {
                String topicName = topicReassignment.name();
                for (OngoingPartitionReassignment partitionReassignment : topicReassignment.partitions()) {
                    reassignmentMap.put(new TopicPartition(topicName, partitionReassignment.partitionIndex()), new PartitionReassignment(partitionReassignment.replicas(), partitionReassignment.addingReplicas(), partitionReassignment.removingReplicas()));
                }
            }
            partitionReassignmentsFuture.complete(reassignmentMap);
        }

        @Override
        void handleFailure(Throwable throwable) {
            partitionReassignmentsFuture.completeExceptionally(throwable);
        }
    }, now);
    return new ListPartitionReassignmentsResult(partitionReassignmentsFuture);
}
Also used : ChannelBuilder(org.apache.kafka.common.network.ChannelBuilder) ArrayList(java.util.ArrayList) ListPartitionReassignmentsResponse(org.apache.kafka.common.requests.ListPartitionReassignmentsResponse) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) ListPartitionReassignmentsRequest(org.apache.kafka.common.requests.ListPartitionReassignmentsRequest) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) Errors(org.apache.kafka.common.protocol.Errors) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) TopicPartition(org.apache.kafka.common.TopicPartition) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) ApiError(org.apache.kafka.common.requests.ApiError) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Aggregations

OngoingPartitionReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment)6 OngoingTopicReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment)6 ListPartitionReassignmentsTopics (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics)4 ListPartitionReassignmentsResponseData (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)4 ArrayList (java.util.ArrayList)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 TopicPartition (org.apache.kafka.common.TopicPartition)2 Uuid (org.apache.kafka.common.Uuid)2 AlterIsrRequestData (org.apache.kafka.common.message.AlterIsrRequestData)2 PartitionData (org.apache.kafka.common.message.AlterIsrRequestData.PartitionData)2 TopicData (org.apache.kafka.common.message.AlterIsrRequestData.TopicData)2 AlterIsrResponseData (org.apache.kafka.common.message.AlterIsrResponseData)2 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)2 ReassignablePartition (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition)2 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)2 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)2 ReassignablePartitionResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse)2 ReassignableTopicResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse)2 ListPartitionReassignmentsRequestData (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData)2