Skip to content

Commit

Permalink
Address penghui's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
murong00 committed Mar 8, 2020
1 parent 7452bca commit 07a222e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,14 @@ protected void internalGetPartitionedStatsInternal(AsyncResponse asyncResponse,
});
}

protected void internalDeleteSubscription(AsyncResponse asyncResponse, String subName, boolean authoritative, boolean force) {
if (force) {
internalDeleteSubscriptionForcefully(asyncResponse, subName, authoritative);
} else {
internalDeleteSubscription(asyncResponse, subName, authoritative);
}
}

protected void internalDeleteSubscription(AsyncResponse asyncResponse, String subName, boolean authoritative) {
if (topicName.isGlobal()) {
try {
Expand All @@ -1067,7 +1075,7 @@ protected void internalDeleteSubscription(AsyncResponse asyncResponse, String su
TopicName topicNamePartition = topicName.getPartition(i);
try {
futures.add(pulsar().getAdminClient().topics()
.deleteSubscriptionAsync(topicNamePartition.toString(), subName));
.deleteSubscriptionAsync(topicNamePartition.toString(), subName, false));
} catch (Exception e) {
log.error("[{}] Failed to delete subscription {} {}", clientAppId(), topicNamePartition, subName,
e);
Expand Down Expand Up @@ -1133,6 +1141,87 @@ private void internalDeleteSubscriptionForNonPartitionedTopic(AsyncResponse asyn
}
}

protected void internalDeleteSubscriptionForcefully(AsyncResponse asyncResponse, String subName, boolean authoritative) {
if (topicName.isGlobal()) {
try {
validateGlobalNamespaceOwnership(namespaceName);
} catch (Exception e) {
log.error("[{}] Failed to delete subscription forcefully {} from topic {}", clientAppId(), subName, topicName, e);
resumeAsyncResponseExceptionally(asyncResponse, e);
return;
}
}
// If the topic name is a partition name, no need to get partition topic metadata again
if (topicName.isPartitioned()) {
internalDeleteSubscriptionForNonPartitionedTopicForcefully(asyncResponse, subName, authoritative);
} else {
getPartitionedTopicMetadataAsync(topicName, authoritative, false).thenAccept(partitionMetadata -> {
if (partitionMetadata.partitions > 0) {
final List<CompletableFuture<Void>> futures = Lists.newArrayList();

for (int i = 0; i < partitionMetadata.partitions; i++) {
TopicName topicNamePartition = topicName.getPartition(i);
try {
futures.add(pulsar().getAdminClient().topics()
.deleteSubscriptionAsync(topicNamePartition.toString(), subName, true));
} catch (Exception e) {
log.error("[{}] Failed to delete subscription forcefully {} {}", clientAppId(), topicNamePartition, subName,
e);
asyncResponse.resume(new RestException(e));
return;
}
}

FutureUtil.waitForAll(futures).handle((result, exception) -> {
if (exception != null) {
Throwable t = exception.getCause();
if (t instanceof NotFoundException) {
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Subscription not found"));
return null;
} else {
log.error("[{}] Failed to delete subscription forcefully {} {}", clientAppId(), topicName, subName, t);
asyncResponse.resume(new RestException(t));
return null;
}
}

asyncResponse.resume(Response.noContent().build());
return null;
});
} else {
internalDeleteSubscriptionForNonPartitionedTopicForcefully(asyncResponse, subName, authoritative);
}
}).exceptionally(ex -> {
log.error("[{}] Failed to delete subscription forcefully {} from topic {}", clientAppId(), subName, topicName, ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
}

private void internalDeleteSubscriptionForNonPartitionedTopicForcefully(AsyncResponse asyncResponse, String subName, boolean authoritative) {
try {
validateAdminAccessForSubscriber(subName, authoritative);
Topic topic = getTopicReference(topicName);
Subscription sub = topic.getSubscription(subName);
if (sub == null) {
asyncResponse.resume(new RestException(Status.NOT_FOUND, "Subscription not found"));
return;
}
sub.deleteForcefully().get();
log.info("[{}][{}] Deleted subscription forcefully {}", clientAppId(), topicName, subName);
asyncResponse.resume(Response.noContent().build());
} catch (Exception e) {
log.error("[{}] Failed to delete subscription forcefully {} from topic {}", clientAppId(), subName, topicName, e);
if (e instanceof WebApplicationException) {
asyncResponse.resume(e);
} else {
log.error("[{}] Failed to delete subscription forcefully {} {}", clientAppId(), topicName, subName, e);
asyncResponse.resume(new RestException(e));
}
}
}

protected void internalSkipAllMessages(AsyncResponse asyncResponse, String subName, boolean authoritative) {
if (topicName.isGlobal()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,21 +1303,21 @@ public void testNamespaceBundleUnload(Integer numBundles) throws Exception {
admin.topics().delete("persistent://prop-xyz/ns1-bundles/ds2");
}

@Test(dataProvider = "topicName")
public void testDeleteSubscription(String topicName) throws Exception {
final String subName = topicName;
final String persistentTopicName = "persistent://prop-xyz/ns1/" + topicName;
@Test
public void testDeleteSubscription() throws Exception {
final String subName = "test-sub";
final String persistentTopicName = "persistent://prop-xyz/ns1/test-sub-topic";

// disable auto subscription creation
pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);

// create a topic and produce some messages
publishMessagesOnPersistentTopic("persistent://prop-xyz/ns1/" + topicName, 5);
publishMessagesOnPersistentTopic(persistentTopicName, 5);
assertEquals(admin.topics().getList("prop-xyz/ns1"),
Lists.newArrayList("persistent://prop-xyz/ns1/" + topicName));
Lists.newArrayList(persistentTopicName));

// create the subscription by PulsarAdmin
admin.topics().createSubscription(topicName, subName, MessageId.earliest);
admin.topics().createSubscription(persistentTopicName, subName, MessageId.earliest);

assertEquals(admin.topics().getSubscriptions(persistentTopicName), Lists.newArrayList(subName));

Expand Down

0 comments on commit 07a222e

Please sign in to comment.