Search in sources :

Example 36 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse 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 37 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse 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 38 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse in project kafka by apache.

the class PartitionLeaderStrategyTest method assertRetriablePartitionError.

private void assertRetriablePartitionError(TopicPartition topicPartition, Errors error) {
    MetadataResponse response = responseWithPartitionData(singletonMap(topicPartition, partitionResponseDataWithError(topicPartition, error)));
    assertRetriableError(topicPartition, response);
}
Also used : MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

Example 39 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse 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 40 with MetadataResponse

use of org.apache.kafka.common.requests.MetadataResponse 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

MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)107 Test (org.junit.jupiter.api.Test)71 HashMap (java.util.HashMap)68 TopicPartition (org.apache.kafka.common.TopicPartition)43 MockTime (org.apache.kafka.common.utils.MockTime)38 Time (org.apache.kafka.common.utils.Time)37 Node (org.apache.kafka.common.Node)33 ArrayList (java.util.ArrayList)30 MockClient (org.apache.kafka.clients.MockClient)29 Cluster (org.apache.kafka.common.Cluster)29 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)28 Errors (org.apache.kafka.common.protocol.Errors)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)27 Map (java.util.Map)26 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)26 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)26 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)25 List (java.util.List)24 LogContext (org.apache.kafka.common.utils.LogContext)22 HashSet (java.util.HashSet)21