Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:distable compaction during shutdown #2345

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat:distable compaction during shutdown
  • Loading branch information
panlei-coder committed Jan 24, 2024
commit 9402fcb29db7ba4155df5968e1fd1aee7bbbdd4b
2 changes: 2 additions & 0 deletions include/pika_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class DB : public std::enable_shared_from_this<DB>, public pstd::noncopyable {
void Compact(const storage::DataType& type);
void CompactRange(const storage::DataType& type, const std::string& start, const std::string& end);

void SetCompactRangeOptions(const bool is_canceled);

std::shared_ptr<pstd::lock::LockMgr> LockMgr();
void DbRWLockWriter();
void DbRWLockReader();
Expand Down
5 changes: 5 additions & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ class PikaServer : public pstd::noncopyable {
void ProcessCronTask();
double HitRatio();

/*
* disable compact
*/
void DisableCompact();

/*
* lastsave used
*/
Expand Down
2 changes: 2 additions & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,9 @@ void ShutdownCmd::DoInitial() {
// no return
void ShutdownCmd::Do() {
DLOG(WARNING) << "handle \'shutdown\'";
slot->DbRWUnLock();
g_pika_server->Exit();
slot->DbRWLockReader();
res_.SetRes(CmdRes::kNone);
}

Expand Down
7 changes: 7 additions & 0 deletions src/pika_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ void DB::InitKeyScan() {
key_scan_info_.duration = -1; // duration -1 mean the task in processing
}

void Slot::SetCompactRangeOptions(const bool is_canceled) {
if (!opened_) {
return;
}
db_->SetCompactRangeOptions(is_canceled);
}

void DB::DbRWLockWriter() { db_rwlock_.lock(); }

DisplayCacheInfo DB::GetCacheInfo() {
Expand Down
20 changes: 20 additions & 0 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void PikaServer::Start() {
}

void PikaServer::Exit() {
g_pika_server->DisableCompact();
exit_mutex_.unlock();
exit_ = true;
}
Expand Down Expand Up @@ -1674,6 +1675,25 @@ void PikaServer::CheckPubsubClientKill(const std::string& userName, const std::v
});
}

void PikaServer::DisableCompact() {
/* disable auto compactions */
std::unordered_map<std::string, std::string> options_map{{"disable_auto_compactions", "true"}};
storage::Status s = g_pika_server->RewriteStorageOptions(storage::OptionType::kColumnFamily, options_map);
if (!s.ok()) {
LOG(FATAL) << "-ERR Set storage::OptionType::kColumnFamily disable_auto_compactions error: " + s.ToString() + "\r\n";
AlexStocks marked this conversation as resolved.
Show resolved Hide resolved
return;
}
g_pika_conf->SetDisableAutoCompaction("true");

/* cancel in-progress manual compactions */
std::shared_lock rwl(dbs_rw_);
for (const auto& db_item : dbs_) {
for (const auto& slot_item : db_item.second->slots_) {
slot_item.second->SetCompactRangeOptions(true);
}
}
}

void DoBgslotscleanup(void* arg) {
auto p = static_cast<PikaServer*>(arg);
PikaServer::BGSlotsCleanup cleanup = p->bgslots_cleanup();
Expand Down
1 change: 1 addition & 0 deletions src/storage/include/storage/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ class Storage {

Status SetOptions(const OptionType& option_type, const std::string& db_type,
const std::unordered_map<std::string, std::string>& options);
void SetCompactRangeOptions(const bool is_canceled);
Status EnableDymayticOptions(const OptionType& option_type,
const std::string& db_type, const std::unordered_map<std::string, std::string>& options);
Status EnableAutoCompaction(const OptionType& option_type,
Expand Down
12 changes: 12 additions & 0 deletions src/storage/src/redis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Redis::~Redis() {
delete handle;
}
delete db_;

if (default_compact_range_options_.canceled) {
delete default_compact_range_options_.canceled;
}
}

Status Redis::GetScanStartPoint(const Slice& key, const Slice& pattern, int64_t cursor, std::string* start_point) {
Expand Down Expand Up @@ -187,4 +191,12 @@ void Redis::SetWriteWalOptions(const bool is_wal_disable) {
default_write_options_.disableWAL = is_wal_disable;
}

void Redis::SetCompactRangeOptions(const bool is_canceled) {
if (!default_compact_range_options_.canceled) {
default_compact_range_options_.canceled = new std::atomic<bool>(is_canceled);
} else {
default_compact_range_options_.canceled->store(is_canceled);
}
}

} // namespace storage
1 change: 1 addition & 0 deletions src/storage/src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Redis {

Status SetOptions(const OptionType& option_type, const std::unordered_map<std::string, std::string>& options);
void SetWriteWalOptions(const bool is_wal_disable);
void SetCompactRangeOptions(const bool is_canceled);

// Common Commands
virtual Status Open(const StorageOptions& storage_options, const std::string& db_path) = 0;
Expand Down
8 changes: 8 additions & 0 deletions src/storage/src/storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,14 @@ Status Storage::SetOptions(const OptionType& option_type, const std::string& db_
return s;
}

void Storage::SetCompactRangeOptions(const bool is_canceled) {
strings_db_->SetCompactRangeOptions(is_canceled);
hashes_db_->SetCompactRangeOptions(is_canceled);
lists_db_->SetCompactRangeOptions(is_canceled);
sets_db_->SetCompactRangeOptions(is_canceled);
zsets_db_->SetCompactRangeOptions(is_canceled);
}

Status Storage::EnableDymayticOptions(const OptionType& option_type,
const std::string& db_type, const std::unordered_map<std::string, std::string>& options) {
Status s;
Expand Down
Loading