Search in sources :

Example 26 with MetadataResponse

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

the class PartitionLeaderStrategy method handleResponse.

@Override
public LookupResult<TopicPartition> handleResponse(Set<TopicPartition> requestPartitions, AbstractResponse abstractResponse) {
    MetadataResponse response = (MetadataResponse) abstractResponse;
    Map<TopicPartition, Throwable> failed = new HashMap<>();
    Map<TopicPartition, Integer> mapped = new HashMap<>();
    for (MetadataResponseData.MetadataResponseTopic topicMetadata : response.data().topics()) {
        String topic = topicMetadata.name();
        Errors topicError = Errors.forCode(topicMetadata.errorCode());
        if (topicError != Errors.NONE) {
            handleTopicError(topic, topicError, requestPartitions, failed);
            continue;
        }
        for (MetadataResponseData.MetadataResponsePartition partitionMetadata : topicMetadata.partitions()) {
            TopicPartition topicPartition = new TopicPartition(topic, partitionMetadata.partitionIndex());
            Errors partitionError = Errors.forCode(partitionMetadata.errorCode());
            if (!requestPartitions.contains(topicPartition)) {
                // topics, so we have to filter any that we are not interested in.
                continue;
            }
            if (partitionError != Errors.NONE) {
                handlePartitionError(topicPartition, partitionError, failed);
                continue;
            }
            int leaderId = partitionMetadata.leaderId();
            if (leaderId >= 0) {
                mapped.put(topicPartition, leaderId);
            } else {
                log.debug("Metadata request for {} returned no error, but the leader is unknown. Will retry", topicPartition);
            }
        }
    }
    return new LookupResult<>(failed, mapped);
}
Also used : HashMap(java.util.HashMap) Errors(org.apache.kafka.common.protocol.Errors) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

Example 27 with MetadataResponse

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

the class AllBrokersStrategy method handleResponse.

@Override
public LookupResult<BrokerKey> handleResponse(Set<BrokerKey> keys, AbstractResponse abstractResponse) {
    validateLookupKeys(keys);
    MetadataResponse response = (MetadataResponse) abstractResponse;
    MetadataResponseData.MetadataResponseBrokerCollection brokers = response.data().brokers();
    if (brokers.isEmpty()) {
        log.debug("Metadata response contained no brokers. Will backoff and retry");
        return LookupResult.empty();
    } else {
        log.debug("Discovered all brokers {} to send requests to", brokers);
    }
    Map<BrokerKey, Integer> brokerKeys = brokers.stream().collect(Collectors.toMap(broker -> new BrokerKey(OptionalInt.of(broker.nodeId())), MetadataResponseData.MetadataResponseBroker::nodeId));
    return new LookupResult<>(Collections.singletonList(ANY_BROKER), Collections.emptyMap(), brokerKeys);
}
Also used : Logger(org.slf4j.Logger) AbstractResponse(org.apache.kafka.common.requests.AbstractResponse) Set(java.util.Set) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) MetadataRequestData(org.apache.kafka.common.message.MetadataRequestData) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Collections(java.util.Collections) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

Example 28 with MetadataResponse

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

the class ConsumerCoordinatorTest method testRefreshOffsetWithValidation.

@Test
public void testRefreshOffsetWithValidation() {
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    subscriptions.assignFromUser(singleton(t1p));
    // Initial leader epoch of 4
    MetadataResponse metadataResponse = RequestTestUtils.metadataUpdateWith("kafka-cluster", 1, Collections.emptyMap(), singletonMap(topic1, 1), tp -> 4);
    client.updateMetadata(metadataResponse);
    // Load offsets from previous epoch
    client.prepareResponse(offsetFetchResponse(t1p, Errors.NONE, "", 100L, Optional.of(3)));
    coordinator.refreshCommittedOffsetsIfNeeded(time.timer(Long.MAX_VALUE));
    // Offset gets loaded, but requires validation
    assertEquals(Collections.emptySet(), subscriptions.initializingPartitions());
    assertFalse(subscriptions.hasAllFetchPositions());
    assertTrue(subscriptions.awaitingValidation(t1p));
    assertEquals(subscriptions.position(t1p).offset, 100L);
    assertNull(subscriptions.validPosition(t1p));
}
Also used : MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 29 with MetadataResponse

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

the class KafkaConsumerTest method testSubscriptionOnInvalidTopic.

@Test
public void testSubscriptionOnInvalidTopic() {
    ConsumerMetadata metadata = createMetadata(subscription);
    MockClient client = new MockClient(time, metadata);
    initMetadata(client, Collections.singletonMap(topic, 1));
    Cluster cluster = metadata.fetch();
    // Invalid topic name due to space
    String invalidTopicName = "topic abc";
    List<MetadataResponse.TopicMetadata> topicMetadata = new ArrayList<>();
    topicMetadata.add(new MetadataResponse.TopicMetadata(Errors.INVALID_TOPIC_EXCEPTION, invalidTopicName, false, Collections.emptyList()));
    MetadataResponse updateResponse = RequestTestUtils.metadataResponse(cluster.nodes(), cluster.clusterResource().clusterId(), cluster.controller().id(), topicMetadata);
    client.prepareMetadataUpdate(updateResponse);
    KafkaConsumer<String, String> consumer = newConsumer(time, client, subscription, metadata, assignor, true, groupInstanceId);
    consumer.subscribe(singleton(invalidTopicName), getConsumerRebalanceListener(consumer));
    assertThrows(InvalidTopicException.class, () -> consumer.poll(Duration.ZERO));
}
Also used : ConsumerMetadata(org.apache.kafka.clients.consumer.internals.ConsumerMetadata) ArrayList(java.util.ArrayList) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Cluster(org.apache.kafka.common.Cluster) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 30 with MetadataResponse

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

the class KafkaConsumerTest method initMetadata.

private void initMetadata(MockClient mockClient, Map<String, Integer> partitionCounts) {
    Map<String, Uuid> metadataIds = new HashMap<>();
    for (String name : partitionCounts.keySet()) {
        metadataIds.put(name, topicIds.get(name));
    }
    MetadataResponse initialMetadata = RequestTestUtils.metadataUpdateWithIds(1, partitionCounts, metadataIds);
    mockClient.updateMetadata(initialMetadata);
}
Also used : Uuid(org.apache.kafka.common.Uuid) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

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