From 2d61deb4867c2c323018f9b57418c2eae15f35a9 Mon Sep 17 00:00:00 2001 From: Willem Thiart Date: Wed, 10 Jan 2018 12:55:46 +1300 Subject: [PATCH 1/2] Batchify API --- include/raft.h | 64 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/include/raft.h b/include/raft.h index 7a8953c7..0f5b8f88 100644 --- a/include/raft.h +++ b/include/raft.h @@ -303,6 +303,21 @@ typedef int ( int vote ); +/** Callback for applying log entries to the state machine. + * @param[in] raft The Raft server making this callback + * @param[in] user_data User data that is passed from Raft server + * @param[in] entries Array of entries to be applied + * @param[in] n_entries Number of entries to be applied + * @return 0 on success */ +typedef int ( +*func_apply_logs_f +) ( + raft_server_t* raft, + void *user_data, + raft_entry_t* entries, + int n_entries + ); + /** Callback for saving log entry changes. * * This callback is used for: @@ -322,8 +337,19 @@ typedef int ( * memory pointed to in the raft_entry_data_t struct. This MUST be done if * the memory is temporary. * @param[in] entry_idx The entries index in the log + * @param[in] n_entries The numberof entries. * @return 0 on success */ typedef int ( +*func_logentries_event_f +) ( + raft_server_t* raft, + void *user_data, + raft_entry_t *entry, + int entry_idx, + int n_entries + ); + +typedef int ( *func_logentry_event_f ) ( raft_server_t* raft, @@ -343,11 +369,6 @@ typedef struct /** Callback for notifying user that a node needs a snapshot sent */ func_send_snapshot_f send_snapshot; - /** Callback for finite state machine application - * Return 0 on success. - * Return RAFT_ERR_SHUTDOWN if you want the server to shutdown. */ - func_logentry_event_f applylog; - /** Callback for persisting vote data * For safety reasons this callback MUST flush the change to disk. */ func_persist_vote_f persist_vote; @@ -361,19 +382,19 @@ typedef struct * For safety reasons this callback MUST flush the change to disk. * Return 0 on success. * Return RAFT_ERR_SHUTDOWN if you want the server to shutdown. */ - func_logentry_event_f log_offer; + func_logentries_event_f offer_logs; /** Callback for removing the oldest entry from the log * For safety reasons this callback MUST flush the change to disk. * @note If memory was malloc'd in log_offer then this should be the right * time to free the memory. */ - func_logentry_event_f log_poll; + func_logentries_event_f poll_logs; - /** Callback for removing the youngest entry from the log + /** Callback for removing the youngest entries from the log * For safety reasons this callback MUST flush the change to disk. * @note If memory was malloc'd in log_offer then this should be the right * time to free the memory. */ - func_logentry_event_f log_pop; + func_logentries_event_f pop_logs; /** Callback for determining which node this configuration log entry * affects. This call only applies to configuration change log entries. @@ -386,6 +407,29 @@ typedef struct /** Callback for catching debugging log messages * This callback is optional */ func_log_f log; + + /** WARNING: the below callbacks are deprecated */ + + /** Callback for adding an entry to the log + * For safety reasons this callback MUST flush the change to disk. */ + func_logentry_event_f log_offer; + + /** Callback for removing the oldest entry from the log + * For safety reasons this callback MUST flush the change to disk. + * @note If memory was malloc'd in log_offer then this should be the right + * time to free the memory. */ + func_logentry_event_f log_poll; + + /** Callback for removing the youngest entry from the log + * For safety reasons this callback MUST flush the change to disk. + * @note If memory was malloc'd in log_offer then this should be the right + * time to free the memory. */ + func_logentry_event_f log_pop; + + /** Callback for finite state machine application + * Return 0 on success. + * Return RAFT_ERR_SHUTDOWN if you want the server to shutdown. */ + func_logentry_event_f applylog; } raft_cbs_t; typedef struct @@ -710,6 +754,8 @@ void raft_set_commit_idx(raft_server_t* me, int commit_idx); * RAFT_ERR_NOMEM memory allocation failure */ int raft_append_entry(raft_server_t* me, raft_entry_t* ety); +int raft_append_entries(raft_server_t* me_, raft_entry_t* etys, int n_etys); + /** Confirm if a msg_entry_response has been committed. * @param[in] r The response we want to check */ int raft_msg_entry_response_committed(raft_server_t* me_, From 5579f1088d9424aee1406480f462e8ab396167af Mon Sep 17 00:00:00 2001 From: Willem Thiart Date: Fri, 12 Jan 2018 11:17:19 +1300 Subject: [PATCH 2/2] Standardise entry log naming Prefer "entries" over "logs" --- include/raft.h | 56 +++++++++++++++++++++++++-------------- include/raft_private.h | 16 +++++++---- src/raft_log.c | 13 +++++++-- src/raft_node.c | 20 +++++++------- src/raft_server.c | 50 +++++++++++++++++++++++++--------- tests/test_snapshotting.c | 14 +++++----- 6 files changed, 112 insertions(+), 57 deletions(-) diff --git a/include/raft.h b/include/raft.h index 0f5b8f88..4b326a8e 100644 --- a/include/raft.h +++ b/include/raft.h @@ -240,14 +240,14 @@ typedef int ( raft_node_t* node ); -/** Callback for detecting when non-voting nodes have obtained enough logs. +/** Callback for detecting when non-voting nodes have obtained enough entries. * This triggers only when there are no pending configuration changes. * @param[in] raft The Raft server making this callback * @param[in] user_data User data that is passed from Raft server * @param[in] node The node * @return 0 does not want to be notified again; otherwise -1 */ typedef int ( -*func_node_has_sufficient_logs_f +*func_node_has_sufficient_entries_f ) ( raft_server_t* raft, void *user_data, @@ -310,7 +310,7 @@ typedef int ( * @param[in] n_entries Number of entries to be applied * @return 0 on success */ typedef int ( -*func_apply_logs_f +*func_apply_entries_f ) ( raft_server_t* raft, void *user_data, @@ -378,31 +378,31 @@ typedef struct * disk atomically. */ func_persist_term_f persist_term; - /** Callback for adding an entry to the log + /** Callback for adding entries to the log * For safety reasons this callback MUST flush the change to disk. * Return 0 on success. * Return RAFT_ERR_SHUTDOWN if you want the server to shutdown. */ - func_logentries_event_f offer_logs; + func_logentries_event_f offer_entries; - /** Callback for removing the oldest entry from the log + /** Callback for removing the oldest entries from the log * For safety reasons this callback MUST flush the change to disk. * @note If memory was malloc'd in log_offer then this should be the right * time to free the memory. */ - func_logentries_event_f poll_logs; + func_logentries_event_f poll_entries; /** Callback for removing the youngest entries from the log * For safety reasons this callback MUST flush the change to disk. * @note If memory was malloc'd in log_offer then this should be the right * time to free the memory. */ - func_logentries_event_f pop_logs; + func_logentries_event_f pop_entries; /** Callback for determining which node this configuration log entry * affects. This call only applies to configuration change log entries. * @return the node ID of the node */ - func_logentry_event_f log_get_node_id; + func_logentry_event_f entry_get_node_id; - /** Callback for detecting when a non-voting node has sufficient logs. */ - func_node_has_sufficient_logs_f node_has_sufficient_logs; + /** Callback for detecting when a non-voting node has sufficient entries. */ + func_node_has_sufficient_entries_f node_has_sufficient_entries; /** Callback for catching debugging log messages * This callback is optional */ @@ -430,6 +430,14 @@ typedef struct * Return 0 on success. * Return RAFT_ERR_SHUTDOWN if you want the server to shutdown. */ func_logentry_event_f applylog; + + /** Callback for detecting when a non-voting node has sufficient entries. */ + func_node_has_sufficient_entries_f node_has_sufficient_logs; + + /** Callback for determining which node this configuration log entry + * affects. This call only applies to configuration change log entries. + * @return the node ID of the node */ + func_logentry_event_f log_get_node_id; } raft_cbs_t; typedef struct @@ -521,12 +529,12 @@ int raft_periodic(raft_server_t* me, int msec_elapsed); * * Might call malloc once to increase the log entry array size. * - * The log_offer callback will be called. + * The offer_entries callback will be called. * * @note The memory pointer (ie. raft_entry_data_t) for each msg_entry_t is * copied directly. If the memory is temporary you MUST either make the * memory permanent (ie. via malloc) OR re-assign the memory within the - * log_offer callback. + * offer_entries callback. * * @param[in] node The node who sent us this message * @param[in] ae The appendentries message @@ -579,12 +587,12 @@ int raft_recv_requestvote_response(raft_server_t* me, * * Might call malloc once to increase the log entry array size. * - * The log_offer callback will be called. + * The offer_entries callback will be called. * * @note The memory pointer (ie. raft_entry_data_t) in msg_entry_t is * copied directly. If the memory is temporary you MUST either make the * memory permanent (ie. via malloc) OR re-assign the memory within the - * log_offer callback. + * offer_entries callback. * * Will fail: *