Skip to content

refactor: throw exception in registerConsumer + lock-free StoreStatsService - #10815

Open
juincen wants to merge 2 commits into
apache:developfrom
juincen:develop
Open

refactor: throw exception in registerConsumer + lock-free StoreStatsService#10815
juincen wants to merge 2 commits into
apache:developfrom
juincen:develop

Conversation

@juincen

@juincen juincen commented Aug 5, 2026

Copy link
Copy Markdown

Changes

1. registerConsumer: throw MQClientException instead of returning boolean

Move the duplicate-group exception throwing from callers into MQClientInstance.registerConsumer() itself, removing redundant if (!registerOK) throw checks in all three consumer implementations.

  • MQClientInstance.java - throw exception instead of returning false
  • DefaultMQPushConsumerImpl.java - remove redundant check
  • DefaultMQPullConsumerImpl.java - remove redundant check
  • DefaultLitePullConsumerImpl.java - remove redundant check

2. StoreStatsService: lock-free AtomicLong for max tracking

Replace volatile long + ReentrantLock with AtomicLong.accumulateAndGet() for thread-safe max-value tracking on the hot message path.

  • putMessageEntireTimeMax / getMessageEntireTimeMax — eliminate lock contention in hot put/get path
  • dispatchMaxBuffer — fix TOCTOU race condition in volatile read-modify-write
  • toString() — chained StringBuilder.append() instead of string concatenation

juincen added 2 commits August 5, 2026 11:45
…of returning boolean

Move the duplicate-group exception throwing from callers into
MQClientInstance.registerConsumer() itself, removing redundant
if (!registerOK) throw checks in all three consumer implementations.
- Replace volatile long + ReentrantLock for putMessageEntireTimeMax and
  getMessageEntireTimeMax with AtomicLong.accumulateAndGet, eliminating
  lock contention in the hot put/get message path.
- Replace volatile long dispatchMaxBuffer with AtomicLong to fix a
  TOCTOU race condition where the max value could be lost under
  concurrent updates.
- Replace string concatenation with chained StringBuilder.append()
  in toString() for better performance.
@juincen juincen changed the title refactor: registerConsumer throws MQClientException directly instead of returning boolean refactor: throw exception in registerConsumer + lock-free StoreStatsService Aug 5, 2026

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary

Two clean improvements: (1) moves duplicate-group exception throwing into registerConsumer() itself, eliminating redundant throw-after-check patterns in all three consumer implementations; (2) replaces volatile long + ReentrantLock with AtomicLong.accumulateAndGet(Math::max) in StoreStatsService for lock-free max tracking.

Findings

  • [Info] DefaultMQPushConsumerImpl.java — The original code called consumeMessageService.shutdown() in the registration-failed error path before throwing. The refactored version removes this since the exception is now thrown inside registerConsumer() before any service state changes. This is correct behavior (the service was never started), but worth confirming that no caller depends on the shutdown side-effect.
  • [Info] StoreStatsService.java:73AtomicLong.accumulateAndGet with Math::max is a good lock-free replacement. Note that under high contention this may spin, but for a stats counter the contention should be low.

LGTM.


Automated review by github-manager-bot

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