SDSTOR-21921 Improve LOG_EVERY_N logic with memory optimization and t…#882
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enhances HomeStore’s rate-limited logging macros (HS_LOG_EVERY_N*) to reduce per-thread memory growth and add time-based (seconds) and hybrid (count-or-time) rate limiting, addressing Issue #880.
Changes:
- Refactors rate-limit tracking to use hashed keys with periodic per-thread cleanup to bound memory usage.
- Adds
HS_LOG_EVERY_N_SECandHS_LOG_EVERY_N_OR_SECmacros for time-based and hybrid rate limiting. - Adjusts
HS_LOG_EVERY_Nbehavior to log the first occurrence and then rate-limit subsequent logs.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| src/lib/common/homestore_assert.hpp | Adds time-based/hybrid LOG_EVERY macros and refactors the per-thread tracking logic for memory-bounded rate limiting. |
| conanfile.py | Bumps package version to 7.5.7. |
| .gitignore | Ignores .worktrees/ directory. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ime-based rate limiting This change enhances LOG_EVERY_N macros to fix memory leaks and add new capabilities: - Refactor check_logged_already() to check_and_format_log() which consolidates rate-limiting decision and suffix formatting into single function - Use hash-based keys instead of full message strings for memory efficiency - Clear entire map every 300s to prevent unbounded memory growth - Store millisecond offsets and counts for time-based rate limiting - Log first occurrence (count==1) in addition to every Nth occurrence - Add HS_LOG_EVERY_N_SEC for time-based rate limiting (log every N seconds) - Add HS_LOG_EVERY_N_OR_SEC for hybrid rate limiting (every N occurrences OR every M seconds) - Use consistent suffix format: "Last logged X.Xs ago, N occurrences" Memory impact: Reduces per-entry storage from ~124 bytes to ~20 bytes (6x improvement) Resolves: eBay#880
53a4ca3 to
a7cf764
Compare
Use milliseconds directly in format string instead of converting to seconds, avoiding rvalue binding issue with fmt::make_format_args. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix count tracking: reset to 0 instead of 1 to avoid off-by-one in rate limiting (was logging at 10, 19, 28... instead of 10, 20, 30...) - Use happened flag to detect first occurrence instead of count==1 - Use swap with empty map to actually release memory during cleanup (clear() only removes entries but retains bucket array capacity) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## stable/v7.x #882 +/- ##
==============================================
Coverage ? 48.22%
==============================================
Files ? 110
Lines ? 12935
Branches ? 6220
==============================================
Hits ? 6238
Misses ? 2577
Partials ? 4120 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Add #include <functional> for std::hash to avoid relying on transitive includes - Handle edge case when both freq=0 and interval_sec=0: always log (fallback behavior) instead of only logging first occurrence then suppressing all subsequent logs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@Besroy I think it is ready for human :) after passing AI. The Copilot review did a good job through that most of the comments make sense |
|
Generally LGTM. Just have a small concern: it works as expected only when interval_sec < 300 or freq * avg_time_per_occur < 300s. Otherwise, the behavior effectively becomes "restart counting every 5 minutes, and log the first occurrence after each reset." Is that acceptable by design? |
|
agree, aside from doc we can have a warning log for |
- Add function comment documenting the 300s cleanup limitation - Log one-time warning when interval_sec exceeds cleanup period - Clarifies expected behavior when using large interval_sec values Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@xiaoxichen pls use squash merge when merging PR , we`d better have one final commit for a single PR |
|
oops,sorry my bad,forgot to squash |
…ime-based rate limiting
This change enhances LOG_EVERY_N macros to fix memory leaks and add new capabilities:
Memory impact: Reduces per-entry storage from ~124 bytes to ~20 bytes (6x improvement)
Resolves: #880