Skip to content

Commit

Permalink
[fix][client]Fix deadlock issue of consumer while using multiple IO t…
Browse files Browse the repository at this point in the history
…hreads (#20669)
  • Loading branch information
codelipenghui authored Jun 29, 2023
1 parent 8469f58 commit d3a8c22
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ protected void cleanup() throws Exception {
super.internalCleanup();
}

@Override
protected void customizeNewPulsarClientBuilder(ClientBuilder clientBuilder) {
clientBuilder.ioThreads(4).connectionsPerBroker(4);
}

// test that reproduces the issue https://github.com/apache/pulsar/issues/12024
// where closing the consumer leads to an endless receive loop
@Test
Expand Down Expand Up @@ -351,4 +356,19 @@ public int choosePartition(Message<?> msg, TopicMetadata metadata) {
}
consumer.close();
}

@Test(invocationCount = 10, timeOut = 30000)
public void testMultipleIOThreads() throws PulsarAdminException, PulsarClientException {
final var topic = TopicName.get(newTopicName()).toString();
final var numPartitions = 100;
admin.topics().createPartitionedTopic(topic, numPartitions);
for (int i = 0; i < 100; i++) {
admin.topics().createNonPartitionedTopic(topic + "-" + i);
}
@Cleanup
final var consumer = pulsarClient.newConsumer(Schema.INT32).topicsPattern(topic + ".*")
.subscriptionName("sub").subscribe();
assertTrue(consumer instanceof MultiTopicsConsumerImpl);
assertTrue(consumer.isConnected());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class MultiTopicsConsumerImpl<T> extends ConsumerBase<T> {
// sum of topicPartitions, simple topic has 1, partitioned topic equals to partition number.
AtomicInteger allTopicPartitionsNumber;

private boolean paused = false;
private volatile boolean paused = false;
private final Object pauseMutex = new Object();
// timeout related to auto check and subscribe partition increasement
private volatile Timeout partitionsAutoUpdateTimeout = null;
Expand Down Expand Up @@ -1059,29 +1059,28 @@ private void doSubscribeTopicPartitions(Schema<T> schema,

CompletableFuture<Consumer<T>> subFuture = new CompletableFuture<>();

consumers.compute(topicName, (key, existingValue) -> {
if (existingValue != null) {
String errorMessage = String.format("[%s] Failed to subscribe for topic [%s] in topics consumer. "
+ "Topic is already being subscribed for in other thread.", topic, topicName);
log.warn(errorMessage);
subscribeResult.completeExceptionally(new PulsarClientException(errorMessage));
return existingValue;
} else {
internalConfig.setStartPaused(paused);
ConsumerImpl<T> newConsumer = createInternalConsumer(internalConfig, topicName,
-1, subFuture, createIfDoesNotExist, schema);

synchronized (pauseMutex) {
synchronized (pauseMutex) {
consumers.compute(topicName, (key, existingValue) -> {
if (existingValue != null) {
String errorMessage =
String.format("[%s] Failed to subscribe for topic [%s] in topics consumer. "
+ "Topic is already being subscribed for in other thread.", topic, topicName);
log.warn(errorMessage);
subscribeResult.completeExceptionally(new PulsarClientException(errorMessage));
return existingValue;
} else {
internalConfig.setStartPaused(paused);
ConsumerImpl<T> newConsumer = createInternalConsumer(internalConfig, topicName,
-1, subFuture, createIfDoesNotExist, schema);
if (paused) {
newConsumer.pause();
} else {
newConsumer.resume();
}
return newConsumer;
}
return newConsumer;
}
});

});
}
futureList = Collections.singletonList(subFuture);
}

Expand Down Expand Up @@ -1409,7 +1408,7 @@ private CompletableFuture<Void> subscribeIncreasedTopicPartitions(String topicNa
}
if (log.isDebugEnabled()) {
log.debug("[{}] create consumer {} for partitionName: {}",
topicName, newConsumer.getTopic(), partitionName);
topicName, newConsumer.getTopic(), partitionName);
}
return subFuture;
})
Expand Down

0 comments on commit d3a8c22

Please sign in to comment.