From 09066446bedd6934b1c93232c28a1c57624227d4 Mon Sep 17 00:00:00 2001 From: cheniujh <1271435567@qq.com> Date: Fri, 13 Dec 2024 17:38:30 +0800 Subject: [PATCH] persist the salveof conf item when slaveof no one is executed (#2973) --- include/pika_conf.h | 1 + src/pika_admin.cc | 1 + src/pika_conf.cc | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/include/pika_conf.h b/include/pika_conf.h index 5f9e5db08..46a8fbd60 100644 --- a/include/pika_conf.h +++ b/include/pika_conf.h @@ -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: diff --git a/src/pika_admin.cc b/src/pika_admin.cc index d07ce3e32..583e0dfa6 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -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; } diff --git a/src/pika_conf.cc b/src/pika_conf.cc index 60c68b361..3de97d54c 100644 --- a/src/pika_conf.cc +++ b/src/pika_conf.cc @@ -932,6 +932,30 @@ int PikaConf::ConfigRewrite() { return static_cast(WriteBack()); } +int PikaConf::ConfigRewriteSlaveOf() { + std::lock_guard l(rwlock_); + SetConfStr("slaveof", slaveof_); + if (!diff_commands_.empty()) { + std::vector 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(WriteBack()); +} + int PikaConf::ConfigRewriteReplicationID() { std::lock_guard l(rwlock_); SetConfStr("replication-id", replication_id_);