This is an automated email from the ASF dual-hosted git repository. lhotari pushed a commit to branch branch-3.0 in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/branch-3.0 by this push: new 94bcf77988c [fix] [broker] Fix wrong logic of method TopicName.getPartition(int index) (#19841) 94bcf77988c is described below commit 94bcf77988ce01f9ff05441e44cb6d04d8e4a158 Author: fengyubiao <yubiao.f...@streamnative.io> AuthorDate: Sun Apr 23 13:42:10 2023 +0800 [fix] [broker] Fix wrong logic of method TopicName.getPartition(int index) (#19841) (cherry picked from commit 6e585acd7d788c0979c113a92e341364c987cbe4) --- .../PartitionKeywordCompatibilityTest.java | 3 +- ...ameForInfiniteHttpCallGetSubscriptionsTest.java | 146 --------------------- .../org/apache/pulsar/common/naming/TopicName.java | 2 +- .../apache/pulsar/common/naming/TopicNameTest.java | 8 ++ 4 files changed, 11 insertions(+), 148 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PartitionKeywordCompatibilityTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PartitionKeywordCompatibilityTest.java index fdf2eb29c5e..86a5fcdc05a 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PartitionKeywordCompatibilityTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PartitionKeywordCompatibilityTest.java @@ -26,6 +26,7 @@ import org.apache.pulsar.client.admin.PulsarAdminException; import org.apache.pulsar.client.api.Consumer; import org.apache.pulsar.client.api.PulsarClientException; import org.apache.pulsar.client.api.SubscriptionType; +import org.apache.pulsar.common.naming.TopicName; import org.apache.pulsar.common.policies.data.AutoTopicCreationOverride; import org.testng.Assert; import org.testng.annotations.AfterClass; @@ -66,7 +67,7 @@ public class PartitionKeywordCompatibilityTest extends BrokerTestBase { .subscribe(); List<String> topics = admin.topics().getList("public/default"); List<String> partitionedTopicList = admin.topics().getPartitionedTopicList("public/default"); - Assert.assertTrue(topics.contains(topicName)); + Assert.assertTrue(topics.contains(TopicName.get(topicName).getPartition(0).toString())); Assert.assertTrue(partitionedTopicList.contains(topicName)); consumer.close(); admin.topics().deletePartitionedTopic(topicName); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/TopicNameForInfiniteHttpCallGetSubscriptionsTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/client/api/TopicNameForInfiniteHttpCallGetSubscriptionsTest.java deleted file mode 100644 index 96f57a59bda..00000000000 --- a/pulsar-broker/src/test/java/org/apache/pulsar/client/api/TopicNameForInfiniteHttpCallGetSubscriptionsTest.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.pulsar.client.api; - -import java.util.UUID; -import java.util.concurrent.TimeUnit; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.apache.pulsar.common.naming.TopicName; -import org.apache.pulsar.common.policies.data.TopicType; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -@Slf4j -@Test(groups = "broker") -public class TopicNameForInfiniteHttpCallGetSubscriptionsTest extends ProducerConsumerBase { - - @BeforeMethod(alwaysRun = true) - @Override - protected void setup() throws Exception { - super.internalSetup(); - super.producerBaseSetup(); - } - - @Override - protected void doInitConf() throws Exception { - super.doInitConf(); - conf.setAllowAutoTopicCreationType(TopicType.PARTITIONED); - conf.setDefaultNumPartitions(1); - } - - @AfterMethod(alwaysRun = true) - @Override - protected void cleanup() throws Exception { - super.internalCleanup(); - } - - @Test - public void testInfiniteHttpCallGetOrCreateSubscriptions() throws Exception { - final String randomStr = UUID.randomUUID().toString().replaceAll("-", ""); - final String partitionedTopicName = "persistent://my-property/my-ns/tp1_" + randomStr; - final String topic_p0 = partitionedTopicName + TopicName.PARTITIONED_TOPIC_SUFFIX + "0"; - final String subscriptionName = "sub1"; - final String topicDLQ = topic_p0 + "-" + subscriptionName + "-DLQ"; - - admin.topics().createPartitionedTopic(partitionedTopicName, 2); - - // Do test. - ProducerAndConsumerEntry pcEntry = triggerDLQCreated(topic_p0, topicDLQ, subscriptionName); - admin.topics().getSubscriptions(topicDLQ); - admin.topics().createSubscription(topicDLQ, "s1", MessageId.earliest); - - // cleanup. - pcEntry.consumer.close(); - pcEntry.producer.close(); - admin.topics().deletePartitionedTopic(partitionedTopicName); - } - - @Test - public void testInfiniteHttpCallGetOrCreateSubscriptions2() throws Exception { - final String randomStr = UUID.randomUUID().toString().replaceAll("-", ""); - final String topicName = "persistent://my-property/my-ns/tp1_" + randomStr + "-partition-0-abc"; - Producer<String> producer = pulsarClient.newProducer(Schema.STRING) - .topic(topicName) - .create(); - - // Do test. - admin.topics().getSubscriptions(topicName); - admin.topics().createSubscription(topicName, "s1", MessageId.earliest); - - // cleanup. - producer.close(); - } - - @Test - public void testInfiniteHttpCallGetOrCreateSubscriptions3() throws Exception { - final String randomStr = UUID.randomUUID().toString().replaceAll("-", ""); - final String topicName = "persistent://my-property/my-ns/tp1_" + randomStr + "-partition-0"; - Producer<String> producer = pulsarClient.newProducer(Schema.STRING) - .topic(topicName) - .create(); - - // Do test. - admin.topics().getSubscriptions(topicName); - admin.topics().createSubscription(topicName, "s1", MessageId.earliest); - - // cleanup. - producer.close(); - } - - @AllArgsConstructor - private static class ProducerAndConsumerEntry { - private Producer<String> producer; - private Consumer<String> consumer; - } - - private ProducerAndConsumerEntry triggerDLQCreated(String topicName, String DLQName, String subscriptionName) throws Exception { - Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING) - .topic(topicName) - .subscriptionName(subscriptionName) - .subscriptionType(SubscriptionType.Shared) - .enableRetry(true) - .deadLetterPolicy(DeadLetterPolicy.builder().deadLetterTopic(DLQName).maxRedeliverCount(2).build()) - .receiverQueueSize(100) - .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest) - .subscribe(); - Producer<String> producer = pulsarClient.newProducer(Schema.STRING) - .topic(topicName) - .create(); - // send messages. - for (int i = 0; i < 5; i++) { - producer.newMessage() - .value("value-" + i) - .sendAsync(); - } - producer.flush(); - // trigger the DLQ created. - for (int i = 0; i < 20; i++) { - Message<String> msg = consumer.receive(5, TimeUnit.SECONDS); - if (msg != null) { - consumer.reconsumeLater(msg, 1, TimeUnit.SECONDS); - } else { - break; - } - } - - return new ProducerAndConsumerEntry(producer, consumer); - } -} \ No newline at end of file diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java index 8b950250f55..20b71eb189b 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java @@ -239,7 +239,7 @@ public class TopicName implements ServiceUnitId { } public TopicName getPartition(int index) { - if (index == -1 || this.toString().contains(PARTITIONED_TOPIC_SUFFIX)) { + if (index == -1 || this.toString().endsWith(PARTITIONED_TOPIC_SUFFIX + index)) { return this; } String partitionName = this.toString() + PARTITIONED_TOPIC_SUFFIX + index; diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java index abd9d5b4278..485bea3f1ad 100644 --- a/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java +++ b/pulsar-common/src/test/java/org/apache/pulsar/common/naming/TopicNameTest.java @@ -329,4 +329,12 @@ public class TopicNameTest { // Ok } } + + @Test + public void testTwoKeyWordPartition(){ + TopicName tp1 = TopicName.get("tenant1/namespace1/tp1-partition-0-DLQ"); + TopicName tp2 = tp1.getPartition(0); + assertNotEquals(tp2.toString(), tp1.toString()); + assertEquals(tp2.toString(), "persistent://tenant1/namespace1/tp1-partition-0-DLQ-partition-0"); + } }