Java Code Examples for org.apache.pulsar.common.naming.TopicName#PARTITIONED_TOPIC_SUFFIX

The following examples show how to use org.apache.pulsar.common.naming.TopicName#PARTITIONED_TOPIC_SUFFIX . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PersistentTopicsBase.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Validate update of number of partition for partitioned topic.
 * If there's already non partition topic with same name and contains partition suffix "-partition-"
 * followed by numeric value X then the new number of partition of that partitioned topic can not be greater
 * than that X else that non partition topic will essentially be overwritten and cause unexpected consequence.
 *
 * @param topicName
 */
private void validatePartitionTopicUpdate(String topicName, int numberOfPartition) {
    List<String> existingTopicList = internalGetList();
    TopicName partitionTopicName = TopicName.get(domain(), namespaceName, topicName);
    PartitionedTopicMetadata metadata = getPartitionedTopicMetadata(partitionTopicName, false, false);
    int oldPartition = metadata.partitions;
    String prefix = topicName + TopicName.PARTITIONED_TOPIC_SUFFIX;
    for (String exsitingTopicName : existingTopicList) {
        if (exsitingTopicName.contains(prefix)) {
            try {
                long suffix = Long.parseLong(exsitingTopicName.substring(
                        exsitingTopicName.indexOf(TopicName.PARTITIONED_TOPIC_SUFFIX)
                                + TopicName.PARTITIONED_TOPIC_SUFFIX.length()));
                // Skip partition of partitioned topic by making sure the numeric suffix greater than old partition number.
                if (suffix >= oldPartition && suffix <= (long) numberOfPartition) {
                    log.warn("[{}] Already have non partition topic {} which contains partition " +
                            "suffix '-partition-' and end with numeric value smaller than the new number of partition. " +
                            "Update of partitioned topic {} could cause conflict.", clientAppId(), exsitingTopicName, topicName);
                    throw new RestException(Status.PRECONDITION_FAILED,
                            "Already have non partition topic" + exsitingTopicName + " which contains partition suffix '-partition-' " +
                                    "and end with numeric value and end with numeric value smaller than the new " +
                                    "number of partition. Update of partitioned topic " + topicName + " could cause conflict.");
                }
            } catch (NumberFormatException e) {
                // Do nothing, if value after partition suffix is not pure numeric value,
                // as it can't conflict with internal created partitioned topic's name.
            }
        }
    }
}
 
Example 2
Source File: ReplicatorTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
/**
 * This validates that broker supports update-partition api for global topics.
 * <pre>
 *  1. Create global topic with 4 partitions
 *  2. Update partition with 8 partitions
 *  3. Create producer on the partition topic which loads all new partitions
 *  4. Check subscriptions are created on all new partitions.
 * </pre>
 * @throws Exception
 */
@Test
public void testUpdateGlobalTopicPartition() throws Exception {
    log.info("--- Starting ReplicatorTest::testUpdateGlobalTopicPartition ---");

    final String cluster1 = pulsar1.getConfig().getClusterName();
    final String cluster2 = pulsar2.getConfig().getClusterName();
    final String namespace = "pulsar/ns-" + System.nanoTime();
    final String topicName = "persistent://" + namespace + "/topic1";
    int startPartitions = 4;
    int newPartitions = 8;
    final String subscriberName = "sub1";
    admin1.namespaces().createNamespace(namespace, Sets.newHashSet(cluster1, cluster2));
    admin1.topics().createPartitionedTopic(topicName, startPartitions);

    PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS)
            .build();
    PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()).statsInterval(0, TimeUnit.SECONDS)
            .build();

    Consumer<byte[]> consumer1 = client1.newConsumer().topic(topicName).subscriptionName(subscriberName)
            .subscribe();
    Consumer<byte[]> consumer2 = client2.newConsumer().topic(topicName).subscriptionName(subscriberName)
            .subscribe();

    admin1.topics().updatePartitionedTopic(topicName, newPartitions);

    assertEquals(admin1.topics().getPartitionedTopicMetadata(topicName).partitions, newPartitions);

    // create producers to load all the partition topics
    Producer<byte[]> producer1 = client1.newProducer().topic(topicName).create();
    Producer<byte[]> producer2 = client2.newProducer().topic(topicName).create();

    for (int i = startPartitions; i < newPartitions; i++) {
        String partitionedTopic = topicName + TopicName.PARTITIONED_TOPIC_SUFFIX + i;
        assertEquals(admin1.topics().getSubscriptions(partitionedTopic).size(), 1);
        assertEquals(admin2.topics().getSubscriptions(partitionedTopic).size(), 1);
    }

    producer1.close();
    producer2.close();
    consumer1.close();
    consumer2.close();

    client1.close();
    client2.close();
}