~ne02ptzero/libfloat

c03e69a2f20a9aaab4cfa328ad972000381f4d03 — Louis Solofrizzo 9 days ago 639a13a
election: Add a possible callback when becoming a follower

Patch: https://lists.sr.ht/~ne02ptzero/libfloat/patches/36566

Signed-off-by: Louis Solofrizzo <lsolofrizzo@scaleway.com>
Acked-by     : Patrik Cyvoct <pcyvoct@scaleway.com>
Acked-by     : Florian Florensa <fflorensa@scaleway.com>

 _________________________________________
/ Soldiers who wish to be a hero Are      \
| practically zero, But those who wish to |
| be civilians, They run into the         |
\ millions.                               /
 -----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
2 files changed, 19 insertions(+), 3 deletions(-)

M libfloat.h
M raft.c
M libfloat.h => libfloat.h +7 -0
@@ 105,6 105,13 @@ struct libfloat_ctx_s {
    void (*become_leader_cb)(struct libfloat_ctx_s *);

    /*!
     * \brief Callback when becoming follower of a cluster
     *
     * \param[in] struct libfloat_ctx_s * - Cluster context
     */
    void (*become_follower_cb)(struct libfloat_ctx_s *);

    /*!
     * \brief Send a "is leader healthy" to a node
     *
     * \param[in] struct libfloat_ctx_s * - Cluster context

M raft.c => raft.c +12 -3
@@ 36,12 36,21 @@ void __libfloat_become_candidate(libfloat_ctx_t *ctx, libfloat_elections_args_t 

void __libfloat_become_follower(libfloat_ctx_t *ctx, libfloat_elections_args_t *args)
{
    DEBUG(ctx, "Becoming follower: reason=%s", args->reason);
    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);

    if (ctx->state == RAFT_STATE_FOLLOWER)
        return;

    if (ctx->become_follower_cb != NULL)
        ctx->become_follower_cb(ctx);

    ctx->state = RAFT_STATE_FOLLOWER;

    DEBUG(ctx, "Becoming follower: reason=%s", args->reason);
    ctx->abort_send_snapshot(ctx);
}

void libfloat_step_down(libfloat_ctx_t *ctx)