├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── LICENSE ├── README.md ├── common.h ├── consensus.hpp ├── docs ├── init_election_pseudocode.cpp ├── meeting_record.md ├── raft开发日志.md ├── raft算法.md └── 选举流程.md ├── entity.h ├── log.hpp ├── main.cpp ├── mem_log.hpp ├── message_bus.hpp ├── nodes.hpp ├── raftcpp.vcxproj ├── raftcpp.vcxproj.filters └── timer.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "iguana"] 2 | path = iguana 3 | url = https://github.com/qicosmos/iguana.git 4 | [submodule "rest_rpc"] 5 | path = rest_rpc 6 | url = https://github.com/qicosmos/rest_rpc.git 7 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.7) 2 | project(raftcpp) 3 | 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11") 5 | 6 | find_package(Boost COMPONENTS system REQUIRED) 7 | include_directories( 8 | "./rest_rpc/include" 9 | "./rest_rpc/third/msgpack/include" 10 | "./iguana" 11 | ) 12 | 13 | add_executable(raftcpp main.cpp) 14 | target_link_libraries(raftcpp ${Boost_LIBRARIES} -lstdc++fs) 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 qicosmos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raftcpp 2 | modern c++ raft library 3 | -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | //#include 7 | #include "entity.h" 8 | 9 | namespace raftcpp { 10 | struct progress_t { 11 | uint64_t match = 0; 12 | uint64_t next = 1; 13 | bool pause = false; 14 | }; 15 | 16 | struct address { 17 | std::string ip; 18 | int port; 19 | int host_id; 20 | progress_t progress; 21 | }; 22 | //REFLECTION(address, ip, port, host_id); 23 | 24 | struct config { 25 | std::vector
peers_addr; 26 | }; 27 | //REFLECTION(config, peers_addr); 28 | 29 | static const int ELECTION_TIMEOUT = 500;//milliseconds 30 | static const int VOTE_TIMEOUT = 500;//milliseconds 31 | static const int RPC_TIMEOUT = 500; 32 | static const int HEARTBEAT_PERIOD = ELECTION_TIMEOUT / 2;//milliseconds 33 | 34 | enum class MessageKey { 35 | restart_election_timer, 36 | election_timeout, 37 | cacel_election_timer, 38 | restart_vote_timer, 39 | vote_timeout, 40 | cancel_vote_timer, 41 | restart_heartbeat_timer, 42 | heartbeat_timeout, 43 | cancel_heartbeat_timer, 44 | pre_request_vote, 45 | request_vote, 46 | append_entry, 47 | heartbeat, 48 | 49 | handle_response_of_request_pre_vote, 50 | handle_response_of_request_vote, 51 | handle_response_of_request_heartbeat, 52 | handle_response_of_append_entry, 53 | 54 | broadcast_request_vote, 55 | broadcast_request_heartbeat, 56 | 57 | active_num, 58 | }; 59 | constexpr MessageKey msg_restart_election_timer = MessageKey::restart_election_timer; 60 | constexpr MessageKey msg_election_timeout = MessageKey::election_timeout; 61 | constexpr MessageKey msg_cancel_election_timer = MessageKey::cacel_election_timer; 62 | 63 | constexpr MessageKey msg_restart_vote_timer = MessageKey::restart_vote_timer; 64 | constexpr MessageKey msg_vote_timeout = MessageKey::vote_timeout; 65 | constexpr MessageKey msg_cancel_vote_timer = MessageKey::cancel_vote_timer; 66 | 67 | constexpr MessageKey msg_restart_heartbeat_timer = MessageKey::restart_heartbeat_timer; 68 | constexpr MessageKey msg_heartbeat_timeout = MessageKey::heartbeat_timeout; 69 | constexpr MessageKey msg_cancel_heartbeat_timer = MessageKey::cancel_heartbeat_timer; 70 | 71 | constexpr MessageKey msg_request_vote = MessageKey::request_vote; 72 | constexpr MessageKey msg_pre_request_vote = MessageKey::pre_request_vote; 73 | constexpr MessageKey msg_append_entry = MessageKey::append_entry; 74 | constexpr MessageKey msg_heartbeat = MessageKey::heartbeat; 75 | 76 | constexpr MessageKey msg_broadcast_request_vote = MessageKey::broadcast_request_vote; 77 | constexpr MessageKey msg_broadcast_request_heartbeat = MessageKey::broadcast_request_heartbeat; 78 | 79 | constexpr MessageKey msg_handle_response_of_request_pre_vote = MessageKey::handle_response_of_request_pre_vote; 80 | constexpr MessageKey msg_handle_response_of_request_vote = MessageKey::handle_response_of_request_vote; 81 | constexpr MessageKey msg_handle_response_of_request_heartbeat = MessageKey::handle_response_of_request_heartbeat; 82 | constexpr MessageKey msg_handle_response_of_append_entry = MessageKey::handle_response_of_append_entry; 83 | 84 | constexpr MessageKey msg_active_num = MessageKey::active_num; 85 | 86 | std::mutex g_print_mtx; 87 | void print(std::string str) { 88 | std::unique_lock lock(g_print_mtx); 89 | std::cout << str; 90 | } 91 | } -------------------------------------------------------------------------------- /consensus.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | #include "entity.h" 6 | #include "common.h" 7 | #include "message_bus.hpp" 8 | #include "mem_log.hpp" 9 | 10 | namespace raftcpp { 11 | static std::default_random_engine g_generator; 12 | 13 | class consensus { 14 | public: 15 | consensus(int host_id, int peers_num) : host_id_(host_id), peers_num_(peers_num), 16 | bus_(message_bus::get()), log_(mem_log_t::get()) { 17 | init(); 18 | } 19 | 20 | void init() { 21 | bus_.subscribe(&consensus::election_timeout, this); 22 | bus_.subscribe(&consensus::vote_timeout, this); 23 | bus_.subscribe(&consensus::start_heartbeat, this); 24 | 25 | bus_.subscribe(&consensus::pre_request_vote, this); 26 | bus_.subscribe(&consensus::request_vote, this); 27 | bus_.subscribe(&consensus::heartbeat, this); 28 | bus_.subscribe(&consensus::append_entry, this); 29 | 30 | bus_.subscribe(&consensus::handle_response_of_request_vote, this); 31 | bus_.subscribe(&consensus::handle_response_of_request_heartbeat, this); 32 | bus_.subscribe(&consensus::handle_response_of_append_entry, this); 33 | 34 | state_ = State::FOLLOWER; 35 | print("start pre_vote timer\n"); 36 | restart_election_timer(ELECTION_TIMEOUT); 37 | } 38 | 39 | /********** rpc service start *********/ 40 | response_vote pre_request_vote(request_vote_t args) { 41 | std::unique_lock lock(mtx_); 42 | response_vote vote = {}; 43 | vote.term = current_term_; 44 | 45 | if (args.term < current_term_) { 46 | return vote; 47 | } 48 | 49 | if (check_state()) { 50 | return vote; 51 | } 52 | 53 | auto last_index = log_.last_index(); 54 | auto last_term = log_.get_term(last_index); 55 | bool log_ok = (args.last_log_term > last_term || 56 | args.last_log_term == last_term && 57 | args.last_log_idx >= last_index); 58 | if (!log_ok) { 59 | vote.vote_granted = false; 60 | } 61 | else { 62 | vote.vote_granted = true; 63 | } 64 | 65 | return vote; 66 | } 67 | 68 | response_vote request_vote(request_vote_t args) { 69 | std::unique_lock lock(mtx_); 70 | response_vote vote{}; 71 | vote.vote_granted = false; 72 | do { 73 | if (args.term < current_term_) { 74 | break; 75 | } 76 | 77 | if (args.term > current_term_) { 78 | if (check_state()) { 79 | break; 80 | } 81 | 82 | step_down_follower(args.term); 83 | } 84 | 85 | if (args.term == 0|| (vote_for_ != -1 && vote_for_ != args.from)) { 86 | break; 87 | } 88 | 89 | auto last_index = log_.last_index(); 90 | auto last_term = log_.get_term(last_index); 91 | bool log_ok = (args.last_log_term > last_term || 92 | args.last_log_term == last_term && 93 | args.last_log_idx >= last_index); 94 | 95 | if ((vote_for_ == -1|| vote_for_==args.from) && log_ok) { 96 | vote_for_ = args.from; 97 | vote.vote_granted = true; 98 | step_down_follower(args.term); 99 | } 100 | 101 | if (!log_ok) 102 | vote.vote_granted = false; 103 | } while (0); 104 | 105 | vote.term = current_term_; 106 | return vote; 107 | } 108 | 109 | void receive_heartbeat_handler(req_heartbeat& args, res_heartbeat& res) { 110 | step_down_follower(current_term_); 111 | reset_leader_id(args.from); 112 | leader_commit_index_ = std::min(args.leader_commit_index, mem_log_t::get().last_index()); 113 | res.term = current_term_; 114 | } 115 | 116 | res_heartbeat heartbeat(req_heartbeat args) { 117 | std::unique_lock lock(mtx_); 118 | print("recieved heartbeat\n"); 119 | res_heartbeat hb{ host_id_, current_term_ }; 120 | if (args.term < current_term_) { 121 | return hb; 122 | } 123 | 124 | if (state_ == State::FOLLOWER) { 125 | receive_heartbeat_handler(args, hb); 126 | return hb; 127 | } 128 | else if (state_ == State::CANDIDATE) { 129 | receive_heartbeat_handler(args, hb); 130 | return hb; 131 | } 132 | else if (state_ == State::LEADER) { 133 | if (args.term > current_term_) { 134 | receive_heartbeat_handler(args, hb); 135 | return hb; 136 | } 137 | } 138 | 139 | return hb; 140 | } 141 | 142 | res_append_entry append_entry(req_append_entry args) { 143 | std::cout << "enter append_entry" << std::endl; 144 | //std::cout << "node {id =" << host_id_ << "} handle append entry request from node { id=" << args.from << "}\n"; 145 | //req_id---log_index in map 146 | res_append_entry res; 147 | res.term = current_term_; 148 | res.from = host_id_; 149 | 150 | if (args.term > current_term_) { 151 | step_down_follower(args.term); 152 | reset_leader_id(args.from); 153 | } 154 | 155 | res.term = current_term_; 156 | if (args.prev_log_index < leader_commit_index_) { 157 | res.last_log_index = leader_commit_index_; 158 | return res; 159 | } 160 | if (args.prev_log_term != log_.get_term(args.prev_log_index)) { 161 | res.reject_hint = log_.last_index(); 162 | return res; 163 | } 164 | uint64_t conflict_index = log_.find_conflict(args.entries); 165 | if (conflict_index == 0) { 166 | res.reject = true; 167 | res.reject_hint = log_.last_index(); 168 | return res; 169 | } 170 | else { 171 | assert(conflict_index > leader_commit_index_); 172 | 173 | auto pos = std::find_if(args.entries.begin(), args.entries.end(), [conflict_index](const entry_t& e) {return e.index == conflict_index; }); 174 | std::vector v(pos, args.entries.end()); 175 | log_.append_may_truncate(v); 176 | } 177 | leader_commit_index_ = std::min(args.leader_commit_index, log_.last_index()); 178 | res.last_log_index = log_.last_index(); 179 | res.reject = false; 180 | return res; 181 | } 182 | /********** rpc service end *********/ 183 | 184 | State state() { 185 | return state_; 186 | } 187 | 188 | uint64_t commit_index() { 189 | return leader_commit_index_; 190 | } 191 | 192 | uint64_t current_term() { 193 | return current_term_; 194 | } 195 | 196 | void set_commit_index(uint64_t comm_idx) { 197 | leader_commit_index_ = comm_idx; 198 | } 199 | 200 | void restart_election_timer(int timeout) { 201 | election_timeout_ = false; 202 | bus_.send_msg(timeout); 203 | } 204 | 205 | void election_timeout() { 206 | print("election timeout\n"); 207 | std::unique_lock lock(mtx_); 208 | election_timeout_ = true; 209 | assert(state_ == State::FOLLOWER); 210 | //if no other peers form configure, just me, become leader 211 | if (peers_num_==0) { 212 | become_candidate(); 213 | return; 214 | } 215 | reset_leader_id(); 216 | pre_vote(); 217 | } 218 | 219 | void pre_vote() { 220 | request_vote_t vote{}; 221 | vote.term = current_term_ + 1; 222 | vote.last_log_idx = log_.last_index(); 223 | 224 | start_vote(true); 225 | print("start pre_vote timer\n"); 226 | restart_election_timer(ELECTION_TIMEOUT); 227 | } 228 | 229 | void become_candidate() { 230 | bus_.send_msg(); 231 | 232 | print("become candidate\n"); 233 | reset_leader_id(); 234 | state_ = State::CANDIDATE; 235 | current_term_++; 236 | vote_for_ = host_id_; 237 | print("start vote timer\n"); 238 | bus_.send_msg(); 239 | 240 | //const LogId last_log_id = _log_manager->last_log_id(true); 241 | 242 | start_vote(); 243 | } 244 | 245 | void start_vote(bool is_pre_vote = false){ 246 | is_pre_vote ? print("start pre_vote\n") : print("start vote\n"); 247 | auto counter = std::make_shared(1); 248 | 249 | handle_majority(*counter, is_pre_vote); 250 | 251 | request_vote_t vote{}; 252 | uint64_t term = current_term_; 253 | vote.term = is_pre_vote ? current_term_ + 1 : current_term_; 254 | vote.last_log_idx = log_.last_index(); 255 | vote.last_log_term = log_.get_term(vote.last_log_idx); 256 | vote.from = host_id_; 257 | 258 | bus_.send_msg(is_pre_vote, term, counter, vote); 259 | } 260 | 261 | void vote_timeout() { 262 | std::unique_lock lock(mtx_); 263 | if (state_ != State::CANDIDATE) { 264 | return; 265 | } 266 | 267 | step_down_follower(current_term_); 268 | } 269 | 270 | void step_down_follower(uint64_t term) { 271 | if (state_ == State::CANDIDATE) { 272 | print("stop vote timer\n"); 273 | bus_.send_msg(); 274 | } 275 | else if (state_ == State::LEADER) { 276 | bus_.send_msg(); 277 | } 278 | 279 | print("become follower\n"); 280 | if (term > current_term_) { 281 | vote_for_ = -1; 282 | current_term_ = term; 283 | } 284 | 285 | state_ = State::FOLLOWER; 286 | print("start pre_vote timer\n"); 287 | restart_election_timer(random_election()); 288 | reset_leader_id(); 289 | } 290 | 291 | void handle_response_of_request_vote(const response_vote& resp_vote, uint64_t term, std::shared_ptr counter, bool is_pre_vote) { 292 | std::unique_lock lock(mtx_); 293 | if (state_ != (is_pre_vote ? State::FOLLOWER : State::CANDIDATE)) { 294 | return; 295 | } 296 | 297 | if (current_term_ != term) { 298 | return; 299 | } 300 | 301 | if (resp_vote.term > current_term_) { 302 | step_down_follower(resp_vote.term); 303 | return; 304 | } 305 | 306 | if (resp_vote.vote_granted) { 307 | (*counter)++; 308 | } 309 | 310 | handle_majority(*counter, is_pre_vote); 311 | } 312 | 313 | void handle_response_of_request_heartbeat(res_heartbeat resp_entry) { 314 | //todo progress 315 | } 316 | 317 | void handle_response_of_append_entry() { 318 | 319 | } 320 | 321 | void handle_majority(int count, bool is_pre_vote) { 322 | if (count > (peers_num_ + 1) / 2) { 323 | if (is_pre_vote) { 324 | print("get major prevote\n"); 325 | become_candidate(); 326 | } 327 | else { 328 | print("get major vote\n"); 329 | become_leader(); 330 | } 331 | } 332 | } 333 | 334 | void become_leader() { 335 | if (state_ != State::CANDIDATE) { 336 | return; 337 | } 338 | 339 | print("become leader\n"); 340 | bus_.send_msg(); 341 | state_ = State::LEADER; 342 | reset_leader_id(host_id_); 343 | bus_.send_msg(); 344 | } 345 | 346 | void start_heartbeat() { 347 | std::unique_lock lock(mtx_); 348 | req_heartbeat entry{}; 349 | entry.from = host_id_; 350 | entry.term = current_term_; 351 | entry.leader_commit_index = leader_commit_index_; 352 | //entry.prev_log_index = todo 353 | //entry.prev_log_term = 354 | 355 | bus_.send_msg(entry); 356 | bus_.send_msg(); 357 | } 358 | 359 | void reset_leader_id(int id = -1) { 360 | leader_id_ = id; 361 | } 362 | 363 | bool check_state() { 364 | if (state_ == State::LEADER) { 365 | if (active_num() + 1 > (peers_num_ + 1) / 2) { 366 | return true; 367 | } 368 | } 369 | else if (state_ == State::FOLLOWER) { 370 | if (leader_id_ != -1 && !election_timeout_) { 371 | return true; 372 | } 373 | } 374 | 375 | return false; 376 | } 377 | 378 | int active_num() { 379 | return bus_.send_msg(); 380 | } 381 | 382 | template 383 | T rand(T n) { 384 | std::uniform_int_distribution dis(0, n - 1); 385 | return dis(g_generator); 386 | } 387 | 388 | int random_election() { 389 | return ELECTION_TIMEOUT + rand(ELECTION_TIMEOUT); 390 | } 391 | 392 | int leader_id() { 393 | return leader_id_; 394 | } 395 | 396 | private: 397 | State state_; 398 | int leader_id_ = -1; 399 | uint64_t current_term_ = 0; 400 | uint64_t leader_commit_index_ = 0; 401 | int vote_for_ = -1; 402 | bool election_timeout_ = false; 403 | 404 | int host_id_; 405 | int peers_num_ = 0; 406 | message_bus& bus_; 407 | mem_log_t& log_; 408 | std::mutex mtx_; 409 | }; 410 | } 411 | 412 | -------------------------------------------------------------------------------- /docs/init_election_pseudocode.cpp: -------------------------------------------------------------------------------- 1 | //init election pseudocode ignore lock 2 | #include 3 | #include 4 | 5 | enum RoleState{ 6 | Follower, 7 | Candidate, 8 | Leader 9 | }; 10 | 11 | enum Tick{ 12 | ElectionTick = 150, 13 | HeartBeatTick = 20 14 | }; 15 | 16 | class RaftState{ 17 | public: 18 | RoleState GetState(); 19 | uint64_t GetTerm(); 20 | void ResetRefresh(); 21 | bool GetRefresh(); 22 | void Vote(uint64_t term) { 23 | if (term > term_) { 24 | term = term_; 25 | refresh_ = true; 26 | state_ = Follower; 27 | } 28 | } 29 | void Heartbeat(uint64_t term) { 30 | if (term > term_) { 31 | term = term_; 32 | refresh_ = true; 33 | state_ = Follower; 34 | }else if (term == term_){ 35 | refresh_ = true; 36 | } 37 | } 38 | void BecomeFollower() {state_ = Follower;} 39 | void BecomeCandidate() { 40 | state_ = Candidate; 41 | term_++; 42 | } 43 | void HoldCandidate() {term_++;} 44 | void BecomeLeader() {state_ = Leader;} 45 | private: 46 | uint64_t term_ = 0; 47 | RoleState state_ = Follower; 48 | bool refresh_ = false; 49 | }; 50 | 51 | class RaftClient{ 52 | public: 53 | bool RpcVote(uint64_t term); 54 | void RpcHeartBeat(uint64_t term); 55 | }; 56 | 57 | class RaftServer{ 58 | public: 59 | //call RollStatus Vote 60 | void RpcVote(); 61 | //call RollStatus HeartBeat 62 | void RpcHeartBeat(); 63 | //call Looper in new thread 64 | void StartLooper(); 65 | private: 66 | void Wait(uint64_t ms); 67 | //[Election, Election + random(Election)] 68 | uint64_t ElectionTimeout(); 69 | void Looper() { 70 | uint64_t count; 71 | while(!stop_) { 72 | switch (state_.GetState()) { 73 | case Follower: 74 | state_.ResetRefresh(); 75 | Wait(ElectionTimeout()); 76 | //fallthrough Candidate immediately 77 | if (!state_.GetRefresh()) state_.BecomeCandidate(); 78 | break; 79 | case Candidate: 80 | count = 0; 81 | for (auto &client : clients_) { 82 | auto ok = client.RpcVote(state_.GetTerm()); 83 | if (ok) count++; 84 | } 85 | if (count + 1 > (clients_.size() + 1) / 2) { 86 | //fallthrough Leader immediately 87 | state_.BecomeLeader(); 88 | }else { 89 | Wait(ElectionTimeout()); 90 | state_.HoldCandidate(); 91 | } 92 | break; 93 | case Leader: 94 | for (auto &client : clients_) { 95 | client.RpcHeartBeat(state_.GetTerm()); 96 | } 97 | Wait(HeartBeatTick); 98 | break; 99 | default: /*asset*/ break; 100 | } 101 | } 102 | } 103 | RaftState state_; 104 | bool stop_ = false; 105 | std::vector clients_; 106 | }; 107 | -------------------------------------------------------------------------------- /docs/meeting_record.md: -------------------------------------------------------------------------------- 1 | # 会议记录 2 | 3 | ## 19.3.9 4 | 5 | 4人 6 | 7 | 江南 哆啦 abbycin Buck'sTribes 8 | 9 | 1 了解各自对raft论文的学习进度 10 | 11 | 2 稍微讨论了一下集群变化 12 | 13 | 3 计划:下周等群主完成rest rpc的异步代码,(各自或参考群主)实现了初始化选举后讨论代码 14 | 15 | 4 计划:讨论测试用例 16 | 17 | 5 计划:实现测试用例 18 | 19 | 后续计划待续 20 | -------------------------------------------------------------------------------- /docs/raft开发日志.md: -------------------------------------------------------------------------------- 1 | # raft开发日志 2 | 3 | ## 第一步:三节点互连 4 | 5 | 现在是创建了N-1个客户端,后续也许可以改成一个客户端,多个连接,减少客户端数量。 6 | 7 | ## 第二步:leader选举+心跳 -------------------------------------------------------------------------------- /docs/raft算法.md: -------------------------------------------------------------------------------- 1 | # raft算法 2 | 3 | # 算法准备 4 | 5 | ## 选举相关的概念 6 | 7 | 1. 当前角色state(follower,candidate,leader) 8 | 2. 任期(current_term) 9 | 3. 投给谁了(votefor) 10 | 4. 票数(vote_count) 11 | 5. 投票结果(granted)支持还是反对 12 | 6. 当前领导人(current_leader) 13 | 14 | ## 日志相关的概念 15 | 1. 日志(term,id,type) 16 | 2. 日志请求(term,leader_id, prev_log_index, prev_log_term, leader_commit_index, log[]) 17 | 18 | ## 选举触发相关的概念 19 | 20 | 1. 心跳超时 21 | 2. 选举超时 22 | 23 | ## 网络相关的概念 24 | 25 | 1. 节点(peer) 26 | 2. rpc投票请求 27 | 3. 处理rpc投票请求 28 | 4. 处理rpc投票响应 29 | 4. 处理rpc心跳(日志)请求 30 | 5. 处理rpc心跳(日志)响应 31 | 6. 自动重连 32 | 7. 处理网络超时 33 | 34 | # follower 35 | 36 | ## follower要做的事 37 | 38 | ### 处理rpc心跳请求 39 | 40 | 1. 看心跳是否超时,超时就忽略 41 | 2. 如果没有选举超时,并且rpc请求中的任期号比自己小,则忽略这次心跳 42 | 3. 如果没有选举超时,并且rpc请求中的任期号不比自己小,则承认该leader的合法地位,设置current_leader为该leader.回到follower状态 43 | 44 | 4. 没有超时则做日志处理 todo 45 | 5. 将等待心跳超时的标识设置为false,无需再等待了。同时将选举超时的标识设置为false,无需再等待了。 46 | 47 | ### 心跳超时处理 48 | 49 | 1. 等待心跳超时: 如果没超时就继续保持follower状态, 如果超时了就等待选举超时. 50 | 2. 等待选举超时: 如果选举没有超时则继续保持follower状态, 如果超时了就转变为candidate状态. 51 | 52 | ### 处理投票请求 53 | 54 | 1. 如果有leader并且没有选举超时则直接回false 55 | 2. 如果term < currentTerm 则返回false 56 | 3. 如果本地的voteFor为空或者为candidateId(幂等性?), 并且候选者的日志至少与接受者的日志一样新,则投给其选票 57 | 4. 怎么定义日志新 58 | ``` 59 | 比较两份日志中最后一条日志条目的索引值和任期号定义谁的日志比较新。 60 | 如果两份日志最后的条目的任期号不同,那么任期号大的日志更加新。 61 | 如果两份日志最后的条目任期号相同,那么日志比较长的那个就更加新。 62 | ``` 63 | 64 | # candiate 65 | 66 | ## candiate要做的事 67 | 68 | ### 发起新的选举 69 | 70 | 1. 递增current_term 71 | 2. 投票给自己 72 | 3. 重置election timer 73 | 4. 向所有的节点发送rpc投票请求 74 | 75 | ### 发起投票请求 76 | 77 | 1. 向所有的peer发送rpc请求 78 | 79 | ### 处理投票结果 80 | 81 | 1. 如果获取了多数投票就转换为leader,设置当前leader为自己,并向其他节点发送心跳来维持统治。 82 | 2. 如果没有收到多数票,保持candidate状态。 83 | 84 | ### 处理投票请求 85 | 86 | 同上 87 | 88 | ### 心跳超时处理 心跳超时之后又收到心跳的处理(这时,可能正在发起选举或投票) 89 | 90 | 1. 如果选举超时,则忽略这次心跳 91 | 2. 如果没有选举超时,并且rpc请求中的任期号比自己小,则忽略这次心跳 92 | 3. 如果没有选举超时,并且rpc请求中的任期号不比自己小,则承认该leader的合法地位,设置current_leader为该leader.回到follower状态 93 | 94 | # leader 95 | 96 | ## leader要做的事 97 | 98 | ### 广播发送心跳 99 | 100 | 向所有的节点发送rpc心跳 101 | 102 | ### 处理心跳响应 103 | 104 | 1. 如果成功,todo 105 | 2. 如果失败,todo 106 | 107 | ### 处理投票请求 108 | 109 | 同上 110 | 111 | -------------------------------------------------------------------------------- /docs/选举流程.md: -------------------------------------------------------------------------------- 1 | ### 节点初始化 2 | 3 | - 状态为Follower 4 | 5 | - 开启选举定时器 6 | 7 | 8 | 9 | ### 选举定时器 超时 10 | 11 | - 加锁 12 | 13 | ```c++ 14 | std::unique_lock lock(mtx_); 15 | ``` 16 | 17 | - 置选举标志为true, 重启选举定时器时置为false 18 | 19 | ```c++ 20 | election_timeout_ = true; 21 | ``` 22 | 23 | - 检查状态必须是Follower 24 | 25 | - 如果配置里没有其他节点,变为候选人 26 | 27 | - 重置 leader id为空 28 | 29 | - 开启(重启) 预投票定时器 30 | 31 | - 执行预投票 32 | 33 | - 设置投票计数为1 34 | - 保存当前的term用于收到预投票响应的时候进行比对 35 | - 填充预投票请求 36 | - 请求的term为当前term + 1 37 | - 请求的last_log_idx 为最新的日志索引 38 | - 请求的last_log_term 为last_log_idx对应的term 39 | - 请求的from 为 本节点的id 40 | - 执行RPC进行预投票 41 | 42 | 43 | 44 | 45 | - 对预投票的请求处理 46 | 47 | - 加锁 48 | 49 | ```C++ 50 | std::unique_lock lock(mtx_); 51 | ``` 52 | 53 | - 如果请求的任期小于当前任期, 投反对票 54 | 55 | - 如果请求的任期大于当前任期, 56 | 57 | - 如果时Leader,且大多数节点处于活跃状态,投反对票 58 | - 如果时Follower, 租约未到期(election_timeout_)且leader id不为空,投反对票 59 | - 执行step down, 60 | 61 | - 如果当前节点的日志的比Candidate(发起请求的节点)更新,投反对票 62 | 63 | - 投赞成票 64 | 65 | 66 | 67 | - 预投票的响应处理 68 | 69 | - 加锁 70 | 71 | ```c++ 72 | std::unique_lock lock(mtx_); 73 | ``` 74 | 75 | - 如果状态不是Follower, 放弃处理 76 | 77 | - 发起请求时传入的term和当前term如果不一致,放弃处理 78 | 79 | - 响应的term大于当前term, 执行回退,设置响应的term为当前term 80 | 81 | - 如果响应里为赞成票,投票计数 加一 82 | 83 | - 判断是否收到大多数投票,如果获得多数票,变成Candidate 84 | 85 | - 预投票定时器超时 86 | 87 | - 不处理 88 | 89 | 90 | 91 | - 变成Candidate之后 92 | 93 | - 关闭选举定时器 94 | - 重置leader_id为空 95 | - 状态修改为Candidate 96 | - 当前任期+1 97 | - 记录已投票的id 为本节点id 98 | - 开启(重启)投票定时器 99 | - 开始投票 100 | 101 | - 开始投票 102 | 103 | - 投票计数置为1 104 | 105 | - 判断是否获取多数票 106 | 107 | - 保存当前任期用于处理投票响应时进行比对 108 | 109 | - 填充投票请求 110 | - 请求的任期为当前任期 111 | - 请求的last_log_idx 为最新的日志索引 112 | - 请求的last_log_term 为last_log_idx对应的term 113 | - 请求的from 为 本节点的id 114 | 115 | - 执行RPC进行预投票 116 | 117 | 118 | 119 | - 接收节点对投票请求的处理 120 | 121 | - 如果请求的任期小于当前任期,投反对票 122 | - 如果请求的任期大于当前的任期,检查状态 123 | - 如果时Leader,且大多数节点处于活跃状态,投反对票 124 | - 如果时Follower, 租约未到期且leader id不为空,投反对票 125 | - 执行step down, 126 | 127 | - 如果已经给其他节点(非发起请求的节点)投过票,投反对票 128 | 129 | - 如果当前节点的日志的比Candidate(发起请求的节点)更新,投反对票 130 | 131 | - 参考论文Chapter3 Basic Raft Algorithm 第13页 RequestVote RPC, Receiver implementation 第2小点 132 | 133 | - 如果未投过票或者给Candidate(发起请求的节点)投过票,投赞成票,记录已投票的id为该节点,执行step down 134 | 135 | 136 | 137 | - 处理投票响应 138 | 139 | - 加锁 140 | 141 | ``` 142 | std::unique_lock lock(mtx_); 143 | ``` 144 | 145 | - 如果状态不是Candidate ,放弃处理 146 | - 如果任期不是当前任期,放弃处理 147 | - 如果响应里的任期大于当前任期,执行step down 148 | - 如果响应里的时赞成票,投票计数+1 149 | - 判断投票是否满足多数票,如果满足,变成leader 150 | 151 | - 投票超时 152 | 153 | - 回退到follower 154 | - 重启选举定时器 155 | 156 | - 变成leader 157 | 158 | - 如果状态不是Candidate, return 159 | - 状态更改为Leader 160 | - 设置leader id 为自己的id 161 | - 启动心跳定时器 162 | 163 | 164 | 165 | - 心跳定时器超时, 发送心跳(TODO 将心跳请求与AppendEntry合并) 166 | 167 | - 填充心跳请求 168 | - from为本机id 169 | - term为节点当前的term 170 | - leader_commit_index为当前节点的commit_index 171 | 172 | 173 | 174 | - 处理心跳请求 175 | 176 | - 加锁 177 | 178 | ```c++ 179 | std::unique_lock lock(mtx_); 180 | ``` 181 | 182 | - 如果请求任期小于当前任期,返回心跳响应 183 | - from为接收节点的id 184 | - term为接收节点的当前任期 185 | 186 | - 如果状态时Follower 187 | - 设置leader id 为请求里的from 188 | - 当前任期更改为请求里的term 189 | - 更新commit index 190 | - 重启选举定时器 191 | - 返回心跳响应 192 | - from为接收节点的id 193 | - term为接收节点的当前任期 194 | 195 | - 如果状态时Candidate 196 | - 执行step down 197 | - 设置leader id 为请求里的from 198 | - 更新commit index 199 | - 返回心跳响应 200 | - from为接收节点的id 201 | - term为接收节点的当前任期 202 | 203 | - 如果状态时Leader(TODO) 204 | 205 | - 如果请求任期大于当前任期 206 | - 执行step down 207 | - 设置leader id 为请求里的from 208 | - 更新commit index 209 | - 返回心跳响应 210 | - from为接收节点的id 211 | - term为接收节点的当前任期 212 | 213 | - 如果请求任期小于或等于当前任期 214 | - 忽略 215 | 216 | -------------------------------------------------------------------------------- /entity.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | namespace raftcpp { 7 | enum class State { 8 | FOLLOWER, 9 | CANDIDATE, 10 | LEADER 11 | }; 12 | 13 | struct entry_t { 14 | /** the entry's term at the point it was created */ 15 | uint64_t term; 16 | 17 | /** the entry's unique ID */ 18 | uint64_t index; 19 | 20 | /** type of entry */ 21 | int32_t type; 22 | 23 | std::string data; 24 | 25 | MSGPACK_DEFINE(term, index, type, data) 26 | }; 27 | 28 | struct request_vote_t { 29 | /** currentTerm, to force other leader/candidate to step down */ 30 | uint64_t term; 31 | 32 | /** candidate requesting vote */ 33 | int from; 34 | 35 | /** index of candidate's last log entry */ 36 | uint64_t last_log_idx; 37 | 38 | /** term of candidate's last log entry */ 39 | uint64_t last_log_term; 40 | 41 | MSGPACK_DEFINE(term, from, last_log_idx, last_log_term) 42 | }; 43 | 44 | struct response_vote { 45 | /** currentTerm, for candidate to update itself */ 46 | uint64_t term = 0; 47 | 48 | /** true means candidate received vote */ 49 | bool vote_granted = false; 50 | 51 | MSGPACK_DEFINE(term, vote_granted) 52 | }; 53 | 54 | struct req_append_entry { 55 | int from; 56 | uint64_t term; 57 | uint64_t prev_log_index; 58 | uint64_t prev_log_term; 59 | uint64_t leader_commit_index; 60 | std::vector entries; 61 | MSGPACK_DEFINE(from, term, prev_log_index, prev_log_term, leader_commit_index, entries) 62 | }; 63 | 64 | struct res_append_entry { 65 | int from; 66 | uint64_t term; 67 | bool reject; 68 | uint64_t last_log_index; 69 | uint64_t reject_hint; //the position of log rejecting 70 | MSGPACK_DEFINE(from, term, reject, last_log_index, reject_hint); 71 | }; 72 | 73 | struct req_heartbeat { 74 | int from; 75 | uint64_t term; 76 | uint64_t leader_commit_index; 77 | MSGPACK_DEFINE(from, term, leader_commit_index); 78 | }; 79 | 80 | struct res_heartbeat { 81 | int from; 82 | uint64_t term; 83 | MSGPACK_DEFINE(from, term); 84 | }; 85 | } -------------------------------------------------------------------------------- /log.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "entity.h" 4 | 5 | namespace raftcpp { 6 | class log_t { 7 | public: 8 | std::pair append(const std::vector& new_entries) { 9 | uint64_t firstIndex = start_index_ + entries_.size(); 10 | uint64_t lastIndex = firstIndex + new_entries.size() - 1; 11 | for (auto& entry : new_entries) { 12 | entries_.push_back(entry); 13 | } 14 | return { firstIndex, lastIndex }; 15 | } 16 | 17 | const entry& get_entry(uint64_t log_index) const { 18 | uint64_t offset = log_index - start_index_; 19 | return entries_.at(offset); 20 | } 21 | 22 | uint64_t get_log_start_index() const { 23 | return start_index_; 24 | } 25 | 26 | uint64_t get_last_log_index() const { 27 | return start_index_ + entries_.size() - 1; 28 | } 29 | 30 | private: 31 | uint64_t start_index_ = 1; 32 | std::deque entries_; 33 | }; 34 | } 35 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "timer.hpp" 6 | #include "nodes.hpp" 7 | #include "consensus.hpp" 8 | using namespace raftcpp; 9 | 10 | struct person { 11 | std::string foo(int& a) { 12 | return std::to_string(a); 13 | } 14 | 15 | void foo1(const double& a) { 16 | std::cout << a << std::endl; 17 | } 18 | int id = 0; 19 | }; 20 | 21 | template 22 | void foo(T t) { 23 | std::cout << t << std::endl; 24 | } 25 | 26 | void foo(int a, double b) { 27 | std::cout << a + b << std::endl; 28 | } 29 | 30 | void foo1(std::string s) { 31 | std::cout << s << std::endl; 32 | } 33 | 34 | void foo3(int a, const std::string& b, std::shared_ptr c) { 35 | std::cout << "foo2\n"; 36 | } 37 | 38 | void test_msg_bus() { 39 | using T = typename function_traits::bare_tuple_type; 40 | message_bus& bus = message_bus::get(); 41 | bus.subscribe(&foo3); 42 | auto c = std::make_shared(2); 43 | bus.send_msg(1, std::string("b"), c); 44 | 45 | //person p; 46 | //bus.subscribe(&person::foo, &p); 47 | //bus.subscribe(&person::foo1, &p); 48 | //bus.subscribe([] {}); 49 | 50 | //std::string s = bus.send_msg(2); 51 | //bus.send_msg(1.5); 52 | 53 | //bus.subscribe(&foo1); 54 | //bus.send_msg(std::string("test")); 55 | 56 | //bus.subscribe([](int t) { 57 | // std::cout << t << std::endl; 58 | //}); 59 | //bus.send_msg(2); 60 | } 61 | 62 | int main() { 63 | //test_msg_bus(); 64 | config conf{ {{"127.0.0.1", 9000, 0}, {"127.0.0.1", 9001, 1}, {"127.0.0.1", 9002, 2}} }; 65 | address host{}; 66 | std::vector
peers; 67 | 68 | { 69 | std::string str; 70 | while (true) { 71 | std::cin >> str; 72 | if (str == "stop") { 73 | break; 74 | } 75 | 76 | int num = atoi(str.data()); 77 | if ((num==0&&str!="0")||num >= conf.peers_addr.size()) { 78 | std::cout << "bad config" << std::endl; 79 | continue; 80 | } 81 | 82 | auto it = conf.peers_addr.begin() + num; 83 | host = *it; 84 | conf.peers_addr.erase(it); 85 | peers = std::move(conf.peers_addr); 86 | break; 87 | } 88 | } 89 | 90 | timer_t timer; 91 | consensus cons(host.host_id, (int)peers.size()); 92 | 93 | nodes_t nodes(host, peers, cons, 1); 94 | 95 | while (true) { 96 | int connected_num = nodes.connect_peers(1); 97 | if (connected_num < (peers.size() + 1) / 2) { 98 | std::cout << "not enough peers" << std::endl; 99 | } 100 | else { 101 | break; 102 | } 103 | } 104 | 105 | //node.init(); 106 | //node.run(); 107 | 108 | std::string str; 109 | std::cin >> str; 110 | } -------------------------------------------------------------------------------- /mem_log.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "entity.h" 3 | 4 | namespace raftcpp { 5 | class mem_log_t { 6 | public: 7 | static mem_log_t& get() { 8 | static mem_log_t instance; 9 | return instance; 10 | } 11 | 12 | std::pair append_may_truncate(const std::vector& new_entries) { 13 | if (new_entries.empty()) { 14 | return { last_index(),last_index() }; 15 | } 16 | auto first_index = new_entries.front().index; 17 | assert(first_index <= last_index() + 1); 18 | if (first_index == last_index() + 1) { 19 | entries_.insert(entries_.end(), new_entries.begin(), new_entries.end()); 20 | return { last_index() - new_entries.size() + 1,last_index() }; 21 | } 22 | if (first_index <= entries_.front().index) { 23 | entries_.clear(); 24 | entries_.insert(entries_.begin(), new_entries.begin(), new_entries.end()); 25 | return { last_index() - new_entries.size() + 1,last_index() }; 26 | } 27 | auto pos = std::find_if(entries_.begin(), entries_.end(), [first_index](const entry_t& entry) {return entry.index == first_index; }); 28 | entries_.erase(pos, entries_.end()); 29 | entries_.insert(entries_.end(), new_entries.begin(), new_entries.end()); 30 | return { last_index() - new_entries.size() + 1,last_index() }; 31 | } 32 | 33 | std::pair append(const std::vector& new_entries) { 34 | if (new_entries.empty()) { 35 | return { last_index(),last_index() }; 36 | } 37 | for (auto it : new_entries) { 38 | entries_.push_back(*it); 39 | } 40 | //LOG_INFO << "after append entries, first_index=" << last_index() - new_entries.size() + 1 << ",last_index=" << last_index(); 41 | return { last_index() - new_entries.size() + 1,last_index() }; 42 | } 43 | std::pair append(std::vector& new_entries) { 44 | auto first_index = start_index_ + entries_.size(); 45 | auto last_index = first_index + new_entries.size() - 1; 46 | for (auto it : new_entries) { 47 | entries_.push_back(it); 48 | } 49 | //LOG_INFO << "after append entries, first_index=" << first_index << ",last_index=" << last_index; 50 | return { first_index,last_index }; 51 | } 52 | 53 | uint64_t find_conflict(const std::vector& entries) { 54 | for (auto& ent : entries) { 55 | if (ent.term != get_term(ent.index)) { 56 | if (ent.index <= last_index()) { 57 | //LOG_INFO << "will cover some entries from:" << ent.index << "\n"; 58 | } 59 | return ent.index; 60 | } 61 | } 62 | 63 | return 0; 64 | } 65 | 66 | std::vector get_entries(uint64_t next, uint64_t nums = 100) { 67 | std::vector results; 68 | if (entries_.empty()) 69 | return results; 70 | uint64_t offset = entries_.front().index; 71 | for (int i = 0; i < nums; ++i) { 72 | uint64_t pos = next - offset + i; 73 | if (pos >= entries_.size()) 74 | break; 75 | results.push_back(entries_.at(pos)); 76 | } 77 | return results; 78 | } 79 | 80 | const entry_t& get_entry(uint64_t index) const { 81 | assert(!entries_.empty()); 82 | uint64_t offset = index - start_index(); 83 | return entries_.at(offset); 84 | } 85 | 86 | uint64_t start_index() const { 87 | if (entries_.empty()) 88 | return 0; 89 | return entries_.front().index; 90 | } 91 | 92 | uint64_t last_index()const { 93 | if (entries_.empty()) 94 | return 0; 95 | return start_index() + entries_.size() - 1; 96 | } 97 | 98 | bool empty() { return entries_.empty(); } 99 | 100 | uint64_t get_term(uint64_t index) { 101 | if (entries_.empty()) 102 | return 0; 103 | if (index < entries_.front().index || index > entries_.back().index) 104 | return 0; 105 | auto offset = index - entries_.front().index; 106 | return entries_.at(offset).term; 107 | } 108 | 109 | private: 110 | mem_log_t() = default; 111 | mem_log_t(const mem_log_t&) = delete; 112 | mem_log_t(mem_log_t&&) = delete; 113 | 114 | uint64_t start_index_ = 1; 115 | std::deque entries_; 116 | }; 117 | } -------------------------------------------------------------------------------- /message_bus.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | namespace raftcpp { 9 | enum class MessageKey; 10 | template 11 | constexpr auto non_const_lvalue_reference_v = !std::is_const_v> && std::is_lvalue_reference_v; 12 | 13 | class message_bus { 14 | public: 15 | static message_bus& get() { 16 | static message_bus instance; 17 | return instance; 18 | } 19 | 20 | template::value>> 21 | void subscribe(const Function & f) { 22 | using Tuple = typename function_traits::tuple_type; 23 | static_assert(!has_non_const_reference::value, "don't support non const lvalue reference!"); 24 | check_duplicate(); 25 | invokers_[key] = { std::bind(&invoker::apply, f, std::placeholders::_1, std::placeholders::_2) }; 26 | } 27 | 28 | template ::value>> 29 | void subscribe(const Function & f, Self * t) { 30 | using Tuple = typename function_traits::tuple_type; 31 | static_assert(!has_non_const_reference::value, "don't support non const lvalue reference!"); 32 | check_duplicate(); 33 | invokers_[key] = { std::bind(&invoker::template apply_mem, f, t, std::placeholders::_1, std::placeholders::_2) }; 34 | } 35 | 36 | //non-void function 37 | template 38 | T send_msg(Args&& ... args) { 39 | auto it = invokers_.find(key); 40 | assert(it != invokers_.end()); 41 | 42 | T t; 43 | call_impl(it, &t, std::forward(args)...); 44 | return t; 45 | } 46 | 47 | //void function 48 | template 49 | void send_msg(Args&& ... args) { 50 | auto it = invokers_.find(key); 51 | assert(it != invokers_.end()); 52 | 53 | call_impl(it, nullptr, std::forward(args)...); 54 | } 55 | 56 | private: 57 | message_bus() {}; 58 | message_bus(const message_bus&) = delete; 59 | message_bus(message_bus&&) = delete; 60 | 61 | template 62 | struct non_const_lvalue_reference_t : public std::integral_constant> { 63 | }; 64 | 65 | template 66 | struct has_non_const_reference; 67 | 68 | template 69 | struct has_non_const_reference> : std::disjunction...> {}; 70 | 71 | template 72 | void call_impl(T it, void* ptr, Args&& ... args) { 73 | using Tuple = decltype(std::make_tuple(std::forward(args)...)); 74 | using storage_type = typename std::aligned_storage::type; 75 | storage_type data; 76 | Tuple* tp = new (&data) Tuple; 77 | *tp = std::forward_as_tuple(std::forward(args)...); 78 | 79 | it->second(tp, ptr); 80 | } 81 | 82 | template 83 | void check_duplicate() { 84 | auto it = invokers_.find(key); 85 | if (it != invokers_.end()) 86 | assert("duplicate register"); 87 | } 88 | 89 | template 90 | struct invoker { 91 | static inline void apply(const Function& func, void* bl, void* result) { 92 | using tuple_type = typename function_traits::bare_tuple_type; 93 | using R = typename function_traits::return_type; 94 | tuple_type* tp = static_cast(bl); 95 | call(func, *tp, result); 96 | } 97 | 98 | template 99 | static typename std::enable_if::value>::type 100 | call(const F& f, std::tuple& tp, void*) { 101 | call_helper(f, std::make_index_sequence{}, tp); 102 | } 103 | 104 | template 105 | static typename std::enable_if::value>::type 106 | call(const F& f, std::tuple& tp, void* result) { 107 | R r = call_helper(f, std::make_index_sequence{}, tp); 108 | if (result) 109 | * (decltype(r)*)result = r; 110 | } 111 | 112 | template 113 | static auto call_helper(const F& f, const std::index_sequence&, std::tuple& tup) {//-> typename std::result_of::type { 114 | using tuple_type = typename function_traits::tuple_type; 115 | return f(((std::tuple_element_t)std::get(tup))...); 116 | } 117 | 118 | template 119 | static inline TupleDest get_dest_tuple(std::index_sequence, TupleSrc& src) { 120 | return std::forward_as_tuple(((std::tuple_element_t)std::get(src))...); 121 | } 122 | 123 | template 124 | static inline void apply_mem(const Function& f, Self* self, void* bl, void* result) { 125 | using bare_tuple_type = typename function_traits::bare_tuple_type; 126 | bare_tuple_type* tp = static_cast(bl); 127 | 128 | using tuple_type = typename function_traits::tuple_type; 129 | auto dest_tp = get_dest_tuple(std::make_index_sequence::value>{}, * tp); 130 | 131 | using return_type = typename function_traits::return_type; 132 | call_mem(f, self, dest_tp, result, std::integral_constant::value>{}); 133 | } 134 | 135 | template 136 | static void call_mem(const F& f, Self* self, const std::tuple& tp, void*, std::true_type) { 137 | call_member_helper(f, self, std::make_index_sequence{}, tp); 138 | } 139 | 140 | template 141 | static void call_mem(const F& f, Self* self, const std::tuple& tp, void* result, std::false_type) { 142 | auto r = call_member_helper(f, self, std::make_index_sequence{}, tp); 143 | if (result) 144 | * (R*)result = r; 145 | } 146 | 147 | template 148 | static R call_member_helper(const F& f, Self* self, const std::index_sequence&, const std::tuple& tup) {//-> decltype((self->*f)(std::get(tup)...)) { 149 | using tuple_type = typename function_traits::tuple_type; 150 | return (self->*f)(((std::tuple_element_t)std::get(tup))...); 151 | } 152 | }; 153 | 154 | private: 155 | std::map> invokers_; 156 | }; 157 | } -------------------------------------------------------------------------------- /nodes.hpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qicosmos/raftcpp/bbd3a57697b62d7cfc618161a43d56d6d29e101a/nodes.hpp -------------------------------------------------------------------------------- /raftcpp.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {AE93BEF4-F68B-470C-8704-315CC7AB689C} 24 | raftcpp 25 | 10.0.17763.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v142 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | rest_rpc/include;rest_rpc/third/msgpack/include;iguana/;$(IncludePath) 74 | 75 | 76 | rest_rpc/include;rest_rpc/third/msgpack/include;iguana/;$(IncludePath) 77 | 78 | 79 | rest_rpc/include;rest_rpc/third/msgpack/include;iguana/;$(IncludePath) 80 | 81 | 82 | rest_rpc/include;rest_rpc/third/msgpack/include;iguana/;$(IncludePath) 83 | 84 | 85 | 86 | Level3 87 | Disabled 88 | true 89 | true 90 | stdcpp17 91 | 4996;%(DisableSpecificWarnings) 92 | 93 | 94 | 95 | 96 | Level3 97 | Disabled 98 | true 99 | true 100 | 4996;%(DisableSpecificWarnings) 101 | stdcpp17 102 | 103 | 104 | 105 | 106 | Level3 107 | MaxSpeed 108 | true 109 | true 110 | true 111 | true 112 | stdcpp17 113 | 4996;%(DisableSpecificWarnings) 114 | 115 | 116 | true 117 | true 118 | 119 | 120 | 121 | 122 | Level3 123 | MaxSpeed 124 | true 125 | true 126 | true 127 | true 128 | 4996;%(DisableSpecificWarnings) 129 | stdcpp17 130 | 131 | 132 | true 133 | true 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /raftcpp.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | 源文件 20 | 21 | 22 | 23 | 24 | 头文件 25 | 26 | 27 | 头文件 28 | 29 | 30 | 头文件 31 | 32 | 33 | 头文件 34 | 35 | 36 | 头文件 37 | 38 | 39 | 头文件 40 | 41 | 42 | 头文件 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /timer.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "common.h" 4 | #include "message_bus.hpp" 5 | 6 | namespace raftcpp { 7 | class timer_t { 8 | public: 9 | timer_t() : election_timer_(ios_), vote_timer_(ios_), heartbeat_timer_(ios_), 10 | work_(ios_), bus_(message_bus::get()) { 11 | init(); 12 | thd_ = std::make_shared([this] { ios_.run(); }); 13 | } 14 | 15 | ~timer_t() { 16 | ios_.stop(); 17 | thd_->join(); 18 | } 19 | 20 | private: 21 | void init() { 22 | bus_.subscribe(&timer_t::restart_election_timer, this); 23 | bus_.subscribe(&timer_t::cacel_election_timer, this); 24 | 25 | bus_.subscribe(&timer_t::restart_vote_timer, this); 26 | bus_.subscribe(&timer_t::cacel_vote_timer, this); 27 | 28 | bus_.subscribe(&timer_t::restart_heartbeat_timer, this); 29 | bus_.subscribe(&timer_t::cacel_heartbeat_timer, this); 30 | } 31 | 32 | void restart_election_timer(int timeout) { 33 | start_timer(election_timer_, timeout); 34 | } 35 | 36 | void cacel_election_timer() { 37 | cancel_timer(election_timer_); 38 | } 39 | 40 | void restart_vote_timer() { 41 | start_timer(vote_timer_, VOTE_TIMEOUT); 42 | } 43 | 44 | void cacel_vote_timer() { 45 | cancel_timer(vote_timer_); 46 | } 47 | 48 | void restart_heartbeat_timer() { 49 | start_timer(heartbeat_timer_, HEARTBEAT_PERIOD); 50 | } 51 | 52 | void cacel_heartbeat_timer() { 53 | cancel_timer(heartbeat_timer_); 54 | } 55 | 56 | template 57 | void start_timer(boost::asio::steady_timer& timer, int timeout) { 58 | timer.expires_from_now(std::chrono::milliseconds(timeout)); 59 | timer.async_wait([this](const boost::system::error_code & ec) { 60 | if (ec) { 61 | return; 62 | } 63 | 64 | bus_.send_msg(); 65 | }); 66 | } 67 | 68 | void cancel_timer(boost::asio::steady_timer& timer) { 69 | boost::system::error_code ignore; 70 | timer.cancel(ignore); 71 | } 72 | private: 73 | asio::io_service ios_; 74 | boost::asio::io_service::work work_; 75 | boost::asio::steady_timer election_timer_; 76 | boost::asio::steady_timer vote_timer_; 77 | boost::asio::steady_timer heartbeat_timer_; 78 | 79 | std::shared_ptr thd_; 80 | bool stop_ = false; 81 | 82 | message_bus& bus_; 83 | }; 84 | } --------------------------------------------------------------------------------