Search in sources :

Example 1 with MetadataResponsePartition

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

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

the class PartitionLeaderStrategyTest method testRetryIfLeaderUnknown.

@Test
public void testRetryIfLeaderUnknown() {
    TopicPartition topicPartition = new TopicPartition("foo", 0);
    Map<TopicPartition, MetadataResponsePartition> responsePartitions = singletonMap(topicPartition, partitionResponseDataWithLeader(topicPartition, -1, Arrays.asList(5, 6, 7)));
    LookupResult<TopicPartition> result = handleLookupResponse(mkSet(topicPartition), responseWithPartitionData(responsePartitions));
    assertEquals(emptyMap(), result.failedKeys);
    assertEquals(emptyMap(), result.mappedKeys);
}
Also used : MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 3 with MetadataResponsePartition

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

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

the class PartitionLeaderStrategyTest method testPartitionSuccessfullyMapped.

@Test
public void testPartitionSuccessfullyMapped() {
    TopicPartition topicPartition1 = new TopicPartition("foo", 0);
    TopicPartition topicPartition2 = new TopicPartition("bar", 1);
    Map<TopicPartition, MetadataResponsePartition> responsePartitions = new HashMap<>(2);
    responsePartitions.put(topicPartition1, partitionResponseDataWithLeader(topicPartition1, 5, Arrays.asList(5, 6, 7)));
    responsePartitions.put(topicPartition2, partitionResponseDataWithLeader(topicPartition2, 1, Arrays.asList(2, 1, 3)));
    LookupResult<TopicPartition> result = handleLookupResponse(mkSet(topicPartition1, topicPartition2), responseWithPartitionData(responsePartitions));
    assertEquals(emptyMap(), result.failedKeys);
    assertEquals(mkSet(topicPartition1, topicPartition2), result.mappedKeys.keySet());
    assertEquals(5, result.mappedKeys.get(topicPartition1));
    assertEquals(1, result.mappedKeys.get(topicPartition2));
}
Also used : MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 5 with MetadataResponsePartition

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

the class PartitionLeaderStrategyTest method testIgnoreUnrequestedPartitions.

@Test
public void testIgnoreUnrequestedPartitions() {
    TopicPartition requestedTopicPartition = new TopicPartition("foo", 0);
    TopicPartition unrequestedTopicPartition = new TopicPartition("foo", 1);
    Map<TopicPartition, MetadataResponsePartition> responsePartitions = new HashMap<>(2);
    responsePartitions.put(requestedTopicPartition, partitionResponseDataWithLeader(requestedTopicPartition, 5, Arrays.asList(5, 6, 7)));
    responsePartitions.put(unrequestedTopicPartition, partitionResponseDataWithError(unrequestedTopicPartition, Errors.UNKNOWN_SERVER_ERROR));
    LookupResult<TopicPartition> result = handleLookupResponse(mkSet(requestedTopicPartition), responseWithPartitionData(responsePartitions));
    assertEquals(emptyMap(), result.failedKeys);
    assertEquals(mkSet(requestedTopicPartition), result.mappedKeys.keySet());
    assertEquals(5, result.mappedKeys.get(requestedTopicPartition));
}
Also used : MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Aggregations

MetadataResponsePartition (org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition)10 TopicPartition (org.apache.kafka.common.TopicPartition)8 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)6 MetadataResponseTopic (org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic)6 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)6 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)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 ByteBuffer (java.nio.ByteBuffer)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)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 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 Uuid (org.apache.kafka.common.Uuid)1