Search in sources :

Example 51 with TopicPartitionInfo

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

the class TopicAdminTest method describeTopicConfigShouldReturnTopicConfigWhenTopicExists.

@Test
public void describeTopicConfigShouldReturnTopicConfigWhenTopicExists() {
    String topicName = "myTopic";
    NewTopic newTopic = TopicAdmin.defineTopic(topicName).config(Collections.singletonMap("foo", "bar")).partitions(1).compacted().build();
    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), null);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        Map<String, Config> result = admin.describeTopicConfigs(newTopic.name());
        assertFalse(result.isEmpty());
        assertEquals(1, result.size());
        Config config = result.get("myTopic");
        assertNotNull(config);
        config.entries().forEach(entry -> assertEquals(newTopic.configs().get(entry.name()), entry.value()));
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TopicConfig(org.apache.kafka.common.config.TopicConfig) Config(org.apache.kafka.clients.admin.Config) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) Test(org.junit.Test)

Example 52 with TopicPartitionInfo

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

the class TopicAdminTest method shouldNotCreateTopicWhenItAlreadyExists.

@Test
public void shouldNotCreateTopicWhenItAlreadyExists() {
    NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
    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, "myTopic", Collections.singletonList(topicPartitionInfo), null);
        TopicAdmin admin = new TopicAdmin(null, mockAdminClient);
        assertFalse(admin.createTopic(newTopic));
        assertTrue(admin.createTopics(newTopic).isEmpty());
        assertTrue(admin.createOrFindTopic(newTopic));
        TopicAdmin.TopicCreationResponse response = admin.createOrFindTopics(newTopic);
        assertTrue(response.isCreatedOrExisting(newTopic.name()));
        assertTrue(response.isExisting(newTopic.name()));
        assertFalse(response.isCreated(newTopic.name()));
    }
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockAdminClient(org.apache.kafka.clients.admin.MockAdminClient) Test(org.junit.Test)

Example 53 with TopicPartitionInfo

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

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeleteAndCompactPolicy.

@Test
public void verifyingTopicCleanupPolicyShouldFailWhenTopicHasDeleteAndCompactPolicy() {
    String topicName = "myTopic";
    Map<String, String> topicConfigs = Collections.singletonMap("cleanup.policy", "delete,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);
        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 54 with TopicPartitionInfo

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

the class KafkaAdminClient method getTopicDescriptionFromCluster.

private TopicDescription getTopicDescriptionFromCluster(Cluster cluster, String topicName, Uuid topicId, Integer authorizedOperations) {
    boolean isInternal = cluster.internalTopics().contains(topicName);
    List<PartitionInfo> partitionInfos = cluster.partitionsForTopic(topicName);
    List<TopicPartitionInfo> partitions = new ArrayList<>(partitionInfos.size());
    for (PartitionInfo partitionInfo : partitionInfos) {
        TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(partitionInfo.partition(), leader(partitionInfo), Arrays.asList(partitionInfo.replicas()), Arrays.asList(partitionInfo.inSyncReplicas()));
        partitions.add(topicPartitionInfo);
    }
    partitions.sort(Comparator.comparingInt(TopicPartitionInfo::partition));
    return new TopicDescription(topicName, isInternal, partitions, validAclOperations(authorizedOperations), topicId);
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) ArrayList(java.util.ArrayList) PartitionInfo(org.apache.kafka.common.PartitionInfo) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo)

Example 55 with TopicPartitionInfo

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

the class WorkerUtilsTest method makeExistingTopicWithOneReplica.

private void makeExistingTopicWithOneReplica(String topicName, int numPartitions) {
    List<TopicPartitionInfo> tpInfo = new ArrayList<>();
    int brokerIndex = 0;
    for (int i = 0; i < numPartitions; ++i) {
        Node broker = cluster.get(brokerIndex);
        tpInfo.add(new TopicPartitionInfo(i, broker, singleReplica, Collections.<Node>emptyList()));
        brokerIndex = (brokerIndex + 1) % cluster.size();
    }
    adminClient.addTopic(false, topicName, tpInfo, null);
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList)

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