-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmr_state_machine.cpp
More file actions
358 lines (291 loc) · 11.4 KB
/
Copy pathmr_state_machine.cpp
File metadata and controls
358 lines (291 loc) · 11.4 KB
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
#pragma once
#include "nuraft.hxx"
#include "MapReduce.h"
#include "KeyValueStore.h"
#include <atomic>
#include <cassert>
#include <iostream>
#include <mutex>
#include <memory>
#include <sstream>
#include <string.h>
using namespace nuraft;
namespace mapreduce_server {
class mr_state_machine : public state_machine {
public:
mr_state_machine(bool async_snapshot = false)
: kv_store_(), last_committed_idx_(0), async_snapshot_(async_snapshot) {}
~mr_state_machine() {}
enum op_type : int {
INSERT_VALUE = 0x0,
DELETE_VALUE = 0x1,
DELETE_KEY = 0x2,
MAP_REDUCE = 0x3
};
struct op_payload {
op_type type_;
std::string key_;
int value_; // For INSERT_KEY
std::string map_op_; // For MAP_REDUCE
std::string reduce_op_; // For MAP_REDUCE
std::vector<std::string> keys_; // For MAP_REDUCE
};
static ptr<buffer> enc_log(const op_payload& payload) {
// Encode from payload to Raft log.
ptr<buffer> ret = buffer::alloc(sizeof(op_payload));
buffer_serializer bs(ret);
// WARNING: We don't consider endian-safety in this example.
bs.put_raw(&payload, sizeof(op_payload));
return ret;
}
static void dec_log(buffer& log, op_payload& payload_out) {
// Decode from Raft log to payload pair.
assert(log.size() == sizeof(op_payload));
buffer_serializer bs(log);
memcpy(&payload_out, bs.get_raw(log.size()), sizeof(op_payload));
}
ptr<buffer> pre_commit(const ulong log_idx, buffer& data) {
// Nothing to do with pre-commit in this example.
return nullptr;
}
ptr<buffer> commit(const ulong log_idx, buffer& data) {
op_payload payload;
dec_log(data, payload);
std::unique_ptr<MapReduce> mr = nullptr;
ptr<buffer> ret;
bool has_map_reduce_results = false;
std::string results_str;
switch (payload.type_) {
case INSERT_VALUE:
kv_store_.insert(payload.key_, payload.value_);
break;
case DELETE_VALUE:
kv_store_.removeValue(payload.key_, payload.value_);
break;
case DELETE_KEY:
kv_store_.removeKey(payload.key_);
break;
case MAP_REDUCE: {
has_map_reduce_results = true;
mr = std::unique_ptr<MapReduce>(new MapReduce(kv_store_));
auto mapReduceResults = mr->performMapReduce(payload.map_op_, payload.reduce_op_, payload.keys_);
add_map_reduce_result(log_idx, mapReduceResults);
break;
}
default:
// Handle unknown operation
break;
}
ret = buffer::alloc( sizeof(log_idx) + sizeof(bool) );
buffer_serializer bs(ret);
bs.put_u64(log_idx);
bs.put_u8(static_cast<uint8_t>(has_map_reduce_results)); // Serialize flag indicating presence of results
last_committed_idx_ = log_idx;
return ret;
}
void commit_config(const ulong log_idx, ptr<cluster_config>& new_conf) {
// Nothing to do with configuration change. Just update committed index.
last_committed_idx_ = log_idx;
}
void rollback(const ulong log_idx, buffer& data) {
// Nothing to do with rollback,
// as this example doesn't do anything on pre-commit.
}
int read_logical_snp_obj(snapshot& s,
void*& user_snp_ctx,
ulong obj_id,
ptr<buffer>& data_out,
bool& is_last_obj)
{
ptr<snapshot_ctx> ctx = nullptr;
{
std::lock_guard<std::mutex> ll(snapshots_lock_);
auto entry = snapshots_.find(s.get_last_log_idx());
if (entry == snapshots_.end()) {
data_out = nullptr;
is_last_obj = true;
return 0;
}
ctx = entry->second;
}
// Serialize the whole KeyValueStore into a string
// (Consider splitting it if too large)
std::string kv_store_serialized = serialize_kv_store(ctx->kv_store_); // Implement this method
if (obj_id == 0) {
// First object, return the serialized key-value store.
data_out = buffer::alloc(kv_store_serialized.size());
buffer_serializer bs(data_out);
bs.put_raw(kv_store_serialized.data(), kv_store_serialized.size());
is_last_obj = true;
}
return 0;
}
void save_logical_snp_obj(snapshot& s,
ulong& obj_id,
buffer& data,
bool is_first_obj,
bool is_last_obj)
{
if (obj_id == 0) {
// Object ID == 0: contains the serialized key-value store.
buffer_serializer bs(data);
std::string kv_store_serialized = bs.get_str(); // Deserialize the string
std::lock_guard<std::mutex> ll(snapshots_lock_);
auto entry = snapshots_.find(s.get_last_log_idx());
entry->second->kv_store_ = deserialize_kv_store(kv_store_serialized); // Implement this method
}
}
bool apply_snapshot(snapshot& s) {
std::lock_guard<std::mutex> ll(snapshots_lock_);
auto entry = snapshots_.find(s.get_last_log_idx());
if (entry == snapshots_.end()) return false;
ptr<snapshot_ctx> ctx = entry->second;
kv_store_ = ctx->kv_store_; // Restore the key-value store from the snapshot context.
return true;
}
void free_user_snp_ctx(void*& user_snp_ctx) {
// In this example, `read_logical_snp_obj` doesn't create
// `user_snp_ctx`. Nothing to do in this function.
}
ptr<snapshot> last_snapshot() {
// Just return the latest snapshot.
std::lock_guard<std::mutex> ll(snapshots_lock_);
auto entry = snapshots_.rbegin();
if (entry == snapshots_.rend()) return nullptr;
ptr<snapshot_ctx> ctx = entry->second;
return ctx->snapshot_;
}
ulong last_commit_index() {
return last_committed_idx_;
}
void create_snapshot(snapshot& s,
async_result<bool>::handler_type& when_done)
{
if (!async_snapshot_) {
// Create a snapshot in a synchronous way (blocking the thread).
create_snapshot_sync(s, when_done);
} else {
// Create a snapshot in an asynchronous way (in a different thread).
create_snapshot_async(s, when_done);
}
}
KeyValueStore get_kv_store() const { return kv_store_; }
std::map<std::string, int> get_map_reduce_results(const ulong log_idx) {
std::lock_guard<std::mutex> ll(snapshots_lock_);
auto entry = map_reduce_results_.find(log_idx);
if (entry == map_reduce_results_.end()) {
return std::map<std::string, int>();
}
return entry->second;
}
private:
struct snapshot_ctx {
snapshot_ctx(ptr<snapshot>& s, const KeyValueStore& kv_store)
: snapshot_(s), kv_store_(kv_store) {}
ptr<snapshot> snapshot_;
KeyValueStore kv_store_;
};
std::string serialize_kv_store(const KeyValueStore& kv_store) {
std::stringstream ss;
for (const auto& kv : kv_store.getAll()) {
ss << kv.first << ":";
for (const auto& val : kv.second) {
ss << val << ",";
}
ss << ";"; // End of the key-value pair
}
return ss.str();
}
KeyValueStore deserialize_kv_store(std::string& data) {
KeyValueStore new_kv_store;
std::istringstream ss(data);
std::string key_value_pair;
while (std::getline(ss, key_value_pair, ';')) {
std::istringstream kv_stream(key_value_pair);
std::string key;
if (!std::getline(kv_stream, key, ':')) {
continue; // No key found, skip
}
std::vector<int> values;
std::string value_str;
while (std::getline(kv_stream, value_str, ',')) {
if (value_str.empty()) {
continue; // Skip empty strings
}
try {
int value = std::stoi(value_str);
values.push_back(value);
} catch (const std::invalid_argument& e) {
// Handle or log the error
continue;
}
}
// Store the key and values
new_kv_store.insertMany(key, values);
}
return new_kv_store;
}
void create_snapshot_internal(ptr<snapshot> ss) {
std::lock_guard<std::mutex> ll(snapshots_lock_);
ptr<snapshot_ctx> ctx = cs_new<snapshot_ctx>(ss, kv_store_);
snapshots_[ss->get_last_log_idx()] = ctx;
// Maintain last 3 snapshots only.
const int MAX_SNAPSHOTS = 3;
int num = snapshots_.size();
auto entry = snapshots_.begin();
for (int ii = 0; ii < num - MAX_SNAPSHOTS; ++ii) {
if (entry == snapshots_.end()) break;
entry = snapshots_.erase(entry);
}
}
void create_snapshot_sync(snapshot& s,
async_result<bool>::handler_type& when_done)
{
// Clone snapshot from `s`.
ptr<buffer> snp_buf = s.serialize();
ptr<snapshot> ss = snapshot::deserialize(*snp_buf);
create_snapshot_internal(ss);
ptr<std::exception> except(nullptr);
bool ret = true;
when_done(ret, except);
std::cout << "snapshot (" << ss->get_last_log_term() << ", "
<< ss->get_last_log_idx() << ") has been created synchronously"
<< std::endl;
}
void create_snapshot_async(snapshot& s,
async_result<bool>::handler_type& when_done)
{
// Clone snapshot from `s`.
ptr<buffer> snp_buf = s.serialize();
ptr<snapshot> ss = snapshot::deserialize(*snp_buf);
// Note that this is a very naive and inefficient example
// that creates a new thread for each snapshot creation.
std::thread t_hdl([this, ss, when_done]{
create_snapshot_internal(ss);
ptr<std::exception> except(nullptr);
bool ret = true;
when_done(ret, except);
std::cout << "snapshot (" << ss->get_last_log_term() << ", "
<< ss->get_last_log_idx() << ") has been created asynchronously"
<< std::endl;
});
t_hdl.detach();
}
void add_map_reduce_result(const ulong log_idx, std::map<std::string, int>& results) {
std::lock_guard<std::mutex> ll(snapshots_lock_);
map_reduce_results_[log_idx] = results;
}
// Key-value store.
KeyValueStore kv_store_;
// MapReduce results (log_index : results, where result -> key : value)
std::map< ulong, std::map<std::string, int> > map_reduce_results_;
// Last committed Raft log number.
std::atomic<uint64_t> last_committed_idx_;
// Keeps the last 3 snapshots, by their Raft log numbers.
std::map< uint64_t, ptr<snapshot_ctx> > snapshots_;
// Mutex for `snapshots_`.
std::mutex snapshots_lock_;
// If `true`, snapshot will be created asynchronously.
bool async_snapshot_;
};
}; // namespace mapreduce_server