This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
/
membership.c
227 lines (182 loc) · 6.34 KB
/
membership.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "membership.h"
#include "../include/raft.h"
#include "assert.h"
#include "configuration.h"
#include "err.h"
#include "log.h"
#include "progress.h"
#include "tracing.h"
#define tracef(...) Tracef(r->tracer, __VA_ARGS__)
int membershipCanChangeConfiguration(struct raft *r)
{
int rv;
if (r->state != RAFT_LEADER || r->transfer != NULL) {
tracef("NOT LEADER");
rv = RAFT_NOTLEADER;
goto err;
}
if (r->configuration_uncommitted_index != 0) {
tracef("r->configuration_uncommitted_index %llu", r->configuration_uncommitted_index);
rv = RAFT_CANTCHANGE;
goto err;
}
if (r->leader_state.promotee_id != 0) {
tracef("r->leader_state.promotee_id %llu", r->leader_state.promotee_id);
rv = RAFT_CANTCHANGE;
goto err;
}
/* In order to become leader at all we are supposed to have committed at
* least the initial configuration at index 1. */
assert(r->configuration_index > 0);
/* The index of the last committed configuration can't be greater than the
* last log index. */
assert(logLastIndex(&r->log) >= r->configuration_index);
/* No catch-up round should be in progress. */
assert(r->leader_state.round_number == 0);
assert(r->leader_state.round_index == 0);
assert(r->leader_state.round_start == 0);
return 0;
err:
assert(rv != 0);
ErrMsgFromCode(r->errmsg, rv);
return rv;
}
bool membershipUpdateCatchUpRound(struct raft *r)
{
unsigned server_index;
raft_index match_index;
raft_index last_index;
raft_time now = r->io->time(r->io);
raft_time round_duration;
bool is_up_to_date;
bool is_fast_enough;
assert(r->state == RAFT_LEADER);
assert(r->leader_state.promotee_id != 0);
server_index =
configurationIndexOf(&r->configuration, r->leader_state.promotee_id);
assert(server_index < r->configuration.n);
match_index = progressMatchIndex(r, server_index);
/* If the server did not reach the target index for this round, it did not
* catch up. */
if (match_index < r->leader_state.round_index) {
tracef("member (index: %u) not yet caught up match_index:%llu round_index:%llu",
server_index, match_index, r->leader_state.round_index);
return false;
}
last_index = logLastIndex(&r->log);
round_duration = now - r->leader_state.round_start;
is_up_to_date = match_index == last_index;
is_fast_enough = round_duration < r->election_timeout;
tracef("member is_up_to_date:%d is_fast_enough:%d",
is_up_to_date, is_fast_enough);
/* If the server's log is fully up-to-date or the round that just terminated
* was fast enough, then the server as caught up. */
if (is_up_to_date || is_fast_enough) {
r->leader_state.round_number = 0;
r->leader_state.round_index = 0;
r->leader_state.round_start = 0;
return true;
}
/* If we get here it means that this catch-up round is complete, but there
* are more entries to replicate, or it was not fast enough. Let's start a
* new round. */
r->leader_state.round_number++;
r->leader_state.round_index = last_index;
r->leader_state.round_start = now;
return false;
}
int membershipUncommittedChange(struct raft *r,
const raft_index index,
const struct raft_entry *entry)
{
struct raft_configuration configuration;
int rv;
assert(r != NULL);
assert(r->state == RAFT_FOLLOWER);
assert(entry != NULL);
assert(entry->type == RAFT_CHANGE);
raft_configuration_init(&configuration);
rv = configurationDecode(&entry->buf, &configuration);
if (rv != 0) {
tracef("failed to decode configuration at index:%llu", index);
goto err;
}
configurationTrace(r, &configuration, "uncommitted config change");
raft_configuration_close(&r->configuration);
r->configuration = configuration;
r->configuration_uncommitted_index = index;
return 0;
err:
assert(rv != 0);
return rv;
}
int membershipRollback(struct raft *r)
{
const struct raft_entry *entry;
int rv;
assert(r != NULL);
assert(r->state == RAFT_FOLLOWER);
assert(r->configuration_uncommitted_index > 0);
tracef("roll back membership");
/* Fetch the last committed configuration entry. */
assert(r->configuration_index != 0);
entry = logGet(&r->log, r->configuration_index);
assert(entry != NULL);
/* Replace the current configuration with the last committed one. */
raft_configuration_close(&r->configuration);
raft_configuration_init(&r->configuration);
rv = configurationDecode(&entry->buf, &r->configuration);
if (rv != 0) {
return rv;
}
configurationTrace(r, &r->configuration, "roll back config");
r->configuration_uncommitted_index = 0;
return 0;
}
void membershipLeadershipTransferInit(struct raft *r,
struct raft_transfer *req,
raft_id id,
raft_transfer_cb cb)
{
req->cb = cb;
req->id = id;
req->start = r->io->time(r->io);
req->send.data = NULL;
r->transfer = req;
}
int membershipLeadershipTransferStart(struct raft *r)
{
const struct raft_server *server;
struct raft_message message;
int rv;
assert(r->transfer->send.data == NULL);
server = configurationGet(&r->configuration, r->transfer->id);
assert(server != NULL);
if (server == NULL) {
tracef("transferee server not found in configuration");
return -1;
}
message.type = RAFT_IO_TIMEOUT_NOW;
message.server_id = server->id;
message.server_address = server->address;
message.timeout_now.term = r->current_term;
message.timeout_now.last_log_index = logLastIndex(&r->log);
message.timeout_now.last_log_term = logLastTerm(&r->log);
r->transfer->send.data = r;
rv = r->io->send(r->io, &r->transfer->send, &message, NULL);
if (rv != 0) {
ErrMsgTransferf(r->io->errmsg, r->errmsg, "send timeout now to %llu",
server->id);
return rv;
}
return 0;
}
void membershipLeadershipTransferClose(struct raft *r)
{
struct raft_transfer *req = r->transfer;
raft_transfer_cb cb = req->cb;
r->transfer = NULL;
if (cb != NULL) {
cb(req);
}
}