Search in sources :

Example 1 with ListPartitionReassignmentsResponseData

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

the class ListPartitionReassignmentsRequest method getErrorResponse.

@Override
public AbstractResponse getErrorResponse(int throttleTimeMs, Throwable e) {
    ApiError apiError = ApiError.fromThrowable(e);
    List<OngoingTopicReassignment> ongoingTopicReassignments = new ArrayList<>();
    if (data.topics() != null) {
        for (ListPartitionReassignmentsTopics topic : data.topics()) {
            ongoingTopicReassignments.add(new OngoingTopicReassignment().setName(topic.name()).setPartitions(topic.partitionIndexes().stream().map(partitionIndex -> new OngoingPartitionReassignment().setPartitionIndex(partitionIndex)).collect(Collectors.toList())));
        }
    }
    ListPartitionReassignmentsResponseData responseData = new ListPartitionReassignmentsResponseData().setTopics(ongoingTopicReassignments).setErrorCode(apiError.error().code()).setErrorMessage(apiError.message()).setThrottleTimeMs(throttleTimeMs);
    return new ListPartitionReassignmentsResponse(responseData);
}
Also used : ListPartitionReassignmentsRequestData(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData) List(java.util.List) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) ByteBufferAccessor(org.apache.kafka.common.protocol.ByteBufferAccessor) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) ArrayList(java.util.ArrayList) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)

Example 2 with ListPartitionReassignmentsResponseData

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

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

the class ReplicationControlManagerTest method testReassignPartitions.

