Signed-off-by: Louis Solofrizzo <lsolofrizzo@scaleway.com>
---
libfloat.h | 1 +
raft.c | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/libfloat.h b/libfloat.h
index 9f8ad38..17b3c89 100644
--- a/libfloat.h
+++ b/libfloat.h
@@ -46,6 +46,7 @@ struct libfloat_ctx_s {
struct {
uint64_t leader_election; /*!< Count of leader elections for this cluster */
uint64_t orphans_logs; /*!< Count of logs that are applied on the leader only */
+ time_t leader_election_time; /*!< Timestamp of the last leader election */
} stat;
struct {
diff --git a/raft.c b/raft.c
index 90d5e55..2cfce3c 100644
--- a/raft.c
+++ b/raft.c
@@ -23,6 +23,7 @@ void libfloat_become_leader(libfloat_ctx_t *ctx)
});
ctx->me->is_up_to_date = 1;
+ ctx->stat.leader_election_time = ctx->time(NULL);
}
void __libfloat_become_candidate(libfloat_ctx_t *ctx, libfloat_elections_args_t *args)
@@ -30,6 +31,7 @@ void __libfloat_become_candidate(libfloat_ctx_t *ctx, libfloat_elections_args_t
DEBUG(ctx, "Becoming candidate: reason=%s", args->reason);
ctx->state = RAFT_STATE_CANDIDATE;
ctx->election_timeout_rand = ctx->conf.election_timeout + ctx->rand() % ctx->conf.election_timeout;
+ ctx->stat.leader_election_time = 0;
}
void __libfloat_become_follower(libfloat_ctx_t *ctx, libfloat_elections_args_t *args)
@@ -38,6 +40,7 @@ void __libfloat_become_follower(libfloat_ctx_t *ctx, libfloat_elections_args_t *
ctx->abort_send_snapshot(ctx);
ctx->state = RAFT_STATE_FOLLOWER;
ctx->election_timeout_rand = ctx->conf.election_timeout + ctx->rand() % ctx->conf.election_timeout;
+ ctx->stat.leader_election_time = 0;
libfloat_vote_for(ctx, 0);
}
@@ -49,6 +52,7 @@ void libfloat_step_down(libfloat_ctx_t *ctx)
ctx->leader = NULL;
ctx->state = RAFT_STATE_NONE;
+ ctx->stat.leader_election_time = 0;
}
void libfloat_send_append_entries_to_all(libfloat_ctx_t *ctx, bool heartbeat)
--
2.37.3