-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.cc
142 lines (122 loc) · 3.3 KB
/
config.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
#include <cstdlib>
#include <cstring>
#include "config.h"
#include "raft_shm.h"
namespace raft {
const struct option arg::LONG_OPTS[] = {
{ "shm-path", required_argument, NULL, arg::ShmPath },
{ "shm-size", required_argument, NULL, arg::ShmSize },
{ "backend-type", required_argument, NULL, arg::Backend },
{ "dir", required_argument, NULL, arg::Dir },
{ "port", required_argument, NULL, arg::Port },
{ "single", no_argument, NULL, arg::Single },
{ "peers", required_argument, NULL, arg::Peers },
{ "verbose", no_argument, NULL, arg::Verbose },
{ "", 0, NULL, 0 }
};
namespace {
uint64_t parse_size(const char *spec);
}
RaftConfig default_config()
{
const static uint64_t US = 1000; // ns
const static uint64_t MS = 1000 * US;
const static uint64_t S = 1000 * MS;
const static uint64_t M = 60 * S;
RaftConfig cfg;
strncpy(cfg.shm_path, SHM_PATH, 255);
cfg.shm_size = SHM_SIZE;
strncpy(cfg.backend_type, "boltdb", 15);
cfg.base_dir[0] = '\0';
cfg.api_workers = 4;
cfg.verbose = false;
cfg.peers[0] = '\0';
cfg.listen_port = 9001;
// as dumped from Go
cfg.HeartbeatTimeout = 1*S;
cfg.ElectionTimeout = 1*S;
cfg.CommitTimeout = 50*MS;
cfg.MaxAppendEntries = 64;
cfg.ShutdownOnRemove = true;
cfg.DisableBootstrapAfterElect = true;
cfg.TrailingLogs = 10240;
cfg.SnapshotInterval = 2*M;
cfg.SnapshotThreshold = 8192;
cfg.EnableSingleNode = false;
cfg.LeaderLeaseTimeout = 500*MS;
cfg.RetainSnapshots = 8;
return cfg;
}
bool arg::is_valid(int option)
{
return (option >= ShmPath && option < END_OPTS);
}
int arg::apply(RaftConfig& cfg, Getopt option, const char *arg)
{
switch (option) {
case arg::ShmPath:
assert(arg);
strncpy(cfg.shm_path, arg, 255);
break;
case arg::ShmSize:
assert(arg);
cfg.shm_size = parse_size(arg);
break;
case arg::Backend:
assert(arg);
strncpy(cfg.backend_type, arg, sizeof(cfg.backend_type)-1);
break;
case arg::Dir:
assert(arg);
strncpy(cfg.base_dir, arg, 255);
break;
case arg::Port:
assert(arg);
cfg.listen_port = atoi(arg);
break;
case arg::Single:
cfg.EnableSingleNode = true;
break;
case arg::Peers:
assert(arg);
strncpy(cfg.peers, arg, 255);
break;
case arg::Verbose:
cfg.verbose = true;
break;
default:
assert(false && "unhandled config flag");
}
return 0;
}
namespace {
uint64_t parse_size(const char *spec)
{
char *endp = nullptr;
uint64_t coeff = strtoul(spec, &endp, 10);
uint64_t val;
assert(endp);
switch (*endp) {
case '\0':
val = coeff;
break;
case 'k':
case 'K':
val = coeff * 1024;
break;
case 'm':
case 'M':
val = coeff * 1024 * 1024;
break;
case 'g':
case 'G':
val = coeff * 1024 * 1024 * 1024;
break;
default:
zlog_error(shm_cat, "Unhandled trailing character %c in size", *endp);
val = coeff;
}
return val;
}
} // end anon namespace
} // end namespace raft