Skip to content

Commit 94edfe4

Browse files
committed
[fix] [broker] Close dispatchers stuck due to mismatch between dispatcher.consumerList and dispatcher.consumerSet (#22270)
(cherry picked from commit cba1600)
1 parent 5ab0e93 commit 94edfe4

File tree

2 files changed

+127
-10
lines changed

2 files changed

+127
-10
lines changed

pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentDispatcherMultipleConsumers.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,7 @@ public synchronized void removeConsumer(Consumer consumer) throws BrokerServiceE
204204
consumerList.remove(consumer);
205205
log.info("Removed consumer {} with pending {} acks", consumer, consumer.getPendingAcks().size());
206206
if (consumerList.isEmpty()) {
207-
cancelPendingRead();
208-
209-
redeliveryMessages.clear();
210-
redeliveryTracker.clear();
211-
if (closeFuture != null) {
212-
log.info("[{}] All consumers removed. Subscription is disconnected", name);
213-
closeFuture.complete(null);
214-
}
215-
totalAvailablePermits = 0;
207+
clearComponentsAfterRemovedAllConsumers();
216208
} else {
217209
if (log.isDebugEnabled()) {
218210
log.debug("[{}] Consumer are left, reading more entries", name);
@@ -229,8 +221,29 @@ public synchronized void removeConsumer(Consumer consumer) throws BrokerServiceE
229221
readMoreEntries();
230222
}
231223
} else {
232-
log.info("[{}] Trying to remove a non-connected consumer: {}", name, consumer);
224+
/**
225+
* This is not an expected scenario, it will never happen in expected.
226+
* Just add a defensive code to avoid the topic can not be unloaded anymore: remove the consumers which
227+
* are not mismatch with {@link #consumerSet}. See more detail: https://github.com/apache/pulsar/pull/22270.
228+
*/
229+
log.error("[{}] Trying to remove a non-connected consumer: {}", name, consumer);
230+
consumerList.removeIf(c -> consumer.equals(c));
231+
if (consumerList.isEmpty()) {
232+
clearComponentsAfterRemovedAllConsumers();
233+
}
234+
}
235+
}
236+
237+
private synchronized void clearComponentsAfterRemovedAllConsumers() {
238+
cancelPendingRead();
239+
240+
redeliveryMessages.clear();
241+
redeliveryTracker.clear();
242+
if (closeFuture != null) {
243+
log.info("[{}] All consumers removed. Subscription is disconnected", name);
244+
closeFuture.complete(null);
233245
}
246+
totalAvailablePermits = 0;
234247
}
235248

236249
@Override
@@ -506,6 +519,9 @@ public synchronized CompletableFuture<Void> disconnectAllConsumers(boolean isRes
506519
if (consumerList.isEmpty()) {
507520
closeFuture.complete(null);
508521
} else {
522+
// Iterator of CopyOnWriteArrayList uses the internal array to do the for-each, and CopyOnWriteArrayList
523+
// will create a new internal array when adding/removing a new item. So remove items in the for-each
524+
// block is safety when the for-each and add/remove are using a same lock.
509525
consumerList.forEach(consumer -> consumer.disconnect(isResetCursor));
510526
cancelPendingRead();
511527
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pulsar.broker.service.persistent;
20+
21+
import com.carrotsearch.hppc.ObjectSet;
22+
import java.util.List;
23+
import lombok.extern.slf4j.Slf4j;
24+
import org.apache.pulsar.broker.BrokerTestUtil;
25+
import org.apache.pulsar.broker.service.Dispatcher;
26+
import org.apache.pulsar.client.api.Consumer;
27+
import org.apache.pulsar.client.api.MessageId;
28+
import org.apache.pulsar.client.api.ProducerConsumerBase;
29+
import org.apache.pulsar.client.api.Schema;
30+
import org.apache.pulsar.client.api.SubscriptionType;
31+
import org.awaitility.reflect.WhiteboxImpl;
32+
import org.testng.annotations.AfterClass;
33+
import org.testng.annotations.BeforeClass;
34+
import org.testng.annotations.Test;
35+
36+
@Slf4j
37+
@Test(groups = "broker-api")
38+
public class PersistentDispatcherMultipleConsumersTest extends ProducerConsumerBase {
39+
40+
@BeforeClass(alwaysRun = true)
41+
@Override
42+
protected void setup() throws Exception {
43+
super.internalSetup();
44+
super.producerBaseSetup();
45+
}
46+
47+
@AfterClass(alwaysRun = true)
48+
@Override
49+
protected void cleanup() throws Exception {
50+
super.internalCleanup();
51+
}
52+
53+
@Test(timeOut = 30 * 1000)
54+
public void testTopicDeleteIfConsumerSetMismatchConsumerList() throws Exception {
55+
final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
56+
final String subscription = "s1";
57+
admin.topics().createNonPartitionedTopic(topicName);
58+
admin.topics().createSubscription(topicName, subscription, MessageId.earliest);
59+
60+
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName).subscriptionName(subscription)
61+
.subscriptionType(SubscriptionType.Shared).subscribe();
62+
// Make an error that "consumerSet" is mismatch with "consumerList".
63+
Dispatcher dispatcher = pulsar.getBrokerService()
64+
.getTopic(topicName, false).join().get()
65+
.getSubscription(subscription).getDispatcher();
66+
ObjectSet<org.apache.pulsar.broker.service.Consumer> consumerSet =
67+
WhiteboxImpl.getInternalState(dispatcher, "consumerSet");
68+
List<org.apache.pulsar.broker.service.Consumer> consumerList =
69+
WhiteboxImpl.getInternalState(dispatcher, "consumerList");
70+
71+
org.apache.pulsar.broker.service.Consumer serviceConsumer = consumerList.get(0);
72+
consumerSet.add(serviceConsumer);
73+
consumerList.add(serviceConsumer);
74+
75+
// Verify: the topic can be deleted successfully.
76+
consumer.close();
77+
admin.topics().delete(topicName, false);
78+
}
79+
80+
@Test(timeOut = 30 * 1000)
81+
public void testTopicDeleteIfConsumerSetMismatchConsumerList2() throws Exception {
82+
final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
83+
final String subscription = "s1";
84+
admin.topics().createNonPartitionedTopic(topicName);
85+
admin.topics().createSubscription(topicName, subscription, MessageId.earliest);
86+
87+
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING).topic(topicName).subscriptionName(subscription)
88+
.subscriptionType(SubscriptionType.Shared).subscribe();
89+
// Make an error that "consumerSet" is mismatch with "consumerList".
90+
Dispatcher dispatcher = pulsar.getBrokerService()
91+
.getTopic(topicName, false).join().get()
92+
.getSubscription(subscription).getDispatcher();
93+
ObjectSet<org.apache.pulsar.broker.service.Consumer> consumerSet =
94+
WhiteboxImpl.getInternalState(dispatcher, "consumerSet");
95+
consumerSet.clear();
96+
97+
// Verify: the topic can be deleted successfully.
98+
consumer.close();
99+
admin.topics().delete(topicName, false);
100+
}
101+
}

0 commit comments

Comments
 (0)