@Test
public void testReassignPartitions() throws Exception {
    ReplicationControlTestContext ctx = new ReplicationControlTestContext();
    ReplicationControlManager replication = ctx.replicationControl;
    ctx.registerBrokers(0, 1, 2, 3);
    ctx.unfenceBrokers(0, 1, 2, 3);
    Uuid fooId = ctx.createTestTopic("foo", new int[][] { new int[] { 1, 2, 3 }, new int[] { 3, 2, 1 } }).topicId();
    ctx.createTestTopic("bar", new int[][] { new int[] { 1, 2, 3 } }).topicId();
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
    ControllerResult<AlterPartitionReassignmentsResponseData> alterResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(asList(3, 2, 1)), new ReassignablePartition().setPartitionIndex(1).setReplicas(asList(0, 2, 1)), new ReassignablePartition().setPartitionIndex(2).setReplicas(asList(0, 2, 1)))), new ReassignableTopic().setName("bar"))));
    assertEquals(new AlterPartitionReassignmentsResponseData().setErrorMessage(null).setResponses(asList(new ReassignableTopicResponse().setName("foo").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(1).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(2).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage("Unable to find partition foo:2."))), new ReassignableTopicResponse().setName("bar"))), alterResult.response());
    ctx.replay(alterResult.records());
    ListPartitionReassignmentsResponseData currentReassigning = new ListPartitionReassignmentsResponseData().setErrorMessage(null).setTopics(asList(new OngoingTopicReassignment().setName("foo").setPartitions(asList(new OngoingPartitionReassignment().setPartitionIndex(1).setRemovingReplicas(asList(3)).setAddingReplicas(asList(0)).setReplicas(asList(0, 2, 1, 3))))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(null));
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("bar").setPartitionIndexes(asList(0, 1, 2)))));
    assertEquals(currentReassigning, replication.listPartitionReassignments(asList(new ListPartitionReassignmentsTopics().setName("foo").setPartitionIndexes(asList(0, 1, 2)))));
    ControllerResult<AlterPartitionReassignmentsResponseData> cancelResult = replication.alterPartitionReassignments(new AlterPartitionReassignmentsRequestData().setTopics(asList(new ReassignableTopic().setName("foo").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null), new ReassignablePartition().setPartitionIndex(1).setReplicas(null), new ReassignablePartition().setPartitionIndex(2).setReplicas(null))), new ReassignableTopic().setName("bar").setPartitions(asList(new ReassignablePartition().setPartitionIndex(0).setReplicas(null))))));
    assertEquals(ControllerResult.atomicOf(Collections.singletonList(new ApiMessageAndVersion(new PartitionChangeRecord().setTopicId(fooId).setPartitionId(1).setReplicas(asList(2, 1, 3)).setLeader(3).setRemovingReplicas(Collections.emptyList()).setAddingReplicas(Collections.emptyList()), (short) 0)), new AlterPartitionReassignmentsResponseData().setErrorMessage(null).setResponses(asList(new ReassignableTopicResponse().setName("foo").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorCode(NO_REASSIGNMENT_IN_PROGRESS.code()).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(1).setErrorCode(NONE.code()).setErrorMessage(null), new ReassignablePartitionResponse().setPartitionIndex(2).setErrorCode(UNKNOWN_TOPIC_OR_PARTITION.code()).setErrorMessage("Unable to find partition foo:2."))), new ReassignableTopicResponse().setName("bar").setPartitions(asList(new ReassignablePartitionResponse().setPartitionIndex(0).setErrorCode(NO_REASSIGNMENT_IN_PROGRESS.code()).setErrorMessage(null)))))), cancelResult);
    log.info("running final alterIsr...");
    ControllerResult<AlterIsrResponseData> alterIsrResult = replication.alterIsr(new AlterIsrRequestData().setBrokerId(3).setBrokerEpoch(103).setTopics(asList(new TopicData().setName("foo").setPartitions(asList(new PartitionData().setPartitionIndex(1).setCurrentIsrVersion(1).setLeaderEpoch(0).setNewIsr(asList(3, 0, 2, 1)))))));
    assertEquals(new AlterIsrResponseData().setTopics(asList(new AlterIsrResponseData.TopicData().setName("foo").setPartitions(asList(new AlterIsrResponseData.PartitionData().setPartitionIndex(1).setErrorCode(FENCED_LEADER_EPOCH.code()))))), alterIsrResult.response());
    ctx.replay(alterIsrResult.records());
    assertEquals(NONE_REASSIGNING, replication.listPartitionReassignments(null));
}
Also used : ReassignablePartition(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition) PartitionChangeRecord(org.apache.kafka.common.metadata.PartitionChangeRecord) ReassignablePartitionResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignablePartitionResponse) ReassignableTopic(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic) TopicData(org.apache.kafka.common.message.AlterIsrRequestData.TopicData) AlterPartitionReassignmentsRequestData(org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData) AlterIsrRequestData(org.apache.kafka.common.message.AlterIsrRequestData) OngoingTopicReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment) ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData) Uuid(org.apache.kafka.common.Uuid) ReassignableTopicResponse(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData.ReassignableTopicResponse) AlterPartitionReassignmentsResponseData(org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData) OngoingPartitionReassignment(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment) PartitionData(org.apache.kafka.common.message.AlterIsrRequestData.PartitionData) ApiMessageAndVersion(org.apache.kafka.server.common.ApiMessageAndVersion) AlterIsrResponseData(org.apache.kafka.common.message.AlterIsrResponseData) ListPartitionReassignmentsTopics(org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 4 with ListPartitionReassignmentsResponseData

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

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

the class RequestResponseTest method createListPartitionReassignmentsResponse.

private ListPartitionReassignmentsResponse createListPartitionReassignmentsResponse() {
    ListPartitionReassignmentsResponseData data = new ListPartitionReassignmentsResponseData();
    data.setTopics(singletonList(new ListPartitionReassignmentsResponseData.OngoingTopicReassignment().setName("topic").setPartitions(singletonList(new ListPartitionReassignmentsResponseData.OngoingPartitionReassignment().setPartitionIndex(0).setReplicas(asList(1, 2)).setAddingReplicas(singletonList(2)).setRemovingReplicas(singletonList(1))))));
    return new ListPartitionReassignmentsResponse(data);
}
Also used : ListPartitionReassignmentsResponseData(org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)

Aggregations

ListPartitionReassignmentsResponseData (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData)6 OngoingPartitionReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingPartitionReassignment)4 OngoingTopicReassignment (org.apache.kafka.common.message.ListPartitionReassignmentsResponseData.OngoingTopicReassignment)4 Test (org.junit.jupiter.api.Test)4 Uuid (org.apache.kafka.common.Uuid)3 AlterPartitionReassignmentsRequestData (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData)3 ReassignableTopic (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignableTopic)3 AlterPartitionReassignmentsResponseData (org.apache.kafka.common.message.AlterPartitionReassignmentsResponseData)3 ListPartitionReassignmentsTopics (org.apache.kafka.common.message.ListPartitionReassignmentsRequestData.ListPartitionReassignmentsTopics)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArrayList (java.util.ArrayList)2 List (java.util.List)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 ReassignablePartition (org.apache.kafka.common.message.AlterPartitionReassignmentsRequestData.ReassignablePartition)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