Skip to content

Comments

Don't recalculate num pending inline on consumer info#7758

Merged
neilalexander merged 2 commits intomainfrom
neil/cinumpending
Jan 23, 2026
Merged

Don't recalculate num pending inline on consumer info#7758
neilalexander merged 2 commits intomainfrom
neil/cinumpending

Conversation

@neilalexander
Copy link
Member

Recalculating num pending inline on consumer info can make them considerably more expensive than intended and can result in unexpectedly taking out the lock on the underlying store of the stream, creating contention with other resources.

We will now only make simple adjustments just to make sure that the numbers aren't completely unrealistic. Num pending calculations still take place when becoming leader or updating the subject filters in the consumer config.

Signed-off-by: Neil Twigg neil@nats.io

@neilalexander neilalexander marked this pull request as ready for review January 23, 2026 12:24
@neilalexander neilalexander requested a review from a team as a code owner January 23, 2026 12:24
Signed-off-by: Neil Twigg <neil@nats.io>
Signed-off-by: Maurice van Veen <github@mauricevanveen.com>
Copy link
Member

@MauriceVanVeen MauriceVanVeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if o.sseq > state.LastSeq {
o.npc = 0
} else if npc > 0 {
o.npc = int64(min(npc, state.Msgs, state.LastSeq-o.sseq+1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated from min(npc, state.Msgs) to min(npc, state.Msgs, state.LastSeq-o.sseq+1), which means:

  • our pending count can't be larger than the total amount of available messages
  • our pending count can't be larger than if every single message from our pointer to the end of the stream would be delivered

@neilalexander neilalexander merged commit 7a6162f into main Jan 23, 2026
48 checks passed
@neilalexander neilalexander deleted the neil/cinumpending branch January 23, 2026 14:49
wallyqs pushed a commit to wallyqs/nats-server that referenced this pull request Jan 26, 2026
…ithAccurateNumPending()

This test demonstrates that when npc underflows (is too low due to double-decrement race):
- info() returns wrong value (20) because checkNumPending() only caps, never recalculates
- infoWithAccurateNumPending() returns correct value (50) because it always calls streamNumPending()

Key insight: After PR nats-io#7758, checkNumPending() no longer triggers recalculation
when npc is below state.Msgs. This means underflow scenarios (npc too low) are
not detected/corrected by info(). For accurate monitoring, use infoWithAccurateNumPending().

https://claude.ai/code/session_011PV3TqBnngV6caLgFPdirH
neilalexander added a commit that referenced this pull request Jan 26, 2026
Includes the following:

- #7750
- #7752
- #7751
- #7753
- #7758
- #7759
- #7763

Signed-off-by: Neil Twigg <neil@nats.io>
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