From 002a132eba6ed7812a466ca789e582575eca71d1 Mon Sep 17 00:00:00 2001 From: Yuecai Liu <38887641+luky116@users.noreply.github.com> Date: Fri, 2 Jun 2023 10:46:33 +0800 Subject: [PATCH] add run id (#1570) * add run id * add comment to run_id * fix if * add . * fix comment * fix * fix * fix --------- Co-authored-by: liuyuecai --- conf/pika.conf | 4 ++++ include/pika_conf.h | 6 ++++++ src/pika_admin.cc | 1 + src/pika_conf.cc | 7 +++++++ src/pstd/include/pstd_string.h | 1 + src/pstd/src/pstd_string.cc | 23 +++++++++++------------ 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/conf/pika.conf b/conf/pika.conf index f79fa0abff..bd8993c8d2 100644 --- a/conf/pika.conf +++ b/conf/pika.conf @@ -7,6 +7,10 @@ # Port 10221 is used for Rsync, and port 11221 is used for Replication, while the listening port is 9221. port : 9221 +# Random value identifying the Pika server, its string length must be 40. +# If not set, Pika will generate a random string with a length of 40 random characters. +# run-id: + # The number of threads for running Pika. # It's not recommended to set this value exceeds # the number of CPU cores on the deployment server. diff --git a/include/pika_conf.h b/include/pika_conf.h index 260d3c5379..95946b4047 100644 --- a/include/pika_conf.h +++ b/include/pika_conf.h @@ -21,6 +21,7 @@ #define kBinlogReadWinDefaultSize 9000 #define kBinlogReadWinMaxSize 90000 +#define configRunIdSize 40 // global class, class members well initialized class PikaConf : public pstd::BaseConf { @@ -109,6 +110,10 @@ class PikaConf : public pstd::BaseConf { std::shared_lock l(rwlock_); return server_id_; } + std::string run_id() { + std::shared_lock l(rwlock_); + return run_id_; + } std::string requirepass() { std::shared_lock l(rwlock_); return requirepass_; @@ -503,6 +508,7 @@ class PikaConf : public pstd::BaseConf { bool daemonize_ = false; int timeout_ = 0; std::string server_id_; + std::string run_id_; std::string requirepass_; std::string masterauth_; std::string userpass_; diff --git a/src/pika_admin.cc b/src/pika_admin.cc index 4446a801de..90a2531991 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -815,6 +815,7 @@ void InfoCmd::InfoServer(std::string& info) { << "\r\n"; tmp_stream << "config_file:" << g_pika_conf->conf_path() << "\r\n"; tmp_stream << "server_id:" << g_pika_conf->server_id() << "\r\n"; + tmp_stream << "run_id:" << g_pika_conf->run_id() << "\r\n"; info.append(tmp_stream.str()); } diff --git a/src/pika_conf.cc b/src/pika_conf.cc index bb9885d435..1ce60ea3c1 100644 --- a/src/pika_conf.cc +++ b/src/pika_conf.cc @@ -11,6 +11,7 @@ #include #include "pstd/include/env.h" +#include "pstd/include/pstd_string.h" #include "include/pika_define.h" @@ -149,6 +150,12 @@ int PikaConf::Load() { } else if (PIKA_SERVER_ID_MAX < std::stoull(server_id_)) { server_id_ = "PIKA_SERVER_ID_MAX"; } + GetConfStr("run-id", &run_id_); + if (run_id_.empty()) { + run_id_ = pstd::getRandomHexChars(configRunIdSize); + } else if (run_id_.length() != configRunIdSize) { + LOG(FATAL) << "run-id " << run_id_ << " is invalid, its string length should be " << configRunIdSize; + } GetConfStr("requirepass", &requirepass_); GetConfStr("masterauth", &masterauth_); GetConfStr("userpass", &userpass_); diff --git a/src/pstd/include/pstd_string.h b/src/pstd/include/pstd_string.h index 7db8c257e6..84de1ddf4a 100644 --- a/src/pstd/include/pstd_string.h +++ b/src/pstd/include/pstd_string.h @@ -57,6 +57,7 @@ std::string IpPortString(const std::string& ip, int port); std::string ToRead(const std::string& str); bool ParseIpPortString(const std::string& ip_port, std::string& ip, int& port); std::string StringTrim(const std::string& ori, const std::string& charlist = " "); +std::string getRandomHexChars(size_t len); } // namespace pstd diff --git a/src/pstd/src/pstd_string.cc b/src/pstd/src/pstd_string.cc index 613951dd8b..3cd0a9a020 100644 --- a/src/pstd/src/pstd_string.cc +++ b/src/pstd/src/pstd_string.cc @@ -33,19 +33,12 @@ */ #include "pstd/include/pstd_string.h" -#include -#include -#include -#include #include -#include #include #include -#include #include #include #include -#include #include #include #include @@ -53,7 +46,7 @@ #include -#include "pstd/include/fmacros.h" +#include "pstd/include/pstd_defer.h" namespace pstd { @@ -537,10 +530,18 @@ int string2d(const char* s, size_t slen, double* dval) { * given execution of Redis, so that if you are talking with an instance * having run_id == A, and you reconnect and it has run_id == B, you can be * sure that it is either a different instance or it was restarted. */ -void getRandomHexChars(char* p, unsigned int len) { +std::string getRandomHexChars(size_t len) { FILE* fp = fopen("/dev/urandom", "r"); + DEFER { + if (fp) { + fclose(fp); + fp = nullptr; + } + }; + char charset[] = "0123456789abcdef"; unsigned int j; + char p[len]; if (!fp || !fread(p, len, 1, fp)) { /* If we can't read from /dev/urandom, do some reasonable effort @@ -578,9 +579,7 @@ void getRandomHexChars(char* p, unsigned int len) { for (j = 0; j < len; j++) { p[j] = charset[p[j] & 0x0F]; } - if (fp) { - fclose(fp); - } + return std::string(p, len); } std::vector& StringSplit(const std::string& s, char delim, std::vector& elems) {