Search in sources :

Example 6 with MetadataResponseTopic

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

the class KafkaAdminClientTest method prepareMetadataResponse.

private static MetadataResponse prepareMetadataResponse(Cluster cluster, Errors topicError, Errors partitionError) {
    List<MetadataResponseTopic> metadata = new ArrayList<>();
    for (String topic : cluster.topics()) {
        List<MetadataResponsePartition> pms = new ArrayList<>();
        for (PartitionInfo pInfo : cluster.availablePartitionsForTopic(topic)) {
            MetadataResponsePartition pm = new MetadataResponsePartition().setErrorCode(partitionError.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(topicError.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) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) PartitionInfo(org.apache.kafka.common.PartitionInfo)

Example 7 with MetadataResponseTopic

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

the class MetadataTest method testIgnoreLeaderEpochInOlderMetadataResponse.

/**
 * Prior to Kafka version 2.4 (which coincides with Metadata version 9), the broker does not propagate leader epoch
 * information accurately while a reassignment is in progress, so we cannot rely on it. This is explained in more
 * detail in MetadataResponse's constructor.
 */
@Test
public void testIgnoreLeaderEpochInOlderMetadataResponse() {
    TopicPartition tp = new TopicPartition("topic", 0);
    MetadataResponsePartition partitionMetadata = new MetadataResponsePartition().setPartitionIndex(tp.partition()).setLeaderId(5).setLeaderEpoch(10).setReplicaNodes(Arrays.asList(1, 2, 3)).setIsrNodes(Arrays.asList(1, 2, 3)).setOfflineReplicas(Collections.emptyList()).setErrorCode(Errors.NONE.code());
    MetadataResponseTopic topicMetadata = new MetadataResponseTopic().setName(tp.topic()).setErrorCode(Errors.NONE.code()).setPartitions(Collections.singletonList(partitionMetadata)).setIsInternal(false);
    MetadataResponseTopicCollection topics = new MetadataResponseTopicCollection();
    topics.add(topicMetadata);
    MetadataResponseData data = new MetadataResponseData().setClusterId("clusterId").setControllerId(0).setTopics(topics).setBrokers(new MetadataResponseBrokerCollection());
    for (short version = ApiKeys.METADATA.oldestVersion(); version < 9; version++) {
        ByteBuffer buffer = MessageUtil.toByteBuffer(data, version);
        MetadataResponse response = MetadataResponse.parse(buffer, version);
        assertFalse(response.hasReliableLeaderEpochs());
        metadata.updateWithCurrentRequestVersion(response, false, 100);
        assertTrue(metadata.partitionMetadataIfCurrent(tp).isPresent());
        MetadataResponse.PartitionMetadata responseMetadata = this.metadata.partitionMetadataIfCurrent(tp).get();
        assertEquals(Optional.empty(), responseMetadata.leaderEpoch);
    }
    for (short version = 9; version <= ApiKeys.METADATA.latestVersion(); version++) {
        ByteBuffer buffer = MessageUtil.toByteBuffer(data, version);
        MetadataResponse response = MetadataResponse.parse(buffer, version);
        assertTrue(response.hasReliableLeaderEpochs());
        metadata.updateWithCurrentRequestVersion(response, false, 100);
        assertTrue(metadata.partitionMetadataIfCurrent(tp).isPresent());
        MetadataResponse.PartitionMetadata responseMetadata = metadata.partitionMetadataIfCurrent(tp).get();
        assertEquals(Optional.of(10), responseMetadata.leaderEpoch);
    }
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) MetadataResponseBrokerCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection) MetadataResponseTopicCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with MetadataResponseTopic

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

the class MetadataTest method testStaleMetadata.

@Test
public void testStaleMetadata() {
    TopicPartition tp = new TopicPartition("topic", 0);
    MetadataResponsePartition partitionMetadata = new MetadataResponsePartition().setPartitionIndex(tp.partition()).setLeaderId(1).setLeaderEpoch(10).setReplicaNodes(Arrays.asList(1, 2, 3)).setIsrNodes(Arrays.asList(1, 2, 3)).setOfflineReplicas(Collections.emptyList()).setErrorCode(Errors.NONE.code());
    MetadataResponseTopic topicMetadata = new MetadataResponseTopic().setName(tp.topic()).setErrorCode(Errors.NONE.code()).setPartitions(Collections.singletonList(partitionMetadata)).setIsInternal(false);
    MetadataResponseTopicCollection topics = new MetadataResponseTopicCollection();
    topics.add(topicMetadata);
    MetadataResponseData data = new MetadataResponseData().setClusterId("clusterId").setControllerId(0).setTopics(topics).setBrokers(new MetadataResponseBrokerCollection());
    metadata.updateWithCurrentRequestVersion(new MetadataResponse(data, ApiKeys.METADATA.latestVersion()), false, 100);
    // Older epoch with changed ISR should be ignored
    partitionMetadata.setPartitionIndex(tp.partition()).setLeaderId(1).setLeaderEpoch(9).setReplicaNodes(Arrays.asList(1, 2, 3)).setIsrNodes(Arrays.asList(1, 2)).setOfflineReplicas(Collections.emptyList()).setErrorCode(Errors.NONE.code());
    metadata.updateWithCurrentRequestVersion(new MetadataResponse(data, ApiKeys.METADATA.latestVersion()), false, 101);
    assertEquals(Optional.of(10), metadata.lastSeenLeaderEpoch(tp));
    assertTrue(metadata.partitionMetadataIfCurrent(tp).isPresent());
    MetadataResponse.PartitionMetadata responseMetadata = this.metadata.partitionMetadataIfCurrent(tp).get();
    assertEquals(Arrays.asList(1, 2, 3), responseMetadata.inSyncReplicaIds);
    assertEquals(Optional.of(10), responseMetadata.leaderEpoch);
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) MetadataResponseBrokerCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseBrokerCollection) MetadataResponseTopicCollection(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopicCollection) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 9 with MetadataResponseTopic

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

the class PartitionLeaderStrategyTest method responseWithTopicError.

private MetadataResponse responseWithTopicError(String topic, Errors error) {
    MetadataResponseTopic responseTopic = new MetadataResponseTopic().setName(topic).setErrorCode(error.code());
    MetadataResponseData responseData = new MetadataResponseData();
    responseData.topics().add(responseTopic);
    return new MetadataResponse(responseData, ApiKeys.METADATA.latestVersion());
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse)

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