[PATCH] Ignore snapshot log if it matches current snapshot
Export this patch
Signed-off-by: Michael Bonfils <mbonfils@scaleway.com>
---
internal.h | 11 ++++++++++ -
log.c | 4 ++ --
snapshot.c | 8 +++++++ -
3 files changed, 19 insertions(+), 4 deletions(-)
diff --git a/internal.h b/internal.h
index 4815bef..d01147e 100644
--- a/internal.h
+++ b/internal.h
@@ -66,7 +66,16 @@ void libfloat_send_append_entries(libfloat_ctx_t *ctx, libfloat_node_t *node, bo
*/
void libfloat_send_append_entries_to_all(libfloat_ctx_t *ctx);
- void libfloat_internal_snapshot_begin(libfloat_ctx_t *ctx);
+ /*!
+ * \brief Start a snapshot
+ *
+ * \param[in] ctx libfloat context
+ * \param[in] id ID of log that trigger snapshot
+ * \param[in] term term of log
+ *
+ * \note snapshot will be ignored if current snapshot matches trigger log
+ */
+ void libfloat_internal_snapshot_begin(libfloat_ctx_t *ctx, libfloat_entry_id_t id, libfloat_term_t term);
#define DEBUG(c, s, ...) do { \
if (c->debug != NULL) \
diff --git a/log.c b/log.c
index abd7cb1..bed02da 100644
--- a/log.c
+++ b/log.c
@@ -105,7 +105,7 @@ bool libfloat_add_log(libfloat_ctx_t *ctx, libfloat_commit_type_t commit_type, l
ctx->snapshot_to_commit = entry->id;
if (entry->data->type == LIBFLOAT_SNAPSHOT_LOG)
- libfloat_internal_snapshot_begin(ctx);
+ libfloat_internal_snapshot_begin(ctx, entry->id, entry->term);
return true;
}
@@ -535,7 +535,7 @@ response:
log->data->buf = ctx->malloc(log->data->len);
memcpy(log->data->buf, req->entries[i]->data->buf, log->data->len);
- libfloat_internal_snapshot_begin(ctx);
+ libfloat_internal_snapshot_begin(ctx, log->id, log->term);
}
else if (!ctx->is_snapshotting)
{
diff --git a/snapshot.c b/snapshot.c
index dbc3859..4034018 100644
--- a/snapshot.c
+++ b/snapshot.c
@@ -108,7 +108,7 @@ static void libfloat_internal_snapshot_apply_log(libfloat_ctx_t *ctx)
}
}
- void libfloat_internal_snapshot_begin(libfloat_ctx_t *ctx)
+ void libfloat_internal_snapshot_begin(libfloat_ctx_t *ctx, libfloat_entry_id_t id, libfloat_term_t term)
{
if (ctx->snapshot == NULL)
{
@@ -118,6 +118,12 @@ void libfloat_internal_snapshot_begin(libfloat_ctx_t *ctx)
return;
}
+ if (id == ctx->persistent.snapshot.index + 1 && term == ctx->persistent.snapshot.term)
+ {
+ DEBUG(ctx, "libfloat_internal_snapshot_begin: A snapshot already exists with previous commit id, skip it");
+ return;
+ }
+
/* First, let's block all writes and init variables */
ctx->is_snapshotting = true;
if (ctx->snapshot_to_commit < ctx->persistent.commit_index)
--
2.25.1
LGTM
--
Louis Solofrizzo
> If you stick a stock of liquor in your locker,
> It is slick to stick a lock upon your stock.
> Or some joker who is slicker,
> Will trick you of your liquor,
> If you fail to lock your liquor with a lock.