Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* add run id

* add comment to run_id

* fix if

* add .

* fix comment

* fix

* fix

* fix

---------

Co-authored-by: liuyuecai <[email protected]>
  • Loading branch information
luky116 and liuyuecai authored Jun 2, 2023
1 parent 5efa8ab commit 002a132
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
4 changes: 4 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#define kBinlogReadWinDefaultSize 9000
#define kBinlogReadWinMaxSize 90000
#define configRunIdSize 40

// global class, class members well initialized
class PikaConf : public pstd::BaseConf {
Expand Down Expand Up @@ -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_;
Expand Down Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
7 changes: 7 additions & 0 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>

#include "pstd/include/env.h"
#include "pstd/include/pstd_string.h"

#include "include/pika_define.h"

Expand Down Expand Up @@ -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_);
Expand Down
1 change: 1 addition & 0 deletions src/pstd/include/pstd_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 11 additions & 12 deletions src/pstd/src/pstd_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,20 @@
*/
#include "pstd/include/pstd_string.h"

#include <arpa/inet.h>
#include <dirent.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>

#include <algorithm>

#include "pstd/include/fmacros.h"
#include "pstd/include/pstd_defer.h"

namespace pstd {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<std::string>& StringSplit(const std::string& s, char delim, std::vector<std::string>& elems) {
Expand Down

0 comments on commit 002a132

Please sign in to comment.