-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbraft_compat.cpp
414 lines (334 loc) · 12.7 KB
/
braft_compat.cpp
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
/* -*- c-basic-offset: 2; -*- */
#include <butil/endpoint.h>
#include <brpc/controller.h> // brpc::Controller
#include <brpc/server.h> // brpc::Server
#include <braft/raft.h> // braft::Node braft::StateMachine
#include <braft/storage.h> // braft::SnapshotWriter
#include <braft/util.h> // braft::AsyncClosureGuard
#include <braft/protobuf_file.h> // braft::ProtoBufFile
#include <braft/cli.h>
namespace bknr {
using std::string;
class BknrStateMachine;
typedef void transaction_callback(BknrStateMachine* fsm, int callback_handle, int success,
const char* status_message);
typedef int LispCallback;
class BknrClosure;
typedef void InvokeClosure(BknrClosure* closure,
int ok,
const char* err);
typedef void DeleteClosure(BknrClosure* closure);
class BknrClosure : public braft::Closure {
public:
BknrClosure(InvokeClosure* invoke_closure,
DeleteClosure* delete_closure)
: _invoke_closure(invoke_closure),
_delete_closure(delete_closure)
{}
~BknrClosure() {
(*_delete_closure)(this);
}
void Run();
public:
InvokeClosure* _invoke_closure;
DeleteClosure* _delete_closure;
};
typedef int OnApplyCallback(BknrStateMachine* fsm,
butil::IOBuf* data,
int datalen,
/* we need this to respond with the result */
BknrClosure *closure);
typedef void OnSnapshotSave(BknrStateMachine* fsm,
braft::SnapshotWriter* writer,
braft::Closure* done);
typedef int OnSnapshotLoad(BknrStateMachine* fsm,
braft::SnapshotReader* reader);
typedef void OnLeaderStart(BknrStateMachine* fsm);
typedef void OnLeaderStop(BknrStateMachine* fsm);
class BknrStateMachine : public braft::StateMachine {
OnApplyCallback* _on_apply_callback;
OnSnapshotSave* _on_snapshot_save;
OnSnapshotLoad* _on_snapshot_load;
OnLeaderStart* _on_leader_start;
OnLeaderStop* _on_leader_stop;
public:
BknrStateMachine (
OnApplyCallback* on_apply_callback,
OnSnapshotSave* on_snapshot_save,
OnSnapshotLoad* on_snapshot_load,
OnLeaderStart* on_leader_start,
OnLeaderStop* on_leader_stop
) : _on_apply_callback(on_apply_callback),
_on_snapshot_save(on_snapshot_save),
_on_snapshot_load(on_snapshot_load),
_on_leader_start(on_leader_start),
_on_leader_stop(on_leader_stop),
_node(NULL) {
}
void on_apply(::braft::Iterator& iter) {
for (; iter.valid(); iter.next()) {
butil::IOBuf data = iter.data();
int len = data.length();
//LOG(INFO) << "Calling OnApplyCallback";
BknrClosure* c = NULL;
if (iter.done()) {
c = dynamic_cast<BknrClosure*>(iter.done());
}
(*_on_apply_callback)(this, &data, len, c);
}
}
int start(string ip,
int port, string config,
int election_timeout_ms,
int snapshot_interval,
string data_path,
string group) {
butil::ip_t ipt;
butil::str2ip(ip.c_str(), &ipt);
butil::EndPoint addr(ipt, port);
braft::NodeOptions node_options;
if (node_options.initial_conf.parse_from(config) != 0) {
LOG(ERROR) << "Fail to parse configuration `" << config << '\'';
return -7;
}
node_options.election_timeout_ms = election_timeout_ms;
node_options.fsm = this;
node_options.node_owns_fsm = false;
node_options.snapshot_interval_s = snapshot_interval;
std::string prefix = "local://" + data_path;
node_options.log_uri = prefix + "/log";
node_options.raft_meta_uri = prefix + "/raft_meta";
node_options.snapshot_uri = prefix + "/snapshot";
node_options.disable_cli = false;
braft::Node* node = new braft::Node(group, braft::PeerId(addr));
if (node->init(node_options) != 0) {
LOG(ERROR) << "Fail to init raft node";
delete node;
return -6;
}
_node = node;
return 0;
}
void shutdown() {
if (_node) {
_node->shutdown(NULL);
}
}
void join() {
if (_node) {
_node->join();
}
}
void on_snapshot_save(braft::SnapshotWriter* writer,
braft::Closure* done) {
(*_on_snapshot_save)(this, writer, done);
}
int on_snapshot_load(braft::SnapshotReader* reader) {
return (*_on_snapshot_load)(this, reader);
}
void on_leader_start(int64_t term) {
_leader_term.store(term, butil::memory_order_release);
LOG(INFO) << "Node becomes leader";
(*_on_leader_start)(this);
}
void on_leader_stop(const butil::Status& status) {
(*_on_leader_stop)(this);
_leader_term.store(-1, butil::memory_order_release);
LOG(INFO) << "Node stepped down : " << status;
}
void on_shutdown() {
LOG(INFO) << "This node is down";
}
void on_error(const ::braft::Error& e) {
LOG(ERROR) << "Met raft error " << e;
}
void on_configuration_committed(const ::braft::Configuration& conf) {
LOG(INFO) << "Configuration of this group is " << conf;
}
void on_stop_following(const ::braft::LeaderChangeContext& ctx) {
LOG(INFO) << "Node stops following " << ctx;
}
void on_start_following(const ::braft::LeaderChangeContext& ctx) {
LOG(INFO) << "Node start following " << ctx;
}
butil::atomic<int64_t> _leader_term;
void apply(const char* data, int data_len, BknrClosure* closure) {
// Serialize request to the replicated write-ahead-log so that all the
// peers in the group receive this request as well.
// Notice that _value can't be modified in this routine otherwise it
// will be inconsistent with others in this group.
// Serialize request to IOBuf
//const int64_t term = _leader_term.load(butil::memory_order_relaxed);
/*
This logic was copied from examples, but our logic for
switching servers is different.
if (term < 0) { return
redirect(response); } */
const int64_t term = _leader_term.load(butil::memory_order_relaxed);
if (term < 0) {
braft::AsyncClosureGuard closure_guard(closure);
LOG(ERROR) << "Attempting to to apply task when we're not a leader";
closure->status().set_error(1, "Attempting to apply task when we're not a leader");
return;
}
butil::IOBuf log;
log.append(data, data_len);
// Apply this log as a braft::Task
braft::Task task;
task.data = &log;
// This callback would be iovoked when the task actually excuted or
// fail
task.done = closure;
// ABA problem can be avoid if expected_term is set
task.expected_term = term;
// Now the task is applied to the group, waiting for the result.
return _node->apply(task);
}
public:
brpc::Server _server;
braft::Node* _node;
private:
};
void BknrClosure::Run() {
// If the status was okay, then we would've called the callback in
// on_apply (see the lisp code). But we need to propagate errors
// back too.
(*_invoke_closure)(this,
status().ok(),
status().error_cstr());
delete this;
}
extern "C" {
BknrStateMachine* make_bknr_state_machine(OnApplyCallback* on_apply_callback,
OnSnapshotSave* on_snapshot_save,
OnSnapshotLoad* on_snapshot_load,
OnLeaderStart* on_leader_start,
OnLeaderStop* on_leader_stop) {
return new BknrStateMachine(on_apply_callback,
on_snapshot_save,
on_snapshot_load,
on_leader_start,
on_leader_stop);
}
void destroy_bknr_state_machine(BknrStateMachine* fsm) {
delete fsm;
}
int start_bknr_state_machine(BknrStateMachine* fsm,
const char* ip,
int port, const char* config,
int election_timeout_ms,
int snapshot_interval,
const char* data_path,
const char* group) {
srand(time(NULL));
if (braft::add_service(&(fsm->_server), port) != 0) {
LOG(ERROR) << "Fail to add raft service";
return -1;
}
string ip_and_port = ip;
ip_and_port += ":";
ip_and_port += std::to_string(port);
// Comment from braft counter/server.cpp: (See T1292)
// It's recommended to start the [brpc?] server before Counter is started to avoid
// the case that it becomes the leader while the service is unreacheable by
// clients.
LOG(INFO) << "Starting brpc server on " << ip_and_port;
if (fsm->_server.Start(ip_and_port.c_str(), NULL) != 0) {
LOG(ERROR) << "Fail to start Server";
return -1;
}
int res = fsm->start(ip, port, config,
election_timeout_ms,
snapshot_interval,
data_path,
group);
if (res != 0) {
return res;
};
return 0;
}
void stop_bknr_state_machine(BknrStateMachine* fsm) {
fsm->shutdown();
fsm->join();
fsm->_server.Stop(0);
}
int bknr_is_leader(BknrStateMachine* fsm) {
return fsm->_node->is_leader();
}
void bknr_apply_transaction(BknrStateMachine* fsm, const char* data, int data_len, BknrClosure* closure) {
fsm->apply(data, data_len, closure);
}
void bknr_iobuf_copy_to(butil::IOBuf* buf, void* arr, int len) {
buf->copy_to(arr, len);
}
void bknr_closure_run(google::protobuf::Closure *closure) {
brpc::ClosureGuard guard(closure);
}
void bknr_closure_set_error(braft::Closure *closure, int error, const char* msg) {
closure->status().set_error(error, msg);
}
void bknr_set_log_level(int level) {
::logging::SetMinLogLevel(level);
}
BknrClosure* bknr_make_closure(InvokeClosure* invokeClosure, DeleteClosure* deleteClosure) {
return new BknrClosure(invokeClosure, deleteClosure);
}
void bknr_snapshot(BknrStateMachine* fsm, braft::Closure* done) {
fsm->_node->snapshot(done);
}
char* return_cstr(const std::string& path) {
char *res = (char*) malloc(path.length() + 10);
path.copy(res, path.length());
res[path.length()] = '\0';
return res;
}
char* bknr_snapshot_writer_get_path(braft::SnapshotWriter* snapshot_writer) {
return return_cstr(snapshot_writer->get_path());
}
int bknr_snapshot_writer_add_file(braft::SnapshotWriter* snapshot_writer, const char* file) {
LOG(ERROR) << "Adding snapshot file: " << file;
return snapshot_writer->add_file(file);
}
char* bknr_snapshot_reader_get_path(braft::SnapshotReader* snapshot_reader) {
return return_cstr(snapshot_reader->get_path());
}
void bknr_transfer_leader(BknrStateMachine* fsm) {
braft::NodeId nodeId = fsm->_node->node_id();
LOG(INFO) << "Transfering leader";
std::vector<braft::PeerId> peers;
fsm->_node->list_peers(&peers);
peers.push_back(nodeId.peer_id);
::braft::Configuration c(peers);
braft::PeerId p = peers[rand() % peers.size()];
::braft::cli::CliOptions cliOptions;
::braft::cli::transfer_leader(nodeId.group_id, c, p, cliOptions);
}
int bknr_get_term(BknrStateMachine* fsm) {
return fsm->_leader_term.load(butil::memory_order_relaxed);
}
char* bknr_leader_id(BknrStateMachine* fsm) {
return return_cstr(fsm->_node->leader_id().to_string());
}
char* bknr_list_peers(BknrStateMachine* fsm) {
string conf;
std::vector<braft::PeerId> peers;
fsm->_node->list_peers(&peers);
for (auto &peer : peers) {
conf += peer.to_string();
conf += ",";
}
return return_cstr(conf);
}
int bknr_is_active(BknrStateMachine* fsm) {
if (!fsm->_node) {
return 0;
}
braft::NodeStatus status;
fsm->_node->get_status(&status);
return braft::is_active_state(status.state);
}
void bknr_vote(BknrStateMachine* fsm, int election_timeout) {
fsm->_node->vote(election_timeout);
}
}
}