Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -681,30 +681,25 @@ wash-data: true
# obd-compact https://github.com/OpenAtomFoundation/pika/issues/2255
compaction-strategy : obd-compact

# For OBD_Compact
################################## Configure For OBD_Compact #####################
# According to the number of sst files in rocksdb,
# compact every `compact-every-num-of-files` file.
compact-every-num-of-files : 10

# For OBD_Compact
# In another search, if the file creation time is
# greater than `force-compact-file-age-seconds`,
# a compaction of the upper and lower boundaries
# of the file will be performed at the same time
# `compact-every-num-of-files` -1
force-compact-file-age-seconds : 300
# Extends the time a file must exist (20 minutes) before triggering forced compaction
force-compact-file-age-seconds : 1200

# For OBD_Compact
# According to the number of sst files in rocksdb,
# compact every `compact-every-num-of-files` file.
force-compact-min-delete-ratio : 10
# The forced deletion ratio threshold - compaction will be triggered when
# the deletion markers percentage in SST files reaches this value
force-compact-min-delete-ratio : 50

# For OBD_Compact
# According to the number of sst files in rocksdb,
# compact every `compact-every-num-of-files` file.
dont-compact-sst-created-in-seconds : 20
# Files created within the last 5 minutes will not be compacted
dont-compact-sst-created-in-seconds : 300

# For OBD_Compact
# According to the number of sst files in rocksdb,
# compact every `compact-every-num-of-files` file.
best-delete-min-ratio : 10
# The optimal deletion ratio - defines the best threshold percentage
# for deletion markers in SST files to optimize compaction efficiency
best-delete-min-ratio : 30

