Search in sources :

Example 1 with ListPartitionReassignmentsRequestData

use of org.apache.kafka.common.message.ListPartitionReassignmentsRequestData in project kafka by apache.

the class RequestResponseTest method createListPartitionReassignmentsRequest.

private ListPartitionReassignmentsRequest createListPartitionReassignmentsRequest(short version) {
    ListPartitionReassignmentsRequestData data = new ListPartitionReassignmentsRequestData();
    data.setTopics(singletonList(new ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics().setName("topic").setPartitionIndexes(singletonList(1))));
    return new ListPartitionReassignmentsRequest.Builder(data).build(version);
}
Also used : ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData)

Example 2 with ListPartitionReassignmentsRequestData

use of org.apache.kafka.common.message.ListPartitionReassignmentsRequestData in project kafka by apache.

the class QuorumControllerTest method testTimeouts.

/**
 * Test that certain controller operations time out if they stay on the controller
 * queue for too long.
 */
@Test
public void testTimeouts() throws Throwable {
    try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(1, Optional.empty())) {
        try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
            QuorumController controller = controlEnv.activeController();
            CountDownLatch countDownLatch = controller.pause();
            CompletableFuture<CreateTopicsResponseData> createFuture = controller.createTopics(new CreateTopicsRequestData().setTimeoutMs(0).setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo")).iterator())));
            long now = controller.time().nanoseconds();
            CompletableFuture<Map<Uuid, ApiError>> deleteFuture = controller.deleteTopics(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<Map<String, ResultOrError<Uuid>>> findTopicIdsFuture = controller.findTopicIds(now, Collections.singletonList("foo"));
            CompletableFuture<Map<Uuid, ResultOrError<String>>> findTopicNamesFuture = controller.findTopicNames(now, Collections.singletonList(Uuid.ZERO_UUID));
            CompletableFuture<List<CreatePartitionsTopicResult>> createPartitionsFuture = controller.createPartitions(now, Collections.singletonList(new CreatePartitionsTopic()));
            CompletableFuture<ElectLeadersResponseData> electLeadersFuture = controller.electLeaders(new ElectLeadersRequestData().setTimeoutMs(0).setTopicPartitions(null));
            CompletableFuture<AlterPartitionReassignmentsResponseData> alterReassignmentsFuture = controller.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTimeoutMs(0).setTopics(Collections.singletonList(new ReassignableTopic())));
            CompletableFuture<ListPartitionReassignmentsResponseData> listReassignmentsFuture = controller.listPartitionReassignments(new ListPartitionReassignmentsRequestData().setTopics(null).setTimeoutMs(0));
            while (controller.time().nanoseconds() == now) {
                Thread.sleep(0, 10);
            }
            countDownLatch.countDown();
            assertYieldsTimeout(createFuture);
            assertYieldsTimeout(deleteFuture);
            assertYieldsTimeout(findTopicIdsFuture);
            assertYieldsTimeout(findTopicNamesFuture);
            assertYieldsTimeout(createPartitionsFuture);
            assertYieldsTimeout(electLeadersFuture);
            assertYieldsTimeout(alterReassignmentsFuture);
            assertYieldsTimeout(listReassignmentsFuture);
        }
    }
}
Also used : ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) CreatableTopicCollection(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) List(java.util.List) LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) CountDownLatch(java.util.concurrent.CountDownLatch) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) Uuid(org.apache.kafka.common.Uuid) CreatableTopic(org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) Map(java.util.Map) HashMap(java.util.HashMap) CreatePartitionsTopic(org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic) Test(org.junit.jupiter.api.Test)

Example 3 with ListPartitionReassignmentsRequestData

use of org.apache.kafka.common.message.ListPartitionReassignmentsRequestData 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

ListPartitionReassignmentsRequestData (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Uuid (org.apache.kafka.common.Uuid)1 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)1 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)1 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)1 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)1 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)1 CreatePartitionsTopic (org.apache.kafka.common.message.CreatePartitionsRequestData.CreatePartitionsTopic)1 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)1 CreatableTopic (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopic)1 CreatableTopicCollection (org.apache.kafka.common.message.CreateTopicsRequestData.CreatableTopicCollection)1 CreateTopicsResponseData (org.apache.kafka.common.message.CreateTopicsResponseData)1 ElectLeadersRequestData (org.apache.kafka.common.message.ElectLeadersRequestData)1