Skip to content

ARTEMIS-6154 Broker should never report a negative message count - #6958

Open
Komalvighe wants to merge 1 commit into
apache:mainfrom
Komalvighe:ARTEMIS-6154-negative-message-count
Open

Komalvighe wants to merge 1 commit into
apache:mainfrom
Komalvighe:ARTEMIS-6154-negative-message-count

Conversation

@Komalvighe

Copy link
Copy Markdown

https://issues.apache.org/jira/browse/ARTEMIS-6154

Problem

QueueImpl#getMessageCount() sums four independently maintained metrics. On a
paged queue the last term is PageSubscriptionImpl#getMessageCount(), which
returns the persisted PageSubscriptionCounter value. That counter drifts below
zero when a message is accounted as removed more than once -- the reporter
reproduced this by purging a queue while a consumer was acknowledging, so
messagesAcked + messagesKilled exceeded messagesAdded.

Nothing floors the result, so the negative value is reported verbatim by:

  • QueueControlImpl#getMessageCount() (the JMX messageCount attribute)
  • QueueView / listQueues, which is what the web console renders

Change

  • PageSubscriptionImpl#getMessageCount() floors at zero, matching the guard
    getPersistentSize() directly below it already has.
  • QueueImpl#getMessageCount() / #getDurableMessageCount() floor the
    aggregate, covering drift in the non-paged metrics as well.
  • LastValueQueue#getMessageCount() floors too -- it subtracts the delivering
    count after super, so it can still go negative on its own.
  • Queue#getMessageCount() javadoc now states the value is never negative.

Clamping at the subscription as well as the aggregate keeps a partially-valid
count accurate: with pending=50 and counter=-100 the result is 50, not 0.
The raw computed value is still emitted by the existing debug log in
QueueImpl#getMessageCount, so the underlying drift remains diagnosable.

Scope

This is a defensive fix for the reported symptom, not a fix for the counter
drift itself. One related symptom is also not fixed here: the reporter notes
the messages become invisible in the console, which comes from
QueueControlImpl#browse() bounding its iteration with
Math.min(page * pageSize, queue.getMessageCount()). With a broken counter that
bound is wrong either way. Happy to follow up on that under a separate JIRA if
reviewers agree it belongs there.

Testing

NegativeMessageCountTest drives the page subscription counter negative on a
paging queue and asserts the reported counts are zero.

  Queue message counts are aggregated from several independently
  maintained metrics. When a message is accounted as removed more than
  once -- for example a queue purge racing with acknowledgements on a
  paged queue -- the page subscription counter drifts below zero and the
  negative value is surfaced verbatim through the JMX messageCount
  attribute and through listQueues in the web console.

  Floor the page subscription count and the aggregated queue counts at
  zero. The raw values are still logged at debug level in
  QueueImpl#getMessageCount so the underlying drift stays diagnosable.

  This is a defensive fix for the reported symptom; it does not address
  the root cause of the counter drift.

Komal Vighe
@GChuf

GChuf commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

I'm worried that this is just masking the actual issues with paging and/or counters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants