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][admin] Fix half deletion when attempt to topic with a incorrect API #23002

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix half delete
  • Loading branch information
poorbarcode committed Jul 4, 2024
commit a4b4fc4a933d8df2fef1b69630c65cc8434b8bdd
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,17 @@ protected void internalDeletePartitionedTopic(AsyncResponse asyncResponse,
.thenCompose(partitionedMeta -> {
final int numPartitions = partitionedMeta.partitions;
if (numPartitions < 1) {
return CompletableFuture.completedFuture(null);
return pulsar().getNamespaceService().checkNonPartitionedTopicExists(topicName)
.thenApply(exists -> {
if (exists) {
throw new RestException(Response.Status.CONFLICT,
String.format("%s is a non-partitioned topic. Instead of calling"
+ " delete-partitioned-topic please call delete.", topicName));
} else {
throw new RestException(Status.NOT_FOUND,
String.format("Topic %s not found.", topicName));
}
});
}
return internalRemovePartitionsAuthenticationPoliciesAsync()
.thenCompose(unused -> internalRemovePartitionsTopicAsync(numPartitions, force));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1169,47 +1169,37 @@ public void deleteTopic(
@ApiParam(value = "Whether leader broker redirected this call to this broker. For internal use.")
@QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
validateTopicName(tenant, namespace, encodedTopic);
internalDeleteTopicAsync(authoritative, force)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
final Throwable t = FutureUtil.unwrapCompletionException(ex);
Consumer<Throwable> exHandler = error -> {
Throwable restException = error;
if (!force && (error instanceof BrokerServiceException.TopicBusyException)) {
restException = new RestException(Response.Status.PRECONDITION_FAILED,
error.getMessage());

getPulsarResources().getNamespaceResources().getPartitionedTopicResources()
.partitionedTopicExistsAsync(topicName).thenAccept(exists -> {
if (exists) {
RestException restException = new RestException(Response.Status.CONFLICT,
String.format("%s is a partitioned topic, please call delete-partitioned-topic"
+ " instead.", topicName));
resumeAsyncResponseExceptionally(asyncResponse, restException);
return;
}
internalDeleteTopicAsync(authoritative, force)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
Throwable t = FutureUtil.unwrapCompletionException(ex);
if (!force && (t instanceof BrokerServiceException.TopicBusyException)) {
ex = new RestException(Response.Status.PRECONDITION_FAILED,
t.getMessage());
}
if (error instanceof IllegalStateException){
restException = new RestException(422/* Unprocessable entity*/, error.getMessage());
} else if (isManagedLedgerNotFoundException(error)) {
restException = new RestException(Response.Status.NOT_FOUND,
if (t instanceof IllegalStateException){
ex = new RestException(422/* Unprocessable entity*/, t.getMessage());
} else if (isManagedLedgerNotFoundException(t)) {
ex = new RestException(Response.Status.NOT_FOUND,
getTopicNotFoundErrorMessage(topicName.toString()));
} else if (isNot307And404Exception(error)) {
log.error("[{}] Failed to delete topic {}", clientAppId(), topicName, error);
} else if (isNot307And404Exception(ex)) {
log.error("[{}] Failed to delete topic {}", clientAppId(), topicName, t);
}
resumeAsyncResponseExceptionally(asyncResponse, restException);
};
if (t instanceof ManagedLedgerException.MetadataNotFoundException) {
pulsar().getPulsarResources().getNamespaceResources().getPartitionedTopicResources()
.partitionedTopicExistsAsync(topicName).thenApply(exists -> {
if (exists) {
RestException restException = new RestException(Response.Status.CONFLICT,
String.format("%s is a partitioned topic, please call delete-partitioned-topic"
+ " instead.", topicName));
resumeAsyncResponseExceptionally(asyncResponse, restException);
} else {
exHandler.accept(t);
}
return null;
}).exceptionally(exIgnore -> {
exHandler.accept(t);
return null;
});
} else {
exHandler.accept(t);
}
return null;
});
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
});

}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.naming.TopicDomain;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.apache.pulsar.common.policies.data.stats.NonPersistentTopicStatsImpl;
import org.apache.pulsar.common.policies.data.stats.TopicStatsImpl;
import org.apache.pulsar.common.util.FutureUtil;
import org.awaitility.Awaitility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
Expand All @@ -74,17 +76,59 @@ protected void cleanup() throws Exception {
}

@Test
public void testAttemptDeleteNonPartitionedForPartitioned() throws Exception {
final String topic = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
admin.topics().createPartitionedTopic(topic, 2);
public void testDeleteNonExistTopic() throws Exception {
// Case 1: call delete for a partitioned topic.
final String topic1 = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
admin.topics().createPartitionedTopic(topic1, 2);
admin.schemas().createSchemaAsync(topic1, Schema.STRING.getSchemaInfo());
Awaitility.await().untilAsserted(() -> {
assertEquals(admin.schemas().getAllSchemas(topic1).size(), 1);
});
try {
admin.topics().delete(topic);
admin.topics().delete(topic1);
fail("expected a 409 error");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("please call delete-partitioned-topic instead"));
}
Awaitility.await().untilAsserted(() -> {
assertEquals(admin.schemas().getAllSchemas(topic1).size(), 1);
});
// cleanup.
admin.topics().deletePartitionedTopic(topic, false);
admin.topics().deletePartitionedTopic(topic1, false);

// Case 2: call delete-partitioned-topi for a non-partitioned topic.
final String topic2 = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
admin.topics().createNonPartitionedTopic(topic2);
admin.schemas().createSchemaAsync(topic2, Schema.STRING.getSchemaInfo());
Awaitility.await().untilAsserted(() -> {
assertEquals(admin.schemas().getAllSchemas(topic2).size(), 1);
});
try {
admin.topics().deletePartitionedTopic(topic2);
fail("expected a 409 error");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("Instead of calling delete-partitioned-topic please call delete"));
}
Awaitility.await().untilAsserted(() -> {
assertEquals(admin.schemas().getAllSchemas(topic2).size(), 1);
});
// cleanup.
admin.topics().delete(topic2, false);

// Case 3: delete topic does not exist.
final String topic3 = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
try {
admin.topics().delete(topic3);
fail("expected a 404 error");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("not found"));
}
try {
admin.topics().deletePartitionedTopic(topic3);
fail("expected a 404 error");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("not found"));
}
}

@Test
Expand Down
Loading