Search in sources :

Example 1 with AlterPartitionReassignmentsResponseData

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

the class RequestResponseTest method createAlterPartitionReassignmentsResponse.

private AlterPartitionReassignmentsResponse createAlterPartitionReassignmentsResponse() {
    AlterPartitionReassignmentsResponseData data = new AlterPartitionReassignmentsResponseData();
    data.responses().add(new AlterPartitionReassignmentsResponseData.ReassignableTopicResponse().setName("topic").setPartitions(singletonList(new AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse().setPartitionIndex(0).setErrorCode(Errors.NONE.code()).setErrorMessage("No reassignment is in progress for topic topic partition 0"))));
    return new AlterPartitionReassignmentsResponse(data);
}
Also used : AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)

Example 2 with AlterPartitionReassignmentsResponseData

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

the class AlterPartitionReassignmentsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    ApiError apiError = ApiError.fromThrowable(e);
    List<ReassignableTopicResponse> topicResponses = new ArrayList<>();
    for (ReassignableTopic topic : data.topics()) {
        List<ReassignablePartitionResponse> partitionResponses = topic.partitions().stream().map(partition -> new ReassignablePartitionResponse().setPartitionIndex(partition.partitionIndex()).setErrorCode(apiError.error().code()).setErrorMessage(apiError.message())).collect(Collectors.toList());
        topicResponses.add(new ReassignableTopicResponse().setName(topic.name()).setPartitions(partitionResponses));
    }
    AlterPartitionReassignmentsResponseData responseData = new AlterPartitionReassignmentsResponseData().setResponses(topicResponses).setErrorCode(apiError.error().code()).setErrorMessage(apiError.message()).setThrottleTimeMs(throttleTimeMs);
    return new AlterPartitionReassignmentsResponse(responseData);
}
Also used : ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) List(java.util.List) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) ArrayList(java.util.ArrayList)

Example 3 with AlterPartitionReassignmentsResponseData

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

the class KafkaAdminClientTest method testAlterPartitionReassignments.

@Test
public void testAlterPartitionReassignments() throws Exception {
    try (AdminClientUnitTestEnv env = mockClientEnv()) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        TopicPartition tp1 = new TopicPartition("A", 0);
        TopicPartition tp2 = new TopicPartition("B", 0);
        Map<TopicPartition, Optional<NewPartitionReassignment>> reassignments = new HashMap<>();
        reassignments.put(tp1, Optional.empty());
        reassignments.put(tp2, Optional.of(new NewPartitionReassignment(Arrays.asList(1, 2, 3))));
        // 1. server returns less responses than number of partitions we sent
        AlterPartitionReassignmentsResponseData responseData1 = new AlterPartitionReassignmentsResponseData();
        ReassignablePartitionResponse normalPartitionResponse = new ReassignablePartitionResponse().setPartitionIndex(0);
        responseData1.setResponses(Collections.singletonList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(responseData1));
        AlterPartitionReassignmentsResult result1 = env.adminClient().alterPartitionReassignments(reassignments);
        Future<Void> future1 = result1.all();
        Future<Void> future2 = result1.values().get(tp1);
        TestUtils.assertFutureError(future1, UnknownServerException.class);
        TestUtils.assertFutureError(future2, UnknownServerException.class);
        // 2. NOT_CONTROLLER error handling
        AlterPartitionReassignmentsResponseData controllerErrResponseData = new AlterPartitionReassignmentsResponseData().setErrorCode(Errors.NOT_CONTROLLER.code()).setErrorMessage(Errors.NOT_CONTROLLER.message()).setResponses(Arrays.asList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse)), new ReassignableTopicResponse().setName("B").setPartitions(Collections.singletonList(normalPartitionResponse))));
        MetadataResponse controllerNodeResponse = RequestTestUtils.metadataResponse(env.cluster().nodes(), env.cluster().clusterResource().clusterId(), 1, Collections.emptyList());
        AlterPartitionReassignmentsResponseData normalResponse = new AlterPartitionReassignmentsResponseData().setResponses(Arrays.asList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse)), new ReassignableTopicResponse().setName("B").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(controllerErrResponseData));
        env.kafkaClient().prepareResponse(controllerNodeResponse);
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(normalResponse));
        AlterPartitionReassignmentsResult controllerErrResult = env.adminClient().alterPartitionReassignments(reassignments);
        controllerErrResult.all().get();
        controllerErrResult.values().get(tp1).get();
        controllerErrResult.values().get(tp2).get();
        // 3. partition-level error
        AlterPartitionReassignmentsResponseData partitionLevelErrData = new AlterPartitionReassignmentsResponseData().setResponses(Arrays.asList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(Errors.INVALID_REPLICA_ASSIGNMENT.message()).setErrorCode(Errors.INVALID_REPLICA_ASSIGNMENT.code()))), new ReassignableTopicResponse().setName("B").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(partitionLevelErrData));
        AlterPartitionReassignmentsResult partitionLevelErrResult = env.adminClient().alterPartitionReassignments(reassignments);
        TestUtils.assertFutureError(partitionLevelErrResult.values().get(tp1), Errors.INVALID_REPLICA_ASSIGNMENT.exception().getClass());
        partitionLevelErrResult.values().get(tp2).get();
        // 4. top-level error
        String errorMessage = "this is custom error message";
        AlterPartitionReassignmentsResponseData topLevelErrResponseData = new AlterPartitionReassignmentsResponseData().setErrorCode(Errors.CLUSTER_AUTHORIZATION_FAILED.code()).setErrorMessage(errorMessage).setResponses(Arrays.asList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse)), new ReassignableTopicResponse().setName("B").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(topLevelErrResponseData));
        AlterPartitionReassignmentsResult topLevelErrResult = env.adminClient().alterPartitionReassignments(reassignments);
        assertEquals(errorMessage, TestUtils.assertFutureThrows(topLevelErrResult.all(), Errors.CLUSTER_AUTHORIZATION_FAILED.exception().getClass()).getMessage());
        assertEquals(errorMessage, TestUtils.assertFutureThrows(topLevelErrResult.values().get(tp1), Errors.CLUSTER_AUTHORIZATION_FAILED.exception().getClass()).getMessage());
        assertEquals(errorMessage, TestUtils.assertFutureThrows(topLevelErrResult.values().get(tp2), Errors.CLUSTER_AUTHORIZATION_FAILED.exception().getClass()).getMessage());
        // 5. unrepresentable topic name error
        TopicPartition invalidTopicTP = new TopicPartition("", 0);
        TopicPartition invalidPartitionTP = new TopicPartition("ABC", -1);
        Map<TopicPartition, Optional<NewPartitionReassignment>> invalidTopicReassignments = new HashMap<>();
        invalidTopicReassignments.put(invalidPartitionTP, Optional.of(new NewPartitionReassignment(Arrays.asList(1, 2, 3))));
        invalidTopicReassignments.put(invalidTopicTP, Optional.of(new NewPartitionReassignment(Arrays.asList(1, 2, 3))));
        invalidTopicReassignments.put(tp1, Optional.of(new NewPartitionReassignment(Arrays.asList(1, 2, 3))));
        AlterPartitionReassignmentsResponseData singlePartResponseData = new AlterPartitionReassignmentsResponseData().setResponses(Collections.singletonList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(singlePartResponseData));
        AlterPartitionReassignmentsResult unrepresentableTopicResult = env.adminClient().alterPartitionReassignments(invalidTopicReassignments);
        TestUtils.assertFutureError(unrepresentableTopicResult.values().get(invalidTopicTP), InvalidTopicException.class);
        TestUtils.assertFutureError(unrepresentableTopicResult.values().get(invalidPartitionTP), InvalidTopicException.class);
        unrepresentableTopicResult.values().get(tp1).get();
        // Test success scenario
        AlterPartitionReassignmentsResponseData noErrResponseData = new AlterPartitionReassignmentsResponseData().setErrorCode(Errors.NONE.code()).setErrorMessage(Errors.NONE.message()).setResponses(Arrays.asList(new ReassignableTopicResponse().setName("A").setPartitions(Collections.singletonList(normalPartitionResponse)), new ReassignableTopicResponse().setName("B").setPartitions(Collections.singletonList(normalPartitionResponse))));
        env.kafkaClient().prepareResponse(new AlterPartitionReassignmentsResponse(noErrResponseData));
        AlterPartitionReassignmentsResult noErrResult = env.adminClient().alterPartitionReassignments(reassignments);
        noErrResult.all().get();
        noErrResult.values().get(tp1).get();
        noErrResult.values().get(tp2).get();
    }
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) AlterPartitionReassignmentsResponse(org.apache.kafka.common.requests.AlterPartitionReassignmentsResponse) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with AlterPartitionReassignmentsResponseData

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

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

