-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraft_shm.cc
577 lines (498 loc) · 15.4 KB
/
raft_shm.cc
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* libraft, C interface to Hashicorp's Raft implementation.
* Copyright (C) 2015 Clayton Wheeler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#include <atomic>
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <getopt.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#include <list>
#include <string>
#include "config.h"
#include "raft_shm.h"
#include "stats.h"
namespace raft {
using boost::interprocess::unique_instance;
namespace api {
// required to avoid stupid link errors...
#define api_call(name, argT, hasRet) \
const CallTag name::tag; \
name::allocator_t* name::allocator;
#include "raft_api_calls.h"
#include "raft_fsm_calls.h"
#undef api_call
}
namespace {
std::list<BaseSlot*> orphan_backlog;
queue::Deque<BaseSlot*> orphaned_calls;
std::thread raft_watcher;
std::thread orphan_gc_thread;
std::vector<const char*> build_raft_argv(const RaftConfig& cfg);
void watch_raft_proc(pid_t raft_pid);
void run_orphan_gc();
void scan_orphans(std::list<BaseSlot*>& calls);
bool try_dispose_orphan(BaseSlot* orphan);
void report_process_status(const char *desc, pid_t pid, int status);
int log_init(const RaftConfig* cfg);
void init_client_allocators();
void init_raft_allocators();
std::vector<std::string> make_tag_names();
const std::vector<std::string> tag_names = make_tag_names();
}
zlog_category_t* msg_cat;
zlog_category_t* fsm_cat;
zlog_category_t* shm_cat;
zlog_category_t* client_cat;
pid_t raft_pid;
managed_mapped_file shm;
Scoreboard* scoreboard;
const char* tag_name(CallTag tag)
{
return tag_names.at((size_t) tag).c_str();
}
bool is_terminal(CallState state)
{
switch (state) {
case CallState::Pending:
case CallState::Dispatched:
return false;
case CallState::Success:
case CallState::Error:
return true;
default: // make poor old gcc happy
assert(false && "unexpected call state!");
}
}
ApplyArgs::ApplyArgs(offset_ptr<char> cmd_buf_, size_t cmd_len_, uint64_t timeout_ns_)
: cmd_buf(cmd_buf_),
cmd_len(cmd_len_),
timeout_ns(timeout_ns_)
{}
BarrierArgs::BarrierArgs(uint64_t timeout_ns_)
: timeout_ns(timeout_ns_)
{}
LogEntry::LogEntry(uint64_t index_, uint64_t term_, raft_log_type log_type_,
shm_handle data_buf_, size_t data_len_)
: index(index_),
term(term_),
log_type(log_type_),
data_buf(data_buf_),
data_len(data_len_)
{}
Filename::Filename(const char* path_)
{
strncpy(path, path_, MAXLEN);
}
NetworkAddr::NetworkAddr(const char* host_, uint16_t port_)
: port(port_)
{
strncpy(host, host_, 255);
}
Scoreboard::Scoreboard()
: is_leader(false),
shutdown_requested(false),
raft_killed(false)
{}
void Scoreboard::wait_for_raft(pid_t raft_pid)
{
while (! is_raft_running) {
// if the process exits, the raft_watcher thread will catch it
usleep(100000); // 100 ms
}
}
bool in_shm_bounds(const void* ptr)
{
char* base = (char*) shm.get_address();
char* cptr = (char*) ptr;
return (cptr >= base) && (cptr < base + shm.get_size());
}
char* allocate_buf(size_t len)
{
char *buf = (char*) shm.allocate(len);
stats->buffer_alloc.inc();
zlog_debug(shm_cat, "Allocated buffer: %p (%zu bytes)", buf, len);
return buf;
}
void free_buf(const char *buf)
{
stats->buffer_free.inc();
zlog_debug(shm_cat, "Freeing buffer: %p", buf);
shm.deallocate((void*) buf);
}
void track_orphan(BaseSlot* slot)
{
zlog_debug(msg_cat, "Enqueueing orphaned call: %p", slot);
orphaned_calls.put(slot);
}
namespace {
const static auto orphan_interval = std::chrono::milliseconds(100);
void run_orphan_gc()
{
try {
zlog_debug(msg_cat, "Orphan GC thread started.");
orphan_backlog = decltype(orphan_backlog)();
auto next_check = std::chrono::system_clock::now() + orphan_interval;
for (;;) {
BaseSlot* orphan;
if (orphan_backlog.empty()) {
orphan = orphaned_calls.take();
} else {
auto res = orphaned_calls.poll_until(next_check);
orphan = res.first ? res.second : nullptr;
}
if (orphan && !try_dispose_orphan(orphan)) {
orphan_backlog.push_back(orphan);
}
if (!orphan_backlog.empty()
&& std::chrono::system_clock::now() > next_check) {
// zlog_debug(msg_cat, "Scanning backlog of %lu orphans.",
// orphan_backlog.size());
scan_orphans(orphan_backlog);
next_check = std::chrono::system_clock::now() + orphan_interval;
}
}
} catch (queue::queue_closed&) {
zlog_debug(msg_cat, "Orphan queue is closed, GC thread exiting.");
return;
}
}
void scan_orphans(std::list<BaseSlot*>& calls)
{
for (auto slot_i = calls.begin(); slot_i != calls.end();) {
BaseSlot* orphan = *slot_i;
if (try_dispose_orphan(orphan)) {
slot_i = calls.erase(slot_i);
} else {
++slot_i;
}
}
}
bool try_dispose_orphan(BaseSlot* orphan)
{
assert(orphan);
std::unique_lock<decltype(orphan->owned)>
slot_lock(orphan->owned, std::try_to_lock);
if (slot_lock && is_terminal(orphan->state)) {
// zlog_debug(msg_cat, "Disposing of terminal orphan: %p", orphan);
orphan->dispose();
return true;
} else {
return false;
}
}
}
void shm_init(const char* path, bool create, const RaftConfig* config)
{
if (log_init(config) != 0) {
fprintf(stderr, "zlog init failed\n");
}
// register on-exit callback to call remove()?
if (create) {
// client side
assert(config);
struct stat shm_stat;
if (stat(path, &shm_stat) == 0) {
if (unlink(path) == -1) {
perror("Failed to remove old shared memory file");
exit(1);
}
} else if (errno != ENOENT) {
perror("Problem with shared memory file");
exit(1);
}
try {
shm = managed_mapped_file(boost::interprocess::create_only,
path, config->shm_size);
} catch (boost::interprocess::interprocess_exception& e) {
zlog_fatal(shm_cat, "Failed to open shared memory file %s: %s",
path, e.what());
exit(1);
}
zlog_debug(shm_cat, "Mapped shared memory file %s, %zd MB.",
path, config->shm_size / 1048576);
scoreboard = shm.construct<Scoreboard>(unique_instance)();
RaftConfig* shared_config = shm.construct<RaftConfig>(unique_instance)();
*shared_config = *config;
strncpy(shared_config->shm_path, path, 255);
stats = shm.construct<Stats>(unique_instance)();
const char* timing_e = getenv("RAFT_TIMING");
scoreboard->msg_timing = (timing_e && *timing_e);
init_client_allocators();
orphaned_calls.reset();
zlog_debug(shm_cat, "Initialized shared memory and scoreboard.");
} else {
// Raft side
assert(!config);
shm = managed_mapped_file(boost::interprocess::open_only,
path);
zlog_debug(shm_cat, "Opened shared memory file %s.", path);
// unlink the file after we've mapped it, nobody else will need it
// XXX: add option to leave it for debugging?
if (unlink(path) == -1) {
perror("Failed to unlink shared memory file");
exit(1);
}
auto shm_config = shm.find<RaftConfig>(unique_instance).first;
if (log_init(shm_config) != 0) {
fprintf(stderr, "zlog init failed\n");
}
auto ret = shm.find<Scoreboard>(unique_instance);
scoreboard = ret.first;
assert(scoreboard);
stats = shm.find<Stats>(unique_instance).first;
init_raft_allocators();
zlog_debug(shm_cat, "Found scoreboard.");
}
zlog_debug(shm_cat, "Mapped shared memory at base address %p.",
raft::shm.get_address());
}
void shm_cleanup()
{
scoreboard->api_queue.close();
orphaned_calls.close();
assert(orphan_gc_thread.joinable());
orphan_gc_thread.join();
if (raft_watcher.joinable())
raft_watcher.join();
shm = decltype(shm)();
}
pid_t run_raft()
{
pid_t kidpid = fork();
if (kidpid == -1) {
perror("Cannot fork");
exit(1);
} else if (kidpid) {
// parent
raft_pid = kidpid;
// start the watcher thread
assert(! raft_watcher.joinable());
raft_watcher = std::thread(watch_raft_proc, raft_pid);
// start the call GC thread
assert(! orphan_gc_thread.joinable());
orphan_gc_thread = std::thread(run_orphan_gc);
return kidpid;
} else {
// child
auto config = shm.find<RaftConfig>(unique_instance).first;
auto argv = build_raft_argv(*config);
int rc = execvp("raft_if", (char * const *)argv.data());
if (rc) {
perror("Exec failed");
}
exit(1);
}
}
void orphan_cleanup(const ApplyArgs args)
{
// zlog_debug(shm_cat, "Cleaning up orphan Apply args.");
const char* raw_buf = (const char*) args.cmd_buf.get();
if (in_shm_bounds((void*) raw_buf)) {
free_raft_buffer(raw_buf);
}
}
BaseSlot::BaseSlot(CallTag tag_)
: tag(tag_),
state(CallState::Pending),
client_state(ClientState::Issued),
retval(0),
error(RAFT_SUCCESS),
timings()
{}
BaseSlot::call_rec BaseSlot::rec()
{
return { tag, pointer(this) };
}
void BaseSlot::reply(RaftError err)
{
error = err;
assert(! is_terminal(state));
state = (err == RAFT_SUCCESS) ? CallState::Success : CallState::Error;
ret_cond.notify_one();
timings.record("reply sent");
}
void BaseSlot::reply(uint64_t retval_)
{
retval = retval_;
reply(RAFT_SUCCESS);
}
void BaseSlot::wait()
{
std::unique_lock<interprocess_mutex> lock(owned);
ret_cond.wait(lock, [&] () { return is_terminal(state); });
client_state = ClientState::Observed;
timings.record("result received");
}
bool BaseSlot::poll()
{
std::unique_lock<interprocess_mutex> lock(owned, std::try_to_lock);
return lock && is_terminal(state);
}
int kill_raft_()
{
scoreboard->raft_killed = true;
return kill(raft_pid, SIGTERM);
}
namespace {
std::vector<const char*> build_raft_argv(const RaftConfig& cfg)
{
std::vector<const char*> args;
args.push_back("raft_if");
args.push_back("--shm-path");
args.push_back(cfg.shm_path);
args.push_back(nullptr);
return args;
}
void watch_raft_proc(pid_t raft_pid)
{
for (;;) {
int status;
pid_t pid = waitpid(raft_pid, &status, 0);
assert(pid != 0);
if (pid > 0) {
if (WIFSTOPPED(status)) {
// attached a debugger or something...
continue;
} else {
report_process_status("Raft process", raft_pid, status);
// TODO: bubble this back up to the client? recover?
if (scoreboard->shutdown_requested
&& WIFEXITED(status)
&& WEXITSTATUS(status) == 0) {
return;
} else if (scoreboard->shutdown_requested
&& scoreboard->raft_killed) {
zlog_warn(shm_cat, "Raft intentionally killed during shutdown!");
return;
} else {
exit(1);
}
}
} else {
perror("waitpid failed");
exit(1);
}
}
}
void report_process_status(const char *desc, pid_t pid, int status)
{
if (WIFEXITED(status)) {
const int exitstatus = WEXITSTATUS(status);
if (exitstatus == 0) {
fprintf(stderr, "%s (pid %d) exited normally.\n",
desc, pid);
} else {
fprintf(stderr, "%s (pid %d) exited abnormally with status %d.\n",
desc, pid, exitstatus);
}
} else if (WIFSIGNALED(status)) {
fprintf(stderr, "%s (pid %d) terminated by signal %d.\n",
desc, pid, WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
fprintf(stderr, "%s (pid %d) stopped by signal %d.\n",
desc, pid, WSTOPSIG(status));
} else {
assert(false && "impossible process status!");
}
}
int log_init(const RaftConfig* cfg)
{
char* v_env = getenv("VERBOSE");
bool run_verbose = ((v_env && *v_env) || (cfg && cfg->verbose));
const char *config_file = run_verbose ? "zlog_verbose.conf" : "zlog.conf";
//fprintf(stderr, "Loading zlog config: %s\n", config_file);
int rc;
if (!msg_cat) {
rc = zlog_init(config_file);
} else {
rc = zlog_reload(config_file);
}
if (rc)
return rc;
msg_cat = zlog_get_category("raft_msg");
fsm_cat = zlog_get_category("raft_fsm");
shm_cat = zlog_get_category("raft_shm");
return 0;
}
void init_client_allocators()
{
#define api_call(name, argT, hasRet) \
api::name::allocator = new api::name::allocator_t(shm.get_segment_manager());
#include "raft_api_calls.h"
#undef api_call
}
void init_raft_allocators()
{
#define api_call(name, argT, hasRet) \
api::name::allocator = new api::name::allocator_t(shm.get_segment_manager());
#include "raft_fsm_calls.h"
#undef api_call
}
std::vector<std::string> make_tag_names()
{
std::vector<std::string> names(256, "*invalid*");
#define api_call(name, argT, hasRet) \
names.at((size_t) CallTag::name) = #name;
#include "raft_api_calls.h"
#include "raft_fsm_calls.h"
#undef api_call
return names;
}
} // end anon namespace
Timings::Timings(time_point t)
: n_entries(0)
{
record("start", t);
}
void Timings::record(const char *tag)
{
record(tag, clock::now());
}
void Timings::record(const char *tag, time_point t)
{
if (n_entries < MAX_ENT) {
entry& ent = entries[n_entries++];
ent.ts = t;
strncpy(ent.tag, tag, 19);
}
}
void Timings::print()
{
if (!scoreboard->msg_timing)
return;
if (n_entries <= 1) {
zlog_warn(msg_cat, "No timing data!");
return;
}
time_point start = entries[0].ts;
time_point prev = entries[0].ts;
for (uint32_t i = 1; i < n_entries; ++i) {
int64_t elapsed_us = std::chrono::duration_cast<std::chrono::microseconds>(entries[i].ts - start).count();
int64_t delta_us = std::chrono::duration_cast<std::chrono::microseconds>(entries[i].ts - prev).count();
fprintf(stderr, "%-20s @ %7" PRId64 " us, delta %7" PRId64 " us.\n",
entries[i].tag, elapsed_us, delta_us);
prev = entries[i].ts;
}
}
}