Search in sources :

Example 31 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project apache-kafka-on-k8s by banzaicloud.

the class MockAdminClient method createTopics.

@Override
public CreateTopicsResult createTopics(Collection<NewTopic> newTopics, CreateTopicsOptions options) {
    Map<String, KafkaFuture<Void>> createTopicResult = new HashMap<>();
    if (timeoutNextRequests > 0) {
        for (final NewTopic newTopic : newTopics) {
            String topicName = newTopic.name();
            KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
            future.completeExceptionally(new TimeoutException());
            createTopicResult.put(topicName, future);
        }
        --timeoutNextRequests;
        return new CreateTopicsResult(createTopicResult);
    }
    for (final NewTopic newTopic : newTopics) {
        KafkaFutureImpl<Void> future = new KafkaFutureImpl<>();
        String topicName = newTopic.name();
        if (allTopics.containsKey(topicName)) {
            future.completeExceptionally(new TopicExistsException(String.format("Topic %s exists already.", topicName)));
            createTopicResult.put(topicName, future);
            continue;
        }
        int replicationFactor = newTopic.replicationFactor();
        List<Node> replicas = new ArrayList<>(replicationFactor);
        for (int i = 0; i < replicationFactor; ++i) {
            replicas.add(brokers.get(i));
        }
        int numberOfPartitions = newTopic.numPartitions();
        List<TopicPartitionInfo> partitions = new ArrayList<>(numberOfPartitions);
        for (int p = 0; p < numberOfPartitions; ++p) {
            partitions.add(new TopicPartitionInfo(p, brokers.get(0), replicas, Collections.<Node>emptyList()));
        }
        allTopics.put(topicName, new TopicMetadata(false, partitions, newTopic.configs()));
        future.complete(null);
        createTopicResult.put(topicName, future);
    }
    return new CreateTopicsResult(createTopicResult);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) KafkaFutureImpl(org.apache.kafka.common.internals.KafkaFutureImpl) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 32 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project apache-kafka-on-k8s by banzaicloud.

the class MockAdminClient method addTopic.

public void addTopic(boolean internal, String name, List<TopicPartitionInfo> partitions, Map<String, String> configs) {
    if (allTopics.containsKey(name)) {
        throw new IllegalArgumentException(String.format("Topic %s was already added.", name));
    }
    List<Node> replicas = null;
    for (TopicPartitionInfo partition : partitions) {
        if (!brokers.contains(partition.leader())) {
            throw new IllegalArgumentException("Leader broker unknown");
        }
        if (!brokers.containsAll(partition.replicas())) {
            throw new IllegalArgumentException("Unknown brokers in replica list");
        }
        if (!brokers.containsAll(partition.isr())) {
            throw new IllegalArgumentException("Unknown brokers in isr list");
        }
        if (replicas == null) {
            replicas = partition.replicas();
        } else if (!replicas.equals(partition.replicas())) {
            throw new IllegalArgumentException("All partitions need to have the same replica nodes.");
        }
    }
    allTopics.put(name, new TopicMetadata(internal, partitions, configs));
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Node(org.apache.kafka.common.Node)

Example 33 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project apache-kafka-on-k8s by banzaicloud.

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.Test)

Example 34 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtilsTest method testCreateRetriesOnTimeout.

@Test
public void testCreateRetriesOnTimeout() throws Throwable {
    adminClient.timeoutNextRequest(1);
    WorkerUtils.createTopics(log, adminClient, Collections.singletonMap(TEST_TOPIC, NEW_TEST_TOPIC), true);
    assertEquals(new TopicDescription(TEST_TOPIC, false, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList()))), adminClient.describeTopics(Collections.singleton(TEST_TOPIC)).values().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.Test)

Example 35 with TopicPartitionInfo

use of org.apache.kafka.common.TopicPartitionInfo in project apache-kafka-on-k8s by banzaicloud.

the class WorkerUtilsTest method testCreateTopicsFailsIfAtLeastOneTopicExists.

@Test(expected = TopicExistsException.class)
public void testCreateTopicsFailsIfAtLeastOneTopicExists() throws Throwable {
    adminClient.addTopic(false, TEST_TOPIC, Collections.singletonList(new TopicPartitionInfo(0, broker1, singleReplica, Collections.<Node>emptyList())), null);
    Map<String, NewTopic> newTopics = new HashMap<>();
    newTopics.put(TEST_TOPIC, NEW_TEST_TOPIC);
    newTopics.put("another-topic", new NewTopic("another-topic", TEST_PARTITIONS, TEST_REPLICATION_FACTOR));
    newTopics.put("one-more-topic", new NewTopic("one-more-topic", TEST_PARTITIONS, TEST_REPLICATION_FACTOR));
    WorkerUtils.createTopics(log, adminClient, newTopics, true);
}
Also used : TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) HashMap(java.util.HashMap) NewTopic(org.apache.kafka.clients.admin.NewTopic) 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