-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[fix][broker] Topic policy reader can't recover when get any exception. #17562
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import javax.annotation.Nonnull; | ||
import org.apache.pulsar.broker.PulsarServerException; | ||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.namespace.NamespaceBundleOwnershipListener; | ||
|
@@ -252,20 +253,19 @@ public CompletableFuture<Void> addOwnedNamespaceBundleAsync(NamespaceBundle name | |
return result; | ||
} | ||
|
||
private void prepareInitPoliciesCache(NamespaceName namespace, CompletableFuture<Void> result) { | ||
private void prepareInitPoliciesCache(@Nonnull NamespaceName namespace, CompletableFuture<Void> result) { | ||
if (policyCacheInitMap.putIfAbsent(namespace, false) == null) { | ||
CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerCompletableFuture = | ||
createSystemTopicClientWithRetry(namespace); | ||
readerCaches.put(namespace, readerCompletableFuture); | ||
readerCompletableFuture.whenComplete((reader, ex) -> { | ||
if (ex != null) { | ||
log.error("[{}] Failed to create reader on __change_events topic", namespace, ex); | ||
result.completeExceptionally(ex); | ||
cleanCacheAndCloseReader(reader.getSystemTopic().getTopicName().getNamespaceObject(), false); | ||
} else { | ||
initPolicesCache(reader, result); | ||
result.thenRun(() -> readMorePolicies(reader)); | ||
} | ||
readerCompletableFuture.thenAccept(reader -> { | ||
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. Use |
||
initPolicesCache(reader, result); | ||
result.thenRun(() -> readMorePolicies(reader)); | ||
}).exceptionally(ex -> { | ||
log.error("[{}] Failed to create reader on __change_events topic", namespace, ex); | ||
cleanCacheAndCloseReader(namespace, false); | ||
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. This method won't have any chance to throw an exception. I think it's safe enough and don't need try-catch. |
||
result.completeExceptionally(ex); | ||
return null; | ||
}); | ||
} | ||
} | ||
|
@@ -367,14 +367,18 @@ private void initPolicesCache(SystemTopicClient.Reader<PulsarEvent> reader, Comp | |
}); | ||
} | ||
|
||
private void cleanCacheAndCloseReader(NamespaceName namespace, boolean cleanOwnedBundlesCount) { | ||
private void cleanCacheAndCloseReader(@Nonnull NamespaceName namespace, boolean cleanOwnedBundlesCount) { | ||
CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> readerFuture = readerCaches.remove(namespace); | ||
policiesCache.entrySet().removeIf(entry -> Objects.equals(entry.getKey().getNamespaceObject(), namespace)); | ||
if (cleanOwnedBundlesCount) { | ||
ownedBundlesCountPerNamespace.remove(namespace); | ||
} | ||
if (readerFuture != null && !readerFuture.isCompletedExceptionally()) { | ||
readerFuture.thenAccept(SystemTopicClient.Reader::closeAsync); | ||
readerFuture.thenCompose(SystemTopicClient.Reader::closeAsync) | ||
.exceptionally(ex -> { | ||
log.warn("[{}] Close change_event reader fail.", namespace, ex); | ||
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. We can add a log to help debug. Maybe we can introduce more mechanisms to clean up the resource. But it's not the current PR focus. |
||
return null; | ||
}); | ||
} | ||
policyCacheInitMap.remove(namespace); | ||
} | ||
|
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.
In the current time reader is null, So we can't clean the cache and close the reader.