Skip to content

Commit

Permalink
[fix] [admin] Make response code to 400 instead of 500 when delete to…
Browse files Browse the repository at this point in the history
…pic fails due to enabled geo-replication (#19879)

Motivation: As expected, If geo-replication is enabled, a topic cannot be deleted. However deleting that topic returns a 500, and no further info.
Modifications: Make response code to 400 instead of 500 when delete topic fails due to enabled geo-replication
(cherry picked from commit a903733)
  • Loading branch information
poorbarcode committed Mar 22, 2023
1 parent 70b8617 commit db6a59b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Expand Up @@ -338,7 +338,10 @@ protected void internalDeleteTopicForcefully(boolean authoritative, boolean dele
try {
pulsar().getBrokerService().deleteTopic(topicName.toString(), true, deleteSchema).get();
} catch (Exception e) {
if (isManagedLedgerNotFoundException(e)) {
Throwable t = FutureUtil.unwrapCompletionException(e);
if (t instanceof IllegalStateException){
throw new RestException(422/* Unprocessable entity*/, t.getMessage());
} else if (isManagedLedgerNotFoundException(e)) {
log.info("[{}] Topic was already not existing {}", clientAppId(), topicName, e);
} else {
log.error("[{}] Failed to delete topic forcefully {}", clientAppId(), topicName, e);
Expand Down Expand Up @@ -1104,7 +1107,9 @@ protected void internalDeleteTopic(boolean authoritative, boolean deleteSchema)
} catch (Exception e) {
Throwable t = e.getCause();
log.error("[{}] Failed to delete topic {}", clientAppId(), topicName, t);
if (t instanceof TopicBusyException) {
if (t instanceof IllegalStateException){
throw new RestException(422/* Unprocessable entity*/, t.getMessage());
} else if (t instanceof TopicBusyException) {
throw new RestException(Status.PRECONDITION_FAILED, "Topic has active producers/subscriptions");
} else if (isManagedLedgerNotFoundException(e)) {
throw new RestException(Status.NOT_FOUND, "Topic not found");
Expand Down
Expand Up @@ -70,6 +70,7 @@
import org.apache.pulsar.broker.service.persistent.PersistentReplicator;
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
Expand Down Expand Up @@ -766,6 +767,33 @@ public void testReplicatorProducerClosing() throws Exception {
assertNull(producer);
}

@Test
public void testDeleteTopicFailure() throws Exception {
final String topicName = BrokerTestUtil.newUniqueName("persistent://pulsar/ns/tp_" + UUID.randomUUID());
admin1.topics().createNonPartitionedTopic(topicName);
try {
admin1.topics().delete(topicName);
fail("Delete topic should fail if enabled replicator");
} catch (Exception ex) {
assertTrue(ex instanceof PulsarAdminException);
assertEquals(((PulsarAdminException) ex).getStatusCode(), 422/* Unprocessable entity*/);
}
}

@Test
public void testDeletePartitionedTopicFailure() throws Exception {
final String topicName = BrokerTestUtil.newUniqueName("persistent://pulsar/ns/tp_" + UUID.randomUUID());
admin1.topics().createPartitionedTopic(topicName, 2);
admin1.topics().createSubscription(topicName, "sub1", MessageId.earliest);
try {
admin1.topics().deletePartitionedTopic(topicName);
fail("Delete topic should fail if enabled replicator");
} catch (Exception ex) {
assertTrue(ex instanceof PulsarAdminException);
assertEquals(((PulsarAdminException) ex).getStatusCode(), 422/* Unprocessable entity*/);
}
}

@Test(priority = 4, timeOut = 30000)
public void testReplicatorProducerName() throws Exception {
log.info("--- Starting ReplicatorTest::testReplicatorProducerName ---");
Expand Down

0 comments on commit db6a59b

Please sign in to comment.