-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Fix IllegalStateException in PersistentReplicator #10098
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
Merged
sijie
merged 6 commits into
apache:master
from
lhotari:lh-fix-illegalstateexception-in-persistentreplicator
Apr 9, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f105f2d
Fix IllegalStateException in PersistentReplicator
lhotari 28faa8e
Add test for verifying subscription replication across regions
lhotari 297dfc5
Add review comment about unique name generation
lhotari 04ebba8
Address review comment
lhotari e4ac3a3
Fix logic once more
lhotari 2cb384b
Inverse the condition to clarify the logic
lhotari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/ReplicatorSubscriptionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /** | ||
| * 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.broker.service; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertFalse; | ||
| import com.google.common.collect.Sets; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeUnit; | ||
| import lombok.Cleanup; | ||
| import org.apache.pulsar.broker.BrokerTestUtil; | ||
| import org.apache.pulsar.client.api.Consumer; | ||
| import org.apache.pulsar.client.api.Message; | ||
| import org.apache.pulsar.client.api.MessageRoutingMode; | ||
| import org.apache.pulsar.client.api.Producer; | ||
| import org.apache.pulsar.client.api.PulsarClient; | ||
| import org.apache.pulsar.client.api.PulsarClientException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.testng.annotations.AfterClass; | ||
| import org.testng.annotations.BeforeClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| /** | ||
| * Tests replicated subscriptions (PIP-33) | ||
| */ | ||
| @Test(groups = "broker") | ||
| public class ReplicatorSubscriptionTest extends ReplicatorTestBase { | ||
| private static final Logger log = LoggerFactory.getLogger(ReplicatorSubscriptionTest.class); | ||
|
|
||
| @Override | ||
| @BeforeClass(timeOut = 300000) | ||
| public void setup() throws Exception { | ||
| super.setup(); | ||
| } | ||
|
|
||
| @Override | ||
| @AfterClass(alwaysRun = true, timeOut = 300000) | ||
| public void cleanup() throws Exception { | ||
| super.cleanup(); | ||
| } | ||
|
|
||
| /** | ||
| * Tests replicated subscriptions across two regions | ||
| */ | ||
| @Test | ||
| public void testReplicatedSubscriptionAcrossTwoRegions() throws Exception { | ||
| String namespace = BrokerTestUtil.newUniqueName("pulsar/replicatedsubscription"); | ||
| String topicName = "persistent://" + namespace + "/mytopic"; | ||
| String subscriptionName = "cluster-subscription"; | ||
| // Subscription replication produces duplicates, https://github.com/apache/pulsar/issues/10054 | ||
| // TODO: duplications shouldn't be allowed, change to "false" when fixing the issue | ||
| boolean allowDuplicates = true; | ||
| // this setting can be used to manually run the test with subscription replication disabled | ||
| // it shows that subscription replication has no impact in behavior for this test case | ||
| boolean replicateSubscriptionState = true; | ||
|
|
||
| admin1.namespaces().createNamespace(namespace); | ||
| admin1.namespaces().setNamespaceReplicationClusters(namespace, Sets.newHashSet("r1", "r2")); | ||
|
|
||
| @Cleanup | ||
| PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()) | ||
| .statsInterval(0, TimeUnit.SECONDS) | ||
| .build(); | ||
|
|
||
| // create subscription in r1 | ||
| createReplicatedSubscription(client1, topicName, subscriptionName, replicateSubscriptionState); | ||
|
|
||
| @Cleanup | ||
| PulsarClient client2 = PulsarClient.builder().serviceUrl(url2.toString()) | ||
| .statsInterval(0, TimeUnit.SECONDS) | ||
| .build(); | ||
|
|
||
| // create subscription in r2 | ||
| createReplicatedSubscription(client2, topicName, subscriptionName, replicateSubscriptionState); | ||
|
|
||
| Set<String> sentMessages = new LinkedHashSet<>(); | ||
|
|
||
| // send messages in r1 | ||
| { | ||
| @Cleanup | ||
| Producer<byte[]> producer = client1.newProducer().topic(topicName) | ||
| .enableBatching(false) | ||
| .messageRoutingMode(MessageRoutingMode.SinglePartition) | ||
| .create(); | ||
| int numMessages = 6; | ||
| for (int i = 0; i < numMessages; i++) { | ||
| String body = "message" + i; | ||
| producer.send(body.getBytes(StandardCharsets.UTF_8)); | ||
| sentMessages.add(body); | ||
| } | ||
| producer.close(); | ||
| } | ||
|
|
||
| Set<String> receivedMessages = new LinkedHashSet<>(); | ||
|
|
||
| // consume 3 messages in r1 | ||
| try (Consumer<byte[]> consumer1 = client1.newConsumer() | ||
| .topic(topicName) | ||
| .subscriptionName(subscriptionName) | ||
| .replicateSubscriptionState(replicateSubscriptionState) | ||
| .subscribe()) { | ||
| readMessages(consumer1, receivedMessages, 3, allowDuplicates); | ||
| } | ||
|
|
||
| // wait for subscription to be replicated | ||
| Thread.sleep(2 * config1.getReplicatedSubscriptionsSnapshotFrequencyMillis()); | ||
|
|
||
| // consume remaining messages in r2 | ||
| try (Consumer<byte[]> consumer2 = client2.newConsumer() | ||
| .topic(topicName) | ||
| .subscriptionName(subscriptionName) | ||
| .replicateSubscriptionState(replicateSubscriptionState) | ||
| .subscribe()) { | ||
| readMessages(consumer2, receivedMessages, -1, allowDuplicates); | ||
| } | ||
|
|
||
| // assert that all messages have been received | ||
| assertEquals(new ArrayList<>(sentMessages), new ArrayList<>(receivedMessages), "Sent and received " + | ||
| "messages don't match."); | ||
| } | ||
|
|
||
| void readMessages(Consumer<byte[]> consumer, Set<String> messages, int maxMessages, boolean allowDuplicates) | ||
| throws PulsarClientException { | ||
| int count = 0; | ||
| while (count < maxMessages || maxMessages == -1) { | ||
| Message<byte[]> message = consumer.receive(2, TimeUnit.SECONDS); | ||
| if (message != null) { | ||
| count++; | ||
| String body = new String(message.getValue(), StandardCharsets.UTF_8); | ||
| if (!allowDuplicates) { | ||
| assertFalse(messages.contains(body), "Duplicate message '" + body + "' detected."); | ||
| } | ||
| messages.add(body); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void createReplicatedSubscription(PulsarClient pulsarClient, String topicName, String subscriptionName, | ||
| boolean replicateSubscriptionState) | ||
| throws PulsarClientException { | ||
| pulsarClient.newConsumer().topic(topicName) | ||
| .subscriptionName(subscriptionName) | ||
| .replicateSubscriptionState(replicateSubscriptionState) | ||
| .subscribe() | ||
| .close(); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.