Skip to content

Commit

Permalink
feat: add defragment command
Browse files Browse the repository at this point in the history
  • Loading branch information
BorysTheDev committed May 3, 2024
1 parent a95419b commit 63abee0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/server/engine_shard_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ void EngineShard::DefragTaskState::ResetScanState() {
// 3. in case the above is OK, make sure that we have a "gap" between usage and commited memory
// (control by mem_defrag_waste_threshold flag)
bool EngineShard::DefragTaskState::CheckRequired() {
if (cursor > kCursorDoneState || underutilized_found) {
if (is_force_defrag || cursor > kCursorDoneState || underutilized_found) {
is_force_defrag = false;
VLOG(2) << "cursor: " << cursor << " and underutilized_found " << underutilized_found;
return true;
}
Expand All @@ -265,6 +266,10 @@ bool EngineShard::DefragTaskState::CheckRequired() {
return false;
}

void EngineShard::ForceDefrag() {
defrag_state_.is_force_defrag = true;
}

bool EngineShard::DoDefrag() {
// --------------------------------------------------------------------------
// NOTE: This task is running with exclusive access to the shard.
Expand Down
3 changes: 3 additions & 0 deletions src/server/engine_shard_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,14 @@ class EngineShard {

TxQueueInfo AnalyzeTxQueue() const;

void ForceDefrag();

private:
struct DefragTaskState {
size_t dbid = 0u;
uint64_t cursor = 0u;
bool underutilized_found = false;
bool is_force_defrag = false;

// check the current threshold and return true if
// we need to do the defragmentation
Expand Down
8 changes: 8 additions & 0 deletions src/server/memory_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ void MemoryCmd::Run(CmdArgList args) {
return Track(args);
}

if (sub_cmd == "DEFRAGMENT") {
shard_set->pool()->DispatchOnAll([this](util::ProactorBase*) {
if (auto* shard = EngineShard::tlocal(); shard)
shard->ForceDefrag();
});
return cntx_->SendSimpleString("OK");
}

string err = UnknownSubCmd(sub_cmd, "MEMORY");
return cntx_->SendError(err, kSyntaxErrType);
}
Expand Down

0 comments on commit 63abee0

Please sign in to comment.