-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_test_utils.h
476 lines (417 loc) · 14.8 KB
/
raft_test_utils.h
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#ifndef test_utils_h
#define test_utils_h
#include <dirent.h>
#include <fcntl.h>
#include <set>
#include <sstream>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/times.h>
#include "raft.h"
#include "rpc.h"
/******************************************************************
For Test Framework
*******************************************************************/
#define ASSERT(condition, message) \
do { \
if (!(condition)) { \
std::cerr << "Assertion `" #condition "` failed in " << __FILE__ << ":" \
<< __LINE__ << " msg: " << message << std::endl; \
std::terminate(); \
} \
} while (false)
#define TEST_CASE(part, name, msg) \
class test_case_##part##_##name##_ut : public unit_test_case { \
public: \
test_case_##part##_##name##_ut(const char *_msg) { this->message = _msg; } \
virtual void body() override; \
}; \
static unit_test_case *part##_##name = \
unit_test_suite::instance()->register_test_case( \
#part, #name, new test_case_##part##_##name##_ut(msg)); \
void test_case_##part##_##name##_ut::body()
#define DEBUG
class unit_test_case {
public:
virtual void body() = 0;
const char *message;
};
class unit_test_suite {
public:
static unit_test_suite *instance();
unit_test_case *register_test_case(const char *part, const char *name,
unit_test_case *obj);
bool run_case(const std::string &part, const std::string &name,
unit_test_case *obj);
bool run_part_case(const std::string &part, const std::string &name);
bool run_all();
bool run(int argc, char **argv);
private:
std::vector<std::pair<std::string,
std::vector<std::pair<std::string, unit_test_case *>>>>
cases; // (part, name) -> case
bool is_counting;
int count;
int passed;
};
void mssleep(int ms);
int remove_directory(const char *path);
/******************************************************************
For Raft Test
*******************************************************************/
std::vector<rpcs *> create_random_rpc_servers(int num);
std::vector<rpcc *> create_rpc_clients(const std::vector<rpcs *> &servers);
// std::vector<raft<list_state_machine, list_command>*> create_raft_nodes(int
// num);
class list_command : public raft_command {
public:
list_command() {}
list_command(const list_command &cmd) { value = cmd.value; }
list_command(int v) : value(v) {}
virtual ~list_command() {}
virtual int size() const override { return 4; }
virtual void serialize(char *buf, int sz) const override {
if (sz != size())
return;
buf[0] = (value >> 24) & 0xff;
buf[1] = (value >> 16) & 0xff;
buf[2] = (value >> 8) & 0xff;
buf[3] = value & 0xff;
}
virtual void deserialize(const char *buf, int sz) override {
if (sz != size())
return;
value = (buf[0] & 0xff) << 24;
value |= (buf[1] & 0xff) << 16;
value |= (buf[2] & 0xff) << 8;
value |= buf[3] & 0xff;
}
int value;
};
marshall &operator<<(marshall &m, const list_command &cmd);
unmarshall &operator>>(unmarshall &u, list_command &cmd);
class list_state_machine : public raft_state_machine {
public:
list_state_machine();
virtual ~list_state_machine() {}
virtual std::vector<char> snapshot() override;
virtual void apply_log(raft_command &cmd) override;
virtual void apply_snapshot(const std::vector<char> &) override;
std::mutex mtx;
std::vector<int> store;
int num_append_logs;
};
template <typename state_machine, typename command> class raft_group {
public:
// typedef raft<list_state_machine, list_command> raft<state_machine,
// command>;
raft_group(int num, const char *storage_dir = "raft_temp");
~raft_group();
int check_exact_one_leader();
void check_no_leader();
void set_reliable(bool value);
void disable_node(int i);
void enable_node(int i);
int check_same_term();
int num_committed(int log_idx);
int get_committed_value(int log_idx);
int append_new_command(int value, int num_committed_server);
int wait_commit(int index, int num_committed_server, int start_term);
int rpc_count(int node);
int restart(int node);
std::vector<raft<state_machine, command> *> nodes;
std::vector<rpcs *> servers;
std::vector<std::vector<rpcc *>> clients;
std::vector<raft_storage<command> *> storages;
std::vector<state_machine *> states;
};
template <typename state_machine, typename command>
raft_group<state_machine, command>::raft_group(int num,
const char *storage_dir) {
printf("raft_group created begin\n");
nodes.resize(num, nullptr);
servers = create_random_rpc_servers(num);
clients.resize(num);
states.resize(num);
storages.resize(num);
remove_directory(storage_dir);
// ASSERT(ret == 0 || , "cannot rmdir " << ret);
ASSERT(mkdir(storage_dir, 0777) >= 0,
"cannot create dir " << std::string(storage_dir));
// printf("raft_group created-0\n");
for (int i = 0; i < num; i++) {
std::string dir_name(storage_dir);
dir_name = dir_name + "/raft_storage_" + std::to_string(i);
ASSERT(mkdir(dir_name.c_str(), 0777) >= 0,
"cannot create dir " << std::string(storage_dir));
raft_storage<command> *storage = new raft_storage<command>(dir_name);
state_machine *state = new state_machine();
auto client = create_rpc_clients(servers);
raft<state_machine, command> *node =
new raft<state_machine, command>(servers[i], client, i, storage, state);
nodes[i] = node;
clients[i] = client;
states[i] = state;
storages[i] = storage;
}
// printf("raft_group created-1\n");
for (int i = 0; i < num; i++)
nodes[i]->start();
// printf("raft_group created\n");
}
template <typename state_machine, typename command>
raft_group<state_machine, command>::~raft_group() {
set_reliable(true);
for (size_t i = 0; i < nodes.size(); i++) {
raft<state_machine, command> *node = nodes[i];
// disbale_node(i);
node->stop();
for (size_t j = 0; j < nodes.size(); j++) {
clients[i][j]->cancel();
delete clients[i][j];
}
delete servers[i];
delete node;
delete states[i];
delete storages[i];
}
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::check_exact_one_leader() {
int num_checks = 10;
int num_nodes = nodes.size();
for (int i = 0; i < num_checks; i++) {
std::map<int, int> term_leaders;
for (int j = 0; j < num_nodes; j++) {
raft<state_machine, command> *node = nodes[j];
if (!servers[j]->reachable())
continue;
int term = -1;
bool is_leader = node->is_leader(term);
if (is_leader) {
ASSERT(term > 0, "term " << term << " should not have a leader.");
ASSERT(term_leaders.find(term) == term_leaders.end(),
"term " << term << " has more than one leader.");
term_leaders[term] = j;
}
}
if (term_leaders.size() > 0) {
auto last_term = term_leaders.rbegin();
return last_term->second; // return the leader index
}
// sleep a while, in case the election is not successful.
mssleep(500 + (random() % 10) * 30);
}
ASSERT(0, "There is no leader"); // no leader
return -1;
}
template <typename state_machine, typename command>
void raft_group<state_machine, command>::check_no_leader() {
int num_nodes = nodes.size();
for (int j = 0; j < num_nodes; j++) {
raft<state_machine, command> *node = nodes[j];
rpcs *server = servers[j];
if (server->reachable()) {
int term = -1;
ASSERT(!node->is_leader(term),
"Node " << j << " is leader, which is unexpected.");
}
}
return;
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::check_same_term() {
int current_term = -1;
int num_nodes = nodes.size();
for (int i = 0; i < num_nodes; i++) {
int term = -1;
raft<state_machine, command> *node = nodes[i];
node->is_leader(term); // get the current term
ASSERT(term >= 0, "invalid term: " << term);
if (current_term == -1)
current_term = term;
ASSERT(current_term == term,
"inconsistent term: " << current_term << ", " << term);
}
return current_term;
}
template <typename state_machine, typename command>
void raft_group<state_machine, command>::disable_node(int i) {
rpcs *server = servers[i];
std::vector<rpcc *> &client = clients[i];
server->set_reachable(false);
for (auto c : client)
c->set_reachable(false);
}
template <typename state_machine, typename command>
void raft_group<state_machine, command>::enable_node(int i) {
rpcs *server = servers[i];
std::vector<rpcc *> &client = clients[i];
server->set_reachable(true);
for (auto c : client)
c->set_reachable(true);
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::num_committed(int log_idx) {
int cnt = 0;
int old_value = 0;
for (size_t i = 0; i < nodes.size(); i++) {
list_state_machine *state = states[i];
bool has_log;
int log_value;
{
std::unique_lock<std::mutex> lock(state->mtx);
if ((int)state->store.size() > log_idx) {
log_value = state->store[log_idx];
has_log = true;
} else {
has_log = false;
}
}
if (has_log) {
cnt++;
if (cnt == 1) {
old_value = log_value;
} else {
ASSERT(old_value == log_value, "inconsistent log value: ("
<< log_value << ", " << old_value
<< ") at idx " << log_idx);
}
}
}
return cnt;
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::get_committed_value(int log_idx) {
// printf("get committed value by log idx %d\n", log_idx);
for (size_t i = 0; i < nodes.size(); i++) {
list_state_machine *state = states[i];
int log_value;
{
std::unique_lock<std::mutex> lock(state->mtx);
if ((int)state->store.size() > log_idx) {
log_value = state->store[log_idx];
return log_value;
}
}
}
ASSERT(false, "log " << log_idx << " is not committed.");
return -1;
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::append_new_command(
int value, int expected_servers) {
list_command cmd(value);
auto start = std::chrono::system_clock::now();
int leader_idx = 0;
while (std::chrono::system_clock::now() < start + std::chrono::seconds(10)) {
int log_idx = -1;
for (size_t i = 0; i < nodes.size(); i++) {
leader_idx = (leader_idx + 1) % nodes.size();
// FIXME: lock?
if (!servers[leader_idx]->reachable())
continue;
int temp_idx, temp_term;
bool is_leader = nodes[leader_idx]->new_command(cmd, temp_term, temp_idx);
if (is_leader) {
log_idx = temp_idx;
break;
}
}
if (log_idx != -1) {
auto check_start = std::chrono::system_clock::now();
while (std::chrono::system_clock::now() <
check_start + std::chrono::seconds(2)) {
int committed_server = num_committed(log_idx);
// std::cout << "nun commited: " << committed_server << std::endl;
if (committed_server >= expected_servers) {
// The log is committed!
int commited_value = get_committed_value(log_idx);
if (commited_value == value){
return log_idx; // and the log is what we want!
}
}
mssleep(20);
}
} else {
// no leader
mssleep(50);
}
}
ASSERT(0, "Cannot make agreement!");
return -1;
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::wait_commit(int index,
int num_committed_server,
int start_term) {
int sleep_for = 10; // ms
for (int iters = 0; iters < 30; iters++) {
int nc = num_committed(index);
if (nc >= num_committed_server)
break;
mssleep(sleep_for);
if (sleep_for < 1000)
sleep_for *= 2;
if (start_term > -1) {
for (auto node : nodes) {
int current_term;
node->is_leader(current_term);
if (current_term > start_term) {
// someone has moved on
// can no longer guarantee that we'll "win"
return -1;
}
}
}
}
int nc = num_committed(index);
ASSERT(nc >= num_committed_server, "only " << nc << " decided for index "
<< index << "; wanted "
<< num_committed_server);
return get_committed_value(index);
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::rpc_count(int node) {
int sum = 0;
if (node == -1) {
for (auto &cl : clients) {
for (auto &cc : cl) {
sum += cc->count();
}
}
} else {
for (auto &cc : clients[node]) {
sum += cc->count();
}
}
return sum;
}
template <typename state_machine, typename command>
int raft_group<state_machine, command>::restart(int node) {
// std::cout << "restart " << node << std::endl;
nodes[node]->stop();
disable_node(node);
servers[node]->unreg_all();
delete nodes[node];
delete states[node];
states[node] = new state_machine();
raft_storage<command> *storage = new raft_storage<command>(
std::string("raft_temp/raft_storage_") + std::to_string(node));
// recreate clients
for (auto &cl : clients[node])
delete cl;
servers[node]->set_reachable(true);
clients[node] = create_rpc_clients(servers);
nodes[node] = new raft<state_machine, command>(servers[node], clients[node],
node, storage, states[node]);
// disable_node(node);
nodes[node]->start();
return 0;
}
template <typename state_machine, typename command>
void raft_group<state_machine, command>::set_reliable(bool value) {
for (auto server : servers)
server->set_reliable(value);
}
#endif // test_utils_h