# Trigger the obd-compact task periodically according to `obd-compact-interval`
# obd-compact-interval >= 600.
obd-compact-interval : 600
10 changes: 10 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ class PikaConf : public pstd::BaseConf {
std::shared_lock l(rwlock_);
return best_delete_min_ratio_;
}
int obd_compact_interval () {
std::shared_lock l(rwlock_);
return obd_compact_interval_;
}
CompactionStrategy compaction_strategy() {
std::shared_lock l(rwlock_);
return compaction_strategy_;
Expand Down Expand Up @@ -984,6 +988,11 @@ class PikaConf : public pstd::BaseConf {
ConfigRewrite();
}

void SetObdCompactInterval(const int value) {
std::unique_lock l(rwlock_);
obd_compact_interval_ = value < 600 ? 600 : value;
}
Comment on lines +991 to +994
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.


size_t GetUnfinishedFullSyncCount() {
std::shared_lock l(rwlock_);
return internal_used_unfinished_full_sync_.size();
Expand Down Expand Up @@ -1038,6 +1047,7 @@ class PikaConf : public pstd::BaseConf {
int force_compact_min_delete_ratio_;
int dont_compact_sst_created_in_seconds_;
int best_delete_min_ratio_;
int obd_compact_interval_;
CompactionStrategy compaction_strategy_;

int64_t resume_check_interval_ = 60; // seconds
Expand Down
1 change: 1 addition & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ class PikaServer : public pstd::noncopyable {
*/
bool have_scheduled_crontask_ = false;
struct timeval last_check_compact_time_;
struct timeval last_strategy_compact_time_;

/*
* ResumeDB used
Expand Down
14 changes: 14 additions & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,12 @@ void ConfigCmd::ConfigGet(std::string& ret) {
EncodeNumber(&config_body, g_pika_conf->db_statistics_level());
}

if (pstd::stringmatch(pattern.data(), "obd-compact-interval", 1)) {
elements += 2;
EncodeString(&config_body, "obd-compact-interval");
EncodeNumber(&config_body, g_pika_conf->obd_compact_interval());
}

std::stringstream resp;
resp << "*" << std::to_string(elements) << "\r\n" << config_body;
ret = resp.str();
Expand Down Expand Up @@ -2370,6 +2376,7 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
"zset-cache-field-num-per-key",
"cache-lfu-decay-time",
"max-conn-rbuf-size",
"obd-compact-interval",
});
res_.AppendStringVector(replyVt);
return;
Expand Down Expand Up @@ -3092,6 +3099,13 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
}
g_pika_conf->SetMaxConnRbufSize(static_cast<int>(ival));
res_.AppendStringRaw("+OK\r\n");
} 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;
}
g_pika_conf->SetObdCompactInterval(ival);
res_.AppendStringRaw("+OK\r\n");
} else {
res_.AppendStringRaw("-ERR Unsupported CONFIG parameter: " + set_item + "\r\n");
}
Expand Down
10 changes: 9 additions & 1 deletion src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ int PikaConf::Load() {
if (best_delete_min_ratio_ < 10) {
best_delete_min_ratio_ = 10;
}

GetConfInt("obd-compact-interval", &obd_compact_interval_);
if (obd_compact_interval_ < 600) {
obd_compact_interval_ = 600;
}

std::string cs_;
GetConfStr("compaction-strategy", &cs_);
Expand Down Expand Up @@ -882,7 +887,10 @@ int PikaConf::ConfigRewrite() {
if (best_delete_min_ratio_ < 10) {
best_delete_min_ratio_ = 10;
}

SetConfInt("obd-compact-interval", obd_compact_interval_);
if (obd_compact_interval_ < 600) {
obd_compact_interval_ = 600;
}
std::string cs_;
SetConfStr("compaction-strategy", cs_);
if (cs_ == "full-compact") {
Expand Down
19 changes: 15 additions & 4 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ PikaServer::PikaServer()
: exit_(false),
slow_cmd_thread_pool_flag_(g_pika_conf->slow_cmd_pool()),
last_check_compact_time_({0, 0}),
last_strategy_compact_time_({0, 0}),
last_check_resume_time_({0, 0}),
repl_state_(PIKA_REPL_NO_CONNECT),
role_(PIKA_ROLE_SINGLE) {
Expand Down Expand Up @@ -1230,10 +1231,19 @@ void PikaServer::AutoCompactRange() {
}
}

if (g_pika_conf->compaction_strategy() == PikaConf::FullCompact) {
DoSameThingEveryDB(TaskType::kCompactAll);
} else if (g_pika_conf->compaction_strategy() == PikaConf::OldestOrBestDeleteRatioSstCompact) {
DoSameThingEveryDB(TaskType::kCompactOldestOrBestDeleteRatioSst);

struct timeval strategy_now;
gettimeofday(&strategy_now, nullptr);
if (last_strategy_compact_time_.tv_sec == 0){
gettimeofday(&last_strategy_compact_time_, nullptr);
}
if (last_strategy_compact_time_.tv_sec > 0 &&
strategy_now.tv_sec - last_strategy_compact_time_.tv_sec >= g_pika_conf->obd_compact_interval()) {
gettimeofday(&last_strategy_compact_time_, nullptr);
if (g_pika_conf->compaction_strategy() == PikaConf::OldestOrBestDeleteRatioSstCompact) {
LOG(INFO) << "[Compaction]schedule OldestOrBestDeleteRatioSstCompact, interval: " << g_pika_conf->obd_compact_interval() << " seconds";
DoSameThingEveryDB(TaskType::kCompactOldestOrBestDeleteRatioSst);
}
}
}

Expand Down Expand Up @@ -1500,6 +1510,7 @@ void PikaServer::InitStorageOptions() {

// For Storage compaction
storage_options_.compact_param_.best_delete_min_ratio_ = g_pika_conf->best_delete_min_ratio();
storage_options_.compact_param_.obd_compact_interval_ = g_pika_conf->obd_compact_interval();
storage_options_.compact_param_.dont_compact_sst_created_in_seconds_ = g_pika_conf->dont_compact_sst_created_in_seconds();
storage_options_.compact_param_.force_compact_file_age_seconds_ = g_pika_conf->force_compact_file_age_seconds();
storage_options_.compact_param_.force_compact_min_delete_ratio_ = g_pika_conf->force_compact_min_delete_ratio();
Expand Down
1 change: 1 addition & 0 deletions src/storage/include/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct StorageOptions {
int force_compact_min_delete_ratio_;
int dont_compact_sst_created_in_seconds_;
int best_delete_min_ratio_;
int obd_compact_interval_;
};
CompactParam compact_param_;
Status ResetOptions(const OptionType& option_type, const std::unordered_map<std::string, std::string>& options_map);
Expand Down
Loading