Skip to content

Commit

Permalink
Display a bunch of slot info (OpenAtomFoundation#910)
Browse files Browse the repository at this point in the history
command is like "pkcluster info slot db0:0-6,8"
  • Loading branch information
whoiami committed May 21, 2020
1 parent c3c7cc9 commit 1fdd10e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
15 changes: 11 additions & 4 deletions include/pika_cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include "include/pika_command.h"


Status ParseSlotGroup(const std::string& slot_group,
std::set<uint32_t>* slots);

class PkClusterInfoCmd : public Cmd {
public:
enum InfoSection {
Expand All @@ -17,11 +21,12 @@ class PkClusterInfoCmd : public Cmd {
};
enum InfoRange {
kSingle = 0x0,
kAll
kAll,
kRange
};
PkClusterInfoCmd(const std::string& name, int arity, uint16_t flag)
: Cmd(name, arity, flag),
info_section_(kInfoErr), info_range_(kAll), partition_id_(0) {}
info_section_(kInfoErr), info_range_(kAll) {}
virtual void Do(std::shared_ptr<Partition> partition = nullptr);
virtual Cmd* Clone() override {
return new PkClusterInfoCmd(*this);
Expand All @@ -32,19 +37,21 @@ class PkClusterInfoCmd : public Cmd {
InfoRange info_range_;

std::string table_name_;
uint32_t partition_id_;
std::set<uint32_t> slots_;

virtual void DoInitial() override;
virtual void Clear() {
info_section_ = kInfoErr;
info_range_ = kAll;
table_name_.clear();
partition_id_ = 0;
slots_.clear();
}
const static std::string kSlotSection;
const static std::string kTableSection;
void ClusterInfoTableAll(std::string* info);
void ClusterInfoTable(std::string* info);
void ClusterInfoSlotRange(const std::string& table_name, const std::set<uint32_t> slots,
std::string* info);
void ClusterInfoSlotAll(std::string* info);
Status GetSlotInfo(const std::string table_name, uint32_t partition_id, std::string* info);
bool ParseInfoSlotSubCmd();
Expand Down
23 changes: 17 additions & 6 deletions src/pika_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ void PkClusterInfoCmd::Do(std::shared_ptr<Partition> partition) {
case kInfoSlot:
if (info_range_ == kAll) {
ClusterInfoSlotAll(&info);
} else if (info_range_ == kSingle) {
} else if (info_range_ == kRange) {
// doesn't process error, if error return nothing
GetSlotInfo(table_name_, partition_id_, &info);
ClusterInfoSlotRange(table_name_, slots_, &info);
}
break;
case kInfoTable:
Expand Down Expand Up @@ -107,7 +107,7 @@ void PkClusterInfoCmd::ClusterInfoTable(std::string* info) {
bool PkClusterInfoCmd::ParseInfoSlotSubCmd() {
if (argv_.size() > 3) {
if (argv_.size() == 4) {
info_range_ = kSingle;
info_range_ = kRange;
std::string tmp(argv_[3]);
size_t pos = tmp.find(':');
std::string slot_num_str;
Expand All @@ -118,12 +118,10 @@ bool PkClusterInfoCmd::ParseInfoSlotSubCmd() {
table_name_ = tmp.substr(0, pos);
slot_num_str = tmp.substr(pos + 1);
}
unsigned long partition_id;
if (!slash::string2ul(slot_num_str.c_str(), slot_num_str.size(), &partition_id)) {
if (!ParseSlotGroup(slot_num_str, &slots_).ok()) {
res_.SetRes(CmdRes::kInvalidParameter, kCmdNamePkClusterInfo);
return false;
}
partition_id_ = partition_id;
} else {
res_.SetRes(CmdRes::kWrongNum, kCmdNamePkClusterInfo);
return false;
Expand Down Expand Up @@ -151,6 +149,19 @@ bool PkClusterInfoCmd::ParseInfoTableSubCmd() {
return true;
}

void PkClusterInfoCmd::ClusterInfoSlotRange(const std::string& table_name,
const std::set<uint32_t> slots, std::string* info) {
std::stringstream tmp_stream;
for (auto partition_id : slots) {
std::string p_info;
Status s = GetSlotInfo(table_name, partition_id, &p_info);
if (!s.ok()) {
continue;
}
tmp_stream << p_info;
}
info->append(tmp_stream.str());
}

void PkClusterInfoCmd::ClusterInfoSlotAll(std::string* info) {
std::stringstream tmp_stream;
Expand Down

0 comments on commit 1fdd10e

Please sign in to comment.