forked from eBay/NuRaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_server.cxx
193 lines (155 loc) · 5.82 KB
/
echo_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
/************************************************************************
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 "echo_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 echo_server {
static const raft_params::return_method_type CALL_TYPE
= raft_params::blocking;
// = raft_params::async_handler;
#include "example_common.hxx"
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;
}
std::cout << "succeeded, "
<< TestSuite::usToString( timer->getTimeUs() )
<< std::endl;
}
void append_log(const std::string& cmd,
const std::vector<std::string>& tokens)
{
if (tokens.size() < 2) {
std::cout << "too few arguments" << std::endl;
return;
}
std::string cascaded_str;
for (size_t ii=1; ii<tokens.size(); ++ii) {
cascaded_str += tokens[ii] + " ";
}
// Create a new log which will contain
// 4-byte length and string data.
ptr<buffer> new_log = buffer::alloc(sizeof(int) + cascaded_str.size());
buffer_serializer bs(new_log);
bs.put_str(cascaded_str);
// 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: "
<< ls->start_index()
<< " - " << (ls->next_slot() - 1) << std::endl
<< "last committed index: "
<< stuff.raft_instance_->get_committed_log_idx() << std::endl;
}
void help(const std::string& cmd,
const std::vector<std::string>& tokens)
{
std::cout
<< "echo message: msg <operand>\n"
<< " e.g.) msg hello world!\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);
return false;
} else if ( cmd == "msg" ) {
// e.g.) msg hello world
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;
}
}; // namespace echo_server;
using namespace echo_server;
int main(int argc, char** argv) {
if (argc < 3) usage(argc, argv);
set_server_info(argc, argv);
std::cout << " -- Echo Server 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;
init_raft( cs_new<echo_state_machine>() );
loop();
return 0;
}