feat:Progressive compact#3229
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds a periodic progressive compaction routine to PikaServer via a new AutoProgressiveCompact() method (tracked by last_progressive_compact_time_), invoked from DoTimingTask on a 60s cadence; also adjusts server log purging to preserve the most recent log per severity and delete older files beyond retention. Changes
Sequence DiagramsequenceDiagram
participant DoTimingTask as DoTimingTask
participant AutoCompact as AutoProgressiveCompact
participant DBManager as DB Manager
participant DB as Database
participant Storage as Storage Engine
DoTimingTask->>AutoCompact: invoke (every ~60s)
activate AutoCompact
AutoCompact->>DBManager: acquire read lock on all DBs
activate DBManager
DBManager-->>AutoCompact: lock acquired
deactivate DBManager
loop per database
AutoCompact->>DB: acquire shared DB lock
activate DB
DB-->>AutoCompact: lock acquired
deactivate DB
AutoCompact->>Storage: LongestNotCompactionSstCompact(kAll)
activate Storage
Storage-->>AutoCompact: compaction result
deactivate Storage
end
AutoCompact->>AutoCompact: update last_progressive_compact_time_
AutoCompact-->>DoTimingTask: return
deactivate AutoCompact
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
src/pika_server.cc (3)
1150-1152: Consider extracting the 60-second interval to a named constant or config parameter.The hardcoded
60is a magic number. For maintainability and potential future configurability, consider using a named constant:+// At file/namespace scope +constexpr int kProgressiveCompactIntervalSeconds = 60; + void PikaServer::AutoProgressiveCompact() { struct timeval now; gettimeofday(&now, nullptr); // Execute progressive compact every 60 seconds if (last_progressive_compact_time_.tv_sec == 0 || - now.tv_sec - last_progressive_compact_time_.tv_sec >= 60) { + now.tv_sec - last_progressive_compact_time_.tv_sec >= kProgressiveCompactIntervalSeconds) {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/pika_server.cc` around lines 1150 - 1152, Replace the magic number 60 with a named constant or configurable parameter to improve readability and maintainability: define a constant (e.g. kProgressiveCompactIntervalSec) or read from configuration, then use that constant in the condition that checks last_progressive_compact_time_.tv_sec and now.tv_sec (the if block using last_progressive_compact_time_.tv_sec and now.tv_sec) and anywhere else the interval is referenced so the interval is centralized and easy to change.
1157-1168: Use RAII guard forDBLockShared()/DBUnlockShared()to ensure exception safety.The manual lock/unlock pattern is exception-unsafe. If any code between
DBLockShared()andDBUnlockShared()throws (e.g.,LOGcould theoretically throw on allocation failure), the lock won't be released. Consider usingstd::shared_lockfor RAII semantics.♻️ Proposed refactor using RAII
std::shared_lock db_rwl(dbs_rw_); for (const auto& db_item : dbs_) { - db_item.second->DBLockShared(); - auto storage = db_item.second->storage(); - if (storage) { - Status s = storage->LongestNotCompactionSstCompact(storage::DataType::kAll); - if (!s.ok()) { - LOG(WARNING) << "Progressive compact for DB: " << db_item.first - << " failed: " << s.ToString(); - } else { - LOG(INFO) << "Progressive compact for DB: " << db_item.first << " completed"; + { + std::shared_lock db_lock(db_item.second->GetDBLock()); + auto storage = db_item.second->storage(); + if (storage) { + Status s = storage->LongestNotCompactionSstCompact(storage::DataType::kAll); + if (!s.ok()) { + LOG(WARNING) << "Progressive compact for DB: " << db_item.first + << " failed: " << s.ToString(); + } else { + LOG(INFO) << "Progressive compact for DB: " << db_item.first << " completed"; + } } } - db_item.second->DBUnlockShared(); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/pika_server.cc` around lines 1157 - 1168, Replace the manual DBLockShared()/DBUnlockShared() pair with an RAII guard to ensure the lock is released on scope exit: create or use a RAII helper (e.g., a DBSharedLockGuard whose constructor calls db->DBLockShared() and whose destructor calls db->DBUnlockShared()), or use std::shared_lock if you can access the underlying shared_mutex; then wrap the code that calls db_item.second->storage() and storage->LongestNotCompactionSstCompact(...) inside that guard's scope (replace the explicit DBLockShared/DBUnlockShared around db_item with the guard). Reference symbols: DBLockShared, DBUnlockShared, db_item, storage(), LongestNotCompactionSstCompact, storage::DataType::kAll.
1164-1166: Consider reducing logging verbosity for successful progressive compaction.Logging at INFO level every 60 seconds for each DB will generate significant log volume. Consider:
- Using
VLOGorLOG_EVERY_Nfor routine success messages- Only logging when actual compaction work was performed (vs. when task was queued)
- Moving success logging to the background task completion handler
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/pika_server.cc` around lines 1164 - 1166, The INFO log "Progressive compact for DB: " << db_item.first << " completed" is too verbose; change it to use a lower verbosity macro (e.g., VLOG or LOG_EVERY_N) and only emit when actual compaction work ran (check the compaction result/flag rather than queued state), and move the success log into the background task completion handler that finalizes progressive compaction (the code paths around db_item.first and the progressive compact completion callback/worker should be updated accordingly).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/pika_server.cc`:
- Line 1350: There is a formatting issue: add a space after the `if` keyword in
the conditional calling pstd::DeleteFile so it reads `if
(!pstd::DeleteFile(log_file)) {`; update the occurrence around the use of
pstd::DeleteFile and log_file to follow this spacing convention (and optionally
scan nearby conditionals for the same pattern) to keep code style consistent.
---
Nitpick comments:
In `@src/pika_server.cc`:
- Around line 1150-1152: Replace the magic number 60 with a named constant or
configurable parameter to improve readability and maintainability: define a
constant (e.g. kProgressiveCompactIntervalSec) or read from configuration, then
use that constant in the condition that checks
last_progressive_compact_time_.tv_sec and now.tv_sec (the if block using
last_progressive_compact_time_.tv_sec and now.tv_sec) and anywhere else the
interval is referenced so the interval is centralized and easy to change.
- Around line 1157-1168: Replace the manual DBLockShared()/DBUnlockShared() pair
with an RAII guard to ensure the lock is released on scope exit: create or use a
RAII helper (e.g., a DBSharedLockGuard whose constructor calls
db->DBLockShared() and whose destructor calls db->DBUnlockShared()), or use
std::shared_lock if you can access the underlying shared_mutex; then wrap the
code that calls db_item.second->storage() and
storage->LongestNotCompactionSstCompact(...) inside that guard's scope (replace
the explicit DBLockShared/DBUnlockShared around db_item with the guard).
Reference symbols: DBLockShared, DBUnlockShared, db_item, storage(),
LongestNotCompactionSstCompact, storage::DataType::kAll.
- Around line 1164-1166: The INFO log "Progressive compact for DB: " <<
db_item.first << " completed" is too verbose; change it to use a lower verbosity
macro (e.g., VLOG or LOG_EVERY_N) and only emit when actual compaction work ran
(check the compaction result/flag rather than queued state), and move the
success log into the background task completion handler that finalizes
progressive compaction (the code paths around db_item.first and the progressive
compact completion callback/worker should be updated accordingly).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: a98ff8cf-2005-4315-a8cd-2ee54b0a0eb1
📒 Files selected for processing (2)
include/pika_server.hsrc/pika_server.cc
| if (interval_days > retention_time) { | ||
| std::string log_file = log_path + "/" + file; | ||
| LOG(INFO) << "Deleting out of date log file: " << log_file; | ||
| if(!pstd::DeleteFile(log_file)) { |
There was a problem hiding this comment.
Minor formatting issue: missing space after if.
- if(!pstd::DeleteFile(log_file)) {
+ if (!pstd::DeleteFile(log_file)) {📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if(!pstd::DeleteFile(log_file)) { | |
| if (!pstd::DeleteFile(log_file)) { |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/pika_server.cc` at line 1350, There is a formatting issue: add a space
after the `if` keyword in the conditional calling pstd::DeleteFile so it reads
`if (!pstd::DeleteFile(log_file)) {`; update the occurrence around the use of
pstd::DeleteFile and log_file to follow this spacing convention (and optionally
scan nearby conditionals for the same pattern) to keep code style consistent.
Summary by CodeRabbit
New Features
Improvements