Search in sources :

Example 16 with TopicPartitionInfo

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

the class WorkerUtils method getMatchingTopicPartitions.

/**
 * Returns list of existing, not internal, topics/partitions that match given pattern and
 * where partitions are in range [startPartition, endPartition]
 * @param adminClient     AdminClient
 * @param topicRegex      Topic regular expression to match
 * @return                List of topic names
 * @throws Throwable      If failed to get list of existing topics
 */
static Collection<TopicPartition> getMatchingTopicPartitions(Admin adminClient, String topicRegex, int startPartition, int endPartition) throws Throwable {
    final Pattern topicNamePattern = Pattern.compile(topicRegex);
    // first get list of matching topics
    List<String> matchedTopics = new ArrayList<>();
    ListTopicsResult res = adminClient.listTopics(new ListTopicsOptions().timeoutMs(ADMIN_REQUEST_TIMEOUT));
    Map<String, TopicListing> topicListingMap = res.namesToListings().get();
    for (Map.Entry<String, TopicListing> topicListingEntry : topicListingMap.entrySet()) {
        if (!topicListingEntry.getValue().isInternal() && topicNamePattern.matcher(topicListingEntry.getKey()).matches()) {
            matchedTopics.add(topicListingEntry.getKey());
        }
    }
    // create a list of topic/partitions
    List<TopicPartition> out = new ArrayList<>();
    DescribeTopicsResult topicsResult = adminClient.describeTopics(matchedTopics, new DescribeTopicsOptions().timeoutMs(ADMIN_REQUEST_TIMEOUT));
    Map<String, TopicDescription> topicDescriptionMap = topicsResult.allTopicNames().get();
    for (TopicDescription desc : topicDescriptionMap.values()) {
        List<TopicPartitionInfo> partitions = desc.partitions();
        for (TopicPartitionInfo info : partitions) {
            if ((info.partition() >= startPartition) && (info.partition() <= endPartition)) {
                out.add(new TopicPartition(desc.name(), info.partition()));
            }
        }
    }
    return out;
}
Also used : Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicPartition(org.apache.kafka.common.TopicPartition) ListTopicsResult(org.apache.kafka.clients.admin.ListTopicsResult) TopicListing(org.apache.kafka.clients.admin.TopicListing) DescribeTopicsResult(org.apache.kafka.clients.admin.DescribeTopicsResult) DescribeTopicsOptions(org.apache.kafka.clients.admin.DescribeTopicsOptions) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ListTopicsOptions(org.apache.kafka.clients.admin.ListTopicsOptions) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with TopicPartitionInfo

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

the class TopicAdminTest method assertTopic.

protected void assertTopic(MockAdminClient admin, String topicName, int expectedPartitions, int expectedReplicas) {
    TopicDescription desc = null;
    try {
        desc = topicDescription(admin, topicName);
    } catch (Throwable t) {
        fail("Failed to find topic description for topic '" + topicName + "'");
    }
    assertEquals(expectedPartitions, desc.partitions().size());
    for (TopicPartitionInfo tp : desc.partitions()) {
        assertEquals(expectedReplicas, tp.replicas().size());
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicDescription(org.apache.kafka.clients.admin.TopicDescription)

Example 18 with TopicPartitionInfo

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

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeletePolicy.

@Test
public void verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeletePolicy() {
    String topicName = "myTopic";
    Map<String, String> topicConfigs = Collections.singletonMap("cleanup.policy", "delete");
    Cluster cluster = createCluster(1);
    try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
        TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
        mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), topicConfigs);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        ConfigException e = assertThrows(ConfigException.class, () -> admin.verifyTopicCleanupPolicyOnlyCompact("myTopic", "worker.topic", "purpose"));
        assertTrue(e.getMessage().contains("to guarantee consistency and durability"));
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Cluster(org.apache.kafka.common.Cluster) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) ConfigException(org.apache.kafka.common.config.ConfigException) Test(org.junit.Test)

Example 19 with TopicPartitionInfo

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

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldReturnTrueWhenTopicHasCorrectPolicy.

@Test
public void verifyingTopicCleanupPolicyShouldReturnTrueWhenTopicHasCorrectPolicy() {
    String topicName = "myTopic";
    Map<String, String> topicConfigs = Collections.singletonMap("cleanup.policy", "compact");
    Cluster cluster = createCluster(1);
    try (MockAdminClient mockAdminClient = new MockAdminClient(cluster.nodes(), cluster.nodeById(0))) {
        TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
        mockAdminClient.addTopic(false, topicName, Collections.singletonList(topicPartitionInfo), topicConfigs);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        boolean result = admin.verifyTopicCleanupPolicyOnlyCompact("myTopic", "worker.topic", "purpose");
        assertTrue(result);
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Cluster(org.apache.kafka.common.Cluster) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) Test(org.junit.Test)

Example 20 with TopicPartitionInfo

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

the class InternalTopicManagerTest method shouldNotThrowExceptionIfExistsWithDifferentReplication.

@Test
public void shouldNotThrowExceptionIfExistsWithDifferentReplication() {
    mockAdminClient.addTopic(false, topic1, Collections.singletonList(new TopicPartitionInfo(0, broker1, cluster, Collections.emptyList())), null);
    // attempt to create it again with replication 1
    final InternalTopicManager internalTopicManager2 = new InternalTopicManager(time, mockAdminClient, new StreamsConfig(config));
    final InternalTopicConfig internalTopicConfig = new RepartitionTopicConfig(topic1, Collections.emptyMap());
    internalTopicConfig.setNumberOfPartitions(1);
    internalTopicManager2.makeReady(Collections.singletonMap(topic1, internalTopicConfig));
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

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