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

Close topics after creating all subscriptions on increment-partitions #778

Merged
merged 1 commit into from
Sep 21, 2017
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 @@ -1303,20 +1303,23 @@ private CompletableFuture<Void> createSubscriptions(DestinationName dn, int numP

// get list of cursors name of partition-1
final String ledgerName = dn.getPartition(1).getPersistenceNamingEncoding();
final Set<Topic> topics = Sets.newConcurrentHashSet();
((ManagedLedgerFactoryImpl) pulsar().getManagedLedgerFactory()).getMetaStore().getCursors(ledgerName,
new MetaStoreCallback<List<String>>() {

@Override
public void operationComplete(List<String> cursors,
org.apache.bookkeeper.mledger.impl.MetaStore.Stat stat) {
List<CompletableFuture<Void>> topicCreationFuture = Lists.newArrayList();
List<CompletableFuture<Void>> subscriptionCreationFuture = Lists.newArrayList();
// create subscriptions for all new partition-topics
cursors.forEach(cursor -> {
String subName = Codec.decode(cursor);
for (int i = partitionMetadata.partitions; i < numPartitions; i++) {
final String topicName = dn.getPartition(i).toString();
CompletableFuture<Void> future = new CompletableFuture<>();
pulsar().getBrokerService().getTopic(topicName).handle((topic, ex) -> {
// cache topic to close all of them after creating all subscriptions
topics.add(topic);
if (ex != null) {
log.warn("[{}] Failed to create topic {}", clientAppId(), topicName);
future.completeExceptionally(ex);
Expand All @@ -1329,29 +1332,37 @@ public void operationComplete(List<String> cursors,
future.completeExceptionally(e);
return null;
} else {
log.info("[{}] Successfully create subsciption {} {}",
log.info("[{}] Successfully created subsciption {} {}",
clientAppId(), topicName, subName);
// close topic
topic.close();
future.complete(null);
return null;
}
});
return null;
}
});
topicCreationFuture.add(future);
subscriptionCreationFuture.add(future);
}
});
// wait for all subscriptions to be created
FutureUtil.waitForAll(topicCreationFuture).handle((res, e) -> {
if (e != null) {
result.completeExceptionally(e);
} else {
log.info("[{}] Successfully create new partitions {}", clientAppId(),
dn.toString());
result.complete(null);
}
FutureUtil.waitForAll(subscriptionCreationFuture).handle((res, subscriptionException) -> {
// close all topics and then complete result future
FutureUtil.waitForAll(
topics.stream().map(topic -> topic.close()).collect(Collectors.toList()))
.handle((closed, topicCloseException) -> {
if (topicCloseException != null) {
log.warn("Failed to close newly created partitioned topics for {} ", dn,
topicCloseException);
}
if (subscriptionException != null) {
result.completeExceptionally(subscriptionException);
} else {
log.info("[{}] Successfully created new partitions {}", clientAppId(),
dn.toString());
result.complete(null);
}
return null;
});
return null;
});
}
Expand Down