codelipenghui opened a new pull request, #24825:
URL: https://github.com/apache/pulsar/pull/24825
### Motivation
When topics are idle (no recent write activity), stats collection needs to
read from storage to get the last publish timestamp since the managed ledger
doesn't cache it. If stats are polled frequently, this causes unnecessary
repeated storage reads for the same value.
### Modifications
- Added `cachedLastPublishTimestamp` field to cache the last publish
timestamp for idle topics
- The cache is only used when `ledger.getLastAddEntryTime() <= 0` (topic is
idle)
- When the topic has recent activity (`ledger.getLastAddEntryTime() > 0`),
we use the ledger's value directly and clear the cache
- This optimization specifically targets idle topics where stats are polled
repeatedly but nothing is being written
- Added comprehensive unit test to verify the caching behavior
### Verifying this change
- [x] Added unit test `testLastPublishTimestampCaching` that verifies:
1. First stats call on idle topic reads from storage and caches
2. Second stats call uses cached value (no storage read)
3. When topic becomes active, uses ledger value and clears cache
4. When topic becomes idle again, reads from storage (cache was properly
cleared)
### Documentation
- [ ] `doc-required` (Your PR needs to update docs and you will update later)
- [x] `doc-not-needed` (Please explain why)
- This is an internal optimization that doesn't change any public APIs or
user-facing behavior
- [ ] `doc` (Your PR contains doc changes)
- [ ] `doc-complete` (Docs have been already added)
🤖 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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]