the class QuorumControllerTest method testEarlyControllerResults.

/**
 * Test that certain controller operations finish immediately without putting an event
 * on the controller queue, if there is nothing to do.
 */
@Test
public void testEarlyControllerResults() 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(120000));
            long deadlineMs = controller.time().nanoseconds() + HOURS.toNanos(1);
            CompletableFuture<Map<Uuid, ApiError>> deleteFuture = controller.deleteTopics(deadlineMs, Collections.emptyList());
            CompletableFuture<Map<String, ResultOrError<Uuid>>> findTopicIdsFuture = controller.findTopicIds(deadlineMs, Collections.emptyList());
            CompletableFuture<Map<Uuid, ResultOrError<String>>> findTopicNamesFuture = controller.findTopicNames(deadlineMs, Collections.emptyList());
            CompletableFuture<List<CreatePartitionsTopicResult>> createPartitionsFuture = controller.createPartitions(deadlineMs, Collections.emptyList());
            CompletableFuture<ElectLeadersResponseData> electLeadersFuture = controller.electLeaders(new ElectLeadersRequestData().setTimeoutMs(120000));
            CompletableFuture<AlterPartitionReassignmentsResponseData> alterReassignmentsFuture = controller.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTimeoutMs(12000));
            createFuture.get();
            deleteFuture.get();
            findTopicIdsFuture.get();
            findTopicNamesFuture.get();
            createPartitionsFuture.get();
            electLeadersFuture.get();
            alterReassignmentsFuture.get();
            countDownLatch.countDown();
        }
    }
}
Also used : LocalLogManagerTestEnv(org.apache.kafka.metalog.LocalLogManagerTestEnv) ElectLeadersResponseData(org.apache.kafka.common.message.ElectLeadersResponseData) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) CountDownLatch(java.util.concurrent.CountDownLatch) CreateTopicsResponseData(org.apache.kafka.common.message.CreateTopicsResponseData) ElectLeadersRequestData(org.apache.kafka.common.message.ElectLeadersRequestData) Uuid(org.apache.kafka.common.Uuid) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) CreateTopicsRequestData(org.apache.kafka.common.message.CreateTopicsRequestData) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Aggregations

AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)8 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)5 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)5 ReassignablePartitionResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse)5 ReassignableTopicResponse (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse)5 Test (org.junit.jupiter.api.Test)5 Uuid (org.apache.kafka.common.Uuid)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 ReassignablePartition (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition)3 ListPartitionReassignmentsResponseData (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)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 CreateTopicsRequestData (org.apache.kafka.common.message.CreateTopicsRequestData)2