Search in sources :

Example 1 with ListPartitionReassignmentsResponse

use of org.apache.kafka.common.requests.ListPartitionReassignmentsResponse in project kafka by apache.

the class KafkaAdminClientTest method testListPartitionReassignments.

@Test
public void testListPartitionReassignments() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        TopicPartition tp1 = new TopicPartition("A", 0);
        OngoingPartitionReassignment tp1PartitionReassignment = new OngoingPartitionReassignment().setPartitionIndex(0).setRemovingReplicas(Arrays.asList(1, 2, 3)).setAddingReplicas(Arrays.asList(4, 5, 6)).setReplicas(Arrays.asList(1, 2, 3, 4, 5, 6));
        OngoingTopicReassignment tp1Reassignment = new OngoingTopicReassignment().setName("A").setPartitions(Collections.singletonList(tp1PartitionReassignment));
        TopicPartition tp2 = new TopicPartition("B", 0);
        OngoingPartitionReassignment tp2PartitionReassignment = new OngoingPartitionReassignment().setPartitionIndex(0).setRemovingReplicas(Arrays.asList(1, 2, 3)).setAddingReplicas(Arrays.asList(4, 5, 6)).setReplicas(Arrays.asList(1, 2, 3, 4, 5, 6));
        OngoingTopicReassignment tp2Reassignment = new OngoingTopicReassignment().setName("B").setPartitions(Collections.singletonList(tp2PartitionReassignment));
        // 1. NOT_CONTROLLER error handling
        ListPartitionReassignmentsResponseData notControllerData = new ListPartitionReassignmentsResponseData().setErrorCode(Errors.NOT_CONTROLLER.code()).setErrorMessage(Errors.NOT_CONTROLLER.message());
        MetadataResponse controllerNodeResponse = RequestTestUtils.metadataResponse(env.cluster().nodes(), env.cluster().clusterResource().clusterId(), 1, Collections.emptyList());
        ListPartitionReassignmentsResponseData reassignmentsData = new ListPartitionReassignmentsResponseData().setTopics(Arrays.asList(tp1Reassignment, tp2Reassignment));
        env.kafkaClient().prepareResponse(new ListPartitionReassignmentsResponse(notControllerData));
        env.kafkaClient().prepareResponse(controllerNodeResponse);
        env.kafkaClient().prepareResponse(new ListPartitionReassignmentsResponse(reassignmentsData));
        ListPartitionReassignmentsResult noControllerResult = env.adminClient().listPartitionReassignments();
        // no error
        noControllerResult.reassignments().get();
        // 2. UNKNOWN_TOPIC_OR_EXCEPTION_ERROR
        ListPartitionReassignmentsResponseData unknownTpData = new ListPartitionReassignmentsResponseData().setErrorCode(Errors.UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage(Errors.UNKNOWN_TOPIC_OR_PARTITION.message());
        env.kafkaClient().prepareResponse(new ListPartitionReassignmentsResponse(unknownTpData));
        ListPartitionReassignmentsResult unknownTpResult = env.adminClient().listPartitionReassignments(new HashSet<>(Arrays.asList(tp1, tp2)));
        TestUtils.assertFutureError(unknownTpResult.reassignments(), UnknownTopicOrPartitionException.class);
        // 3. Success
        ListPartitionReassignmentsResponseData responseData = new ListPartitionReassignmentsResponseData().setTopics(Arrays.asList(tp1Reassignment, tp2Reassignment));
        env.kafkaClient().prepareResponse(new ListPartitionReassignmentsResponse(responseData));
        ListPartitionReassignmentsResult responseResult = env.adminClient().listPartitionReassignments();
        Map<TopicPartition, PartitionReassignment> reassignments = responseResult.reassignments().get();
        PartitionReassignment tp1Result = reassignments.get(tp1);
        assertEquals(tp1PartitionReassignment.addingReplicas(), tp1Result.addingReplicas());
        assertEquals(tp1PartitionReassignment.removingReplicas(), tp1Result.removingReplicas());
        assertEquals(tp1PartitionReassignment.replicas(), tp1Result.replicas());
        assertEquals(tp1PartitionReassignment.replicas(), tp1Result.replicas());
        PartitionReassignment tp2Result = reassignments.get(tp2);
        assertEquals(tp2PartitionReassignment.addingReplicas(), tp2Result.addingReplicas());
        assertEquals(tp2PartitionReassignment.removingReplicas(), tp2Result.removingReplicas());
        assertEquals(tp2PartitionReassignment.replicas(), tp2Result.replicas());
        assertEquals(tp2PartitionReassignment.replicas(), tp2Result.replicas());
    }
}
Also used : OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ListPartitionReassignmentsResponse(org.apache.kafka.common.requests.ListPartitionReassignmentsResponse) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 2 with ListPartitionReassignmentsResponse

use of org.apache.kafka.common.requests.ListPartitionReassignmentsResponse 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

TopicPartition (org.apache.kafka.common.TopicPartition)2 OngoingPartitionReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment)2 OngoingTopicReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment)2 ListPartitionReassignmentsResponse (org.apache.kafka.common.requests.ListPartitionReassignmentsResponse)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 ListPartitionReassignmentsRequestData (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData)1 ListPartitionReassignmentsTopics (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics)1 ListPartitionReassignmentsResponseData (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)1 ChannelBuilder (org.apache.kafka.common.network.ChannelBuilder)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractResponse (org.apache.kafka.common.requests.AbstractResponse)1 ApiError (org.apache.kafka.common.requests.ApiError)1 ListPartitionReassignmentsRequest (org.apache.kafka.common.requests.ListPartitionReassignmentsRequest)1 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)1 Test (org.junit.jupiter.api.Test)1