-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix][client] Fix retry logic for BinaryProtoLookupService.getTopicsUnderNamespace method #24974
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
Open
lhotari
wants to merge
7
commits into
apache:master
Choose a base branch
from
lhotari:lh-fix-gettopics-retries
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
570e0b6
Change EventLoopGroup reference to ScheduledExecutorService in Client…
lhotari 4597343
Use FrameDecoderUtil for creating LengthFieldBasedFrameDecoder in tests
lhotari 6e67855
Remove code duplication in creating lookup
lhotari b2ccccf
Pass ScheduledExecutorService to BinaryProtoLookupService
lhotari 1bb219c
Fix get topics of namespace retries
lhotari 2562c19
Remove invalid tests
lhotari b3ab4f0
Don't retry if the error cannot be retried
lhotari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,6 @@ | |
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.apache.commons.lang3.mutable.MutableObject; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.apache.pulsar.client.api.PulsarClientException; | ||
|
|
@@ -61,7 +60,7 @@ public class BinaryProtoLookupService implements LookupService { | |
| private final PulsarClientImpl client; | ||
| private final ServiceNameResolver serviceNameResolver; | ||
| private final boolean useTls; | ||
| private final ExecutorService scheduleExecutor; | ||
| private final ScheduledExecutorService scheduleExecutor; | ||
| private final String listenerName; | ||
| private final int maxLookupRedirects; | ||
| private final ExecutorService lookupPinnedExecutor; | ||
|
|
@@ -83,27 +82,29 @@ public class BinaryProtoLookupService implements LookupService { | |
|
|
||
| /** | ||
| * @deprecated use {@link | ||
| * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ExecutorService, ExecutorService)} instead. | ||
| * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ScheduledExecutorService, ExecutorService)} | ||
| * instead. | ||
| */ | ||
| @Deprecated | ||
| public BinaryProtoLookupService(PulsarClientImpl client, | ||
| String serviceUrl, | ||
| boolean useTls, | ||
| ExecutorService scheduleExecutor) | ||
| ScheduledExecutorService scheduleExecutor) | ||
| throws PulsarClientException { | ||
| this(client, serviceUrl, null, useTls, scheduleExecutor); | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated use {@link | ||
| * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ExecutorService, ExecutorService)} instead. | ||
| * #BinaryProtoLookupService(PulsarClientImpl, String, String, boolean, ScheduledExecutorService, ExecutorService)} | ||
| * instead. | ||
| */ | ||
| @Deprecated | ||
| public BinaryProtoLookupService(PulsarClientImpl client, | ||
| String serviceUrl, | ||
| String listenerName, | ||
| boolean useTls, | ||
| ExecutorService scheduleExecutor) | ||
| ScheduledExecutorService scheduleExecutor) | ||
| throws PulsarClientException { | ||
| this(client, serviceUrl, listenerName, useTls, scheduleExecutor, null); | ||
| } | ||
|
|
@@ -112,7 +113,7 @@ public BinaryProtoLookupService(PulsarClientImpl client, | |
| String serviceUrl, | ||
| String listenerName, | ||
| boolean useTls, | ||
| ExecutorService scheduleExecutor, | ||
| ScheduledExecutorService scheduleExecutor, | ||
| ExecutorService lookupPinnedExecutor) | ||
| throws PulsarClientException { | ||
| this.client = client; | ||
|
|
@@ -409,15 +410,16 @@ public CompletableFuture<GetTopicsResult> getTopicsUnderNamespace(NamespaceName | |
| try { | ||
| return topicsUnderNamespaceInProgress.computeIfAbsent(key, k -> { | ||
| CompletableFuture<GetTopicsResult> topicsFuture = new CompletableFuture<>(); | ||
| AtomicLong opTimeoutMs = new AtomicLong(client.getConfiguration().getOperationTimeoutMs()); | ||
| long opTimeoutMs = client.getConfiguration().getOperationTimeoutMs(); | ||
| Backoff backoff = new BackoffBuilder() | ||
| .setInitialTime(100, TimeUnit.MILLISECONDS) | ||
| .setMandatoryStop(opTimeoutMs.get() * 2, TimeUnit.MILLISECONDS) | ||
| .setMandatoryStop(opTimeoutMs * 2, TimeUnit.MILLISECONDS) | ||
| .setMax(1, TimeUnit.MINUTES) | ||
| .create(); | ||
|
|
||
| long startTimeNanos = System.nanoTime(); | ||
| long retryUntilNanos = startTimeNanos + TimeUnit.MILLISECONDS.toNanos(opTimeoutMs); | ||
| newFutureCreated.setValue(topicsFuture); | ||
| getTopicsUnderNamespace(namespace, backoff, opTimeoutMs, topicsFuture, mode, | ||
| getTopicsUnderNamespace(namespace, backoff, startTimeNanos, retryUntilNanos, topicsFuture, mode, | ||
| topicsPattern, topicsHash); | ||
| return topicsFuture; | ||
| }); | ||
|
|
@@ -433,50 +435,56 @@ public CompletableFuture<GetTopicsResult> getTopicsUnderNamespace(NamespaceName | |
| private void getTopicsUnderNamespace( | ||
| NamespaceName namespace, | ||
| Backoff backoff, | ||
| AtomicLong remainingTime, | ||
| long startTimeNanos, | ||
| long retryUntilNanos, | ||
| CompletableFuture<GetTopicsResult> getTopicsResultFuture, | ||
| Mode mode, | ||
| String topicsPattern, | ||
| String topicsHash) { | ||
| long startTime = System.nanoTime(); | ||
|
|
||
| client.getCnxPool().getConnection(serviceNameResolver).thenAcceptAsync(clientCnx -> { | ||
| client.getCnxPool().getConnection(serviceNameResolver).thenComposeAsync(clientCnx -> { | ||
| long requestId = client.newRequestId(); | ||
| ByteBuf request = Commands.newGetTopicsOfNamespaceRequest( | ||
| namespace.toString(), requestId, mode, topicsPattern, topicsHash); | ||
|
|
||
| clientCnx.newGetTopicsOfNamespace(request, requestId).whenComplete((r, t) -> { | ||
| if (t != null) { | ||
| histoListTopics.recordFailure(System.nanoTime() - startTime); | ||
| getTopicsResultFuture.completeExceptionally(t); | ||
| ByteBuf request = Commands.newGetTopicsOfNamespaceRequest(namespace.toString(), requestId, mode, | ||
| topicsPattern, topicsHash); | ||
| return clientCnx.newGetTopicsOfNamespace(request, requestId).whenComplete((r, t) -> { | ||
| client.getCnxPool().releaseConnection(clientCnx); | ||
| }); | ||
| }, lookupPinnedExecutor).whenComplete((r, t) -> { | ||
| if (t != null) { | ||
| Throwable cause = FutureUtil.unwrapCompletionException(t); | ||
| if (cause instanceof PulsarClientException && !PulsarClientException.isRetriableError(cause)) { | ||
|
Contributor
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. Is it covered in the test here? |
||
| histoListTopics.recordFailure(System.nanoTime() - startTimeNanos); | ||
| getTopicsResultFuture.completeExceptionally(cause); | ||
| return; | ||
| } | ||
| long nowNanos = System.nanoTime(); | ||
| if (nowNanos > retryUntilNanos) { | ||
| histoListTopics.recordFailure(System.nanoTime() - startTimeNanos); | ||
| log.warn("[namespace: {}] Error while getTopicsUnderNamespace -- No more retries left." | ||
| + " Last error was {}", namespace, t.getMessage()); | ||
| getTopicsResultFuture.completeExceptionally(new PulsarClientException.TimeoutException( | ||
| format("Could not get topics of namespace %s within configured timeout", | ||
| namespace.toString()))); | ||
| } else { | ||
| histoListTopics.recordSuccess(System.nanoTime() - startTime); | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("[namespace: {}] Success get topics list in request: {}", | ||
| namespace, requestId); | ||
| long nextDelay = backoff.next(); | ||
| log.warn("[namespace: {}] Error while getTopicsUnderNamespace -- Will try again in" | ||
| + " {} ms. Error was {}", namespace, nextDelay, t.getMessage()); | ||
| if (!getTopicsResultFuture.isDone()) { | ||
| scheduleExecutor.schedule(() -> { | ||
| getTopicsUnderNamespace(namespace, backoff, startTimeNanos, retryUntilNanos, | ||
| getTopicsResultFuture, mode, topicsPattern, topicsHash); | ||
| }, nextDelay, TimeUnit.MILLISECONDS); | ||
| } else { | ||
| log.info("[namespace: {}] Ignoring retry in getTopicsUnderNamespace -- Future is already " | ||
| + "completed", namespace); | ||
| } | ||
| getTopicsResultFuture.complete(r); | ||
| } | ||
| client.getCnxPool().releaseConnection(clientCnx); | ||
| }); | ||
| }, lookupPinnedExecutor).exceptionally((e) -> { | ||
| long nextDelay = Math.min(backoff.next(), remainingTime.get()); | ||
| if (nextDelay <= 0) { | ||
| getTopicsResultFuture.completeExceptionally( | ||
| new PulsarClientException.TimeoutException( | ||
| format("Could not get topics of namespace %s within configured timeout", | ||
| namespace.toString()))); | ||
| return null; | ||
| } else { | ||
| histoListTopics.recordSuccess(System.nanoTime() - startTimeNanos); | ||
| if (log.isDebugEnabled()) { | ||
| log.debug("[namespace: {}] Success get topics list", namespace); | ||
| } | ||
| getTopicsResultFuture.complete(r); | ||
| } | ||
|
|
||
| ((ScheduledExecutorService) scheduleExecutor).schedule(() -> { | ||
| log.warn("[namespace: {}] Could not get connection while getTopicsUnderNamespace -- Will try again in" | ||
| + " {} ms", namespace, nextDelay); | ||
| remainingTime.addAndGet(-nextDelay); | ||
| getTopicsUnderNamespace(namespace, backoff, remainingTime, getTopicsResultFuture, | ||
| mode, topicsPattern, topicsHash); | ||
| }, nextDelay, TimeUnit.MILLISECONDS); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This can be removed.