Skip to content

Commit

Permalink
Optimize the CPU usage of the app
Browse files Browse the repository at this point in the history
  • Loading branch information
LLiuJJ committed Aug 11, 2024
1 parent daa3534 commit 5f5125a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 52 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ build-dev:
chmod +x scripts/build-dev.sh
docker run -it --rm -v $(realpath .):/eraft eraft/eraftkv:$(IMAGE_VERSION) /eraft/scripts/build-dev.sh

# run all unit test
tests:
chmod +x scripts/run-tests.sh
docker run -it --rm -v $(realpath .):/eraft eraft/eraftkv:$(IMAGE_VERSION) /eraft/scripts/run-tests.sh

create-net:
docker network create --subnet=172.18.0.0/16 mytestnetwork

Expand Down
39 changes: 25 additions & 14 deletions raftcore/include/eraft/raft_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <condition_variable>
#include <cstdint>
#include <iostream>
#include <mutex>

#include "eraft/estatus.h"
#include "eraft/raft_config.h"
Expand Down Expand Up @@ -113,6 +114,12 @@ class RaftServer {
*/
void RunApply();

/**
* @brief
*
*/
void NotifyToApply();

/**
* @brief
*
Expand Down Expand Up @@ -249,20 +256,6 @@ class RaftServer {
*/
EStatus ElectionStart(bool is_prevote);


/**
* @brief Get the Last Applied Entry object
*
* @return Entry*
*/
eraftkv::Entry* GetLastAppliedEntry();
/**
* @brief Get the First Entry Idx object
*
* @return int64_t
*/
int64_t GetFirstEntryIdx();

/**
* @brief
*
Expand Down Expand Up @@ -550,6 +543,24 @@ class RaftServer {
*/
bool is_snapshoting_;

/**
* @brief
*
*/
bool ready_to_apply_;

/**
* @brief
*
*/
std::mutex apply_ready_mtx_;

/**
* @brief
*
*/
std::condition_variable apply_ready_cv_;

private:
/**
* @brief
Expand Down
57 changes: 28 additions & 29 deletions raftcore/src/raft_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ RaftServer::RaftServer(RaftConfig raft_config,
, open_auto_apply_(true)
, is_snapshoting_(false)
, snap_db_path_(raft_config.snap_path)
, election_running_(true) {
, election_running_(true)
, ready_to_apply_(false) {
this->log_store_ = log_store;
this->store_ = store;
this->net_ = net;
Expand Down Expand Up @@ -106,7 +107,7 @@ RaftServer::~RaftServer() {
}

/**
* @brief
* @brief generate random election timeout for election
*
* @return EStatus
*/
Expand All @@ -120,7 +121,7 @@ EStatus RaftServer::ResetRandomElectionTimeout() {
}

/**
* @brief
* @brief run server mainloop
*
* @param raft_config
* @param log_store
Expand All @@ -141,14 +142,19 @@ RaftServer* RaftServer::RunMainLoop(RaftConfig raft_config,
}

/**
* @brief
* @brief run apply
*
*/
void RaftServer::RunApply() {
while (true) {
{
std::unique_lock<std::mutex> lock(apply_ready_mtx_);
apply_ready_cv_.wait(lock, [this] { return this->ready_to_apply_; });
}
if (open_auto_apply_) {
this->ApplyEntries();
}
this->ready_to_apply_ = false;
}
}

Expand Down Expand Up @@ -467,6 +473,17 @@ EStatus RaftServer::ApplyEntries() {
return EStatus::kOk;
}

/**
* @brief
*
*/
void RaftServer::NotifyToApply() {
std::lock_guard<std::mutex> lock(this->apply_ready_mtx_);
this->ready_to_apply_ = true;
this->apply_ready_cv_.notify_one();
}


/**
* @brief
*
Expand Down Expand Up @@ -882,12 +899,12 @@ EStatus RaftServer::AdvanceCommitIndexForLeader() {
}
sort(match_idxs.begin(), match_idxs.end());
int64_t new_commit_index = match_idxs[match_idxs.size() / 2];
if (new_commit_index > this->commit_idx_) {
if (this->MatchLog(this->current_term_, new_commit_index)) {
this->commit_idx_ = new_commit_index;
this->log_store_->PersisLogMetaState(this->commit_idx_,
this->last_applied_idx_);
}
if (new_commit_index > this->commit_idx_ &&
this->MatchLog(this->current_term_, new_commit_index)) {
this->commit_idx_ = new_commit_index;
this->log_store_->PersisLogMetaState(this->commit_idx_,
this->last_applied_idx_);
this->NotifyToApply();
}
return EStatus::kOk;
}
Expand All @@ -899,13 +916,13 @@ EStatus RaftServer::AdvanceCommitIndexForLeader() {
* @return EStatus
*/
EStatus RaftServer::AdvanceCommitIndexForFollower(int64_t leader_commit) {

int64_t new_commit_index =
std::min(leader_commit, this->log_store_->GetLastEty()->id());
if (new_commit_index > this->commit_idx_) {
this->commit_idx_ = new_commit_index;
this->log_store_->PersisLogMetaState(this->commit_idx_,
this->last_applied_idx_);
this->NotifyToApply();
}
return EStatus::kOk;
}
Expand Down Expand Up @@ -1139,24 +1156,6 @@ EStatus RaftServer::SnapshotingStart(int64_t ety_idx) {
return EStatus::kOk;
}

/**
* @brief Get the Last Applied Entry object
*
* @return Entry*
*/
eraftkv::Entry* RaftServer::GetLastAppliedEntry() {
return nullptr;
}

/**
* @brief Get the First Entry Idx object
*
* @return int64_t
*/
int64_t RaftServer::GetFirstEntryIdx() {
return 0;
}

std::vector<RaftNode*> RaftServer::GetNodes() {
return nodes_;
}
Expand Down
Empty file modified scripts/run-ci-tests.sh
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -xe

# run test exe
/eraft/build/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 add_group 1 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090
/eraft/build/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 query_groups
/eraft/build/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 set_slot 1 0-9
/eraft/build/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 run_bench 600
/eraft/build/example/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 add_group 1 172.18.0.10:8088,172.18.0.11:8089,172.18.0.12:8090
/eraft/build/example/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 query_groups
/eraft/build/example/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 set_slot 1 0-9
/eraft/build/example/eraftkv-ctl 172.18.0.2:8088,172.18.0.3:8089,172.18.0.4:8090 run_bench 100

0 comments on commit 5f5125a

Please sign in to comment.