Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pulsar-broker] If cursor is not durable, close dispatcher when all consumers are removed from subscription #5340

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,22 @@ public synchronized void removeConsumer(Consumer consumer) throws BrokerServiceE

if (!cursor.isDurable()) {
// If cursor is not durable, we need to clean up the subscription as well
close();
this.close().thenRun(() -> {
synchronized (this) {
if (dispatcher != null) {
dispatcher.close().thenRun(() -> {
log.info("[{}][{}] Successfully closed dispatcher for reader", topicName, subName);
}).exceptionally(ex -> {
log.error("[{}][{}] Failed to close dispatcher for reader", topicName, subName, ex);
return null;
});
}
}
}).exceptionally(exception -> {
log.error("[{}][{}] Failed to close subscription for reader", topicName, subName, exception);
return null;
});

// when topic closes: it iterates through concurrent-subscription map to close each subscription. so,
// topic.remove again try to access same map which creates deadlock. so, execute it in different thread.
topic.getBrokerService().pulsar().getExecutor().submit(() ->{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,30 @@ public void testAddRemoveConsumer() throws Exception {
}
}

@Test
public void testAddRemoveConsumerDurableCursor() throws Exception {
doReturn(false).when(cursorMock).isDurable();

PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "non-durable-sub", cursorMock, false);

Consumer consumer = new Consumer(sub, SubType.Exclusive, topic.getName(), 1, 0, "Cons1", 50000, serverCnx,
"myrole-1", Collections.emptyMap(), false, InitialPosition.Latest);

sub.addConsumer(consumer);
assertFalse(sub.getDispatcher().isClosed());
sub.removeConsumer(consumer);

// The dispatcher is closed asynchronously
for (int i = 0; i < 100; i++) {
if (sub.getDispatcher().isClosed()) {
break;
}
Thread.sleep(100);
}
assertTrue(sub.getDispatcher().isClosed());
}

public void testMaxConsumersShared() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock, false);
Expand Down