Skip to content

Conversation

@chenbt-hz
Copy link
Collaborator

@chenbt-hz chenbt-hz commented Jul 14, 2025

fix: #3123

1.修复当前obd-compact bug容易触发、大数据量时full-compact长期执行的问题
2.移除了full-compact定期执行
3.支持obd-compact周期动态修改配置

(未修复可能潜在的obd-compact逻辑bug)

Summary by CodeRabbit

  • New Features

    • Introduced a configurable interval for the "obd-compact" compaction strategy, allowing users to set how frequently this compaction runs.
    • Added support for viewing and modifying the new "obd-compact-interval" parameter via configuration commands.
  • Improvements

    • Updated default compaction strategy settings to optimize performance and efficiency.
    • Enhanced configuration comments for greater clarity and detail.
  • Bug Fixes

    • Enforced minimum thresholds for certain compaction parameters to prevent misconfiguration.

@coderabbitai
Copy link

coderabbitai bot commented Jul 14, 2025

Walkthrough

A new configuration parameter obd-compact-interval was introduced to control the minimum interval between executions of the "OldestOrBestDeleteRatioSstCompact" compaction strategy. Associated logic was added to enforce this interval in the compaction scheduling, update configuration handling, and support get/set commands for the new parameter.

Changes

File(s) Change Summary
conf/pika.conf Updated compaction strategy parameter values, added obd-compact-interval, and improved comments.
include/pika_conf.h, src/pika_conf.cc Added obd_compact_interval_ member, getter/setter, and logic to load/rewrite the new parameter.
src/pika_admin.cc Added get/set support for obd-compact-interval in config commands.
include/pika_server.h, src/pika_server.cc Added last_strategy_compact_time_ member and enforced interval-based compaction scheduling logic.
src/storage/include/storage/storage.h Added obd_compact_interval_ to StorageOptions::CompactParam.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PikaServer
    participant PikaConf
    participant Storage

    User->>PikaServer: CONFIG SET obd-compact-interval <value>
    PikaServer->>PikaConf: SetObdCompactInterval(value)
    PikaConf-->>PikaServer: Update obd_compact_interval_

    loop Every AutoCompactRange interval
        PikaServer->>PikaConf: Get obd_compact_interval_
        PikaServer->>PikaServer: Check last_strategy_compact_time_
        alt If interval elapsed
            PikaServer->>Storage: Schedule compaction (if strategy matches)
            PikaServer->>PikaServer: Update last_strategy_compact_time_
        end
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Address compaction scheduling issues related to "OldestOrBestDeleteRatioSstCompact" (#3123)

Possibly related PRs

Suggested labels

4.0.0, 4.0.1

Suggested reviewers

  • wangshao1
  • dingxiaoshuai123

Poem

A bunny hops with careful cheer,
New compaction rules now appear!
With intervals set and logic tight,
Compaction runs just when it’s right.
No more chaos, no more fright—
The storage stays healthy,
And all is light! 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the ☢️ Bug Something isn't working label Jul 14, 2025
@chenbt-hz chenbt-hz requested review from Mixficsol and wangshao1 July 14, 2025 09:21
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
include/pika_server.h (1)

553-553: Consider using std::chrono for timestamp consistency.

Based on the project's established patterns, consider using std::chrono::steady_clock::time_point instead of struct timeval for better type safety and consistency with the codebase's timestamp handling approach.

-  struct timeval last_strategy_compact_time_;
+  std::chrono::steady_clock::time_point last_strategy_compact_time_;
src/pika_admin.cc (1)

2102-2109: Consider adding upper bound validation for interval parameter.

The current validation only checks for non-negative values, but interval parameters typically benefit from reasonable upper bounds to prevent configuration errors.

} else if (set_item == "obd-compact-interval") {
    if (pstd::string2int(value.data(), value.size(), &ival) == 0 || ival < 0) {
      res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'obd-compact-interval'\r\n");
      return;
    }
+   // Consider adding reasonable upper bound validation
+   // if (ival > MAX_REASONABLE_INTERVAL) {
+   //   res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'obd-compact-interval', value too large\r\n");
+   //   return;
+   // }
    g_pika_conf->SetObdCompactInterval(ival);
    res_.AppendStringRaw("+OK\r\n");

Otherwise, the implementation correctly:

  • Validates integer parsing
  • Ensures non-negative values
  • Calls the appropriate configuration setter
  • Returns proper success/error responses
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b077fc and 78e990c.

📒 Files selected for processing (7)
  • conf/pika.conf (1 hunks)
  • include/pika_conf.h (3 hunks)
  • include/pika_server.h (1 hunks)
  • src/pika_admin.cc (3 hunks)
  • src/pika_conf.cc (2 hunks)
  • src/pika_server.cc (3 hunks)
  • src/storage/include/storage/storage.h (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
include/pika_server.h (2)
Learnt from: hahahashen
PR: OpenAtomFoundation/pikiwidb#338
File: src/client.h:33-48
Timestamp: 2024-06-14T14:09:58.236Z
Learning: The `PClient` class in `src/client.h` uses `std::chrono` for handling timestamps for better type safety and clarity.
Learnt from: hahahashen
PR: OpenAtomFoundation/pikiwidb#338
File: src/client.h:33-48
Timestamp: 2024-10-09T12:55:29.966Z
Learning: The `PClient` class in `src/client.h` uses `std::chrono` for handling timestamps for better type safety and clarity.
src/pika_server.cc (2)
Learnt from: hahahashen
PR: OpenAtomFoundation/pikiwidb#338
File: src/client.h:33-48
Timestamp: 2024-10-09T12:55:29.966Z
Learning: The `PClient` class in `src/client.h` uses `std::chrono` for handling timestamps for better type safety and clarity.
Learnt from: hahahashen
PR: OpenAtomFoundation/pikiwidb#338
File: src/client.h:33-48
Timestamp: 2024-06-14T14:09:58.236Z
Learning: The `PClient` class in `src/client.h` uses `std::chrono` for handling timestamps for better type safety and clarity.
🧬 Code Graph Analysis (2)
src/pika_admin.cc (2)
src/pstd/src/pstd_string.cc (8)
  • stringmatch (186-189)
  • stringmatch (186-186)
  • string2int (350-439)
  • string2int (350-350)
  • string2int (444-457)
  • string2int (444-444)
  • string2int (462-475)
  • string2int (462-462)
src/pstd/include/pstd_string.h (4)
  • stringmatch (45-45)
  • string2int (48-48)
  • string2int (49-49)
  • string2int (50-50)
src/pika_conf.cc (1)
src/pstd/src/base_conf.cc (4)
  • GetConfInt (98-109)
  • GetConfInt (98-98)
  • SetConfInt (246-257)
  • SetConfInt (246-246)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Analyze (go)
  • GitHub Check: build_on_macos
  • GitHub Check: build_on_ubuntu
  • GitHub Check: build_on_centos
🔇 Additional comments (11)
src/storage/include/storage/storage.h (1)

84-84: LGTM! Well-integrated storage parameter.

The addition of obd_compact_interval_ to the CompactParam struct is well-placed and follows the existing naming conventions. The integration with the storage options structure is appropriate for the OBD compaction strategy timing control.

src/pika_conf.cc (2)

338-341: LGTM! Proper configuration loading with validation.

The configuration loading logic correctly uses the standard GetConfInt method and appropriately enforces a minimum value of 600 seconds to prevent overly frequent compaction operations.


890-893: LGTM! Consistent configuration rewriting pattern.

The configuration rewriting logic follows the established pattern and correctly enforces the minimum value constraint. While there's minor code duplication with the loading logic, this is consistent with how other configuration parameters are handled in this codebase.

include/pika_conf.h (2)

146-149: LGTM: Getter method follows established pattern.

The getter method implementation is correct and consistent with other configuration getters in the class.


1050-1050: LGTM: Member variable declaration is correct.

The member variable declaration follows the naming convention and is appropriately placed with other compact-related variables.

conf/pika.conf (1)

684-705: Parameter updates look reasonable for fixing compact loop issues.

The changes to the OBD_Compact configuration parameters appear well-thought-out:

  1. force-compact-file-age-seconds: Increased from 300 to 1200 seconds (5 to 20 minutes) - gives files more time to mature before forced compaction
  2. force-compact-min-delete-ratio: Increased from 10 to 50 - higher threshold before triggering compaction
  3. dont-compact-sst-created-in-seconds: Increased from 20 to 300 seconds - protects newer files from unnecessary compaction
  4. best-delete-min-ratio: Increased from 10 to 30 - optimizes compaction trigger threshold
  5. obd-compact-interval: New parameter with 600 second minimum - adds periodic control

These adjustments should help reduce the frequency of compaction triggers and address the compact loop error mentioned in the PR objectives. The documentation is also clear and helpful.

src/pika_server.cc (3)

49-49: LGTM: Member variable initialization is correct.

The initialization of last_strategy_compact_time_ to {0, 0} is appropriate for a timeval structure.


1513-1513: LGTM: Storage options configuration is correct.

The assignment properly propagates the obd_compact_interval configuration value to the storage options structure.


1234-1247: No thread-safety issue with last_strategy_compact_time_

The AutoCompactRange() routine (and its gettimeofday/last_strategy_compact_time_ logic) is only ever invoked from DoTimingTask(), which runs on a single dedicated timing thread—there are no concurrent calls to AutoCompactRange(). Therefore:

  • No mutex is needed to guard last_strategy_compact_time_.
  • Adding error-checks around gettimeofday() is a matter of defensive style; in practice it never fails unless passed an invalid pointer.
  • Switching to std::chrono would be a global stylistic change rather than a PR-specific fix.

Likely an incorrect or invalid review comment.

src/pika_admin.cc (2)

2311-2316: LGTM: Configuration retrieval implementation is correct.

The implementation follows the established pattern for configuration parameter retrieval. The pattern matching, element counting, and encoding logic are consistent with other parameters in the codebase.


2379-2379: LGTM: Parameter correctly added to configurable list.

The parameter is properly added to the list of configurable parameters returned by CONFIG SET *.

Comment on lines +991 to +994
void SetObdCompactInterval(const int value) {
std::unique_lock l(rwlock_);
obd_compact_interval_ = value < 600 ? 600 : value;
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add TryPushDiffCommands call for configuration change tracking.

The setter method correctly enforces the minimum value of 600 seconds and uses proper thread safety with unique_lock. However, it's missing the TryPushDiffCommands call that most other setters use to track configuration changes for persistence.

 void SetObdCompactInterval(const int value) {
   std::unique_lock l(rwlock_);
+  TryPushDiffCommands("obd-compact-interval", std::to_string(value));
   obd_compact_interval_ = value < 600 ? 600 : value;
 }
📝 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.

Suggested change
void SetObdCompactInterval(const int value) {
std::unique_lock l(rwlock_);
obd_compact_interval_ = value < 600 ? 600 : value;
}
void SetObdCompactInterval(const int value) {
std::unique_lock l(rwlock_);
TryPushDiffCommands("obd-compact-interval", std::to_string(value));
obd_compact_interval_ = value < 600 ? 600 : value;
}
🤖 Prompt for AI Agents
In include/pika_conf.h around lines 991 to 994, the SetObdCompactInterval setter
correctly enforces a minimum value and uses thread safety but lacks the
TryPushDiffCommands call. Add a call to TryPushDiffCommands after setting
obd_compact_interval_ to ensure configuration changes are tracked for
persistence, following the pattern used in other setter methods.

@chenbt-hz
Copy link
Collaborator Author

@Mixficsol @wangshao1
这里的逻辑是不是要改掉?此处目前触发force CompactRange压的都是default cf
如果是其他数据类型的墓碑过多,是否会仅触发default cf可能导致错误?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

☢️ Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

loop compact error

2 participants