|
| 1 | +#ifndefRAFT_H |
| 2 | +#defineRAFT_H |
| 3 | + |
| 4 | +#include<arpa/inet.h> |
| 5 | +#include<stdbool.h> |
| 6 | +#include"dtmdlimits.h" |
| 7 | + |
| 8 | +#defineNOBODY -1 |
| 9 | + |
| 10 | +#defineMAJORITY_IS_NOT_ENOUGH// wait for unanimous ack for applying a new entry |
| 11 | + |
| 12 | +#defineDEFAULT_LISTENHOST "0.0.0.0" |
| 13 | +#defineDEFAULT_LISTENPORT 5431 |
| 14 | + |
| 15 | +#defineROLE_FOLLOWER 0 |
| 16 | +#defineROLE_CANDIDATE 1 |
| 17 | +#defineROLE_LEADER 2 |
| 18 | + |
| 19 | +#ifRAFT_KEEP_APPLIED >=RAFT_LOGLEN |
| 20 | +#error please ensure RAFT_KEEP_APPLIED < RAFT_LOGLEN |
| 21 | +#endif |
| 22 | + |
| 23 | +#ifHEARTBEAT_TIMEOUT_MS >=ELECTION_TIMEOUT_MS_MIN |
| 24 | +#error please ensure HEARTBEAT_TIMEOUT_MS < ELECTION_TIMEOUT_MS_MIN (considerably) |
| 25 | +#endif |
| 26 | + |
| 27 | +#ifELECTION_TIMEOUT_MS_MIN >=ELECTION_TIMEOUT_MS_MAX |
| 28 | +#error please ensure ELECTION_TIMEOUT_MS_MIN < ELECTION_TIMEOUT_MS_MAX |
| 29 | +#endif |
| 30 | + |
| 31 | +// raft module does not care what you mean by action and argument |
| 32 | +typedefstructraft_entry_t { |
| 33 | +intterm; |
| 34 | +boolsnapshot;// true if this is a snapshot entry |
| 35 | +union { |
| 36 | +struct {// snapshot == false |
| 37 | +intaction; |
| 38 | +intargument; |
| 39 | +}; |
| 40 | +struct {// snapshot == true |
| 41 | +intminarg; |
| 42 | +intmaxarg; |
| 43 | +}; |
| 44 | +}; |
| 45 | +}raft_entry_t; |
| 46 | + |
| 47 | +typedefvoid (*raft_applier_t)(intaction,intargument); |
| 48 | + |
| 49 | +typedefstructraft_log_t { |
| 50 | +intfirst; |
| 51 | +intsize;// number of entries past first |
| 52 | +intacked;// number of entries replicated to the majority of servers |
| 53 | +intapplied;// number of entries applied to the state machine |
| 54 | +raft_entry_tentries[RAFT_LOGLEN];// wraps around |
| 55 | +}raft_log_t; |
| 56 | + |
| 57 | +typedefstructraft_server_t { |
| 58 | +intseqno;// the rpc sequence number |
| 59 | +inttosend;// index of the next entry to send |
| 60 | +intacked;// index of the highest entry known to be replicated |
| 61 | + |
| 62 | +char*host; |
| 63 | +intport; |
| 64 | +structsockaddr_inaddr; |
| 65 | +}raft_server_t; |
| 66 | + |
| 67 | +typedefstructraft_t { |
| 68 | +intterm;// current term (latest term we have seen) |
| 69 | +intvote;// who received our vote in current term |
| 70 | +introle; |
| 71 | +intme;// my id |
| 72 | +intvotes;// how many votes are for me (if candidate) |
| 73 | +intleader;// the id of the leader |
| 74 | +raft_log_tlog; |
| 75 | + |
| 76 | +intsock; |
| 77 | + |
| 78 | +intservernum; |
| 79 | +raft_server_tservers[MAX_SERVERS]; |
| 80 | + |
| 81 | +inttimer; |
| 82 | + |
| 83 | +raft_applier_tapplier; |
| 84 | +}raft_t; |
| 85 | + |
| 86 | +#defineRAFT_LOG(RAFT,INDEX) ((RAFT)->log.entries[(INDEX) % (RAFT_LOGLEN)]) |
| 87 | + |
| 88 | +#defineRAFT_MSG_UPDATE 0 // append entry |
| 89 | +#defineRAFT_MSG_DONE 1 // entry appended |
| 90 | +#defineRAFT_MSG_CLAIM 2 // vote for me |
| 91 | +#defineRAFT_MSG_VOTE 3 // my vote |
| 92 | + |
| 93 | +typedefstructraft_msg_t { |
| 94 | +intmsgtype; |
| 95 | +intterm; |
| 96 | +intfrom; |
| 97 | +intseqno; |
| 98 | +}raft_msg_t; |
| 99 | + |
| 100 | +typedefstructraft_msg_update_t { |
| 101 | +raft_msg_tmsg; |
| 102 | +intprevindex;// the index of the preceding log entry |
| 103 | +intprevterm;// the term of the preceding log entry |
| 104 | + |
| 105 | +boolempty;// the message is just a heartbeat if empty |
| 106 | +raft_entry_tentry; |
| 107 | + |
| 108 | +intacked;// the leader's acked number |
| 109 | +}raft_msg_update_t; |
| 110 | + |
| 111 | +typedefstructraft_msg_done_t { |
| 112 | +raft_msg_tmsg; |
| 113 | +intindex;// the index of the appended entry |
| 114 | +intterm;// the term of the appended entry |
| 115 | +boolsuccess; |
| 116 | +}raft_msg_done_t; |
| 117 | + |
| 118 | +typedefstructraft_msg_claim_t { |
| 119 | +raft_msg_tmsg; |
| 120 | +intindex;// the index of my last entry |
| 121 | +intterm;// the term of my last entry |
| 122 | +}raft_msg_claim_t; |
| 123 | + |
| 124 | +typedefstructraft_msg_vote_t { |
| 125 | +raft_msg_tmsg; |
| 126 | +boolgranted; |
| 127 | +}raft_msg_vote_t; |
| 128 | + |
| 129 | +// configuration |
| 130 | +voidraft_init(raft_t*r); |
| 131 | +boolraft_add_server(raft_t*r,char*host,intport); |
| 132 | +boolraft_set_myid(raft_t*r,intmyid); |
| 133 | + |
| 134 | +// log actions |
| 135 | +boolraft_emit(raft_t*r,intaction,intargument); |
| 136 | +intraft_apply(raft_t*r,raft_applier_tapplier); |
| 137 | + |
| 138 | +// control |
| 139 | +voidraft_tick(raft_t*r,intmsec); |
| 140 | +voidraft_handle_message(raft_t*r,raft_msg_t*m); |
| 141 | +raft_msg_t*raft_recv_message(raft_t*r); |
| 142 | +intraft_create_udp_socket(raft_t*r); |
| 143 | +voidraft_start_next_term(raft_t*r); |
| 144 | + |
| 145 | +#endif |