Skip to content

Commit

Permalink
persist the salveof conf item when slaveof no one is executed (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheniujh authored Dec 13, 2024
1 parent 7fcb916 commit 0906644
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ class PikaConf : public pstd::BaseConf {
int cache_lfu_decay_time() { return cache_lfu_decay_time_; }
int Load();
int ConfigRewrite();
int ConfigRewriteSlaveOf();
int ConfigRewriteReplicationID();

private:
Expand Down
1 change: 1 addition & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void SlaveofCmd::Do() {
if (is_none_) {
res_.SetRes(CmdRes::kOk);
g_pika_conf->SetSlaveof(std::string());
g_pika_conf->ConfigRewriteSlaveOf();
return;
}

Expand Down
24 changes: 24 additions & 0 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,30 @@ int PikaConf::ConfigRewrite() {
return static_cast<int>(WriteBack());
}

int PikaConf::ConfigRewriteSlaveOf() {
std::lock_guard l(rwlock_);
SetConfStr("slaveof", slaveof_);
if (!diff_commands_.empty()) {
std::vector<pstd::BaseConf::Rep::ConfItem> filtered_items;
for (const auto& diff_command : diff_commands_) {
if (!diff_command.second.empty()) {
pstd::BaseConf::Rep::ConfItem item(pstd::BaseConf::Rep::kConf, diff_command.first, diff_command.second);
filtered_items.push_back(item);
}
}
if (!filtered_items.empty()) {
pstd::BaseConf::Rep::ConfItem comment_item(pstd::BaseConf::Rep::kComment,
"# Generated by ReplicationID CONFIG REWRITE\n");
PushConfItem(comment_item);
for (const auto& item : filtered_items) {
PushConfItem(item);
}
}
diff_commands_.clear();
}
return static_cast<int>(WriteBack());
}

int PikaConf::ConfigRewriteReplicationID() {
std::lock_guard l(rwlock_);
SetConfStr("replication-id", replication_id_);
Expand Down

0 comments on commit 0906644

Please sign in to comment.