-
Notifications
You must be signed in to change notification settings - Fork 1.5k
WIP: AMQ-9855: VMTransport, Defensive copy of messages to prevent mutation #1659
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
Draft
pradeep85841
wants to merge
2
commits into
apache:main
Choose a base branch
from
pradeep85841:AMQ-9855-vmtransport-defensive-copy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+197
−5
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
187 changes: 187 additions & 0 deletions
187
...q-unit-tests/src/test/java/org/apache/activemq/command/ActiveMQTextMessageStressTest.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,187 @@ | ||
| /** | ||
| * 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.activemq.command; | ||
|
|
||
| import jakarta.jms.Connection; | ||
| import jakarta.jms.MessageConsumer; | ||
| import jakarta.jms.Session; | ||
| import jakarta.jms.Topic; | ||
| import jakarta.jms.MessageProducer; | ||
| import jakarta.jms.TextMessage; | ||
| import jakarta.jms.JMSException; | ||
|
|
||
| import org.apache.activemq.ActiveMQConnectionFactory; | ||
| import org.apache.activemq.broker.BrokerService; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.Future; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNotSame; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| public class ActiveMQTextMessageStressTest { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(ActiveMQTextMessageStressTest.class); | ||
| private BrokerService broker; | ||
| private Connection connection; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| broker = new BrokerService(); | ||
| broker.setPersistent(false); | ||
| broker.setUseJmx(false); | ||
| broker.addConnector("vm://localhost"); | ||
| broker.start(); | ||
|
|
||
| ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost"); | ||
| connection = cf.createConnection(); | ||
| connection.setClientID("HIGH_CONC_TEST"); | ||
| connection.start(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| if (connection != null) { | ||
| connection.close(); | ||
| } | ||
| if (broker != null) { | ||
| broker.stop(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testConcurrentProducersAndConsumers() throws Exception { | ||
| final int MESSAGE_COUNT = 50; | ||
| final int PRODUCERS = 2; | ||
| final int DURABLE_CONSUMERS = 2; | ||
| final int NON_DURABLE_CONSUMERS = 2; | ||
| final int TOTAL_CONSUMERS = DURABLE_CONSUMERS + NON_DURABLE_CONSUMERS; | ||
|
|
||
| Session tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
| Topic topic = tmpSession.createTopic("HIGH_CONC.TOPIC"); | ||
|
|
||
| List<MessageConsumer> consumers = new ArrayList<>(); | ||
| List<Session> consumerSessions = new ArrayList<>(); | ||
|
|
||
| for (int i = 1; i <= DURABLE_CONSUMERS; i++) { | ||
| Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
| consumers.add(s.createDurableSubscriber(topic, "Durable-" + i)); | ||
| consumerSessions.add(s); | ||
| } | ||
| for (int i = 1; i <= NON_DURABLE_CONSUMERS; i++) { | ||
| Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
| consumers.add(s.createConsumer(topic)); | ||
| consumerSessions.add(s); | ||
| } | ||
|
|
||
| ExecutorService executor = Executors.newFixedThreadPool(PRODUCERS + TOTAL_CONSUMERS); | ||
| CountDownLatch producerLatch = new CountDownLatch(PRODUCERS); | ||
|
|
||
| // Producers | ||
| for (int p = 1; p <= PRODUCERS; p++) { | ||
| final int producerId = p; | ||
| executor.submit(() -> { | ||
| try { | ||
| Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
| MessageProducer producer = s.createProducer(topic); | ||
| for (int m = 1; m <= MESSAGE_COUNT; m++) { | ||
| TextMessage msg = s.createTextMessage("P" + producerId + "-M" + m); | ||
| producer.send(msg); | ||
| } | ||
| s.close(); | ||
| } catch (JMSException e) { | ||
| LOG.error("Producer error", e); | ||
| } finally { | ||
| producerLatch.countDown(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Consumers | ||
| List<Future<List<TextMessage>>> consumerFutures = new ArrayList<>(); | ||
| for (MessageConsumer consumer : consumers) { | ||
| consumerFutures.add(executor.submit(() -> { | ||
| List<TextMessage> received = new ArrayList<>(); | ||
| try { | ||
| for (int i = 0; i < MESSAGE_COUNT * PRODUCERS; i++) { | ||
| TextMessage msg = (TextMessage) consumer.receive(10000); | ||
| assertNotNull("Consumer should receive a message", msg); | ||
|
|
||
| // Hammer the message to trigger race condition on unmarshalling | ||
| for (int j = 0; j < 10; j++) { | ||
| String txt = msg.getText(); | ||
| assertNotNull("Text should never be null during stress", txt); | ||
| // Clear state to force unmarshalling on the next call | ||
| ((ActiveMQTextMessage) msg).clearUnMarshalledState(); | ||
| } | ||
| received.add(msg); | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.error("Consumer error", e); | ||
| } | ||
| return received; | ||
| })); | ||
| } | ||
|
|
||
| producerLatch.await(30, TimeUnit.SECONDS); | ||
|
|
||
| List<List<TextMessage>> allConsumed = new ArrayList<>(); | ||
| for (Future<List<TextMessage>> f : consumerFutures) { | ||
| allConsumed.add(f.get(30, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| // Validate independent instances and data integrity | ||
| for (int i = 0; i < allConsumed.size(); i++) { | ||
| List<TextMessage> consumerMsgs = allConsumed.get(i); | ||
| assertEquals("Consumer " + i + " did not receive all messages", MESSAGE_COUNT * PRODUCERS, consumerMsgs.size()); | ||
|
|
||
| for (int j = i + 1; j < allConsumed.size(); j++) { | ||
| List<TextMessage> otherMsgs = allConsumed.get(j); | ||
|
|
||
| for (int k = 0; k < consumerMsgs.size(); k++) { | ||
| TextMessage m1 = consumerMsgs.get(k); | ||
| TextMessage m2 = otherMsgs.get(k); | ||
|
|
||
| assertNotSame("Message wrappers MUST be different instances across consumers", m1, m2); | ||
| assertEquals("Content must match", m1.getText(), m2.getText()); | ||
| assertNotNull("Content should not be null", m1.getText()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| executor.shutdown(); | ||
| if (!executor.awaitTermination(5, TimeUnit.SECONDS)) { | ||
| executor.shutdownNow(); | ||
| } | ||
|
|
||
| for (Session s : consumerSessions) { | ||
| s.close(); | ||
| } | ||
| tmpSession.close(); | ||
| } | ||
| } | ||
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.
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.
DURABLE_CONSUMERS is always 0, so this should either be changed or removed.