-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathcalc_server.cxx
265 lines (222 loc) · 8.59 KB
/
calc_server.cxx
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
/************************************************************************
Copyright 2017-2019 eBay Inc.
Author/Developer(s): Jung-Sang Ahn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#include "calc_state_machine.hxx"
#include "in_memory_state_mgr.hxx"
#include "logger_wrapper.hxx"
#include "nuraft.hxx"
#include "test_common.h"
#include <iostream>
#include <sstream>
#include <stdio.h>
using namespace nuraft;
namespace calc_server {
static raft_params::return_method_type CALL_TYPE
= raft_params::blocking;
// = raft_params::async_handler;
static bool ASYNC_SNAPSHOT_CREATION = false;
#include "example_common.hxx"
calc_state_machine* get_sm() {
return static_cast<calc_state_machine*>( stuff.sm_.get() );
}
void handle_result(ptr<TestSuite::Timer> timer,
raft_result& result,
ptr<std::exception>& err)
{
if (result.get_result_code() != cmd_result_code::OK) {
// Something went wrong.
// This means committing this log failed,
// but the log itself is still in the log store.
std::cout << "failed: " << result.get_result_code() << ", "
<< TestSuite::usToString( timer->getTimeUs() )
<< std::endl;
return;
}
ptr<buffer> buf = result.get();
uint64_t ret_value = buf->get_ulong();
std::cout << "succeeded, "
<< TestSuite::usToString( timer->getTimeUs() )
<< ", return value: "
<< ret_value
<< ", state machine value: "
<< get_sm()->get_current_value()
<< std::endl;
}
void append_log(const std::string& cmd,
const std::vector<std::string>& tokens)
{
char cmd_char = cmd[0];
int operand = atoi( tokens[0].substr(1).c_str() );
calc_state_machine::op_type op = calc_state_machine::ADD;
switch (cmd_char) {
case '+': op = calc_state_machine::ADD; break;
case '-': op = calc_state_machine::SUB; break;
case '*': op = calc_state_machine::MUL; break;
case '/':
op = calc_state_machine::DIV;
if (!operand) {
std::cout << "cannot divide by zero" << std::endl;
return;
}
break;
default: op = calc_state_machine::SET; break;
};
// Serialize and generate Raft log to append.
ptr<buffer> new_log = calc_state_machine::enc_log( {op, operand} );
// To measure the elapsed time.
ptr<TestSuite::Timer> timer = cs_new<TestSuite::Timer>();
// Do append.
ptr<raft_result> ret = stuff.raft_instance_->append_entries( {new_log} );
if (!ret->get_accepted()) {
// Log append rejected, usually because this node is not a leader.
std::cout << "failed to replicate: "
<< ret->get_result_code() << ", "
<< TestSuite::usToString( timer->getTimeUs() )
<< std::endl;
return;
}
// Log append accepted, but that doesn't mean the log is committed.
// Commit result can be obtained below.
if (CALL_TYPE == raft_params::blocking) {
// Blocking mode:
// `append_entries` returns after getting a consensus,
// so that `ret` already has the result from state machine.
ptr<std::exception> err(nullptr);
handle_result(timer, *ret, err);
} else if (CALL_TYPE == raft_params::async_handler) {
// Async mode:
// `append_entries` returns immediately.
// `handle_result` will be invoked asynchronously,
// after getting a consensus.
ret->when_ready( std::bind( handle_result,
timer,
std::placeholders::_1,
std::placeholders::_2 ) );
} else {
assert(0);
}
}
void print_status(const std::string& cmd,
const std::vector<std::string>& tokens)
{
ptr<log_store> ls = stuff.smgr_->load_log_store();
std::cout
<< "my server id: " << stuff.server_id_ << std::endl
<< "leader id: " << stuff.raft_instance_->get_leader() << std::endl
<< "Raft log range: ";
if (ls->start_index() >= ls->next_slot()) {
// Start index can be the same as next slot when the log store is empty.
std::cout << "(empty)" << std::endl;
} else {
std::cout << ls->start_index()
<< " - " << (ls->next_slot() - 1) << std::endl;
}
std::cout
<< "last committed index: "
<< stuff.raft_instance_->get_committed_log_idx() << std::endl
<< "current term: "
<< stuff.raft_instance_->get_term() << std::endl
<< "last snapshot log index: "
<< (stuff.sm_->last_snapshot()
? stuff.sm_->last_snapshot()->get_last_log_idx() : 0) << std::endl
<< "last snapshot log term: "
<< (stuff.sm_->last_snapshot()
? stuff.sm_->last_snapshot()->get_last_log_term() : 0) << std::endl
<< "state machine value: "
<< get_sm()->get_current_value() << std::endl;
}
void help(const std::string& cmd,
const std::vector<std::string>& tokens)
{
std::cout
<< "modify value: <+|-|*|/><operand>\n"
<< " +: add <operand> to state machine's value.\n"
<< " -: subtract <operand> from state machine's value.\n"
<< " *: multiple state machine'value by <operand>.\n"
<< " /: divide state machine's value by <operand>.\n"
<< " e.g.) +123\n"
<< "\n"
<< "add server: add <server id> <address>:<port>\n"
<< " e.g.) add 2 127.0.0.1:20000\n"
<< "\n"
<< "get current server status: st (or stat)\n"
<< "\n"
<< "get the list of members: ls (or list)\n"
<< "\n";
}
bool do_cmd(const std::vector<std::string>& tokens) {
if (!tokens.size()) return true;
const std::string& cmd = tokens[0];
if (cmd == "q" || cmd == "exit") {
stuff.launcher_.shutdown(5);
stuff.reset();
return false;
} else if ( cmd[0] == '+' ||
cmd[0] == '-' ||
cmd[0] == '*' ||
cmd[0] == '/' ) {
// e.g.) +1
append_log(cmd, tokens);
} else if ( cmd == "add" ) {
// e.g.) add 2 localhost:12345
add_server(cmd, tokens);
} else if ( cmd == "st" || cmd == "stat" ) {
print_status(cmd, tokens);
} else if ( cmd == "ls" || cmd == "list" ) {
server_list(cmd, tokens);
} else if ( cmd == "h" || cmd == "help" ) {
help(cmd, tokens);
}
return true;
}
void check_additional_flags(int argc, char** argv) {
for (int ii = 1; ii < argc; ++ii) {
if (strcmp(argv[ii], "--async-handler") == 0) {
CALL_TYPE = raft_params::async_handler;
} else if (strcmp(argv[ii], "--async-snapshot-creation") == 0) {
ASYNC_SNAPSHOT_CREATION = true;
}
}
}
void calc_usage(int argc, char** argv) {
std::stringstream ss;
ss << "Usage: \n";
ss << " " << argv[0] << " <server id> <IP address and port> [<options>]";
ss << std::endl << std::endl;
ss << " options:" << std::endl;
ss << " --async-handler: use async type handler." << std::endl;
ss << " --async-snapshot-creation: create snapshots asynchronously."
<< std::endl << std::endl;
std::cout << ss.str();
exit(0);
}
}; // namespace calc_server;
using namespace calc_server;
int main(int argc, char** argv) {
if (argc < 3) calc_usage(argc, argv);
set_server_info(argc, argv);
check_additional_flags(argc, argv);
std::cout << " -- Replicated Calculator with Raft --" << std::endl;
std::cout << " Version 0.1.0" << std::endl;
std::cout << " Server ID: " << stuff.server_id_ << std::endl;
std::cout << " Endpoint: " << stuff.endpoint_ << std::endl;
if (CALL_TYPE == raft_params::async_handler) {
std::cout << " async handler is enabled" << std::endl;
}
if (ASYNC_SNAPSHOT_CREATION) {
std::cout << " snapshots are created asynchronously" << std::endl;
}
init_raft( cs_new<calc_state_machine>(ASYNC_SNAPSHOT_CREATION) );
loop();
return 0;
}