Search in sources :

Example 56 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.

the class WorkerUtilsTest method testCreatesNotExistingTopics.

@Test
public void testCreatesNotExistingTopics() throws Throwable {
    // should be no topics before the call
    assertEquals(0, adminClient.listTopics().names().get().size());
    WorkerUtils.createTopics(log, adminClient, Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC), false);
    assertEquals(Collections.singleton(TEST_TOPIC), adminClient.listTopics().names().get());
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).topicNameValues().get(TEST_TOPIC).get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.jupiter.api.Test)

Example 57 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.

the class WorkerUtilsTest method testCreateOneTopic.

@Test
public void testCreateOneTopic() throws Throwable {
    Map<String, NewTopic> newTopics = Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC);
    WorkerUtils.createTopics(log, adminClient, newTopics, true);
    assertEquals(Collections.singleton(TEST_TOPIC), adminClient.listTopics().names().get());
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).topicNameValues().get(TEST_TOPIC).get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) NewTopic(org.apache.kafka.clients.admin.NewTopic) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) Test(org.junit.jupiter.api.Test)

Example 58 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.

the class WorkerUtilsTest method testCreatesOneTopicVerifiesOneTopic.

@Test
public void testCreatesOneTopicVerifiesOneTopic() throws Throwable {
    final String existingTopic = "existing-topic";
    List<TopicPartitionInfo> tpInfo = new ArrayList<>();
    tpInfo.add(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()));
    tpInfo.add(new TopicPartitionInfo(1, broker2, singleReplica, Collections.<Node>emptyList()));
    adminClient.addTopic(false, existingTopic, tpInfo, null);
    Map<String, NewTopic> topics = new HashMap<>();
    topics.put(existingTopic, new NewTopic(existingTopic, tpInfo.size(), TEST_REPLICATION_FACTOR));
    topics.put(TEST_TOPIC, NEW_TEST_TOPIC);
    WorkerUtils.createTopics(log, adminClient, topics, false);
    assertEquals(Utils.mkSet(existingTopic, TEST_TOPIC), adminClient.listTopics().names().get());
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.jupiter.api.Test)

Example 59 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project kafka by apache.

the class TransactionsCommandTest method testFindHangingLookupTopicPartitionsForTopic.

@Test
public void testFindHangingLookupTopicPartitionsForTopic() throws Exception {
    String topic = "foo";
    String[] args = new String[] { "--bootstrap-server", "localhost:9092", "find-hanging", "--topic", topic };
    Node node0 = new Node(0, "localhost", 9092);
    Node node1 = new Node(1, "localhost", 9093);
    Node node5 = new Node(5, "localhost", 9097);
    TopicPartitionInfo partition0 = new TopicPartitionInfo(0, node0, Arrays.asList(node0, node1), Arrays.asList(node0, node1));
    TopicPartitionInfo partition1 = new TopicPartitionInfo(1, node1, Arrays.asList(node1, node5), Arrays.asList(node1, node5));
    TopicDescription description = new TopicDescription(topic, false, Arrays.asList(partition0, partition1));
    expectDescribeTopics(singletonMap(topic, description));
    DescribeProducersResult result = Mockito.mock(DescribeProducersResult.class);
    Mockito.when(result.all()).thenReturn(completedFuture(emptyMap()));
    Mockito.when(admin.describeProducers(Arrays.asList(new TopicPartition(topic, 0), new TopicPartition(topic, 1)), new DescribeProducersOptions())).thenReturn(result);
    execute(args);
    assertNormalExit();
    assertNoHangingTransactions();
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicPartition(org.apache.kafka.common.TopicPartition) Node(org.apache.kafka.common.Node) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) DescribeProducersResult(org.apache.kafka.clients.admin.DescribeProducersResult) DescribeProducersOptions(org.apache.kafka.clients.admin.DescribeProducersOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 60 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project flink by apache.

the class TopicPatternSubscriber method getSubscribedTopicPartitions.

@Override
public Set<TopicPartition> getSubscribedTopicPartitions(AdminClient adminClient) {
    LOG.debug("Fetching descriptions for all topics on Kafka cluster");
    final Map<String, TopicDescription> allTopicMetadata = getAllTopicMetadata(adminClient);
    Set<TopicPartition> subscribedTopicPartitions = new HashSet<>();
    allTopicMetadata.forEach((topicName, topicDescription) -> {
        if (topicPattern.matcher(topicName).matches()) {
            for (TopicPartitionInfo partition : topicDescription.partitions()) {
                subscribedTopicPartitions.add(new TopicPartition(topicDescription.name(), partition.partition()));
            }
        }
    });
    return subscribedTopicPartitions;
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicPartition(org.apache.kafka.common.TopicPartition) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) HashSet(java.util.HashSet)

Aggregations

TopicPartitionInfo (org.apache.kafka.common.TopicPartitionInfo)62 Test (org.junit.Test)33 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)31 Node (org.apache.kafka.common.Node)28 ArrayList (java.util.ArrayList)20 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)18 NewTopic (org.apache.kafka.clients.admin.NewTopic)16 HashMap (java.util.HashMap)14 Cluster (org.apache.kafka.common.Cluster)11 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)11 StreamsConfig (org.apache.kafka.streams.StreamsConfig)11 Test (org.junit.jupiter.api.Test)10 TopicPartition (org.apache.kafka.common.TopicPartition)8 ConfigResource (org.apache.kafka.common.config.ConfigResource)8 Map (java.util.Map)7 AdminClient (org.apache.kafka.clients.admin.AdminClient)7 Config (org.apache.kafka.clients.admin.Config)7 TopicMetadataAndConfig (org.apache.kafka.clients.admin.CreateTopicsResult.TopicMetadataAndConfig)7 TopicConfig (org.apache.kafka.common.config.TopicConfig)7 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)6