Search in sources :

Example 1 with MetadataResponseData

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

the class MetadataResponse method prepareResponse.

// Visible for testing
public static MetadataResponse prepareResponse(boolean hasReliableEpoch, int throttleTimeMs, Collection<Node> brokers, String clusterId, int controllerId, List<MetadataResponseTopic> topics, int clusterAuthorizedOperations) {
    MetadataResponseData responseData = new MetadataResponseData();
    responseData.setThrottleTimeMs(throttleTimeMs);
    brokers.forEach(broker -> responseData.brokers().add(new MetadataResponseBroker().setNodeId(broker.id()).setHost(broker.host()).setPort(broker.port()).setRack(broker.rack())));
    responseData.setClusterId(clusterId);
    responseData.setControllerId(controllerId);
    responseData.setClusterAuthorizedOperations(clusterAuthorizedOperations);
    topics.forEach(topicMetadata -> responseData.topics().add(topicMetadata));
    return new MetadataResponse(responseData, hasReliableEpoch);
}
Also used : MetadataResponseBroker(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBroker) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData)

Example 2 with MetadataResponseData

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

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

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

the class AllBrokersStrategyTest method testHandleResponseWithNoBrokers.

@Test
public void testHandleResponseWithNoBrokers() {
    AllBrokersStrategy strategy = new AllBrokersStrategy(logContext);
    MetadataResponseData response = new MetadataResponseData();
    AdminApiLookupStrategy.LookupResult<AllBrokersStrategy.BrokerKey> lookupResult = strategy.handleResponse(AllBrokersStrategy.LOOKUP_KEYS, new MetadataResponse(response, ApiKeys.METADATA.latestVersion()));
    assertEquals(Collections.emptyMap(), lookupResult.failedKeys);
    assertEquals(Collections.emptyMap(), lookupResult.mappedKeys);
}
Also used : MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 5 with MetadataResponseData

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

the class AllBrokersStrategyTest method testHandleResponseWithInvalidLookupKeys.

@Test
public void testHandleResponseWithInvalidLookupKeys() {
    AllBrokersStrategy strategy = new AllBrokersStrategy(logContext);
    AllBrokersStrategy.BrokerKey key1 = new AllBrokersStrategy.BrokerKey(OptionalInt.empty());
    AllBrokersStrategy.BrokerKey key2 = new AllBrokersStrategy.BrokerKey(OptionalInt.of(1));
    MetadataResponse response = new MetadataResponse(new MetadataResponseData(), ApiKeys.METADATA.latestVersion());
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(mkSet(key1), response));
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(mkSet(key2), response));
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(mkSet(key1, key2), response));
    Set<AllBrokersStrategy.BrokerKey> keys = new HashSet<>(AllBrokersStrategy.LOOKUP_KEYS);
    keys.add(key2);
    assertThrows(IllegalArgumentException.class, () -> strategy.handleResponse(keys, response));
}
Also used : MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

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