Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* pika support codis slot mgirate (OpenAtomFoundation#48)

* codis slot migrate support

* add slotsreload slotsreloadoff slotsdel slotsscan command

* info command add slots reloading status

* support migrate large keys

* slotsmgrttagone command response fix

* dbsize command get  keys number in time if set slotmigrate mode

* fix some bugs like config get *, slots migrate command only execute on master

* slot migrate support keys with expire time (OpenAtomFoundation#49)

* support masterauth and fix slave replication auth (OpenAtomFoundation#50)

* support masterauth and fix slave replication auth

* compitible with official auth
  • Loading branch information
KernelMaker authored Jan 12, 2017
1 parent 3618f6e commit 4895ee1
Show file tree
Hide file tree
Showing 18 changed files with 1,332 additions and 5 deletions.
4 changes: 4 additions & 0 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ write-buffer-size : 268435456
timeout : 60
# Requirepass
requirepass :
# Masterauth
masterauth :
# Userpass
userpass :
# User Blacklist
Expand All @@ -26,6 +28,8 @@ userblacklist :
dump-prefix :
# daemonize [yes | no]
#daemonize : yes
# slotmigrate [yes | no]
#slotmigrate : no
# Dump Path
dump-path : ./dump/
# pidfile Path
Expand Down
12 changes: 12 additions & 0 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ const std::string kCmdNameConfig = "config";
const std::string kCmdNameMonitor = "monitor";
const std::string kCmdNameDbsize = "dbsize";

//Migrate slot
const std::string kCmdNameSlotsMgrtSlot = "slotsmgrtslot";
const std::string kCmdNameSlotsMgrtTagSlot = "slotsmgrttagslot";
const std::string kCmdNameSlotsMgrtOne = "slotsmgrtone";
const std::string kCmdNameSlotsMgrtTagOne = "slotsmgrttagone";
const std::string kCmdNameSlotsInfo = "slotsinfo";
const std::string kCmdNameSlotsHashKey = "slotshashkey";
const std::string kCmdNameSlotsReload = "slotsreload";
const std::string kCmdNameSlotsReloadOff = "slotsreloadoff";
const std::string kCmdNameSlotsDel = "slotsdel";
const std::string kCmdNameSlotsScan = "slotsscan";

//Kv
const std::string kCmdNameSet = "set";
const std::string kCmdNameGet = "get";
Expand Down
12 changes: 12 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class PikaConf : public slash::BaseConf {
int timeout() { RWLock l(&rwlock_, false); return timeout_; }

std::string requirepass() { RWLock l(&rwlock_, false); return requirepass_; }
std::string masterauth() { RWLock l(&rwlock_, false); return masterauth_; }
bool slotmigrate() { RWLock l(&rwlock_, false); return slotmigrate_; }
std::string bgsave_path() { RWLock l(&rwlock_, false); return bgsave_path_; }
std::string bgsave_prefix() { RWLock l(&rwlock_, false); return bgsave_prefix_; }
std::string userpass() { RWLock l(&rwlock_, false); return userpass_; }
Expand Down Expand Up @@ -91,6 +93,14 @@ class PikaConf : public slash::BaseConf {
RWLock l(&rwlock_, true);
requirepass_ = value;
}
void SetMasterAuth(const std::string &value) {
RWLock l(&rwlock_, true);
masterauth_ = value;
}
void SetSlotMigrate(const std::string &value) {
RWLock l(&rwlock_, true);
slotmigrate_ = (value == "yes") ? true : false;
}
void SetUserPass(const std::string &value) {
RWLock l(&rwlock_, true);
userpass_ = value;
Expand Down Expand Up @@ -143,8 +153,10 @@ class PikaConf : public slash::BaseConf {
int write_buffer_size_;
int log_level_;
bool daemonize_;
bool slotmigrate_;
int timeout_;
std::string requirepass_;
std::string masterauth_;
std::string userpass_;
std::vector<std::string> user_blacklist_;
std::string bgsave_path_;
Expand Down
50 changes: 50 additions & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,51 @@ class PikaServer
bgsave_info_.bgsaving = false;
}


/*
* BGSlotsReload used
*/
struct BGSlotsReload {
bool reloading;
time_t start_time;
std::string s_start_time;
int64_t cursor;
std::string pattern;
int64_t count;
BGSlotsReload() : reloading(false), cursor(0), pattern("*"), count(100){}
void Clear() {
reloading = false;
pattern = "*";
count = 100;
cursor = 0;
}
};
BGSlotsReload bgslots_reload() {
slash::MutexLock l(&bgsave_protector_);
return bgslots_reload_;
}
bool GetSlotsreloading() {
slash::MutexLock l(&bgsave_protector_);
return bgslots_reload_.reloading;
}
void SetSlotsreloading(bool reloading) {
slash::MutexLock l(&bgsave_protector_);
bgslots_reload_.reloading = reloading;
}
void SetSlotsreloadingCursor(int64_t cursor) {
slash::MutexLock l(&bgsave_protector_);
bgslots_reload_.cursor = cursor;
}
int64_t GetSlotsreloadingCursor() {
slash::MutexLock l(&bgsave_protector_);
return bgslots_reload_.cursor;
}
void Bgslotsreload();
void StopBgslotsreload() {
slash::MutexLock l(&bgsave_protector_);
bgslots_reload_.reloading = false;
}

/*
* PurgeLog used
*/
Expand Down Expand Up @@ -309,6 +354,11 @@ void SignalNextBinlogBGSerial();
bgsave_info_.Clear();
}

/*
* BGSlotsReload use
*/
BGSlotsReload bgslots_reload_;
static void DoBgslotsreload(void* arg);

/*
* Purgelogs use
Expand Down
120 changes: 120 additions & 0 deletions include/pika_slot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#ifndef PIKA_SLOT_H_
#define PIKA_SLOT_H_

#include "pika_command.h"
#include "pika_client_conn.h"
#include "strings.h"

const std::string SlotKeyPrefix = "_internal:slotkey:4migrate:";
const size_t MaxKeySendSize = 10 * 1024;
//crc 32
#define HASH_SLOTS_MASK 0x000003ff
#define HASH_SLOTS_SIZE (HASH_SLOTS_MASK + 1)

const uint32_t IEEE_POLY = 0xedb88320;
extern uint32_t crc32tab[256];

void CRC32TableInit(uint32_t poly);

extern void InitCRC32Table();

extern uint32_t CRC32Update(uint32_t crc, const char *buf, int len);

extern int SlotNum(const std::string &str);
extern int KeyType(const std::string key, std::string &key_type);

extern void SlotKeyAdd(const std::string type, const std::string key);
extern void SlotKeyRem(const std::string key);
extern void KeyNotExistsRem(const std::string type, const std::string key);


class SlotsMgrtTagSlotCmd : public Cmd {
public:
SlotsMgrtTagSlotCmd() {}
virtual void Do();
private:
std::string dest_ip_;
int64_t dest_port_;
int64_t timeout_ms_;
int64_t slot_num_;
std::string key_;
char key_type_;

virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
int SlotKeyPop();
};

class SlotsMgrtTagOneCmd : public Cmd {
public:
SlotsMgrtTagOneCmd() {}
virtual void Do();
private:
std::string dest_ip_;
int64_t dest_port_;
int64_t timeout_ms_;
std::string key_;
int64_t slot_num_;
char key_type_;

virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
int KeyTypeCheck();
int SlotKeyRemCheck();
};

class SlotsInfoCmd : public Cmd {
public:
SlotsInfoCmd() {}
virtual void Do();
private:
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class SlotsHashKeyCmd : public Cmd {
public:
SlotsHashKeyCmd() {}
virtual void Do();
private:
std::vector<std::string> keys_;
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class SlotsReloadCmd : public Cmd {
public:
SlotsReloadCmd() {}
virtual void Do();
private:
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class SlotsReloadOffCmd : public Cmd {
public:
SlotsReloadOffCmd() {}
virtual void Do();
private:
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class SlotsDelCmd : public Cmd {
public:
SlotsDelCmd() {}
virtual void Do();
private:
std::vector<std::string> slots_;
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class SlotsScanCmd : public Cmd {
public:
SlotsScanCmd() : pattern_("*"), count_(10) {}
virtual void Do();
private:
std::string key_, pattern_;
int64_t cursor_, count_;
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
virtual void Clear() {
pattern_ = "*";
count_ = 10;
}
};

#endif
2 changes: 2 additions & 0 deletions src/pika.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pika_command.h"
#include "pika_conf.h"
#include "pika_define.h"
#include "pika_slot.h"
#include "env.h"

PikaConf *g_pika_conf;
Expand Down Expand Up @@ -165,6 +166,7 @@ int main(int argc, char *argv[]) {
PikaGlogInit();
PikaSignalSetup();
InitCmdInfoTable();
InitCRC32Table();

LOG(INFO) << "Server at: " << path;
g_pika_server = new PikaServer();
Expand Down
45 changes: 43 additions & 2 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "pika_conf.h"
#include "pika_admin.h"
#include "pika_server.h"
#include "pika_slot.h"

#include <sys/utsname.h>

Expand Down Expand Up @@ -532,6 +533,10 @@ void InfoCmd::InfoStats(std::string &info) {
time_t current_time_s = time(NULL);
tmp_stream << "is_bgsaving:" << (is_bgsaving ? "Yes, " : "No, ") << bgsave_info.s_start_time << ", "
<< (is_bgsaving ? (current_time_s - bgsave_info.start_time) : 0) << "\r\n";
PikaServer::BGSlotsReload bgslotsreload_info = g_pika_server->bgslots_reload();
bool is_reloading = g_pika_server->GetSlotsreloading();
tmp_stream << "is_slots_reloading:" << (is_reloading ? "Yes, " : "No, ") << bgslotsreload_info.s_start_time << ", "
<< (is_reloading ? (current_time_s - bgslotsreload_info.start_time) : 0) << "\r\n";
PikaServer::KeyScanInfo key_scan_info = g_pika_server->key_scan_info();
bool is_scaning = g_pika_server->key_scaning();
tmp_stream << "is_scaning_keyspace:" << (is_scaning ? ("Yes, " + key_scan_info.s_start_time) + "," : "No");
Expand Down Expand Up @@ -776,6 +781,10 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "requirepass");
EncodeString(&ret, g_pika_conf->requirepass());
} else if (get_item == "masterauth") {
ret = "*2\r\n";
EncodeString(&ret, "masterauth");
EncodeString(&ret, g_pika_conf->masterauth());
} else if (get_item == "userpass") {
ret = "*2\r\n";
EncodeString(&ret, "userpass");
Expand All @@ -792,6 +801,10 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "daemonize");
EncodeString(&ret, g_pika_conf->daemonize() ? "yes" : "no");
} else if (get_item == "slotmigrate") {
ret = "*2\r\n";
EncodeString(&ret, "slotmigrate");
EncodeString(&ret, g_pika_conf->slotmigrate() ? "yes" : "no");
} else if (get_item == "dump-path") {
ret = "*2\r\n";
EncodeString(&ret, "dump-path");
Expand Down Expand Up @@ -857,7 +870,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, "slaveof");
EncodeString(&ret, g_pika_conf->slaveof());
} else if (get_item == "*") {
ret = "*66\r\n";
ret = "*70\r\n";
EncodeString(&ret, "port");
EncodeInt32(&ret, g_pika_conf->port());
EncodeString(&ret, "thread-num");
Expand All @@ -880,12 +893,16 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeInt32(&ret, g_pika_conf->timeout());
EncodeString(&ret, "requirepass");
EncodeString(&ret, g_pika_conf->requirepass());
EncodeString(&ret, "masterauth");
EncodeString(&ret, g_pika_conf->masterauth());
EncodeString(&ret, "userpass");
EncodeString(&ret, g_pika_conf->userpass());
EncodeString(&ret, "userblacklist");
EncodeString(&ret, g_pika_conf->suser_blacklist());
EncodeString(&ret, "daemonize");
EncodeInt32(&ret, g_pika_conf->daemonize());
EncodeString(&ret, "slotmigrate");
EncodeInt32(&ret, g_pika_conf->slotmigrate());
EncodeString(&ret, "dump-path");
EncodeString(&ret, g_pika_conf->bgsave_path());
EncodeString(&ret, "dump-prefix");
Expand Down Expand Up @@ -932,10 +949,12 @@ void ConfigCmd::ConfigGet(std::string &ret) {
void ConfigCmd::ConfigSet(std::string& ret) {
std::string set_item = config_args_v_[1];
if (set_item == "*") {
ret = "*13\r\n";
ret = "*15\r\n";
EncodeString(&ret, "loglevel");
EncodeString(&ret, "timeout");
EncodeString(&ret, "requirepass");
EncodeString(&ret, "masterauth");
EncodeString(&ret, "slotmigrate");
EncodeString(&ret, "userpass");
EncodeString(&ret, "userblacklist");
EncodeString(&ret, "dump-prefix");
Expand Down Expand Up @@ -973,6 +992,12 @@ void ConfigCmd::ConfigSet(std::string& ret) {
} else if (set_item == "requirepass") {
g_pika_conf->SetRequirePass(value);
ret = "+OK\r\n";
} else if (set_item == "masterauth") {
g_pika_conf->SetMasterAuth(value);
ret = "+OK\r\n";
} else if (set_item == "slotmigrate") {
g_pika_conf->SetSlotMigrate(value);
ret = "+OK\r\n";
} else if (set_item == "userpass") {
g_pika_conf->SetUserPass(value);
ret = "+OK\r\n";
Expand Down Expand Up @@ -1081,6 +1106,22 @@ void DbsizeCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info)
}

void DbsizeCmd::Do() {
if (g_pika_conf->slotmigrate()){
int64_t dbsize = 0;
for (int i = 0; i < HASH_SLOTS_SIZE; ++i){
int64_t card = 0;
card = g_pika_server->db()->SCard(SlotKeyPrefix+std::to_string(i));
if (card >= 0) {
dbsize += card;
}else {
res_.SetRes(CmdRes::kErrOther, "Get dbsize error");
return;
}
}
res_.AppendInteger(dbsize);
return;
}

PikaServer::KeyScanInfo key_scan_info = g_pika_server->key_scan_info();
std::vector<uint64_t> &key_nums_v = key_scan_info.key_nums_v;
if (key_scan_info.key_nums_v.size() != 5) {
Expand Down
Loading

0 comments on commit 4895ee1

Please sign in to comment.