Commit bda11c54 authored by Willem Thiart's avatar Willem Thiart
Browse files

Remove malloc from entry appending

Added documentation to mention that user is responsible for managing
memory.
parent 1817773d
...@@ -220,16 +220,21 @@ typedef int ( ...@@ -220,16 +220,21 @@ typedef int (
); );
/** Callback for saving log entry changes. /** Callback for saving log entry changes.
*
* This callback is used for: * This callback is used for:
* <ul> * <ul>
* <li>Adding entries to the log (ie. offer) * <li>Adding entries to the log (ie. offer)
* <li>Removing the first entry from the log (ie. polling) * <li>Removing the first entry from the log (ie. polling)
* <li>Removing the last entry from the log (ie. popping) * <li>Removing the last entry from the log (ie. popping)
* </ul> * </ul>
*
* This callback MUST flush the change to disk. * This callback MUST flush the change to disk.
*
* @param[in] raft The Raft server making this callback * @param[in] raft The Raft server making this callback
* @param[in] user_data User data that is passed from Raft server * @param[in] user_data User data that is passed from Raft server
* @param[in] entry The entry that the event is happening to * @param[in] entry The entry that the event is happening to
* The user is allowed to change the memory pointed to in the
* raft_entry_data_t struct.
* @param[in] entry_idx The entries index in the log * @param[in] entry_idx The entries index in the log
* @return 0 on success */ * @return 0 on success */
typedef int ( typedef int (
...@@ -351,7 +356,16 @@ void raft_set_request_timeout(raft_server_t* me, int msec); ...@@ -351,7 +356,16 @@ void raft_set_request_timeout(raft_server_t* me, int msec);
int raft_periodic(raft_server_t* me, int msec_elapsed); int raft_periodic(raft_server_t* me, int msec_elapsed);
/** Receive an appendentries message. /** Receive an appendentries message.
* This function will block if it needs to append the message. *
* Will block (ie. by syncing to disk) if we need to append a message.
*
* Might call malloc once to increase the log entry array size.
*
* @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.
*
* @param[in] node Index of the node who sent us this message * @param[in] node Index of the node who sent us this message
* @param[in] ae The appendentries message * @param[in] ae The appendentries message
* @param[out] r The resulting response * @param[out] r The resulting response
...@@ -387,11 +401,18 @@ int raft_recv_requestvote_response(raft_server_t* me, ...@@ -387,11 +401,18 @@ int raft_recv_requestvote_response(raft_server_t* me,
int node, int node,
msg_requestvote_response_t* r); msg_requestvote_response_t* r);
/** Receive an entry message from client. /** Receive an entry message from the client.
* *
* Append the entry to the log and send appendentries to followers. * Append the entry to the log and send appendentries to followers.
* *
* This function will block if it needs to append the message. * Will block (ie. by syncing to disk) if we need to append a message.
*
* Might call malloc once to increase the log entry array size.
*
* @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.
* *
* Will fail: * Will fail:
* <ul> * <ul>
......
...@@ -314,14 +314,11 @@ int raft_recv_appendentries( ...@@ -314,14 +314,11 @@ int raft_recv_appendentries(
{ {
msg_entry_t* cmd = &ae->entries[i]; msg_entry_t* cmd = &ae->entries[i];
/* TODO: replace malloc with mempoll/arena */ raft_entry_t c;
raft_entry_t* c = (raft_entry_t*)malloc(sizeof(raft_entry_t)); c.term = cmd->term;
c->term = cmd->term; c.id = cmd->id;
memcpy(&c->data, &cmd->data, sizeof(raft_entry_data_t)); memcpy(&c.data, &cmd->data, sizeof(raft_entry_data_t));
c->data.buf = (unsigned char*)malloc(cmd->data.len); int e = raft_append_entry(me_, &c);
memcpy(c->data.buf, cmd->data.buf, cmd->data.len);
c->id = cmd->id;
int e = raft_append_entry(me_, c);
if (-1 == e) if (-1 == e)
{ {
__log(me_, "AE failure; couldn't append entry"); __log(me_, "AE failure; couldn't append entry");
...@@ -433,9 +430,7 @@ int raft_recv_entry(raft_server_t* me_, int node, msg_entry_t* e, ...@@ -433,9 +430,7 @@ int raft_recv_entry(raft_server_t* me_, int node, msg_entry_t* e,
ety.term = me->current_term; ety.term = me->current_term;
ety.id = e->id; ety.id = e->id;
ety.data.len = e->data.len; memcpy(&ety.data, &e->data, e->data.len);
ety.data.buf = malloc(e->data.len);
memcpy(ety.data.buf, e->data.buf, e->data.len);
raft_append_entry(me_, &ety); raft_append_entry(me_, &ety);
for (i = 0; i < me->num_nodes; i++) for (i = 0; i < me->num_nodes; i++)
if (me->nodeid != i) if (me->nodeid != i)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment