Search in sources :

Example 1 with MetadataResponseTopic

use of org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic in project kafka by apache.

the class MetadataTest method buildTopicCollection.

private MetadataResponseTopicCollection buildTopicCollection(String topic, MetadataResponsePartition partitionMetadata) {
    MetadataResponseTopic topicMetadata = new MetadataResponseTopic().setErrorCode(Errors.NONE.code()).setName(topic).setIsInternal(false);
    topicMetadata.setPartitions(Collections.singletonList(partitionMetadata));
    MetadataResponseTopicCollection topics = new MetadataResponseTopicCollection();
    topics.add(topicMetadata);
    return topics;
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponseTopicCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection)

Example 2 with MetadataResponseTopic

use of org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic in project kafka by apache.

the class KafkaAdminClientTest method expectMetadataRequest.

private void expectMetadataRequest(AdminClientUnitTestEnv env, TopicPartition topicPartition, Node leader) {
    MetadataResponseData.MetadataResponseTopicCollection responseTopics = new MetadataResponseData.MetadataResponseTopicCollection();
    MetadataResponseTopic responseTopic = new MetadataResponseTopic().setName(topicPartition.topic()).setErrorCode(Errors.NONE.code());
    responseTopics.add(responseTopic);
    MetadataResponsePartition responsePartition = new MetadataResponsePartition().setErrorCode(Errors.NONE.code()).setPartitionIndex(topicPartition.partition()).setLeaderId(leader.id()).setReplicaNodes(singletonList(leader.id())).setIsrNodes(singletonList(leader.id()));
    responseTopic.partitions().add(responsePartition);
    env.kafkaClient().prepareResponse(request -> {
        if (!(request instanceof MetadataRequest)) {
            return false;
        }
        MetadataRequest metadataRequest = (MetadataRequest) request;
        return metadataRequest.topics().equals(singletonList(topicPartition.topic()));
    }, new MetadataResponse(new MetadataResponseData().setTopics(responseTopics), MetadataResponseData.HIGHEST_SUPPORTED_VERSION));
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

Example 3 with MetadataResponseTopic

use of org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic in project kafka by apache.

the class PartitionLeaderStrategyTest method responseWithPartitionData.

private MetadataResponse responseWithPartitionData(Map<TopicPartition, MetadataResponsePartition> responsePartitions) {
    MetadataResponseData responseData = new MetadataResponseData();
    for (Map.Entry<TopicPartition, MetadataResponsePartition> entry : responsePartitions.entrySet()) {
        TopicPartition topicPartition = entry.getKey();
        MetadataResponseTopic responseTopic = responseData.topics().find(topicPartition.topic());
        if (responseTopic == null) {
            responseTopic = new MetadataResponseTopic().setName(topicPartition.topic()).setErrorCode(Errors.NONE.code());
            responseData.topics().add(responseTopic);
        }
        responseTopic.partitions().add(entry.getValue());
    }
    return new MetadataResponse(responseData, ApiKeys.METADATA.latestVersion());
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 4 with MetadataResponseTopic

use of org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic in project kafka by apache.

the class TopicAdminTest method prepareMetadataResponse.

private MetadataResponse prepareMetadataResponse(Cluster cluster, Errors error) {
    List<MetadataResponseTopic> metadata = new ArrayList<>();
    for (String topic : cluster.topics()) {
        List<MetadataResponseData.MetadataResponsePartition> pms = new ArrayList<>();
        for (PartitionInfo pInfo : cluster.availablePartitionsForTopic(topic)) {
            MetadataResponseData.MetadataResponsePartition pm = new MetadataResponseData.MetadataResponsePartition().setErrorCode(error.code()).setPartitionIndex(pInfo.partition()).setLeaderId(pInfo.leader().id()).setLeaderEpoch(234).setReplicaNodes(Arrays.stream(pInfo.replicas()).map(Node::id).collect(Collectors.toList())).setIsrNodes(Arrays.stream(pInfo.inSyncReplicas()).map(Node::id).collect(Collectors.toList())).setOfflineReplicas(Arrays.stream(pInfo.offlineReplicas()).map(Node::id).collect(Collectors.toList()));
            pms.add(pm);
        }
        MetadataResponseTopic tm = new MetadataResponseTopic().setErrorCode(error.code()).setName(topic).setIsInternal(false).setPartitions(pms);
        metadata.add(tm);
    }
    return MetadataResponse.prepareResponse(true, 0, cluster.nodes(), cluster.clusterResource().clusterId(), cluster.controller().id(), metadata, MetadataResponse.AUTHORIZED_OPERATIONS_OMITTED);
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) PartitionInfo(org.apache.kafka.common.PartitionInfo) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo)

Example 5 with MetadataResponseTopic

use of org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic in project kafka by apache.

the class GetListOffsetsCallsBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    MetadataResponseData data = new MetadataResponseData();
    List<MetadataResponseTopic> mrTopicList = new ArrayList<>();
    Set<String> topics = new HashSet<>();
    for (int topicIndex = 0; topicIndex < topicCount; topicIndex++) {
        Uuid topicId = Uuid.randomUuid();
        String topicName = "topic-" + topicIndex;
        MetadataResponseTopic mrTopic = new MetadataResponseTopic().setTopicId(topicId).setName(topicName).setErrorCode((short) 0).setIsInternal(false);
        List<MetadataResponsePartition> mrPartitionList = new ArrayList<>();
        for (int partition = 0; partition < partitionCount; partition++) {
            TopicPartition tp = new TopicPartition(topicName, partition);
            topics.add(tp.topic());
            futures.put(tp, new KafkaFutureImpl<>());
            topicPartitionOffsets.put(tp, OffsetSpec.latest());
            MetadataResponsePartition mrPartition = new MetadataResponsePartition().setLeaderId(partition % numNodes).setPartitionIndex(partition).setIsrNodes(Arrays.asList(0, 1, 2)).setReplicaNodes(Arrays.asList(0, 1, 2)).setOfflineReplicas(Collections.emptyList()).setErrorCode((short) 0);
            mrPartitionList.add(mrPartition);
        }
        mrTopic.setPartitions(mrPartitionList);
        mrTopicList.add(mrTopic);
    }
    data.setTopics(new MetadataResponseData.MetadataResponseTopicCollection(mrTopicList.listIterator()));
    long deadline = 0L;
    short version = 0;
    context = new MetadataOperationContext<>(topics, new ListOffsetsOptions(), deadline, futures);
    context.setResponse(Optional.of(new MetadataResponse(data, version)));
    AdminClientUnitTestEnv adminEnv = new AdminClientUnitTestEnv(mockCluster());
    admin = (KafkaAdminClient) adminEnv.adminClient();
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) ListOffsetsOptions(org.apache.kafka.clients.admin.ListOffsetsOptions) ArrayList(java.util.ArrayList) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) HashSet(java.util.HashSet) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

MetadataResponseTopic (org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic)9 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)7 MetadataResponsePartition (org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition)6 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)6 TopicPartition (org.apache.kafka.common.TopicPartition)4 ArrayList (java.util.ArrayList)3 MetadataResponseTopicCollection (org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection)3 Node (org.apache.kafka.common.Node)2 PartitionInfo (org.apache.kafka.common.PartitionInfo)2 MetadataResponseBrokerCollection (org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection)2 Test (org.junit.jupiter.api.Test)2 ByteBuffer (java.nio.ByteBuffer)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)1 ListOffsetsOptions (org.apache.kafka.clients.admin.ListOffsetsOptions)1 TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)1