From 5b05c7bffd5acd3eb3218b741f644385b46a1fda Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 11 Aug 2024 10:38:28 +0800 Subject: [PATCH] ad ci build flow --- .github/workflows/ci.yml | 34 ++++++++ raftcore/include/eraft/kv_storage.h | 17 ++-- raftcore/include/eraft/log_storage.h | 19 +++++ raftcore/include/eraft/network.h | 32 ------- raftcore/include/eraft/raft_config.h | 1 + raftcore/include/eraft/util.h | 122 +++++++++++++++++++++++++-- raftcore/src/raft_server.cc | 69 ++++++++++++++- 7 files changed, 245 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..f50513b6 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,34 @@ +name: CI + +on: [push, pull_request] + +jobs: + test-ubuntu: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + - name: Install gcc-10 and g++-10 + run: | + sudo apt install -y gcc-10 g++-10 + sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 20 + sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 20 + - name: Install build dependencies + run: sudo apt-get update && sudo apt-get install -y clang-format build-essential autoconf automake libtool lcov libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev libzstd-dev + - name: Install RocksDB + run: sudo apt-get update && sudo apt-get install librocksdb-dev + - name: Install latest version cmake + run: | + sudo wget https://github.com/Kitware/CMake/releases/download/v3.26.3/cmake-3.26.3-linux-x86_64.sh -q -O /tmp/cmake-install.sh && sudo chmod u+x /tmp/cmake-install.sh + sudo mkdir /opt/cmake-3.26.3 && sudo /tmp/cmake-install.sh --skip-license --prefix=/opt/cmake-3.26.3 + sudo rm /tmp/cmake-install.sh && sudo ln -sf /opt/cmake-3.26.3/bin/* /usr/local/bin + - name: Install gtest manually + run: sudo apt-get install libgtest-dev && cd /usr/src/gtest && sudo cmake CMakeLists.txt && sudo make && sudo cp lib/*.a /usr/lib && sudo ln -s /usr/lib/libgtest.a /usr/local/lib/libgtest.a && sudo ln -s /usr/lib/libgtest_main.a /usr/local/lib/libgtest_main.a + - name: Install grpc + run: sudo git clone https://github.com/grpc/grpc.git && cd grpc && sudo git checkout v1.28.0 && sudo git submodule update --init && sudo mkdir .build && cd .build && sudo cmake .. -DgRPC_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release && sudo make install -j4 && cd .. && sudo rm -rf .build/CMakeCache.txt && cd .build && sudo cmake .. -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DgRPC_PROTOBUF_PROVIDER=package -DgRPC_ZLIB_PROVIDER=package -DgRPC_CARES_PROVIDER=package -DgRPC_SSL_PROVIDER=package -DCMAKE_BUILD_TYPE=Release && sudo make install -j4 + - name: Install Google benchmark + run: sudo git clone https://github.com/google/benchmark.git && sudo git clone https://github.com/google/googletest.git benchmark/googletest && cd benchmark && sudo cmake -E make_directory "build" && sudo cmake -E chdir "build" cmake -DCMAKE_BUILD_TYPE=Release ../ && sudo cmake --build "build" --config Release --target install + - name: Build + run: | + mkdir build && cd build + cmake .. + make -j4 \ No newline at end of file diff --git a/raftcore/include/eraft/kv_storage.h b/raftcore/include/eraft/kv_storage.h index e6e8fda7..47e112c4 100644 --- a/raftcore/include/eraft/kv_storage.h +++ b/raftcore/include/eraft/kv_storage.h @@ -115,6 +115,14 @@ class Storage { */ virtual std::pair GetKV(std::string key) = 0; + /** + * @brief + * + * @param key + * @return EStatus + */ + virtual EStatus DelKV(std::string key) = 0; + /** * @brief * @@ -145,15 +153,6 @@ class Storage { virtual EStatus ProductSST(std::string snap_base_path, std::string sst_file_path) = 0; - /** - * @brief - * - * @param key - * @return EStatus - */ - virtual EStatus DelKV(std::string key) = 0; - - /** * @brief Create a Checkpoint * diff --git a/raftcore/include/eraft/log_storage.h b/raftcore/include/eraft/log_storage.h index 0ba29ecf..7b1fd255 100644 --- a/raftcore/include/eraft/log_storage.h +++ b/raftcore/include/eraft/log_storage.h @@ -50,6 +50,11 @@ class LogStore { */ virtual ~LogStore() {} + /** + * @brief + * + * @return EStatus + */ virtual EStatus Reinit() = 0; /** @@ -146,8 +151,22 @@ class LogStore { */ virtual eraftkv::Entry* GetLastEty() = 0; + /** + * @brief + * + * @param commit_idx + * @param applied_idx + * @return EStatus + */ virtual EStatus PersisLogMetaState(int64_t commit_idx, int64_t applied_idx) = 0; + /** + * @brief + * + * @param commit_idx + * @param applied_idx + * @return EStatus + */ virtual EStatus ReadMetaState(int64_t* commit_idx, int64_t* applied_idx) = 0; }; diff --git a/raftcore/include/eraft/network.h b/raftcore/include/eraft/network.h index b7f7ab50..43d1d456 100644 --- a/raftcore/include/eraft/network.h +++ b/raftcore/include/eraft/network.h @@ -107,35 +107,3 @@ class Network { virtual EStatus InsertPeerNodeConnection(int64_t peer_id, std::string addr) = 0; }; - -/** - * @brief - * - */ -class Event { - public: - /** - * @brief Destroy the Event object - * - */ - virtual ~Event() {} - - /** - * @brief - * - * @param raft - * @param state - */ - virtual void RaftStateChangeEvent(RaftServer* raft, int state) = 0; - - /** - * @brief - * - * @param raft - * @param node - * @param ety - */ - virtual void RaftGroupMembershipChangeEvent(RaftServer* raft, - RaftNode* node, - eraftkv::Entry* ety) = 0; -}; \ No newline at end of file diff --git a/raftcore/include/eraft/raft_config.h b/raftcore/include/eraft/raft_config.h index e22e02ed..27d2cf52 100644 --- a/raftcore/include/eraft/raft_config.h +++ b/raftcore/include/eraft/raft_config.h @@ -41,6 +41,7 @@ #include "eraft/log_storage.h" #include "eraft/network.h" #include "eraft/raft_node.h" + /** * @brief * diff --git a/raftcore/include/eraft/util.h b/raftcore/include/eraft/util.h index 9b9160ba..0dfdf732 100644 --- a/raftcore/include/eraft/util.h +++ b/raftcore/include/eraft/util.h @@ -44,13 +44,49 @@ #include namespace fs = std::filesystem; + +/** + * @brief + * + */ class DirectoryTool { public: - static bool IsDir(const std::string dirpath); - static bool MkDir(const std::string dirpath); - static int StaticDirSize(const std::string dirpath); + /** + * @brief + * + * @param dirpath + * @return true + * @return false + */ + static bool IsDir(const std::string dirpath); + /** + * @brief + * + * @param dirpath + * @return true + * @return false + */ + static bool MkDir(const std::string dirpath); + /** + * @brief + * + * @param dirpath + * @return int + */ + static int StaticDirSize(const std::string dirpath); + /** + * @brief + * + * @param dirpath + * @return std::vector + */ static std::vector ListDirFiles(const std::string dirpath); - static void DeleteDir(const std::string dirpath); + /** + * @brief + * + * @param dirpath + */ + static void DeleteDir(const std::string dirpath); DirectoryTool(); ~DirectoryTool(); }; @@ -58,51 +94,104 @@ class DirectoryTool { class EncodeDecodeTool { public: + /** + * @brief + * + * @param dst + * @param value + */ static void EncodeFixed64(char* dst, uint64_t value) { uint8_t* const buffer = reinterpret_cast(dst); std::memcpy(buffer, &value, sizeof(uint64_t)); } - + /** + * @brief + * + * @param dst + * @param value + */ static void PutFixed64(std::string* dst, uint64_t value) { char buf[sizeof(value)]; EncodeFixed64(buf, value); dst->append(buf, sizeof(buf)); } + /** + * @brief + * + * @param buffer + * @return uint64_t + */ static uint64_t DecodeFixed64(const uint8_t* buffer) { uint64_t result; std::memcpy(&result, buffer, sizeof(uint64_t)); return result; } + /** + * @brief + * + * @param dst + * @param value + */ static void EncodeFixed16(char* dst, uint16_t value) { uint8_t* const buffer = reinterpret_cast(dst); std::memcpy(buffer, &value, sizeof(uint16_t)); } + /** + * @brief + * + * @param dst + * @param value + */ static void PutFixed16(std::string* dst, uint16_t value) { char buf[sizeof(value)]; EncodeFixed16(buf, value); dst->append(buf, sizeof(buf)); } + /** + * @brief + * + * @param buffer + * @return uint16_t + */ static uint16_t DecodeFixed16(const uint8_t* buffer) { uint16_t result; std::memcpy(&result, buffer, sizeof(uint16_t)); return result; } + /** + * @brief + * + * @param dst + * @param value + */ static void EncodeFixed32(char* dst, uint32_t value) { uint8_t* const buffer = reinterpret_cast(dst); std::memcpy(buffer, &value, sizeof(uint32_t)); } + /** + * @brief + * + * @param dst + * @param value + */ static void PutFixed32(std::string* dst, uint32_t value) { char buf[sizeof(value)]; EncodeFixed32(buf, value); dst->append(buf, sizeof(buf)); } + /** + * @brief + * + * @param buffer + * @return uint32_t + */ static uint32_t DecodeFixed32(const uint8_t* buffer) { uint32_t result; std::memcpy(&result, buffer, sizeof(uint32_t)); @@ -167,11 +256,26 @@ class StringUtil { return tmp_s; } + /** + * @brief + * + * @param str + * @param suffix + * @return true + * @return false + */ static bool endsWith(const std::string& str, const std::string& suffix) { return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); } + /** + * @brief + * + * @param str + * @param delim + * @return std::vector + */ static std::vector Split(const std::string& str, char delim) { std::stringstream ss(str); std::string item; @@ -187,5 +291,13 @@ class StringUtil { class HashUtil { public: + /** + * @brief + * + * @param crc + * @param s + * @param l + * @return uint64_t + */ static uint64_t CRC64(uint64_t crc, const char* s, uint64_t l); }; diff --git a/raftcore/src/raft_server.cc b/raftcore/src/raft_server.cc index 17a07ce4..5deca40e 100644 --- a/raftcore/src/raft_server.cc +++ b/raftcore/src/raft_server.cc @@ -43,9 +43,12 @@ #include "eraft/util.h" /** - * @brief Construct a new Raft Server object + * @brief construct a new raftserver * * @param raft_config + * @param log_store + * @param store + * @param net */ RaftServer::RaftServer(RaftConfig raft_config, LogStore* log_store, @@ -92,12 +95,21 @@ RaftServer::RaftServer(RaftConfig raft_config, } } +/** + * @brief destroy the raftserver + * + */ RaftServer::~RaftServer() { delete this->log_store_; delete this->net_; delete this->store_; } +/** + * @brief + * + * @return EStatus + */ EStatus RaftServer::ResetRandomElectionTimeout() { // make rand election timeout in (election_timeout, 2 * election_timout) auto rand_tick = @@ -107,6 +119,15 @@ EStatus RaftServer::ResetRandomElectionTimeout() { return EStatus::kOk; } +/** + * @brief + * + * @param raft_config + * @param log_store + * @param store + * @param net + * @return RaftServer* + */ RaftServer* RaftServer::RunMainLoop(RaftConfig raft_config, LogStore* log_store, Storage* store, @@ -119,7 +140,10 @@ RaftServer* RaftServer::RunMainLoop(RaftConfig raft_config, return svr; } - +/** + * @brief + * + */ void RaftServer::RunApply() { while (true) { if (open_auto_apply_) { @@ -443,6 +467,14 @@ EStatus RaftServer::ApplyEntries() { return EStatus::kOk; } +/** + * @brief + * + * @param last_idx + * @param term + * @return true + * @return false + */ bool RaftServer::IsUpToDate(int64_t last_idx, int64_t term) { return last_idx >= this->log_store_->LastIndex() && term >= this->log_store_->GetLastEty()->term(); @@ -575,6 +607,15 @@ EStatus RaftServer::HandleRequestVoteResp(RaftNode* from_node, return EStatus::kOk; } +/** + * @brief + * + * @param payload + * @param new_log_index + * @param new_log_term + * @param is_success + * @return EStatus + */ EStatus RaftServer::Propose(std::string payload, int64_t* new_log_index, int64_t* new_log_term, @@ -826,7 +867,11 @@ bool RaftServer::MatchLog(int64_t term, int64_t index) { this->log_store_->Get(index)->term() == term); } - +/** + * @brief + * + * @return EStatus + */ EStatus RaftServer::AdvanceCommitIndexForLeader() { std::vector match_idxs; for (auto node : this->nodes_) { @@ -847,6 +892,12 @@ EStatus RaftServer::AdvanceCommitIndexForLeader() { return EStatus::kOk; } +/** + * @brief + * + * @param leader_commit + * @return EStatus + */ EStatus RaftServer::AdvanceCommitIndexForFollower(int64_t leader_commit) { int64_t new_commit_index = @@ -1119,10 +1170,22 @@ int64_t RaftServer::GetLeaderId() { return leader_id_; } +/** + * @brief + * + * @return true + * @return false + */ bool RaftServer::IsLeader() { return leader_id_ == id_; } +/** + * @brief + * + * @return true + * @return false + */ bool RaftServer::IsSnapshoting() { return is_snapshoting_; } \ No newline at end of file