-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicated-log.h
436 lines (363 loc) · 13.9 KB
/
replicated-log.h
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
#ifndef PROTOTYPE_REPLICATED_LOG_H
#define PROTOTYPE_REPLICATED_LOG_H
#include <condition_variable>
#include <mutex>
#include <thread>
#include <immer/box.hpp>
#include <immer/flex_vector.hpp>
#include <immer/flex_vector_transient.hpp>
#include <velocypack/Buffer.h>
#include <velocypack/Builder.h>
#include <velocypack/Iterator.h>
#include <velocypack/Slice.h>
#include <velocypack/velocypack-aliases.h>
#include <iostream>
#include "futures.h"
#include "priority-queue.h"
#include "scheduler.h"
namespace rlog {
using log_index_type = std::size_t;
using log_term_type = std::size_t;
struct ReplicatedLogTrxAPI {
virtual ~ReplicatedLogTrxAPI() = default;
virtual auto insert(VPackBufferUInt8 payload)
-> std::pair<log_index_type, log_term_type> = 0;
struct WaitResult {
log_term_type term;
explicit WaitResult(log_term_type term) : term(term) {}
};
auto waitFor(std::pair<log_index_type, log_term_type> p) -> futures::future<WaitResult> {
auto [index, term] = p;
return waitFor(index, term);
}
virtual auto waitFor(log_index_type, log_term_type) -> futures::future<WaitResult> = 0;
};
struct ReplicatedLogLeadershipAPI {
virtual ~ReplicatedLogLeadershipAPI() = default;
virtual void assumeLeadership(log_term_type) = 0;
};
struct LogEntry {
log_index_type index;
log_term_type term;
VPackBufferUInt8 payload;
void intoBuilder(VPackBuilder& builder) const {
VPackObjectBuilder ob(&builder);
builder.add("index", VPackValue(index));
builder.add("term", VPackValue(term));
builder.add("payload", VPackSlice(payload.data()));
}
explicit LogEntry(VPackSlice slice) {
index = slice.get("index").getNumber<log_index_type>();
term = slice.get("term").getNumber<log_term_type>();
auto payloadSlice = slice.get("payload");
payload.append(payloadSlice.begin(), payloadSlice.byteSize());
}
LogEntry(log_index_type index, log_term_type term, VPackBufferUInt8 payload)
: index(index), term(term), payload(std::move(payload)) {}
};
struct LogIterator {
virtual ~LogIterator() = default;
virtual auto next() -> std::optional<immer::box<LogEntry>> = 0;
};
/* seite 4 */
struct AppendEntriesResult {
log_term_type term;
bool success;
explicit AppendEntriesResult(VPackSlice slice) {
term = slice.get("term").getNumber<log_term_type>();
success = slice.get("success").isTrue();
}
void intoBuilder(VPackBuilder& builder) const {
VPackObjectBuilder ob(&builder);
builder.add("term", VPackValue(term));
builder.add("success", VPackValue(success));
}
AppendEntriesResult(log_term_type term, bool success)
: term(term), success(success) {}
};
struct AppendEntriesRequest {
log_term_type term;
log_index_type prevLogIndex;
log_term_type prevLogTerm;
log_index_type commitIndex;
std::unique_ptr<LogIterator> entries;
AppendEntriesRequest(log_term_type term, log_index_type prevLogIndex,
log_term_type prevLogTerm, log_index_type commitIndex,
std::unique_ptr<LogIterator> entries)
: term(term),
prevLogIndex(prevLogIndex),
prevLogTerm(prevLogTerm),
commitIndex(commitIndex),
entries(std::move(entries)) {}
void intoBuilder(VPackBuilder& builder) const {
VPackObjectBuilder ob(&builder);
builder.add("term", VPackValue(term));
builder.add("prevLogIndex", VPackValue(prevLogIndex));
builder.add("prevLogTerm", VPackValue(prevLogTerm));
builder.add("commitIndex", VPackValue(commitIndex));
{
VPackArrayBuilder ab(&builder, "entries");
while (auto e = entries->next()) {
e->get().intoBuilder(builder);
}
}
}
explicit AppendEntriesRequest(VPackSlice slice) {
term = slice.get("term").getNumber<log_index_type>();
prevLogIndex = slice.get("prevLogIndex").getNumber<log_index_type>();
prevLogTerm = slice.get("prevLogTerm").getNumber<log_index_type>();
commitIndex = slice.get("commitIndex").getNumber<log_index_type>();
using container = std::vector<immer::box<LogEntry>>;
auto entriesSlice = slice.get("entries");
container v;
v.reserve(entriesSlice.length());
for (auto const& e : VPackArrayIterator(slice.get("entries"))) {
v.emplace_back(e);
}
struct Iterator final : LogIterator {
auto next() -> std::optional<immer::box<LogEntry>> override {
if (iter == v.end()) {
return std::nullopt;
} else {
return *(iter++);
}
}
explicit Iterator(container vp) : v(std::move(vp)), iter(v.begin()) {}
container v;
container::iterator iter;
};
entries = std::make_unique<Iterator>(std::move(v));
}
};
struct ReplicatedLogReplicationAPI {
virtual ~ReplicatedLogReplicationAPI() = default;
virtual auto appendEntries(AppendEntriesRequest const&) -> AppendEntriesResult = 0;
};
struct ParticipantAPI {
virtual ~ParticipantAPI() = default;
virtual auto appendEntries(AppendEntriesRequest const&)
-> futures::future<AppendEntriesResult> = 0;
};
struct Participant {
explicit Participant(std::shared_ptr<ParticipantAPI> api)
: api(std::move(api)) {}
std::shared_ptr<ParticipantAPI> api;
log_index_type index = 0;
bool replicationOngoing = false;
};
struct ReplicatedLogReplicatorAPI {
virtual ~ReplicatedLogReplicatorAPI() = default;
virtual auto getCommitIndex() -> log_index_type = 0;
virtual void updateCommitIndex(log_index_type) = 0;
virtual auto getLogEntry(log_index_type) -> LogEntry = 0;
virtual auto getLogFrom(log_index_type)
-> std::pair<immer::box<LogEntry>, std::unique_ptr<LogIterator>> = 0;
};
struct ReplicatedLog final : ReplicatedLogTrxAPI, ReplicatedLogLeadershipAPI, ReplicatedLogReplicationAPI {
explicit ReplicatedLog(std::vector<std::shared_ptr<ParticipantAPI>> const& parts,
std::shared_ptr<sched::scheduler> scheduler)
: scheduler(std::move(scheduler)) {
std::transform(parts.begin(), parts.end(), std::back_inserter(participants),
[](std::shared_ptr<ParticipantAPI> const& api) {
return Participant{api};
});
}
auto insert(VPackBufferUInt8 payload) -> std::pair<log_index_type, log_term_type> override {
std::unique_lock guard(_mutex);
if (!is_leader) {
throw std::logic_error("not a leader");
}
std::cout << "Inserting " << currentIndex + 1 << " " << currentTerm << " "
<< VPackSlice(payload.data()).toJson() << std::endl;
_log = std::move(_log).push_back({currentIndex + 1, currentTerm, std::move(payload)});
currentIndex += 1;
triggerReplication();
return std::make_pair(currentIndex, currentTerm);
}
auto waitFor(log_index_type index, log_term_type term)
-> futures::future<WaitResult> override {
std::unique_lock guard(_mutex);
if (!is_leader) {
throw std::logic_error("not a leader");
}
std::cout << "waiting for " << index << " " << term << std::endl;
return std::invoke([&] {
if (index <= commitIndex) {
std::cout << "wait condition already satsified" << std::endl;
return futures::make_fulfilled_promise<WaitResult>(
std::in_place, _log[index - 1]->term);
}
auto&& [f, p] = futures::make_promise<WaitResult>();
notifications.emplace(index, term, std::move(p));
return std::move(f);
})
.then_value([term, index](WaitResult&& result) {
std::cout << "waiting completed " << index << " " << term
<< " with actual term " << result.term << std::endl;
if (result.term != term) {
throw std::logic_error("index has wrong term");
}
return result;
});
}
void assumeLeadership(log_term_type term) override {
std::unique_lock guard(_mutex);
std::cout << "Assuming leadership for term " << term << std::endl;
is_leader = true;
currentTerm = term;
}
auto appendEntries(const AppendEntriesRequest& req) -> AppendEntriesResult override {
std::unique_lock guard(_mutex);
std::cout << "received append entries with term " << req.term << " prevLog = ("
<< req.prevLogIndex << " " << req.prevLogTerm << std::endl;
if (!is_leader && req.term >= currentTerm) {
// update my term
currentTerm = req.term;
commitIndex = req.commitIndex;
// check log end
if (req.prevLogIndex < _log.size()) {
auto& entry = _log.operator[](req.prevLogIndex);
if (entry->term == req.prevLogTerm) {
// append to the log
auto t = _log.transient();
t.take(req.prevLogIndex);
while (auto opt = req.entries->next()) {
t.push_back(*opt);
}
_log = t.persistent();
std::cout << "appended entries" << std::endl;
return {currentTerm, true};
} else {
std::cout << "term of prevlog index does not match " << entry->term << " != " << req.prevLogTerm << std::endl;
}
} else {
std::cout << "prevlog index is after my log head" << _log.size() << std::endl;
}
} else {
std::cout << "my term is higher " << currentTerm << std::endl;
}
std::cout << "returning false to replication " << std::endl;
return {currentTerm, false};
}
private:
log_index_type quorumIndex(std::size_t quorum_size) const {
quorum_size = std::max(1ul, std::min(quorum_size, participants.size()));
std::vector<log_index_type> indexes;
std::transform(participants.begin(), participants.end(), std::back_inserter(indexes),
[](Participant const& p) { return p.index; });
std::nth_element(indexes.begin(), indexes.begin() + (quorum_size - 1),
indexes.end(), std::greater<log_index_type>{});
return indexes.at(quorum_size - 1);
}
void updateCommitIndex(log_index_type index) {
assert(index > commitIndex);
currentIndex = index;
while (!notifications.empty()) {
if (notifications.find_max().index <= index) {
auto&& top = notifications.extract_max();
std::move(top.promise).fulfill(std::in_place, _log[index - 1]->term);
} else {
break;
}
}
}
void checkCommitIndex() {
auto index = quorumIndex(participants.size() / 2 + 1);
if (index > commitIndex) {
std::cout << "increment commit index to " << index << std::endl;
updateCommitIndex(index);
}
}
void triggerReplication() noexcept {
for (auto&& p : participants) {
if (!p.replicationOngoing && p.index < currentIndex) {
startReplication(p);
}
}
}
futures::future<void> sendReplication(Participant& p) {
return futures::capture_into_future([&] {
std::unique_lock guard(_mutex);
auto const& prevLog = _log[p.index];
auto log = _log.drop(p.index);
auto req = AppendEntriesRequest(currentTerm, prevLog->index,
prevLog->term, commitIndex,
std::make_unique<Iterator>(log));
return p.api->appendEntries(req).then_value([log](rlog::AppendEntriesResult&& result) {
return std::make_tuple(result, log.size());
});
})
.then_value([this, &p](std::tuple<rlog::AppendEntriesResult, std::size_t>&& tup) {
std::unique_lock guard(_mutex);
auto&& [result, length] = tup;
if (result.success) {
p.index += length;
std::cout << "Participant is now at " << p.index << std::endl;
checkCommitIndex();
if (p.index < currentIndex) {
return sendReplication(p);
}
} else if (p.index > 0) {
p.index -= 1;
} else {
throw std::logic_error("follower does not accept any entries");
}
return futures::make_fulfilled_promise();
});
}
void startReplication(Participant& p) {
p.replicationOngoing = true;
scheduler
->async([&]() -> futures::future<void> { return sendReplication(p); })
.finally([&](expect::expected<void>&& e) noexcept {
std::unique_lock guard(_mutex);
p.replicationOngoing = false;
std::cout << "stopping replication for one participant" << std::endl;
try {
e.rethrow_error();
} catch (std::exception const& ex) {
std::cout << "replication request ended with exception: " << ex.what()
<< std::endl;
} catch (...) {
std::cout << "replication request ended with unknown exception" << std::endl;
}
});
}
struct NotifyRequest {
NotifyRequest(log_index_type index_, log_term_type term_,
futures::promise<ReplicatedLogTrxAPI::WaitResult> promise_)
: index(index_), term(term_), promise(std::move(promise_)) {}
log_index_type index;
log_term_type term;
futures::promise<ReplicatedLogTrxAPI::WaitResult> promise;
bool operator<(NotifyRequest const& other) const {
return other.index < index;
}
};
static_assert(std::is_nothrow_move_assignable_v<NotifyRequest>);
struct Iterator : LogIterator {
using vector_type = immer::flex_vector<immer::box<LogEntry>>;
explicit Iterator(vector_type v) : log(std::move(v)), iter(log.begin()) {}
auto next() -> std::optional<immer::box<LogEntry>> override {
if (iter == log.end()) {
return std::nullopt;
} else {
return *(iter++);
}
}
private:
vector_type log;
vector_type::iterator iter;
};
std::mutex mutable _mutex;
foobar::priority_queue<NotifyRequest> notifications;
immer::flex_vector<immer::box<LogEntry>> _log;
std::vector<Participant> participants;
log_index_type currentIndex = 0;
log_term_type currentTerm = 0;
log_index_type commitIndex = 0;
bool is_leader = false;
std::shared_ptr<sched::scheduler> scheduler;
};
} // namespace rlog
#endif // PROTOTYPE_REPLICATED_LOG_H