Skip to content
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][metadata] Don't execute Bookkeeper metadata callbacks on Zookeeper event thread #17620

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import static org.apache.bookkeeper.util.BookKeeperConstants.AVAILABLE_NODE;
import static org.apache.bookkeeper.util.BookKeeperConstants.COOKIE_NODE;
import static org.apache.bookkeeper.util.BookKeeperConstants.READONLY;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.apache.bookkeeper.discover.RegistrationClient;
import org.apache.bookkeeper.net.BookieId;
import org.apache.bookkeeper.versioning.Version;
Expand All @@ -46,6 +49,7 @@ public class PulsarRegistrationClient implements RegistrationClient {

private final Map<RegistrationListener, Boolean> writableBookiesWatchers = new ConcurrentHashMap<>();
private final Map<RegistrationListener, Boolean> readOnlyBookiesWatchers = new ConcurrentHashMap<>();
private final ScheduledExecutorService executor;

public PulsarRegistrationClient(MetadataStore store,
String ledgersRootPath) {
Expand All @@ -60,11 +64,15 @@ public PulsarRegistrationClient(MetadataStore store,
this.bookieAllRegistrationPath = ledgersRootPath + "/" + COOKIE_NODE;
this.bookieReadonlyRegistrationPath = this.bookieRegistrationPath + "/" + READONLY;

this.executor = Executors
gaozhangmin marked this conversation as resolved.
Show resolved Hide resolved
.newSingleThreadScheduledExecutor(new DefaultThreadFactory("pulsar-registration-client"));

store.registerListener(this::updatedBookies);
}

@Override
public void close() {
executor.shutdownNow();
}

@Override
Expand Down Expand Up @@ -99,7 +107,7 @@ private CompletableFuture<Versioned<Set<BookieId>>> getChildren(String path) {
public CompletableFuture<Void> watchWritableBookies(RegistrationListener registrationListener) {
writableBookiesWatchers.put(registrationListener, Boolean.TRUE);
return getWritableBookies()
.thenAccept(registrationListener::onBookiesChanged);
.thenAcceptAsync(registrationListener::onBookiesChanged, executor);
}

@Override
Expand All @@ -111,7 +119,7 @@ public void unwatchWritableBookies(RegistrationListener registrationListener) {
public CompletableFuture<Void> watchReadOnlyBookies(RegistrationListener registrationListener) {
readOnlyBookiesWatchers.put(registrationListener, Boolean.TRUE);
return getReadOnlyBookies()
.thenAccept(registrationListener::onBookiesChanged);
.thenAcceptAsync(registrationListener::onBookiesChanged, executor);
}

@Override
Expand All @@ -124,11 +132,11 @@ private void updatedBookies(Notification n) {
if (n.getPath().startsWith(bookieReadonlyRegistrationPath)) {
getReadOnlyBookies().thenAccept(bookies ->
readOnlyBookiesWatchers.keySet()
.forEach(w -> w.onBookiesChanged(bookies)));
.forEach(w -> executor.execute(() -> w.onBookiesChanged(bookies))));
} else if (n.getPath().startsWith(bookieRegistrationPath)) {
getWritableBookies().thenAccept(bookies ->
writableBookiesWatchers.keySet()
.forEach(w -> w.onBookiesChanged(bookies)));
.forEach(w -> executor.execute(() -> w.onBookiesChanged(bookies))));
}
}
}
Expand Down