|
| 1 | +#ifndef RAFT_H |
| 2 | +#define RAFT_H |
| 3 | + |
| 4 | +#include <arpa/inet.h> |
| 5 | +#include <stdbool.h> |
| 6 | +#include "dtmdlimits.h" |
| 7 | + |
| 8 | +#define NOBODY -1 |
| 9 | + |
| 10 | +#define MAJORITY_IS_NOT_ENOUGH // wait for unanimous ack for applying a new entry |
| 11 | + |
| 12 | +#define DEFAULT_LISTENHOST "0.0.0.0" |
| 13 | +#define DEFAULT_LISTENPORT 5431 |
| 14 | + |
| 15 | +#define ROLE_FOLLOWER 0 |
| 16 | +#define ROLE_CANDIDATE 1 |
| 17 | +#define ROLE_LEADER 2 |
| 18 | + |
| 19 | +#if RAFT_KEEP_APPLIED >= RAFT_LOGLEN |
| 20 | +#error please ensure RAFT_KEEP_APPLIED < RAFT_LOGLEN |
| 21 | +#endif |
| 22 | + |
| 23 | +#if HEARTBEAT_TIMEOUT_MS >= ELECTION_TIMEOUT_MS_MIN |
| 24 | +#error please ensure HEARTBEAT_TIMEOUT_MS < ELECTION_TIMEOUT_MS_MIN (considerably) |
| 25 | +#endif |
| 26 | + |
| 27 | +#if ELECTION_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 | +typedef struct raft_entry_t { |
| 33 | + int term; |
| 34 | + bool snapshot; // true if this is a snapshot entry |
| 35 | + union { |
| 36 | + struct { // snapshot == false |
| 37 | + int action; |
| 38 | + int argument; |
| 39 | + }; |
| 40 | + struct { // snapshot == true |
| 41 | + int minarg; |
| 42 | + int maxarg; |
| 43 | + }; |
| 44 | + }; |
| 45 | +} raft_entry_t; |
| 46 | + |
| 47 | +typedef void (*raft_applier_t)(int action, int argument); |
| 48 | + |
| 49 | +typedef struct raft_log_t { |
| 50 | + int first; |
| 51 | + int size; // number of entries past first |
| 52 | + int acked; // number of entries replicated to the majority of servers |
| 53 | + int applied; // number of entries applied to the state machine |
| 54 | + raft_entry_t entries[RAFT_LOGLEN]; // wraps around |
| 55 | +} raft_log_t; |
| 56 | + |
| 57 | +typedef struct raft_server_t { |
| 58 | + int seqno; // the rpc sequence number |
| 59 | + int tosend; // index of the next entry to send |
| 60 | + int acked; // index of the highest entry known to be replicated |
| 61 | + |
| 62 | + char *host; |
| 63 | + int port; |
| 64 | + struct sockaddr_in addr; |
| 65 | +} raft_server_t; |
| 66 | + |
| 67 | +typedef struct raft_t { |
| 68 | + int term; // current term (latest term we have seen) |
| 69 | + int vote; // who received our vote in current term |
| 70 | + int role; |
| 71 | + int me; // my id |
| 72 | + int votes; // how many votes are for me (if candidate) |
| 73 | + int leader; // the id of the leader |
| 74 | + raft_log_t log; |
| 75 | + |
| 76 | + int sock; |
| 77 | + |
| 78 | + int servernum; |
| 79 | + raft_server_t servers[MAX_SERVERS]; |
| 80 | + |
| 81 | + int timer; |
| 82 | + |
| 83 | + raft_applier_t applier; |
| 84 | +} raft_t; |
| 85 | + |
| 86 | +#define RAFT_LOG(RAFT, INDEX) ((RAFT)->log.entries[(INDEX) % (RAFT_LOGLEN)]) |
| 87 | + |
| 88 | +#define RAFT_MSG_UPDATE 0 // append entry |
| 89 | +#define RAFT_MSG_DONE 1 // entry appended |
| 90 | +#define RAFT_MSG_CLAIM 2 // vote for me |
| 91 | +#define RAFT_MSG_VOTE 3 // my vote |
| 92 | + |
| 93 | +typedef struct raft_msg_t { |
| 94 | + int msgtype; |
| 95 | + int term; |
| 96 | + int from; |
| 97 | + int seqno; |
| 98 | +} raft_msg_t; |
| 99 | + |
| 100 | +typedef struct raft_msg_update_t { |
| 101 | + raft_msg_t msg; |
| 102 | + int previndex; // the index of the preceding log entry |
| 103 | + int prevterm; // the term of the preceding log entry |
| 104 | + |
| 105 | + bool empty; // the message is just a heartbeat if empty |
| 106 | + raft_entry_t entry; |
| 107 | + |
| 108 | + int acked; // the leader's acked number |
| 109 | +} raft_msg_update_t; |
| 110 | + |
| 111 | +typedef struct raft_msg_done_t { |
| 112 | + raft_msg_t msg; |
| 113 | + int index; // the index of the appended entry |
| 114 | + int term; // the term of the appended entry |
| 115 | + bool success; |
| 116 | +} raft_msg_done_t; |
| 117 | + |
| 118 | +typedef struct raft_msg_claim_t { |
| 119 | + raft_msg_t msg; |
| 120 | + int index; // the index of my last entry |
| 121 | + int term; // the term of my last entry |
| 122 | +} raft_msg_claim_t; |
| 123 | + |
| 124 | +typedef struct raft_msg_vote_t { |
| 125 | + raft_msg_t msg; |
| 126 | + bool granted; |
| 127 | +} raft_msg_vote_t; |
| 128 | + |
| 129 | +// configuration |
| 130 | +void raft_init(raft_t *r); |
| 131 | +bool raft_add_server(raft_t *r, char *host, int port); |
| 132 | +bool raft_set_myid(raft_t *r, int myid); |
| 133 | + |
| 134 | +// log actions |
| 135 | +bool raft_emit(raft_t *r, int action, int argument); |
| 136 | +int raft_apply(raft_t *r, raft_applier_t applier); |
| 137 | + |
| 138 | +// control |
| 139 | +void raft_tick(raft_t *r, int msec); |
| 140 | +void raft_handle_message(raft_t *r, raft_msg_t *m); |
| 141 | +raft_msg_t *raft_recv_message(raft_t *r); |
| 142 | +int raft_create_udp_socket(raft_t *r); |
| 143 | +void raft_start_next_term(raft_t *r); |
| 144 | + |
| 145 | +#endif |
0 commit comments