-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,15 @@ | |
/** | ||
* Copyright (c) 2013, Willem-Hendrik Thiart | ||
* Use of this source code is governed by a BSD-style license that can be | ||
* found in the LICENSE file. | ||
* found in the LICENSE file. | ||
* | ||
* @file | ||
* @author Willem Thiart [email protected] | ||
* @version 0.1 | ||
*/ | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/** The ID that this node used to have. | ||
* So that we can tell which nodes were removed/added when the | ||
* configuration changes */ | ||
|
@@ -25,7 +26,8 @@ typedef struct { | |
void* udata_address; | ||
} raft_node_configuration_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* candidate's term */ | ||
int term; | ||
|
||
|
@@ -39,7 +41,8 @@ typedef struct { | |
int last_log_term; | ||
} msg_requestvote_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* the entry's unique ID */ | ||
unsigned int id; | ||
|
||
|
@@ -50,23 +53,26 @@ typedef struct { | |
unsigned int len; | ||
} msg_entry_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* the entry's unique ID */ | ||
unsigned int id; | ||
|
||
/* whether or not the entry was committed */ | ||
int was_committed; | ||
} msg_entry_response_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* currentTerm, for candidate to update itself */ | ||
int term; | ||
|
||
/* true means candidate received vote */ | ||
int vote_granted; | ||
} msg_requestvote_response_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
int term; | ||
int leader_id; | ||
int prev_log_idx; | ||
|
@@ -76,7 +82,8 @@ typedef struct { | |
int leader_commit; | ||
} msg_appendentries_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* currentTerm, for leader to update itself */ | ||
int term; | ||
|
||
|
@@ -96,7 +103,8 @@ typedef struct { | |
typedef void* raft_server_t; | ||
typedef void* raft_node_t; | ||
|
||
typedef enum { | ||
typedef enum | ||
{ | ||
RAFT_MSG_REQUESTVOTE, | ||
RAFT_MSG_REQUESTVOTE_RESPONSE, | ||
RAFT_MSG_APPENDENTRIES, | ||
|
@@ -105,112 +113,100 @@ typedef enum { | |
RAFT_MSG_ENTRY_RESPONSE, | ||
} raft_message_type_e; | ||
|
||
|
||
typedef int ( | ||
*func_send_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
raft_message_type_e msg_type, | ||
const unsigned char *send_data, | ||
const int len | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_requestvote_f | ||
*func_send_requestvote_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_requestvote_t* msg | ||
); | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_requestvote_response_f | ||
*func_send_requestvote_response_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_requestvote_response_t* msg | ||
); | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_appendentries_f | ||
*func_send_appendentries_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_appendentries_t* msg | ||
); | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_appendentries_response_f | ||
*func_send_appendentries_response_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_appendentries_response_t* msg | ||
); | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_entries_f | ||
*func_send_entries_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_entry_t* msg | ||
); | ||
); | ||
|
||
/** | ||
* @param raft The Raft server making this callback | ||
* @param udata User data that is passed from Raft server | ||
* @param node The peer's ID that we are sending this message to | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_send_entries_response_f | ||
*func_send_entries_response_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
int node, | ||
msg_entry_response_t* msg | ||
); | ||
); | ||
|
||
#ifndef HAVE_FUNC_LOG | ||
#define HAVE_FUNC_LOG | ||
typedef void ( | ||
*func_log_f | ||
*func_log_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
const char *buf, | ||
... | ||
); | ||
); | ||
#endif | ||
|
||
/** | ||
|
@@ -221,15 +217,16 @@ typedef void ( | |
* @param len Length in bytes of data to be applied | ||
* @return 0 on error */ | ||
typedef int ( | ||
*func_applylog_f | ||
*func_applylog_f | ||
) ( | ||
raft_server_t* raft, | ||
void *udata, | ||
const unsigned char *data, | ||
const int len | ||
); | ||
); | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
func_send_requestvote_f send_requestvote; | ||
func_send_requestvote_response_f send_requestvote_response; | ||
func_send_appendentries_f send_appendentries; | ||
|
@@ -240,7 +237,8 @@ typedef struct { | |
func_applylog_f applylog; | ||
} raft_cbs_t; | ||
|
||
typedef struct { | ||
typedef struct | ||
{ | ||
/* entry's term */ | ||
unsigned int term; | ||
/* the entry's unique ID */ | ||
|
@@ -272,15 +270,15 @@ void raft_free(raft_server_t* me_); | |
* Callbacks need to be set by the user for CRaft to work. | ||
* | ||
* @param funcs Callbacks | ||
* @param udata The context that we include when making a callback */ | ||
* @param udata "User data" - user's context that's included in a callback */ | ||
void raft_set_callbacks(raft_server_t* me, raft_cbs_t* funcs, void* udata); | ||
|
||
/** | ||
* Set configuration | ||
* @param nodes Array of nodes. End of array is marked by NULL entry | ||
* @param my_idx Index of the node that refers to this Raft server */ | ||
void raft_set_configuration(raft_server_t* me_, | ||
raft_node_configuration_t* nodes, int my_idx); | ||
raft_node_configuration_t* nodes, int my_idx); | ||
|
||
/** | ||
* Set election timeout | ||
|
@@ -302,40 +300,40 @@ int raft_periodic(raft_server_t* me, int msec_elapsed); | |
|
||
/** | ||
* Receive an appendentries message | ||
* @param node Index of the node who sent us this message | ||
* @param ae The appendentries message | ||
* @param node Index of the node who sent us this message | ||
* @param ae The appendentries message | ||
* @return 0 on error */ | ||
int raft_recv_appendentries(raft_server_t* me, int node, | ||
msg_appendentries_t* ae); | ||
msg_appendentries_t* ae); | ||
|
||
/** | ||
* Receive a response from an appendentries message we sent | ||
* @param node Index of the node who sent us this message | ||
* @param node Index of the node who sent us this message | ||
* @param r The appendentries response message | ||
* @return 0 on error */ | ||
int raft_recv_appendentries_response(raft_server_t* me_, | ||
int node, msg_appendentries_response_t* r); | ||
int node, msg_appendentries_response_t* r); | ||
/** | ||
* Receive a requestvote message | ||
* @param node Index of the node who sent us this message | ||
* @param node Index of the node who sent us this message | ||
* @param vr The requestvote message | ||
* @return 0 on error */ | ||
int raft_recv_requestvote(raft_server_t* me, int node, | ||
msg_requestvote_t* vr); | ||
msg_requestvote_t* vr); | ||
|
||
/** | ||
* Receive a response from a requestvote message we sent | ||
* @param node Index of the node who sent us this message | ||
* @param node Index of the node who sent us this message | ||
* @param r The requestvote response message | ||
* @param node The node this response was sent by */ | ||
int raft_recv_requestvote_response(raft_server_t* me, int node, | ||
msg_requestvote_response_t* r); | ||
msg_requestvote_response_t* r); | ||
|
||
/** | ||
* Receive an entry message from client. | ||
* Append the entry to the log | ||
* Send appendentries to followers | ||
* @param node Index of the node who sent us this message | ||
* Send appendentries to followers | ||
* @param node Index of the node who sent us this message | ||
* @param e The entry message */ | ||
int raft_recv_entry(raft_server_t* me, int node, msg_entry_t* e); | ||
|
||
|