jimmydivvy commented on a change in pull request #14433:
URL: https://github.com/apache/pulsar/pull/14433#discussion_r813498552
##########
File path:
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerBuilderImpl.java
##########
@@ -119,52 +118,58 @@ public ConsumerBuilderImpl(PulsarClientImpl client,
Schema<T> schema) {
return FutureUtil.failedFuture(
new InvalidConfigurationException("KeySharedPolicy must
set with KeyShared subscription"));
}
+ CompletableFuture<Void> applyDLQConfig;
if (conf.isRetryEnable() && conf.getTopicNames().size() > 0) {
TopicName topicFirst =
TopicName.get(conf.getTopicNames().iterator().next());
- String retryLetterTopic =
- topicFirst + "-" + conf.getSubscriptionName() +
RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
- String deadLetterTopic =
- topicFirst + "-" + conf.getSubscriptionName() +
RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
+ AtomicReference<String> retryLetterTopic =
+ new AtomicReference<>(topicFirst + "-" +
conf.getSubscriptionName()
+ + RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX);
+ AtomicReference<String> deadLetterTopic =
+ new AtomicReference<>(topicFirst + "-" +
conf.getSubscriptionName()
+ + RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX);
//Issue 9327: do compatibility check in case of the default retry
and dead letter topic name changed
String oldRetryLetterTopic = topicFirst.getNamespace() + "/" +
conf.getSubscriptionName()
+ RetryMessageUtil.RETRY_GROUP_TOPIC_SUFFIX;
String oldDeadLetterTopic = topicFirst.getNamespace() + "/" +
conf.getSubscriptionName()
+ RetryMessageUtil.DLQ_GROUP_TOPIC_SUFFIX;
- try {
- if (client.getPartitionedTopicMetadata(oldRetryLetterTopic)
- .get(client.conf.getOperationTimeoutMs(),
TimeUnit.MILLISECONDS).partitions > 0) {
- retryLetterTopic = oldRetryLetterTopic;
- }
- if (client.getPartitionedTopicMetadata(oldDeadLetterTopic)
- .get(client.conf.getOperationTimeoutMs(),
TimeUnit.MILLISECONDS).partitions > 0) {
- deadLetterTopic = oldDeadLetterTopic;
- }
- } catch (InterruptedException | TimeoutException e) {
- return FutureUtil.failedFuture(e);
- } catch (ExecutionException e) {
- return FutureUtil.failedFuture(e.getCause());
- }
-
- if (conf.getDeadLetterPolicy() == null) {
- conf.setDeadLetterPolicy(DeadLetterPolicy.builder()
-
.maxRedeliverCount(RetryMessageUtil.MAX_RECONSUMETIMES)
- .retryLetterTopic(retryLetterTopic)
- .deadLetterTopic(deadLetterTopic)
- .build());
+ applyDLQConfig =
client.getPartitionedTopicMetadata(oldRetryLetterTopic)
Review comment:
I spiked this rough solution earlier. I'm having some issues getting my
build environment setup so I haven't tested (or even compiled) this, but it
might be useful.
```java
/** Check if the specified topic exists. If so, return its name directly. If
now, return the alternative */
private CompletableFuture<String> getTopicOrElse(String checkOld, String
elseNew){
client
.getPartitionedTopicMetadata(checkOld)
.thenApply( meta -> {
if( meta.partitions > 0) {
return checkOld;
} else {
return elseNew;
}
});
}
```
```java
// inside subscribeAsync()
// If there is no DeadLetterPolicy, create a placeholder. We'll fill this in
further below.
if( conf.getDeadLetterPolicy() == null ){
conf.setDeadLetterPolicy(DeadLetterPolicy.builder().maxRedeliverCount(RetryMessageUtil.MAX_RECONSUMETIMES).build());
}
// If the caller did not specify a RetryLetter topic, generate one
automatically
// First, check for an existing topic using the pre-2.9 naming scheme (To
ensure backwards compatibility)
// If that's not found, then default to the 2.9+ naming scheme.
CompletableFuture<String> resolveRetryTopic =
StringUtils.isBlank(conf.getDeadLetterPolicy().getRetryLetterTopic())
? getTopicOrElse(oldRetryLetterTopic, newRetryLetterTopic)
:
CompletableFuture.completedFuture(conf.getDeadLetterPolicy().getRetryLetterTopic());
// Do the same for DLQ Topic name
CompletableFuture<String> resolveDLQTopic =
StringUtils.isBlank(conf.getDeadLetterPolicy().getDeadLetterTopic())
? getTopicOrElse(oldDeadLetterTopic, newDeadLetterTopic)
:
CompletableFuture.completedFuture(conf.getDeadLetterPolicy().getDeadLetterTopic());
// Wait for both the Retry and DLQ Topic names to resolve
resolveRetryTopic
.thenAcceptBoth(resolveDLQTopic, (retryTopicName,dlqTopicName) -> {
// Update the DeadLetterPolicy with the generated names
// (If these were already specified, this will simply write the
existing value back)
conf.getDeadLetterPolicy().setRetryLetterTopic(retryTopicName);
conf.getDeadLetterPolicy().setDeadLetterTopic(dlqTopicName);
})
.thenCompose ( __ -> {
if (interceptorList == null || interceptorList.size() == 0) {
return client.subscribeAsync(conf, schema, null);
} else {
return client.subscribeAsync(conf, schema, new
ConsumerInterceptors<>(interceptorList));
}
});
```
--
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]