forked from daos-stack/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_node.c
198 lines (169 loc) · 5.07 KB
/
raft_node.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
/**
* 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
* @brief Representation of a peer
* @author Willem Thiart [email protected]
* @version 0.1
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "raft.h"
#define RAFT_NODE_VOTED_FOR_ME (1 << 0)
#define RAFT_NODE_HAS_SUFFICIENT_LOG (1 << 1)
typedef struct
{
raft_server_t* server;
void* udata;
int next_idx;
int match_idx;
/* index of last offered log entry for this node */
int offered_idx;
/* index of last committed log entry for this node */
int applied_idx;
int flags;
int id;
} raft_node_private_t;
raft_node_t* raft_node_new(void* udata, int id)
{
raft_node_private_t* me;
me = (raft_node_private_t*)calloc(1, sizeof(raft_node_private_t));
if (!me)
return NULL;
me->udata = udata;
me->next_idx = 1;
me->match_idx = 0;
me->offered_idx = -1;
me->applied_idx = -2; /* start with addition not committed */
me->id = id;
me->flags = 0;
return (raft_node_t*)me;
}
void raft_node_free(raft_node_t* me_)
{
free(me_);
}
void raft_node_set_server(raft_node_t* me_, raft_server_t *server)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->server = server;
}
int raft_node_get_next_idx(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->next_idx;
}
void raft_node_set_next_idx(raft_node_t* me_, int nextIdx)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
/* log index begins at 1 */
me->next_idx = nextIdx < 1 ? 1 : nextIdx;
}
int raft_node_get_match_idx(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->match_idx;
}
void raft_node_set_match_idx(raft_node_t* me_, int matchIdx)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->match_idx = matchIdx;
}
int raft_node_get_offered_idx(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->offered_idx;
}
void raft_node_set_offered_idx(raft_node_t* me_, int offeredIdx)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->offered_idx = offeredIdx;
}
int raft_node_get_applied_idx(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->applied_idx;
}
void raft_node_set_applied_idx(raft_node_t* me_, int appliedIdx)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->applied_idx = appliedIdx;
}
void* raft_node_get_udata(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return me->udata;
}
void raft_node_set_udata(raft_node_t* me_, void* udata)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->udata = udata;
}
void raft_node_vote_for_me(raft_node_t* me_, const int vote)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
if (vote)
me->flags |= RAFT_NODE_VOTED_FOR_ME;
else
me->flags &= ~RAFT_NODE_VOTED_FOR_ME;
}
int raft_node_has_vote_for_me(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return (me->flags & RAFT_NODE_VOTED_FOR_ME) != 0;
}
int raft_node_has_sufficient_logs(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return (me->flags & RAFT_NODE_HAS_SUFFICIENT_LOG) != 0;
}
void raft_node_set_has_sufficient_logs(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
me->flags |= RAFT_NODE_HAS_SUFFICIENT_LOG;
}
int raft_node_is_voting(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
if (!me)
return 0;
if (me->offered_idx == -1)
return raft_node_is_voting_committed(me_);
raft_entry_t *ety = raft_get_entry_from_idx(me->server, me->offered_idx);
return (!ety || ety->type == RAFT_LOGTYPE_ADD_NODE);
}
int raft_node_is_active(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
if (me->offered_idx == -1)
return raft_node_is_addition_committed(me_);
raft_entry_t *ety = raft_get_entry_from_idx(me->server, me->offered_idx);
/* A node is active by default, unless explicitly marked for removal */
return (!ety || ety->type != RAFT_LOGTYPE_REMOVE_NODE);
}
int raft_node_is_voting_committed(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
if (me->applied_idx < 0)
return 0;
raft_entry_t *ety = raft_get_entry_from_idx(me->server, me->applied_idx);
return (!ety || ety->type == RAFT_LOGTYPE_ADD_NODE);
}
int raft_node_is_addition_committed(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
if (me->applied_idx < -1)
return 0;
raft_entry_t *ety = raft_get_entry_from_idx(me->server, me->applied_idx);
/* Addition is committed if highest applied entry is not for removal */
return (!ety || ety->type != RAFT_LOGTYPE_REMOVE_NODE);
}
int raft_node_get_id(raft_node_t* me_)
{
raft_node_private_t* me = (raft_node_private_t*)me_;
return (NULL == me) ? -1 : me->id;
}