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 2 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 @@ -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"
poorbarcode marked this conversation as resolved.
Show resolved Hide resolved
+ " 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 @@ -29,6 +29,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.Encoded;
Expand All @@ -44,6 +45,7 @@
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.bookkeeper.mledger.ManagedLedgerException;
import org.apache.bookkeeper.mledger.Position;
import org.apache.bookkeeper.mledger.PositionFactory;
import org.apache.pulsar.broker.admin.AdminResource;
Expand Down Expand Up @@ -1167,7 +1169,17 @@ 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)

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"
poorbarcode marked this conversation as resolved.
Show resolved Hide resolved
+ " instead.", topicName));
resumeAsyncResponseExceptionally(asyncResponse, restException);
return;
}
internalDeleteTopicAsync(authoritative, force)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
Throwable t = FutureUtil.unwrapCompletionException(ex);
Expand All @@ -1186,6 +1198,8 @@ public void deleteTopic(
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
});

}

@GET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertThrows;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -32,6 +33,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import lombok.Cleanup;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
Expand All @@ -40,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 @@ -71,6 +75,62 @@ protected void cleanup() throws Exception {
super.internalCleanup();
}

@Test
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(topic1);
nodece marked this conversation as resolved.
Show resolved Hide resolved
fail("expected a 409 error");
} catch (Exception ex) {
assertTrue(ex.getMessage().contains("please call delete-partitioned-topic instead"));
}
Awaitility.await().untilAsserted(() -> {
nodece marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(admin.schemas().getAllSchemas(topic1).size(), 1);
});
// cleanup.
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
public void testPeekMessages() throws Exception {
@Cleanup
Expand Down
Loading