You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@activemq.apache.org by "papinifrancesco (via GitHub)" <gi...@apache.org> on 2026/10/02 19:43:43 UTC
[PR] Implement recoverExpired() for the JDBC topic store (#2630) [activemq]
papinifrancesco opened a new pull request, #2634:
URL: https://github.com/apache/activemq/pull/2634
Link to #2630
### Problems
1. **Topic expiry loads the whole JDBC backlog into the heap.** `Topic`'s expiry task uses `TopicMessageStore.recoverExpired()` only for KahaDB. Every other store falls back to `doBrowse()`, which on JDBC runs `SELECT ID, MSG FROM ACTIVEMQ_MSGS WHERE CONTAINER=? ORDER BY ID` with no row limit. Drivers that read the whole result set up front (PostgreSQL by default) load the entire durable backlog every `expireMessagesPeriod`, until the broker runs out of memory, even when no message has a TTL. Reproduction and CI evidence: https://github.com/papinifrancesco/activemq-jdbc-topic-expiry-oom
2. **Expiring a message can drop earlier, non-expired messages of the same subscription.** The JDBC store records a durable subscription's acks as a high-water mark (`UPDATE ACTIVEMQ_ACKS SET LAST_ACKED_ID=?`). When a message with a TTL expires behind one without a TTL, the expiry task (or a browse) acks the expired message, `LAST_ACKED_ID` moves past the earlier one, and after a restart that earlier message is never delivered. KahaDB tracks acks per message and is not affected.
### Changes
- `JDBCTopicMessageStore.recoverExpired()` is implemented. Per subscription, `DefaultJDBCAdapter.doRecoverExpired()` runs two plain-SQL statements (new in `Statements`, overridable like the others):
1. per ack row: the last acked id, the id of the first pending message that is not expired or belongs to a prepared XA transaction, and the highest message id;
2. the expired messages between the last acked id and that id, with `setMaxRows(maxExpirePageSize)`.
So only the run of expired messages directly after `LAST_ACKED_ID` is returned (per priority with prioritized messages), and acking them can never ack a message that has not expired. Messages already loaded for another subscription are shared, and the listener's `hasSpace()` is honoured as in the KahaDB implementation.
- `Topic` uses `recoverExpired()` for JDBC as well as KahaDB. The `getMessageCount() == 0` shortcut stays KahaDB-only, since on JDBC it is a `COUNT` query on every run. The `recoverExpired()` part of the expiry task moves to `expireFromStore()`.
- `Topic.doBrowse()` (JMX browse, `StatisticsBroker`) no longer acks the expired messages it browses on JDBC; it expires through `expireFromStore()` instead.
### Behaviour change
On JDBC, expired messages queued behind a message that has not expired stay in the store until that message is consumed. They are still dropped at dispatch.
### Tests
- `JDBCRecoverExpiredTest` (H2): stopping at the first non-expired message, the page limit (messages shared between subscriptions), a subset of subscriptions, prioritized messages, a prepared XA transaction, the recovery listener.
- `JDBCDurableSubExpirationOrderTest` (H2): end-to-end with a broker restart, so the result comes from the database and not from a cursor in memory, for both the expiry task and a topic browse. On current `main` without this change, `testExpiredMessageDoesNotAckEarlierMessage` and `testBrowseDoesNotAckEarlierMessage` fail.
- `JDBCPersistenceAdapterExpiredMessageTest.testMaxExpirePageSize` now proxies `recoverExpired()`, which the expiry task calls instead of `recover()`.
- No regressions in the `store/jdbc` package and the topic expiry / durable subscription tests (`DurableSubscriptionOffline*Test`, `ActiveDurableSubscriptionBrowseExpireTest`, `AMQ6122Test`, `JdbcDurableSubDupTest`, `MessageExpirationTest`, `ExpiredMessages*Test`, `KahaDBRecoverExpiredTest`, ...). Four of those fail on my machine (Windows, 2 CPUs) with and without this change, so they are not caused by it: `ExpiredMessagesTest` (2), `JDBCXACommitExceptionTest.testNonTxEnqueueOverNetworkErrorsRestart`, `JmsSendReceiveWithMessageExpirationTest.testConsumeExpiredTopicDurable`.
- On PostgreSQL 17 (https://github.com/papinifrancesco/activemq-jdbc-topic-expiry-oom/actions/runs/37039551631): 6.3.2 and 6.2.10 with this change survive a 200,000 × 5 KB backlog of an offline durable subscriber that makes the unpatched brokers run out of memory. With a 30 s TTL on every message, the expiry task acks 400 messages per run with no out-of-memory error.
### Left out
A manual browse of a JDBC topic still reads the whole result set on PostgreSQL (`JDBCMessageStore.recover()`). Only an explicit JMX or statistics browse triggers it, never the periodic task.
### Disclosure
This change, its tests and the reproduction were written with an AI assistant (Claude Code, Anthropic). In line with the ASF guidance on generative tooling, the commits carry a `Co-Authored-By` trailer naming the tool.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Limit the topic browse query on the JDBC store (#2630) [activemq]
Posted by "mattrpav (via GitHub) via gitbox" <gi...@activemq.apache.org>.
mattrpav commented on code in PR #2634:
URL: https://github.com/apache/activemq/pull/2634#discussion_r4456045242
##########
activemq-broker/src/main/java/org/apache/activemq/store/MessageStore.java:
##########
@@ -138,6 +138,21 @@ public interface MessageStore extends Service {
*/
void recover(MessageRecoveryListener container) throws Exception;
+ /**
+ * Recover at most maxReturned messages to be delivered.
+ *
+ * The default implementation relies on {@link MessageRecoveryListener#hasSpace()} to
+ * stop the recovery. A store that would otherwise read every message before the
+ * listener can stop it should bound the read itself.
+ *
+ * @param listener
+ * @param maxReturned the maximum number of messages to recover
+ * @throws Exception
+ */
+ default void recover(MessageRecoveryListener listener, int maxReturned) throws Exception {
Review Comment:
We already have this method that should meet the need, right?
```
void recoverNextMessages(int maxReturned, MessageRecoveryListener listener) throws Exception;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Limit the topic browse query on the JDBC store (#2630) [activemq]
Posted by "papinifrancesco (via GitHub)" <gi...@apache.org>.
papinifrancesco commented on PR #2634:
URL: https://github.com/apache/activemq/pull/2634#issuecomment-6077127871
Thanks @mattrpav, agreed. This PR is now only that fix, one commit on current `main`:
- `MessageStore.recover(MessageRecoveryListener, int maxReturned)`: a new default method that calls `recover(listener)`, so KahaDB and the memory store run exactly the code they run today. The proxy stores delegate it.
- The JDBC store overrides it and applies the limit to the query (`setMaxRows`). Its listener already stops at the page size (AMQ-6067), so what a browse returns or expires doesn't change; pgjdbc just no longer loads the whole topic first.
- `Topic.doBrowse()` passes its page size. No store-type checks; this also covers a JMX browse.
On PostgreSQL, with this patch 6.3.2 and 6.2.10 survive the 200,000-message backlog from #2630 with a 1 GB heap, where unpatched they run out of memory ([CI run](https://github.com/papinifrancesco/activemq-jdbc-topic-expiry-oom/actions/runs/37823663429)). New H2 test: `JDBCTopicBrowseLimitTest`.
One question: you added `MessageRecoveryContext` (AMQ-9646). Would you rather the JDBC store implement `recoverMessages(MessageRecoveryContext)` and the browse go through it? A plain overload kept this change small, since JDBC has no implementation of the context's offset and start/end ids yet.
The rest is now #2640: on the JDBC store, expiry acks can lose pending messages or redeliver consumed ones, with tests for both. The previous version of this PR is linked there as one possible fix.
@cshannon: with this version, KahaDB's code path is unchanged.
**Disclosure:** as before, the change, the tests and the reproduction were written with an AI assistant (Claude Code, Anthropic). The test results come from local runs and the linked CI run.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Implement recoverExpired() for the JDBC topic store (#2630) [activemq]
Posted by "mattrpav (via GitHub)" <gi...@apache.org>.
mattrpav commented on code in PR #2634:
URL: https://github.com/apache/activemq/pull/2634#discussion_r4195588134
##########
activemq-broker/src/main/java/org/apache/activemq/broker/region/Topic.java:
##########
@@ -719,11 +719,14 @@ public Message[] browse() {
public void doBrowse(final List<Message> browseList, final int max) {
try {
if (topicStore != null) {
+ // a JDBC ack moves the last acked id of the subscription, so expiring a browsed
+ // message would also ack the non-expired messages before it
+ final boolean expireFromStore = topicStore.getType() == StoreType.JDBC;
Review Comment:
This breaks modularity. There shouldn't be a check for an underlying store type and a subsequent change in logic. If there is a problem with the JDBC ack'n with browsed messages, we should fix that in the JDBC layer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Implement recoverExpired() for the JDBC topic store (#2630) [activemq]
Posted by "mattrpav (via GitHub)" <gi...@apache.org>.
mattrpav commented on PR #2634:
URL: https://github.com/apache/activemq/pull/2634#issuecomment-6017006847
This is a significant change, and I'm wondering if there is a better way to update this (perhaps add new first-class methods to the store) vs branching logic based on the back-end store type.
Is there a way to address the first part-- add a limit query based on maxBrowsePageSize without dissecting the entire Topic expiry code flow? That would allow us to address the core issue quickly, and then spend some time on the design to optimize expiry processing as a follow-on.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Limit the topic browse query on the JDBC store (#2630) [activemq]
Posted by "papinifrancesco (via GitHub) via gitbox" <gi...@activemq.apache.org>.
papinifrancesco commented on PR #2634:
URL: https://github.com/apache/activemq/pull/2634#issuecomment-6615109584
Thanks @mattrpav. I tried it: `Topic.doBrowse()` calls `topicStore.resetBatching()` and then `topicStore.recoverNextMessages(max, listener)`, under a lock for the `doBrowse()` callers.
On JDBC it works. `doRecoverNextMessages()` already limits the query (`setMaxRows`, plus `LIMIT` on PostgreSQL), and nothing else uses the topic store's batch position.
On KahaDB it changes the browse. `recoverNextMessages(int, listener)` iterates the destination's default order-index cursor, and durable subscription recovery moves that same cursor ([`KahaDBStore` L1260-L1314](https://github.com/apache/activemq/blob/730b34d3af5574406e3924e72f4bd57ceda6eac0/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/KahaDBStore.java#L1260-L1314): `resetCursorPosition()`, `setBatch()`, `stoppedIterating()`). The two calls aren't atomic, so a dispatch to a durable subscriber between `resetBatching()` and `recoverNextMessages()` makes the browse start in the middle of the topic. Today's `recover(listener)` uses `iteratorIsolated()` and isn't affected.
A test that recovers 20 messages for a durable subscription right before the browse reads (50 messages, page size 10):
| | JDBC (H2) | KahaDB |
|---|---|---|
| `resetBatching()` + `recoverNextMessages()` | starts at message0 | starts at **message20** |
| this PR, `recover(listener, maxReturned)` | starts at message0 | starts at message0 |
It would also change an AMQ-6067 assertion: `JDBCPersistenceAdapterExpiredMessageTest` checks that `hasSpace()` is called during expiry, and JDBC's `recoverNextMessages()` doesn't call it (the row count is the limit instead).
So I'd keep a stateless bounded read. Two ways:
1. This PR as it is: `recover(listener, maxReturned)`, a default method that calls `recover(listener)`, overridden by the JDBC store only.
2. Your `recoverMessages(MessageRecoveryContext)` with the isolated cursor, which KahaDB already implements. `Topic.doBrowse()` would use it, and I'd add a JDBC implementation (head of the store, at most N rows). The memory store would need one too, or a default that falls back to `recover(listener)`.
Which do you prefer? Either way I can add the test above to the PR.
Disclosure: as with the rest of this PR, the trial, the test and the runs above were done with Claude Code (Anthropic's AI assistant).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact
Re: [PR] Limit the topic browse query on the JDBC store (#2630) [activemq]
Posted by "papinifrancesco (via GitHub) via gitbox" <gi...@activemq.apache.org>.
papinifrancesco commented on PR #2634:
URL: https://github.com/apache/activemq/pull/2634#issuecomment-6605696401
Hi @mattrpav, a gentle ping: this PR is now the minimal fix you suggested (a row limit on the JDBC browse query, no store-type branching). @cshannon, KahaDB keeps its current code path, as you asked. The CI run is waiting for approval. Could someone approve it and review the PR when there's time? If it's accepted, a backport to 6.3.x/6.4.x would help, since it missed 6.4.0 and 6.3.3.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@activemq.apache.org
For additional commands, e-mail: gitbox-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact