Skip to content

Commit

Permalink
修复compact,bgsave,info keyspace命令未指定db名称 (#2194)
Browse files Browse the repository at this point in the history
Co-authored-by: denghao.denghao <denghao.denghao@bigo.sg>
  • Loading branch information
u6th9d and denghao.denghao authored Dec 8, 2023
1 parent a6d90d8 commit fe251d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class PikaServer : public pstd::noncopyable {
bool IsDBExist(const std::string& db_name);
bool IsDBSlotExist(const std::string& db_name, uint32_t slot_id);
bool IsDBBinlogIoError(const std::string& db_name);
std::set<std::string> GetAllDBName();
pstd::Status DoSameThingSpecificDB(const std::set<std::string>& dbs, const TaskArg& arg);
std::shared_mutex& GetDBLock() {
return dbs_rw_;
Expand Down
8 changes: 7 additions & 1 deletion src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ void BgsaveCmd::DoInitial() {
bgsave_dbs_.insert(db);
}
}
} else {
bgsave_dbs_ = g_pika_server->GetAllDBName();
}
}

Expand All @@ -323,8 +325,10 @@ void CompactCmd::DoInitial() {

if (argv_.size() == 1) {
struct_type_ = "all";
compact_dbs_ = g_pika_server->GetAllDBName();
} else if (argv_.size() == 2) {
struct_type_ = argv_[1];
compact_dbs_ = g_pika_server->GetAllDBName();
} else if (argv_.size() == 3) {
std::vector<std::string> dbs;
pstd::StringSplit(argv_[1], COMMA, dbs);
Expand Down Expand Up @@ -866,6 +870,8 @@ void InfoCmd::DoInitial() {
keyspace_scan_dbs_.insert(db);
}
}
} else {
keyspace_scan_dbs_ = g_pika_server->GetAllDBName();
}
LogCommand();
return;
Expand Down Expand Up @@ -1234,7 +1240,7 @@ void InfoCmd::InfoKeyspace(std::string& info) {

std::shared_lock rwl(g_pika_server->dbs_rw_);
for (const auto& db_item : g_pika_server->dbs_) {
if (keyspace_scan_dbs_.empty() || keyspace_scan_dbs_.find(db_item.first) != keyspace_scan_dbs_.end()) {
if (keyspace_scan_dbs_.find(db_item.first) != keyspace_scan_dbs_.end()) {
db_name = db_item.second->GetDBName();
key_scan_info = db_item.second->GetKeyScanInfo();
key_infos = key_scan_info.key_infos;
Expand Down
17 changes: 10 additions & 7 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@ bool PikaServer::IsDBBinlogIoError(const std::string& db_name) {
return db ? db->IsBinlogIoError() : true;
}

std::set<std::string> PikaServer::GetAllDBName() {
std::set<std::string> dbs;
std::shared_lock l(dbs_rw_);
for (const auto& db_item : dbs_) {
dbs.insert(db_item.first);
}
return dbs;
}

Status PikaServer::DoSameThingSpecificDB(const std::set<std::string>& dbs, const TaskArg& arg) {
std::shared_lock rwl(dbs_rw_);
for (const auto& db_item : dbs_) {
Expand Down Expand Up @@ -1357,13 +1366,7 @@ void PikaServer::AutoCompactRange() {
if (last_check_compact_time_.tv_sec == 0 || now.tv_sec - last_check_compact_time_.tv_sec >= interval * 3600) {
gettimeofday(&last_check_compact_time_, nullptr);
if ((static_cast<double>(free_size) / static_cast<double>(total_size)) * 100 >= usage) {
std::set<std::string> dbs;
{
std::shared_lock l(dbs_rw_);
for (const auto& db_item : dbs_) {
dbs.insert(db_item.first);
}
}
std::set<std::string> dbs = g_pika_server->GetAllDBName();
Status s = DoSameThingSpecificDB(dbs, {TaskType::kCompactAll});
if (s.ok()) {
LOG(INFO) << "[Interval]schedule compactRange, freesize: " << free_size / 1048576
Expand Down

0 comments on commit fe251d5

Please sign in to comment.