-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[#2198] Fix pending sends count on rolled back send #2199
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
Open
mattrpav
wants to merge
2
commits into
apache:main
Choose a base branch
from
mattrpav:amq-gh-2198-pending-sends-count
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.
+185
−1
Open
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
183 changes: 183 additions & 0 deletions
183
activemq-unit-tests/src/test/java/org/apache/activemq/bugs/PendingSendsRollbackLeakTest.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,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.activemq.bugs; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.file.Files; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import jakarta.jms.Connection; | ||
| import jakarta.jms.DeliveryMode; | ||
| import jakarta.jms.Session; | ||
|
|
||
| import org.apache.activemq.ActiveMQConnectionFactory; | ||
| import org.apache.activemq.broker.BrokerService; | ||
| import org.apache.activemq.broker.region.Queue; | ||
| import org.apache.activemq.command.ActiveMQQueue; | ||
| import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter; | ||
| import org.apache.activemq.test.annotations.ParallelTest; | ||
| import org.apache.activemq.util.IOHelper; | ||
| import org.apache.activemq.util.Wait; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.experimental.categories.Category; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Reproduces the pendingSends counter leak on transaction rollback. | ||
| * | ||
| * Queue.doMessageSend() increments the pendingSends counter for every send. | ||
| * On commit, CursorAddSync.afterCommit() -> messageSent() decrements it. | ||
| * On rollback, CursorAddSync.afterRollback() must also decrement it — | ||
| * without that, every rolled-back transactional send permanently inflates | ||
| * pendingSends. | ||
| * | ||
| * The leaked counter makes Queue.singlePendingSend() return false forever, | ||
| * which disables cursor caching via QueueStorePrefetch.canEnableCash(). | ||
| * The queue is then stuck in store page-in mode, degrading throughput and | ||
| * amplifying duplicateFromStore events. | ||
| */ | ||
| @Category(ParallelTest.class) | ||
| public class PendingSendsRollbackLeakTest { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(PendingSendsRollbackLeakTest.class); | ||
| private static final String QUEUE_NAME = "TEST.PENDING.SENDS.ROLLBACK"; | ||
| private static final int ROLLBACK_SEND_COUNT = 5; | ||
|
|
||
| private BrokerService broker; | ||
| private Connection connection; | ||
| private File dataDir; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| var baseDir = new File(IOHelper.getDefaultDataDirectory()); | ||
| Files.createDirectories(baseDir.toPath()); | ||
| dataDir = Files.createTempDirectory(baseDir.toPath(), "PendingSendsRollback-").toFile(); | ||
| dataDir.deleteOnExit(); | ||
|
|
||
| broker = new BrokerService(); | ||
| broker.setDataDirectoryFile(dataDir); | ||
| broker.setUseJmx(false); | ||
| broker.setDeleteAllMessagesOnStartup(true); | ||
| broker.getSystemUsage().getMemoryUsage().setLimit(64 * 1024 * 1024); | ||
|
|
||
| var pa = new KahaDBPersistenceAdapter(); | ||
| pa.setDirectory(new File(dataDir, "kahadb")); | ||
| broker.setPersistenceAdapter(pa); | ||
|
|
||
| broker.addConnector("tcp://localhost:0"); | ||
| broker.start(); | ||
| broker.waitUntilStarted(); | ||
|
|
||
| var factory = new ActiveMQConnectionFactory( | ||
| broker.getTransportConnectors().get(0).getConnectUri()); | ||
| connection = factory.createConnection(); | ||
| connection.start(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| if (connection != null) { | ||
| connection.close(); | ||
| } | ||
| if (broker != null) { | ||
| broker.deleteAllMessages(); | ||
| broker.stop(); | ||
| broker.waitUntilStopped(); | ||
| } | ||
| } | ||
|
|
||
| @Test(timeout = 60_000) | ||
| public void testRollbackDecrementsPendingSends() throws Exception { | ||
| var dest = new ActiveMQQueue(QUEUE_NAME); | ||
| var queue = (Queue) broker.getDestination(dest); | ||
|
|
||
| assertEquals("pendingSends should start at 0", 0, getPendingSends(queue)); | ||
| assertTrue("singlePendingSend should be true initially", queue.singlePendingSend()); | ||
|
|
||
| // Send persistent messages in a transaction, then roll back | ||
| try(var txSession = connection.createSession(true, Session.SESSION_TRANSACTED); | ||
| var producer = txSession.createProducer(dest)) { | ||
|
|
||
| producer.setDeliveryMode(DeliveryMode.PERSISTENT); | ||
|
|
||
| for (int i = 0; i < ROLLBACK_SEND_COUNT; i++) { | ||
| producer.send(txSession.createTextMessage("rollback-msg-" + i)); | ||
| } | ||
|
|
||
| txSession.rollback(); | ||
| } | ||
|
|
||
| // Rollback processing is synchronous with the rollback() call, but | ||
| // allow a grace period in case any async completion is in flight. | ||
| Wait.waitFor(() -> getPendingSends(queue) == 0, 5000, 10); | ||
|
|
||
| int leaked = getPendingSends(queue); | ||
| LOG.info("After rollback of {} sends: pendingSends={}, singlePendingSend={}", | ||
| ROLLBACK_SEND_COUNT, leaked, queue.singlePendingSend()); | ||
|
|
||
| assertEquals("PENDING SENDS LEAK: pendingSends should return to 0 after " + | ||
| "transaction rollback, but " + leaked + " send(s) leaked. " + | ||
| "CursorAddSync.afterRollback() must decrement pendingSends the same " + | ||
| "way afterCommit() -> messageSent() does. A leaked counter disables " + | ||
| "cursor caching (singlePendingSend() false forever).", | ||
| 0, getPendingSends(queue)); | ||
| assertTrue("singlePendingSend should recover after rollback", queue.singlePendingSend()); | ||
|
|
||
| assertEquals("message count should be 0 after rollback", | ||
| 0, queue.getDestinationStatistics().getMessages().getCount()); | ||
| } | ||
|
|
||
| @Test(timeout = 60_000) | ||
| public void testCommitKeepsPendingSendsBalanced() throws Exception { | ||
| var dest = new ActiveMQQueue(QUEUE_NAME + ".COMMIT"); | ||
| var queue = (Queue) broker.getDestination(dest); | ||
|
|
||
| try(var txSession = connection.createSession(true, Session.SESSION_TRANSACTED); | ||
| var producer = txSession.createProducer(dest)) { | ||
|
|
||
| producer.setDeliveryMode(DeliveryMode.PERSISTENT); | ||
|
|
||
| for (int i = 0; i < ROLLBACK_SEND_COUNT; i++) { | ||
| producer.send(txSession.createTextMessage("commit-msg-" + i)); | ||
| } | ||
|
|
||
| txSession.commit(); | ||
| } | ||
|
|
||
| assertTrue("pendingSends should return to 0 after commit", | ||
| Wait.waitFor(() -> getPendingSends(queue) == 0, 5000, 100)); | ||
| assertTrue("Queue should have " + ROLLBACK_SEND_COUNT + " messages after commit", | ||
| Wait.waitFor(() -> queue.getDestinationStatistics().getMessages().getCount() == ROLLBACK_SEND_COUNT, | ||
| 5000, 100)); | ||
| } | ||
|
|
||
| private int getPendingSends(Queue queue) { | ||
| try { | ||
| var f = Queue.class.getDeclaredField("pendingSends"); | ||
|
Member
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. nit: instead of reflection, we could have added an accessor with |
||
| f.setAccessible(true); | ||
| return ((AtomicInteger) f.get(queue)).get(); | ||
| } catch (ReflectiveOperationException e) { | ||
| throw new IllegalStateException("Unable to read Queue.pendingSends", e); | ||
| } | ||
| } | ||
| } | ||
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.