-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Issue 5597][pulsar-client-java] retry when getPartitionedTopicMetadata failed #5603
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.URI; | ||
| import java.net.URL; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
|
|
@@ -127,8 +128,9 @@ public void close() throws IOException { | |
| public <T> CompletableFuture<T> get(String path, Class<T> clazz) { | ||
| final CompletableFuture<T> future = new CompletableFuture<>(); | ||
| try { | ||
| String requestUrl = new URL(serviceNameResolver.resolveHostUri().toURL(), path).toString(); | ||
| String remoteHostName = serviceNameResolver.resolveHostUri().getHost(); | ||
|
Comment on lines
-130
to
-131
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sijie maybe a bug here, the host in requestUrl not equal with remoteHostName if multi-broker service url provided, and will step forward two service url every request. please take a look.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry I don't quite get this. Can you show me an example?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the PulsarServiceNameResolver.resolveHostUri() called twice(every time it will return the next address) inside HttpClient.get() ,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh I see. yes. the original code has a bug there. |
||
| URI hostUri = serviceNameResolver.resolveHostUri(); | ||
| String requestUrl = new URL(hostUri.toURL(), path).toString(); | ||
| String remoteHostName = hostUri.getHost(); | ||
| AuthenticationDataProvider authData = authentication.getAuthData(remoteHostName); | ||
|
|
||
| CompletableFuture<Map<String, String>> authFuture = new CompletableFuture<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -641,17 +641,45 @@ public CompletableFuture<Integer> getNumberOfPartitions(String topic) { | |
|
|
||
| public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) { | ||
|
|
||
| CompletableFuture<PartitionedTopicMetadata> metadataFuture; | ||
| CompletableFuture<PartitionedTopicMetadata> metadataFuture = new CompletableFuture<>(); | ||
|
|
||
| try { | ||
| TopicName topicName = TopicName.get(topic); | ||
| metadataFuture = lookup.getPartitionedTopicMetadata(topicName); | ||
| AtomicLong opTimeoutMs = new AtomicLong(conf.getOperationTimeoutMs()); | ||
| Backoff backoff = new BackoffBuilder() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not direct related to this change. but we should consider adding backoff for other places as well. |
||
| .setInitialTime(100, TimeUnit.MILLISECONDS) | ||
| .setMandatoryStop(opTimeoutMs.get() * 2, TimeUnit.MILLISECONDS) | ||
| .setMax(0, TimeUnit.MILLISECONDS) | ||
| .useUserConfiguredIntervals(conf.getDefaultBackoffIntervalNanos(), | ||
| conf.getMaxBackoffIntervalNanos()) | ||
| .create(); | ||
| getPartitionedTopicMetadata(topicName, backoff, opTimeoutMs, metadataFuture); | ||
| } catch (IllegalArgumentException e) { | ||
| return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage())); | ||
| } | ||
| return metadataFuture; | ||
| } | ||
|
|
||
| private void getPartitionedTopicMetadata(TopicName topicName, | ||
| Backoff backoff, | ||
| AtomicLong remainingTime, | ||
| CompletableFuture<PartitionedTopicMetadata> future) { | ||
| lookup.getPartitionedTopicMetadata(topicName).thenAccept(future::complete).exceptionally(e -> { | ||
| long nextDelay = Math.min(backoff.next(), remainingTime.get()); | ||
| if (nextDelay <= 0) { | ||
| future.completeExceptionally(new PulsarClientException | ||
| .TimeoutException("Could not getPartitionedTopicMetadata within configured timeout.")); | ||
| return null; | ||
| } | ||
|
|
||
| timer.newTimeout( task -> { | ||
| remainingTime.addAndGet(-nextDelay); | ||
| getPartitionedTopicMetadata(topicName, backoff, remainingTime, future); | ||
| }, nextDelay, TimeUnit.MILLISECONDS); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<List<String>> getPartitionsForTopic(String topic) { | ||
| return getPartitionedTopicMetadata(topic).thenApply(metadata -> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert the exception time.