congbobo184 commented on code in PR #19737:
URL: https://github.com/apache/pulsar/pull/19737#discussion_r1136504678


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java:
##########
@@ -428,6 +430,26 @@ public AtomicLong getPendingWriteOps() {
         return pendingWriteOps;
     }
 
+    public CompletableFuture<Void> unloadSubscription(String subName) {
+        synchronized (ledger) {
+            final PersistentSubscription sub = subscriptions.get(subName);
+            if (sub == null) {

Review Comment:
   if sub == null, may be we should return exception



##########
pulsar-broker/src/test/java/org/apache/pulsar/client/api/UnloadSubscriptionTest.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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 static org.apache.pulsar.client.api.SubscriptionType.Shared;
+import static org.apache.pulsar.client.api.SubscriptionType.Key_Shared;
+import static org.apache.pulsar.client.api.SubscriptionType.Failover;
+import static org.apache.pulsar.client.api.SubscriptionType.Exclusive;
+import static org.testng.Assert.assertEquals;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+import org.apache.pulsar.client.impl.BatchMessageIdImpl;
+import org.apache.pulsar.client.impl.MessageIdImpl;
+import org.apache.pulsar.client.impl.TopicMessageIdImpl;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+@Slf4j
+@Test(groups = "broker-api")
+public class UnloadSubscriptionTest extends ProducerConsumerBase {
+
+    @BeforeClass(alwaysRun = true)
+    @Override
+    protected void setup() throws Exception {
+        super.internalSetup();
+        super.producerBaseSetup();
+    }
+
+    @Override
+    protected void doInitConf() throws Exception {
+        super.doInitConf();
+        conf.setSystemTopicEnabled(false);
+        conf.setTransactionCoordinatorEnabled(false);
+    }
+
+    @AfterClass(alwaysRun = true)
+    @Override
+    protected void cleanup() throws Exception {
+        super.internalCleanup();
+    }
+
+    @DataProvider(name = "unloadCases")
+    public Object[][] unloadCases (){
+        // [msgCount, enabledBatch, maxMsgPerBatch, subType, ackMsgCount]
+        return new Object[][]{
+                {100, false, 1, Exclusive, 0},
+                {100, false, 1, Failover, 0},
+                {100, false, 1, Shared, 0},
+                {100, false, 1, Key_Shared, 0},
+                {100, true, 5, Exclusive, 0},
+                {100, true, 5, Failover, 0},
+                {100, true, 5, Shared, 0},
+                {100, true, 5, Key_Shared, 0},
+                {100, false, 1, Exclusive, 50},
+                {100, false, 1, Failover, 50},
+                {100, false, 1, Shared, 50},
+                {100, false, 1, Key_Shared, 50},
+                {100, true, 5, Exclusive, 50},
+                {100, true, 5, Failover, 50},
+                {100, true, 5, Shared, 50},
+                {100, true, 5, Key_Shared, 50},
+        };
+    }
+
+    @Test(dataProvider = "unloadCases", invocationCount = 50)
+    public void testSingleConsumer(int msgCount, boolean enabledBatch, int 
maxMsgPerBatch, SubscriptionType subType,
+                                   int ackMsgCount) throws Exception {
+        final String topicName = "persistent://my-property/my-ns/tp-" + 
UUID.randomUUID().toString();
+        final String subName = "sub";
+        ListenerConsumer listenerConsumer = createListener(topicName, subName, 
subType);
+        ProducerAndMessageIds producerAndMessageIds =
+                createProducerAndSendMessages(topicName, msgCount, 
enabledBatch, maxMsgPerBatch);
+        log.info("send message-ids:{}-{}", 
producerAndMessageIds.messageIds.size(),
+                toString(producerAndMessageIds.messageIds));
+        Awaitility.await().untilAsserted(() -> {
+            Set<String> allMessages = listenerConsumer.listener.messageSet;
+            assertEquals(allMessages.size(), msgCount);
+        });
+
+        if (ackMsgCount > 0){
+            List<MessageId> messageIdsToAck = 
producerAndMessageIds.messageIds.subList(0, ackMsgCount);
+            log.info("ack message-ids: {}", toString(messageIdsToAck));
+            listenerConsumer.consumer.acknowledge(messageIdsToAck);
+        }
+
+        listenerConsumer.listener.messageSet.clear();
+        PersistentTopic persistentTopic = getPersistentTopic(topicName);
+        persistentTopic.unloadSubscription(subName);
+        Awaitility.await().untilAsserted(() -> {
+            Set<String> allMessages = listenerConsumer.listener.messageSet;
+            assertEquals(allMessages.size(), msgCount - ackMsgCount);

Review Comment:
   this assert, when consumer util to msgCount - ackMsgCount the return 
directly, so we haven check the consumer can't receive the message anymore. 
same as `testMultiConsumer`



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java:
##########
@@ -428,6 +430,26 @@ public AtomicLong getPendingWriteOps() {
         return pendingWriteOps;
     }
 
+    public CompletableFuture<Void> unloadSubscription(String subName) {
+        synchronized (ledger) {
+            final PersistentSubscription sub = subscriptions.get(subName);
+            if (sub == null) {
+                return CompletableFuture.completedFuture(null);
+            }
+            if (Compactor.COMPACTION_SUBSCRIPTION.equals(sub.getName())){
+                return CompletableFuture.failedFuture(new 
RestException(Response.Status.BAD_REQUEST,
+                        "Could not reload the compaction subscription"));
+            }
+            // Fence old subscription -> Rewind cursor -> Replace with a new 
subscription.
+            return sub.disconnect().thenAccept(ignore -> {

Review Comment:
   the disconnect is an async method, so why do we need to lock this method by 
`synchronized (ledger)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to