~ne02ptzero/libfloat

ref: 0a170e94a6d19b9027a52113a2ef3324b45a4dae libfloat/raft.c -rw-r--r-- 4.4 KiB
0a170e94 — Michael Bonfils Add callback to abort snapshot synchronization a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "internal.h"

bool libfloat_am_i_leader(libfloat_ctx_t *ctx)
{
    return ctx->state == RAFT_STATE_LEADER;
}

void libfloat_become_leader(libfloat_ctx_t *ctx)
{
    libfloat_node_t     *node;

    ctx->state = RAFT_STATE_LEADER;

    if (ctx->become_leader_cb != NULL && ctx->leader != ctx->me)
        ctx->become_leader_cb(ctx);

    ctx->leader = ctx->me;

    for_every_node(ctx, node, {
        /* Let's notify all the nodes */
        if (node != ctx->me)
            libfloat_send_append_entries(ctx, node, true);
    });

    ctx->me->is_up_to_date = 1;
}

void libfloat_become_candidate(libfloat_ctx_t *ctx)
{
    DEBUG(ctx, "Becoming candidate");
    ctx->state = RAFT_STATE_CANDIDATE;
    ctx->election_timeout_rand = ctx->conf.election_timeout + ctx->rand() % ctx->conf.election_timeout;
}

void libfloat_become_follower(libfloat_ctx_t *ctx)
{
    libfloat_node_t             *node = NULL;

    DEBUG(ctx, "Becoming follower");
    for_every_node(ctx, node, {
        if (ctx->me == node)
            continue;

        if (node->snapshot_in_progress)
        {
            ctx->abort_snapshot(ctx, node);
            node->snapshot_in_progress = false;
        }
    });
    ctx->state = RAFT_STATE_FOLLOWER;
    ctx->election_timeout_rand = ctx->conf.election_timeout + ctx->rand() % ctx->conf.election_timeout;
    libfloat_vote_for(ctx, 0);
}

void libfloat_step_down(libfloat_ctx_t *ctx)
{
    DEBUG(ctx, "Force stepping down");
    ctx->stepping_down = true;
    if (ctx->state == RAFT_STATE_LEADER)
        ctx->leader = NULL;

    ctx->state = RAFT_STATE_NONE;
}

void libfloat_send_append_entries_to_all(libfloat_ctx_t *ctx)
{
    libfloat_node_t     *node;

    for_every_node(ctx, node, {
        if (node != ctx->me)
            libfloat_send_append_entries(ctx, node, false);
    });
}

void libfloat_set_current_term(libfloat_ctx_t *ctx, libfloat_term_t term)
{
    ctx->persistent.term = term;
    ctx->write_current_term(ctx, ctx->persistent.term);
}

void libfloat_set_current_commit_index(libfloat_ctx_t *ctx, libfloat_entry_id_t id)
{
    if (id <= ctx->persistent.commit_index)
        return;

    ctx->persistent.commit_index = id;
    ctx->write_commit_index(ctx, ctx->persistent.commit_index);
}

const char *libfloat_cluster_state_to_str(libfloat_ctx_t *ctx)
{
    switch (ctx->state)
    {
        case RAFT_STATE_NONE:
            return "NONE";
        case RAFT_STATE_FOLLOWER:
            return "FOLLOWER";
        case RAFT_STATE_CANDIDATE:
            return "CANDIDATE";
        case RAFT_STATE_LEADER:
            return "LEADER";
    }

    return NULL;
}

void libfloat_reload_state(libfloat_ctx_t *ctx)
{
    ctx->persistent.term = ctx->get_current_term(ctx);
    ctx->persistent.voted_for = ctx->get_vote(ctx);
    ctx->persistent.commit_index = ctx->get_commit_index(ctx);
    if (!ctx->get_last_snapshot(ctx, &ctx->persistent.snapshot.index, &ctx->persistent.snapshot.term))
    {
        /* No snapshot to load, let's set the defaults */
        ctx->persistent.snapshot.index = 0;
        ctx->persistent.snapshot.term = 0;
    }

    /* when reloading a snapshot, ctx may have older logs and we want to drop them */
    if (kh_size(ctx->persistent.log) > 0)
    {
        libfloat_log_memory_cleanup(ctx);
    }

    /* XXX some part of code try to load missing log but other not */
    for (size_t i = ctx->persistent.snapshot.index + 1; i <= ctx->persistent.commit_index; i++)
    {
        libfloat_log_entry_t    *log;
        khint_t                 iterator;
        int                     absent;

        log = ctx->calloc(1, sizeof(*log));
        log->id = i;

        log->node_acks.prev = &log->node_acks;
        log->node_acks.next = &log->node_acks;

        iterator = kh_put(libfloat_entry_id_t, ctx->persistent.log, log->id, &absent);
        kh_value(ctx->persistent.log, iterator) = log;
    }
}

bool libfloat_am_i_up_to_date(libfloat_ctx_t *ctx)
{
    if (ctx->state == RAFT_STATE_LEADER)
        return true;

    return ctx->me->is_up_to_date;
}

void libfloat_debug(libfloat_ctx_t *ctx, const char *fmt, ...)
{
    va_list     ap;
    char        log[1024] = { 0 };

    va_start(ap, fmt);
    ctx->vsnprintf(log, sizeof(log), fmt, ap);
    va_end(ap);

    ctx->debug(ctx, log);
}

void libfloat_error(libfloat_ctx_t *ctx, const char *fmt, ...)
{
    va_list     ap;
    char        log[1024] = { 0 };

    va_start(ap, fmt);
    ctx->vsnprintf(log, sizeof(log), fmt, ap);
    va_end(ap);

    ctx->error(ctx, log);
}