-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix] Combination of autocreate + forced delete of partitioned topic with active consumer leaves topic metadata inconsistent. #17308
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |||||||||||||||||||||||||||||||||||||||||
| import io.netty.util.HashedWheelTimer; | ||||||||||||||||||||||||||||||||||||||||||
| import lombok.Cleanup; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import org.apache.pulsar.client.admin.PulsarAdminException; | ||||||||||||||||||||||||||||||||||||||||||
| import org.apache.pulsar.client.api.Producer; | ||||||||||||||||||||||||||||||||||||||||||
| import org.apache.pulsar.client.api.ProducerAccessMode; | ||||||||||||||||||||||||||||||||||||||||||
| import org.apache.pulsar.client.api.PulsarClient; | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -331,7 +332,12 @@ public void topicDeleted(String ignored, boolean partitioned) throws Exception { | |||||||||||||||||||||||||||||||||||||||||
| p1.send("msg-1"); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (partitioned) { | ||||||||||||||||||||||||||||||||||||||||||
| admin.topics().deletePartitionedTopic(topic, true); | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| admin.topics().deletePartitionedTopic(topic, true); | ||||||||||||||||||||||||||||||||||||||||||
| fail("expected error because partitioned topic has active producer"); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (PulsarAdminException.ServerSideErrorException e) { | ||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ServerSideError is not very user friendly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @eolivelli I return the same TopicBusyException as other cases when topic deletion is not possible. pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/PersistentTopic.java Lines 1129 to 1148 in 34c2262
|
||||||||||||||||||||||||||||||||||||||||||
| // expected | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| admin.topics().delete(topic, true); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.pulsar.tests.integration.topics; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.pulsar.client.api.Consumer; | ||
| import org.apache.pulsar.client.api.Message; | ||
| import org.apache.pulsar.client.api.Producer; | ||
| import org.apache.pulsar.client.api.PulsarClient; | ||
| import org.apache.pulsar.tests.integration.docker.ContainerExecException; | ||
| import org.apache.pulsar.tests.integration.docker.ContainerExecResult; | ||
| import org.apache.pulsar.tests.integration.suites.PulsarTestSuite; | ||
| import org.apache.pulsar.tests.integration.topologies.PulsarClusterSpec; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertNotEquals; | ||
| import static org.testng.Assert.fail; | ||
|
|
||
| /** | ||
| * Test cases for compaction. | ||
| */ | ||
| @Slf4j | ||
| public class TestTopicDeletion extends PulsarTestSuite { | ||
|
|
||
| final private boolean unload = false; | ||
| final private int numBrokers = 2; | ||
|
|
||
| public void setupCluster() throws Exception { | ||
| brokerEnvs.put("managedLedgerMaxEntriesPerLedger", "10"); | ||
| brokerEnvs.put("brokerDeleteInactivePartitionedTopicMetadataEnabled", "false"); | ||
| brokerEnvs.put("brokerDeleteInactiveTopicsEnabled", "false"); | ||
| this.setupCluster(""); | ||
| } | ||
|
|
||
| protected PulsarClusterSpec.PulsarClusterSpecBuilder beforeSetupCluster( | ||
| String clusterName, | ||
| PulsarClusterSpec.PulsarClusterSpecBuilder specBuilder) { | ||
| specBuilder.numBrokers(numBrokers); | ||
| specBuilder.enableContainerLog(true); | ||
| return specBuilder; | ||
| } | ||
|
|
||
| @Test(dataProvider = "ServiceUrls", timeOut=300_000) | ||
| public void testPartitionedTopicForceDeletion(Supplier<String> serviceUrl) throws Exception { | ||
|
|
||
| log.info("Creating tenant and namespace"); | ||
|
|
||
| final String tenant = "test-partitioned-topic-" + randomName(4); | ||
| final String namespace = tenant + "/ns1"; | ||
| final String topic = "persistent://" + namespace + "/partitioned-topic"; | ||
| final int numPartitions = numBrokers * 3; | ||
| final int numKeys = numPartitions * 50; | ||
| final String subscriptionName = "sub1"; | ||
|
|
||
| this.createTenantName(tenant, pulsarCluster.getClusterName(), "admin"); | ||
|
|
||
| this.createNamespace(namespace); | ||
|
|
||
| pulsarCluster.runAdminCommandOnAnyBroker("namespaces", | ||
| "set-clusters", "--clusters", pulsarCluster.getClusterName(), namespace); | ||
|
|
||
| pulsarCluster.runAdminCommandOnAnyBroker("namespaces", | ||
| "set-retention", "--size", "100M", "--time", "100m", namespace); | ||
|
|
||
| this.createPartitionedTopic(topic, numPartitions); | ||
|
|
||
| try (PulsarClient client = PulsarClient.builder().serviceUrl(serviceUrl.get()).build()) { | ||
|
|
||
| log.info("Creating consumer"); | ||
| Consumer<byte[]> consumer = client.newConsumer() | ||
| .topic(topic) | ||
| .subscriptionName(subscriptionName) | ||
| .subscribe(); | ||
|
|
||
| log.info("Producing messages"); | ||
| try(Producer<byte[]> producer = client.newProducer() | ||
| .topic(topic) | ||
| .create() | ||
| ) { | ||
| for (int i = 0; i < numKeys; i++) { | ||
| producer.newMessage() | ||
| .key("" + i) | ||
| .value(("value-" + i).getBytes(UTF_8)) | ||
| .sendAsync(); | ||
| } | ||
| producer.flush(); | ||
| log.info("Successfully wrote {} values", numKeys); | ||
| } | ||
|
|
||
| log.info("Consuming half of the messages"); | ||
| for (int i = 0; i < numKeys / 2; i++) { | ||
| Message<byte[]> m = consumer.receive(1, TimeUnit.MINUTES); | ||
| log.info("Read value {}", m.getKey()); | ||
| } | ||
|
|
||
| if (unload) { | ||
| log.info("Unloading topic"); | ||
| pulsarCluster.runAdminCommandOnAnyBroker("topics", | ||
| "unload", topic); | ||
| } | ||
|
|
||
| ContainerExecResult res; | ||
| log.info("Deleting the topic"); | ||
| try { | ||
| res = pulsarCluster.runAdminCommandOnAnyBroker("topics", | ||
| "delete-partitioned-topic", "--force", topic); | ||
| assertNotEquals(0, res.getExitCode()); | ||
| } catch (ContainerExecException e) { | ||
| log.info("Second delete failed with ContainerExecException, could be ok", e); | ||
| if (!e.getMessage().contains("with error code 1")) { | ||
| fail("Expected different error code"); | ||
| } | ||
| } | ||
|
|
||
| log.info("Close the consumer and delete the topic again"); | ||
| consumer.close(); | ||
|
|
||
| res = pulsarCluster.runAdminCommandOnAnyBroker("topics", | ||
| "delete-partitioned-topic", "--force", topic); | ||
| assertNotEquals(0, res.getExitCode()); | ||
|
|
||
| Thread.sleep(5000); | ||
| // should succeed | ||
| log.info("Creating the topic again"); | ||
| this.createPartitionedTopic(topic, numBrokers * 2); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private ContainerExecResult createTenantName(final String tenantName, | ||
| final String allowedClusterName, | ||
| final String adminRoleName) throws Exception { | ||
| ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker( | ||
| "tenants", "create", "--allowed-clusters", allowedClusterName, | ||
| "--admin-roles", adminRoleName, tenantName); | ||
| assertEquals(0, result.getExitCode()); | ||
| return result; | ||
| } | ||
|
|
||
| private ContainerExecResult createNamespace(final String Ns) throws Exception { | ||
| ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker( | ||
| "namespaces", | ||
| "create", | ||
| "--clusters", | ||
| pulsarCluster.getClusterName(), Ns); | ||
| assertEquals(0, result.getExitCode()); | ||
| return result; | ||
| } | ||
|
|
||
| private ContainerExecResult createPartitionedTopic(final String partitionedTopicName, int numPartitions) | ||
| throws Exception { | ||
| ContainerExecResult result = pulsarCluster.runAdminCommandOnAnyBroker( | ||
| "topics", | ||
| "create-partitioned-topic", | ||
| "--partitions", "" + numPartitions, | ||
| partitionedTopicName); | ||
| assertEquals(0, result.getExitCode()); | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR can not solve every scenario:
The cmd delete topic is executed between these two instructions:
consumer lookupandconsumer subscribe, even if the partitioned topic is deleted successfully, but the client already has the topic-meta(which has been deleted), then the consumer subscribes with the topic name "topic-partition-x". You can reproduce like this: