forked from daos-stack/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_log.c
324 lines (272 loc) · 7.28 KB
/
raft_log.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
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
/**
* 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 ADT for managing Raft log entries (aka entries)
* @author Willem Thiart [email protected]
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "raft.h"
#include "raft_private.h"
#include "raft_log.h"
#define INITIAL_CAPACITY 10
typedef struct
{
/* size of array */
int size;
/* the amount of elements in the array */
int count;
/* position of the queue */
int front;
/* we compact the log, and thus need to increment the Base Log Index */
int base;
/* term of the base */
int base_term;
raft_entry_t* entries;
/* callbacks */
raft_cbs_t *cb;
void* raft;
} log_private_t;
static int __ensurecapacity(log_private_t * me, int n)
{
int newsize;
raft_entry_t *temp;
if ((me->count + n) <= me->size)
return 0;
for (newsize = me->size; newsize < (me->count + n); newsize *= 2)
;
temp = (raft_entry_t*) calloc(1, sizeof(raft_entry_t) * newsize);
if (!temp)
return RAFT_ERR_NOMEM;
if (0 < me->count)
{
int k = me->size - me->front;
if (me->count <= k)
{
memcpy(&temp[0], &me->entries[me->front],
sizeof(raft_entry_t) * me->count);
}
else
{
memcpy(&temp[0], &me->entries[me->front],
sizeof(raft_entry_t) * k);
memcpy(&temp[k], &me->entries[0],
sizeof(raft_entry_t) * (me->count - k));
}
}
/* clean up old entries */
free(me->entries);
me->size = newsize;
me->entries = temp;
me->front = 0;
return 0;
}
void log_load_from_snapshot(log_t *me_, int idx, int term)
{
log_private_t* me = (log_private_t*)me_;
log_clear(me_);
me->base = idx;
me->base_term = term;
}
log_t* log_alloc(int initial_size)
{
log_private_t* me = (log_private_t*)calloc(1, sizeof(log_private_t));
if (!me)
return NULL;
me->size = initial_size;
log_clear((log_t*)me);
me->entries = (raft_entry_t*)calloc(1, sizeof(raft_entry_t) * me->size);
if (!me->entries) {
free(me);
return NULL;
}
return (log_t*)me;
}
log_t* log_new()
{
return log_alloc(INITIAL_CAPACITY);
}
void log_set_callbacks(log_t* me_, raft_cbs_t* funcs, void* raft)
{
log_private_t* me = (log_private_t*)me_;
me->raft = raft;
me->cb = funcs;
}
void log_clear(log_t* me_)
{
log_private_t* me = (log_private_t*)me_;
me->count = 0;
me->front = 0;
me->base = 0;
me->base_term = 0;
}
static int has_idx(log_private_t* me, int idx)
{
return me->base < idx && idx <= me->base + me->count;
}
/* Return the me->entries[] subscript for idx. */
static int subscript(log_private_t* me, int idx)
{
return (me->front + (idx - (me->base + 1))) % me->size;
}
/* Return the maximal number of contiguous entries in me->entries[]
* starting from and including idx up to at the most n entries. */
static int batch_up(log_private_t* me, int idx, int n)
{
assert(n > 0);
int lo = subscript(me, idx);
int hi = subscript(me, idx + n - 1);
return (lo <= hi) ? (hi - lo + 1) : (me->size - lo);
}
/* Return the maximal number of contiguous entries in me->entries[]
* starting from and including idx down to at the most n entries. */
static int batch_down(log_private_t* me, int idx, int n)
{
assert(n > 0);
int hi = subscript(me, idx);
int lo = subscript(me, idx - n + 1);
return (lo <= hi) ? (hi - lo + 1) : (hi + 1);
}
raft_entry_t* log_get_from_idx(log_t* me_, int idx, int *n_etys)
{
log_private_t* me = (log_private_t*)me_;
if (!has_idx(me, idx))
{
*n_etys = 0;
return NULL;
}
*n_etys = batch_up(me, idx, (me->base + me->count) - idx + 1);
return &me->entries[subscript(me, idx)];
}
raft_entry_t* log_get_at_idx(log_t* me_, int idx)
{
int n;
return log_get_from_idx(me_, idx, &n);
}
int log_count(log_t* me_)
{
return ((log_private_t*)me_)->count;
}
int log_append(log_t* me_, raft_entry_t* ety, int *n)
{
log_private_t* me = (log_private_t*)me_;
int i, e = 0;
e = __ensurecapacity(me, *n);
if (e != 0)
return e;
for (i = 0; i < *n; )
{
int idx = me->base + me->count + 1;
int k = batch_up(me, idx, *n - i);
int start = subscript(me, me->base + me->count + 1);
raft_entry_t *ptr = &me->entries[start];
int batch_size = k;
memcpy(ptr, &ety[i], k * sizeof(raft_entry_t));
if (me->cb && me->cb->log_offer)
e = me->cb->log_offer(me->raft, raft_get_udata(me->raft),
ptr, idx, &k);
if (k > 0)
{
me->count += k;
i += k;
raft_offer_log(me->raft, ptr, k, idx);
}
if (0 != e) {
*n = i;
return e;
}
assert(batch_size == k);
}
return 0;
}
int log_delete(log_t* me_, int idx)
{
log_private_t* me = (log_private_t*)me_;
int e = 0;
if (!has_idx(me, idx))
return -1;
while (idx <= me->base + me->count)
{
int k = batch_down(me, me->base + me->count,
me->base + me->count - idx + 1);
int start = subscript(me, me->base + me->count - k + 1);
raft_entry_t *ptr = &me->entries[start];
int batch_size = k;
if (me->cb && me->cb->log_pop)
e = me->cb->log_pop(me->raft, raft_get_udata(me->raft),
ptr, idx, &k);
if (k > 0)
{
raft_pop_log(me->raft, ptr, k, idx);
me->count -= k;
}
if (0 != e)
return e;
assert(k == batch_size);
}
return 0;
}
int log_poll(log_t* me_, int idx)
{
log_private_t* me = (log_private_t*)me_;
int e = 0;
if (!has_idx(me, idx))
return -1;
while (me->base + 1 <= idx)
{
int k = batch_up(me, me->base + 1, idx - (me->base + 1) + 1);
int batch_size = k;
if (me->cb && me->cb->log_poll)
e = me->cb->log_poll(me->raft, raft_get_udata(me->raft),
&me->entries[me->front], me->base + 1, &k);
if (k > 0)
{
me->base_term = me->entries[subscript(me, me->base + k)].term;
me->front += k;
me->front %= me->size;
me->count -= k;
me->base += k;
}
if (0 != e)
return e;
assert(k == batch_size);
}
return 0;
}
raft_entry_t *log_peektail(log_t * me_)
{
log_private_t* me = (log_private_t*)me_;
if (0 == me->count)
return NULL;
return &me->entries[subscript(me, me->base + me->count)];
}
void log_empty(log_t * me_)
{
log_private_t* me = (log_private_t*)me_;
me->front = 0;
me->count = 0;
}
void log_free(log_t * me_)
{
log_private_t* me = (log_private_t*)me_;
free(me->entries);
free(me);
}
int log_get_current_idx(log_t* me_)
{
log_private_t* me = (log_private_t*)me_;
return log_count(me_) + me->base;
}
int log_get_base(log_t* me_)
{
return ((log_private_t*)me_)->base;
}
int log_get_base_term(log_t* me_)
{
return ((log_private_t*)me_)->base_term;
}