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

[reproduce] Consumer crash after some partitions were deleted #91

Closed
wants to merge 1 commit into from
Closed
Changes from all 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 @@ -18,23 +18,29 @@
*/
package org.apache.pulsar.broker.service;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.ConsumerBuilder;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.api.TopicMessageId;
import org.apache.pulsar.common.policies.data.InactiveTopicDeleteMode;
import org.awaitility.Awaitility;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Slf4j
Expand Down Expand Up @@ -109,4 +115,96 @@
producer1.close();
admin.topics().deletePartitionedTopic(topic);
}

private enum SubscribeTopicType {
MULTI_PARTITIONED_TOPIC,
REGEX_TOPIC;
}

@DataProvider(name = "subscribeTopicTypes")
public Object[][] subTopicTypes() {
return new Object[][]{
//{SubscribeTopicType.MULTI_PARTITIONED_TOPIC},
{SubscribeTopicType.REGEX_TOPIC}
};
}

private void setSubscribeTopic(ConsumerBuilder consumerBuilder, SubscribeTopicType subscribeTopicType,
String topicName, String topicPattern) {
if (subscribeTopicType.equals(SubscribeTopicType.MULTI_PARTITIONED_TOPIC)) {
consumerBuilder.topic(topicName);
} else {
consumerBuilder.topicsPattern(Pattern.compile(topicPattern));
}
}

@Test(dataProvider = "subscribeTopicTypes")
public void testConsumerAfterOnePartDeleted(SubscribeTopicType subscribeTopicType) throws Exception {
final String topic = BrokerTestUtil.newUniqueName("persistent://public/default/tp");
final String topicPattern = "persistent://public/default/tp.*";
final String partition0 = topic + "-partition-0";
final String partition1 = topic + "-partition-1";
final String subscription = "s1";
admin.topics().createPartitionedTopic(topic, 2);
admin.topics().createSubscription(topic, subscription, MessageId.earliest);

// create consumers and producers.
Producer<String> producer0 = pulsarClient.newProducer(Schema.STRING).topic(partition0)
.enableBatching(false).create();
Producer<String> producer1 = pulsarClient.newProducer(Schema.STRING).topic(partition1)
.enableBatching(false).create();
ConsumerBuilder<String> consumerBuilder1 = pulsarClient.newConsumer(Schema.STRING)
.subscriptionName(subscription)
.isAckReceiptEnabled(true)
.subscriptionType(SubscriptionType.Shared);
setSubscribeTopic(consumerBuilder1, subscribeTopicType, topic, topicPattern);
Consumer<String> consumer1 = consumerBuilder1.subscribe();

// Make consume all messages for one topic, do not consume any messages for another one.
producer0.send("partition-0-1");
producer1.send("partition-1-1");
producer1.send("partition-1-2");
producer1.send("partition-1-4");
admin.topics().skipAllMessages(partition0, subscription);

// Wait for topic GC.
// Partition 0 will be deleted about 20s later, left 2min to avoid flaky.
producer0.close();
Awaitility.await().atMost(2, TimeUnit.MINUTES).untilAsserted(() -> {
CompletableFuture<Optional<Topic>> tp1 = pulsar.getBrokerService().getTopic(partition0, false);
CompletableFuture<Optional<Topic>> tp2 = pulsar.getBrokerService().getTopic(partition1, false);
assertTrue(tp1 == null || !tp1.get().isPresent());
assertTrue(tp2 != null && tp2.get().isPresent());
});

// Verify that the messages under "partition-1" still can be ack.
for (int i = 0; i < 2; i++) {
Message msg = consumer1.receive(2, TimeUnit.SECONDS);
assertNotNull(msg, "Expected at least received 3 messages.");
TopicMessageId messageId = (TopicMessageId) msg.getMessageId();
if (messageId.getOwnerTopic().equals(partition1)) {
consumer1.acknowledgeAsync(msg);
}
}
consumer1.close();

// Verify that the consumer subscribed with partitioned topic can be created successful.
ConsumerBuilder<String> consumerBuilder2 = pulsarClient.newConsumer(Schema.STRING)
.subscriptionName(subscription)
.isAckReceiptEnabled(true)
.subscriptionType(SubscriptionType.Shared);
setSubscribeTopic(consumerBuilder2, subscribeTopicType, topic, topicPattern);
Consumer<String> consumer2 = consumerBuilder2.subscribe();

Check failure on line 197 in pulsar-broker/src/test/java/org/apache/pulsar/broker/service/TopicGCTest.java

View workflow job for this annotation

GitHub Actions / CI - Unit - Brokers - Broker Group 1

TopicGCTest.testConsumerAfterOnePartDeleted

{"errorMsg":"Topic persistent://public/default/tp-a278ebde-9fcc-4aee-b533-1368ebc9a83b-partition-0 does not exist","reqId":4187780034002731907, "remote":"localhost/127.0.0.1:44195", "local":"/127.0.0.1:35094"}
producer1.send("partition-1-5");
Message<String> msg = consumer2.receive(2, TimeUnit.SECONDS);
String receivedMsgValue = msg.getValue();
log.info("received msg: {}", receivedMsgValue);
consumer2.acknowledge(msg);

// cleanup.
consumer2.close();
producer0.close();
producer1.close();
admin.topics().deletePartitionedTopic(topic);
}
}
Loading