Skip to content

Commit

Permalink
[improve][broker][PIP-149]Make TruncateTopic pure async (apache#16464)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonHxy authored Jul 13, 2022
1 parent e6396bb commit cbcaf40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4923,73 +4923,37 @@ protected void handleTopicPolicyException(String methodName, Throwable thr, Asyn
resumeAsyncResponseExceptionally(asyncResponse, cause);
}

protected void internalTruncateNonPartitionedTopic(AsyncResponse asyncResponse, boolean authoritative) {
Topic topic;
try {
validateAdminAccessForTenant(topicName.getTenant());
validateTopicOwnership(topicName, authoritative);
topic = getTopicReference(topicName);
} catch (Exception e) {
log.error("[{}] Failed to truncate topic {}", clientAppId(), topicName, e);
resumeAsyncResponseExceptionally(asyncResponse, e);
return;
}
CompletableFuture<Void> future = topic.truncate();
future.thenAccept(a -> {
asyncResponse.resume(new RestException(Response.Status.NO_CONTENT.getStatusCode(),
Response.Status.NO_CONTENT.getReasonPhrase()));
}).exceptionally(e -> {
asyncResponse.resume(e);
return null;
});
protected CompletableFuture<Void> internalTruncateNonPartitionedTopicAsync(boolean authoritative) {
return validateAdminAccessForTenantAsync(topicName.getTenant())
.thenCompose(__ -> validateTopicOwnershipAsync(topicName, authoritative))
.thenCompose(__ -> getTopicReferenceAsync(topicName))
.thenCompose(Topic::truncate);
}

protected void internalTruncateTopic(AsyncResponse asyncResponse, boolean authoritative) {
protected CompletableFuture<Void> internalTruncateTopicAsync(boolean authoritative) {

// If the topic name is a partition name, no need to get partition topic metadata again
if (topicName.isPartitioned()) {
internalTruncateNonPartitionedTopic(asyncResponse, authoritative);
return internalTruncateNonPartitionedTopicAsync(authoritative);
} else {
getPartitionedTopicMetadataAsync(topicName, authoritative, false).whenComplete((meta, t) -> {
return getPartitionedTopicMetadataAsync(topicName, authoritative, false).thenCompose(meta -> {
if (meta.partitions > 0) {
final List<CompletableFuture<Void>> futures = Lists.newArrayList();
final List<CompletableFuture<Void>> futures = new ArrayList<>(meta.partitions);
for (int i = 0; i < meta.partitions; i++) {
TopicName topicNamePartition = topicName.getPartition(i);
try {
futures.add(pulsar().getAdminClient().topics()
futures.add(
pulsar().getAdminClient().topics()
.truncateAsync(topicNamePartition.toString()));
} catch (Exception e) {
log.error("[{}] Failed to truncate topic {}", clientAppId(), topicNamePartition, e);
asyncResponse.resume(new RestException(e));
return;
return FutureUtil.failedFuture(new RestException(e));
}
}
FutureUtil.waitForAll(futures).handle((result, exception) -> {
if (exception != null) {
Throwable th = exception.getCause();
if (th instanceof NotFoundException) {
asyncResponse.resume(new RestException(Status.NOT_FOUND, th.getMessage()));
} else if (th instanceof WebApplicationException) {
asyncResponse.resume(th);
} else {
log.error("[{}] Failed to truncate topic {}", clientAppId(), topicName, exception);
asyncResponse.resume(new RestException(exception));
}
} else {
asyncResponse.resume(Response.noContent().build());
}
return null;
});
return FutureUtil.waitForAll(futures);
} else {
internalTruncateNonPartitionedTopic(asyncResponse, authoritative);
return internalTruncateNonPartitionedTopicAsync(authoritative);
}
}).exceptionally(ex -> {
// If the exception is not redirect exception we need to log it.
if (!isRedirectException(ex)) {
log.error("[{}] Failed to truncate topic {}", clientAppId(), topicName, ex);
}
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.pulsar.broker.admin.impl.PersistentTopicsBase;
import org.apache.pulsar.broker.service.BrokerServiceException;
import org.apache.pulsar.broker.web.RestException;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.client.impl.ResetCursorData;
Expand Down Expand Up @@ -3937,8 +3938,19 @@ public void truncateTopic(
@ApiParam(value = "Whether leader broker redirected this call to this broker. For internal use.")
@QueryParam("authoritative") @DefaultValue("false") boolean authoritative){
validateTopicName(tenant, namespace, encodedTopic);
internalTruncateTopic(asyncResponse, authoritative);

internalTruncateTopicAsync(authoritative)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
Throwable t = FutureUtil.unwrapCompletionException(ex);
if (!isRedirectException(t)) {
log.error("[{}] Failed to truncate topic {}", clientAppId(), topicName, t);
}
if (t instanceof PulsarAdminException.NotFoundException) {
t = new RestException(Response.Status.NOT_FOUND, t.getMessage());
}
resumeAsyncResponseExceptionally(asyncResponse, t);
return null;
});
}

@POST
Expand Down

0 comments on commit cbcaf40

Please sign in to comment.