[PATCH] ae: Fix libfloat_count and optimize the routine
Export this patch
Signed-off-by: Louis Solofrizzo <lsolofrizzo@scaleway.com>
---
externals/list.c | 5 ++++-
internal.h | 4 +++-
log.c | 12 +++++++-----
3 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/externals/list.c b/externals/list.c
index c359e2b..2e2c842 100644
--- a/externals/list.c
+++ b/externals/list.c
@@ -34,8 +34,11 @@ size_t libfloat_list_count(const libfloat_list_t *head)
libfloat_list_t *iter;
size_t i = 0;
+ if (head == NULL || head->next == NULL || head->prev == NULL)
+ return 0;
+
for(iter = head->next; iter != head; iter = iter->next)
i++;
return i;
-}
\ No newline at end of file
+}
diff --git a/internal.h b/internal.h
index 2e0e5d6..2d52382 100644
--- a/internal.h
+++ b/internal.h
@@ -105,10 +105,12 @@ void libfloat_set_error_str(const char *str);
* \param[in,out] log Entry to update
* \param[in] id ID of the node that acked this log
*
+ * \return The total number of acknowledges on this log
+ *
* \note This function is replay-safe, eg; If a node tells us twice that a log
* X is replicated, we only count it once.
*/
-void libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, libfloat_node_id_t id);
+size_t libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, libfloat_node_id_t id);
/*!
* \brief Free the list of acknowledges of a log
diff --git a/log.c b/log.c
index d761f14..2d740b0 100644
--- a/log.c
+++ b/log.c
@@ -1,6 +1,6 @@
#include "internal.h"
-void libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, libfloat_node_id_t id)
+size_t libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, libfloat_node_id_t id)
{
bool found = false;
libfloat_node_ack_t *ptr, *tmp;
@@ -20,6 +20,8 @@ void libfloat_log_add_node_ack(libfloat_ctx_t *ctx, libfloat_log_entry_t *log, l
ptr->id = id;
libfloat_list_add_tail(&ptr->next, &log->node_acks);
}
+
+ return libfloat_list_count(&log->node_acks);
}
void libfloat_log_free_acks(libfloat_ctx_t *ctx, libfloat_log_entry_t *log)
@@ -597,6 +599,7 @@ void libfloat_append_entries_response(libfloat_ctx_t *ctx, libfloat_rpc_append_e
{
libfloat_node_t *node = libfloat_get_node(ctx, resp->node);
libfloat_log_entry_t *log = NULL;
+ size_t acks = 0;
if (node == NULL)
{
@@ -722,10 +725,9 @@ void libfloat_append_entries_response(libfloat_ctx_t *ctx, libfloat_rpc_append_e
log = kh_value(ctx->persistent.log, iterator);
+ acks = libfloat_log_add_node_ack(ctx, log, node->id);
- libfloat_log_add_node_ack(ctx, log, node->id);
-
- if (libfloat_list_count(&log->node_acks) >= ctx->n_nodes)
+ if (acks >= ctx->n_nodes)
{
if (log->commit_type == LIBFLOAT_ABSOLUTELY_CONSISTENT)
{
@@ -746,7 +748,7 @@ void libfloat_append_entries_response(libfloat_ctx_t *ctx, libfloat_rpc_append_e
log->data = NULL;
}
}
- else if (libfloat_list_count(&log->node_acks) >= ctx->n_nodes / 2 + 1)
+ else if (acks >= ctx->n_nodes / 2 + 1)
{
if (log->commit_type <= LIBFLOAT_STRONGLY_CONSISTENT)
{
--
2.37.3
LG