forked from daos-stack/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_server_properties.c
254 lines (213 loc) · 6.05 KB
/
raft_server_properties.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
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
/**
* Copyright (c) 2013, Willem-Hendrik Thiart
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* @file
* @author Willem Thiart [email protected]
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
/* for varags */
#include <stdarg.h>
#include "raft.h"
#include "raft_log.h"
#include "raft_private.h"
void raft_set_election_timeout(raft_server_t* me_, int millisec)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
me->election_timeout = millisec;
raft_randomize_election_timeout(me_);
}
void raft_set_request_timeout(raft_server_t* me_, int millisec)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
me->request_timeout = millisec;
}
int raft_get_nodeid(raft_server_t* me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return me->node_id;
}
int raft_get_election_timeout(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->election_timeout;
}
int raft_get_request_timeout(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->request_timeout;
}
int raft_get_num_nodes(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->num_nodes;
}
int raft_get_num_voting_nodes(raft_server_t* me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
int i, num = 0;
for (i = 0; i < me->num_nodes; i++)
if (raft_node_is_active(me->nodes[i]) && raft_node_is_voting(me->nodes[i]))
num++;
return num;
}
int raft_get_timeout_elapsed(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->timeout_elapsed;
}
int raft_get_log_count(raft_server_t* me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return log_count(me->log);
}
int raft_get_voted_for(raft_server_t* me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return me->voted_for;
}
int raft_set_current_term(raft_server_t* me_, const int term)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
if (me->current_term < term)
{
int voted_for = -1;
if (me->cb.persist_term)
{
int e = me->cb.persist_term(me_, me->udata, term, voted_for);
if (0 != e)
return e;
}
me->current_term = term;
me->voted_for = voted_for;
}
return 0;
}
int raft_get_current_term(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->current_term;
}
int raft_get_current_idx(raft_server_t* me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return log_get_current_idx(me->log);
}
void raft_set_commit_idx(raft_server_t* me_, int idx)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
assert(me->commit_idx <= idx);
assert(idx <= raft_get_current_idx(me_));
me->commit_idx = idx;
}
void raft_set_last_applied_idx(raft_server_t* me_, int idx)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
me->last_applied_idx = idx;
}
int raft_get_last_applied_idx(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->last_applied_idx;
}
int raft_get_commit_idx(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->commit_idx;
}
void raft_set_state(raft_server_t* me_, int state)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
/* if became the leader, then update the current leader entry */
if (state == RAFT_STATE_LEADER)
me->leader_id = me->node_id;
me->state = state;
}
int raft_get_state(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->state;
}
raft_node_t* raft_get_node(raft_server_t *me_, int nodeid)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
int i;
for (i = 0; i < me->num_nodes; i++)
if (nodeid == raft_node_get_id(me->nodes[i]))
return me->nodes[i];
return NULL;
}
raft_node_t* raft_get_my_node(raft_server_t *me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return raft_get_node(me_, me->node_id);
}
raft_node_t* raft_get_node_from_idx(raft_server_t* me_, const int idx)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
return me->nodes[idx];
}
int raft_get_current_leader(raft_server_t* me_)
{
raft_server_private_t* me = (void*)me_;
return me->leader_id;
}
raft_node_t* raft_get_current_leader_node(raft_server_t* me_)
{
raft_server_private_t* me = (void*)me_;
return raft_get_node(me_, me->leader_id);
}
void* raft_get_udata(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->udata;
}
int raft_is_follower(raft_server_t* me_)
{
return raft_get_state(me_) == RAFT_STATE_FOLLOWER;
}
int raft_is_leader(raft_server_t* me_)
{
return raft_get_state(me_) == RAFT_STATE_LEADER;
}
int raft_is_candidate(raft_server_t* me_)
{
return raft_get_state(me_) == RAFT_STATE_CANDIDATE;
}
int raft_is_self(raft_server_t* me_, raft_node_t* node)
{
raft_server_private_t* me = (void*)me_;
return (node && raft_node_get_id(node) == me->node_id);
}
int raft_get_last_log_term(raft_server_t* me_)
{
int current_idx = raft_get_current_idx(me_);
int term;
int got = raft_get_entry_term(me_, current_idx, &term);
assert(got);
return term;
}
int raft_is_connected(raft_server_t* me_)
{
return ((raft_server_private_t*)me_)->connected;
}
int raft_snapshot_is_in_progress(raft_server_t *me_)
{
return ((raft_server_private_t*)me_)->snapshot_in_progress;
}
raft_entry_t *raft_get_last_applied_entry(raft_server_t *me_)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
if (raft_get_last_applied_idx(me_) == 0)
return NULL;
return log_get_at_idx(me->log, raft_get_last_applied_idx(me_));
}
int raft_get_snapshot_last_idx(raft_server_t *me_)
{
return ((raft_server_private_t*)me_)->snapshot_last_idx;
}
int raft_get_snapshot_last_term(raft_server_t *me_)
{
return ((raft_server_private_t*)me_)->snapshot_last_term;
}
void raft_set_snapshot_metadata(raft_server_t *me_, int term, int idx)
{
raft_server_private_t* me = (raft_server_private_t*)me_;
me->snapshot_last_term = term;
me->snapshot_last_idx = idx;
}