forked from baidu/braft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
153 lines (140 loc) · 5.73 KB
/
client.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
// Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved
//
// 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
//
// http://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 <gflags/gflags.h>
#include <bthread/bthread.h>
#include <brpc/channel.h>
#include <brpc/controller.h>
#include <braft/raft.h>
#include <braft/util.h>
#include <braft/route_table.h>
#include "counter.pb.h"
DEFINE_bool(log_each_request, false, "Print log for each request");
DEFINE_bool(use_bthread, false, "Use bthread to send requests");
DEFINE_int32(add_percentage, 100, "Percentage of fetch_add");
DEFINE_int64(added_by, 1, "Num added to each peer");
DEFINE_int32(thread_num, 1, "Number of threads sending requests");
DEFINE_int32(timeout_ms, 1000, "Timeout for each request");
DEFINE_string(conf, "", "Configuration of the raft group");
DEFINE_string(group, "Counter", "Id of the replication group");
bvar::LatencyRecorder g_latency_recorder("counter_client");
static void* sender(void* arg) {
while (!brpc::IsAskedToQuit()) {
braft::PeerId leader;
// Select leader of the target group from RouteTable
if (braft::rtb::select_leader(FLAGS_group, &leader) != 0) {
// Leader is unknown in RouteTable. Ask RouteTable to refresh leader
// by sending RPCs.
butil::Status st = braft::rtb::refresh_leader(
FLAGS_group, FLAGS_timeout_ms);
if (!st.ok()) {
// Not sure about the leader, sleep for a while and the ask again.
LOG(WARNING) << "Fail to refresh_leader : " << st;
bthread_usleep(FLAGS_timeout_ms * 1000L);
}
continue;
}
// Now we known who is the leader, construct Stub and then sending
// rpc
brpc::Channel channel;
if (channel.Init(leader.addr, NULL) != 0) {
LOG(ERROR) << "Fail to init channel to " << leader;
bthread_usleep(FLAGS_timeout_ms * 1000L);
continue;
}
example::CounterService_Stub stub(&channel);
brpc::Controller cntl;
cntl.set_timeout_ms(FLAGS_timeout_ms);
// Randomly select which request we want send;
example::CounterResponse response;
if (butil::fast_rand_less_than(100) < (size_t)FLAGS_add_percentage) {
example::FetchAddRequest request;
request.set_value(FLAGS_added_by);
stub.fetch_add(&cntl, &request, &response, NULL);
} else {
example::GetRequest request;
stub.get(&cntl, &request, &response, NULL);
}
if (cntl.Failed()) {
LOG(WARNING) << "Fail to send request to " << leader
<< " : " << cntl.ErrorText();
// Clear leadership since this RPC failed.
braft::rtb::update_leader(FLAGS_group, braft::PeerId());
bthread_usleep(FLAGS_timeout_ms * 1000L);
continue;
}
if (!response.success()) {
LOG(WARNING) << "Fail to send request to " << leader
<< ", redirecting to "
<< (response.has_redirect()
? response.redirect() : "nowhere");
// Update route table since we have redirect information
braft::rtb::update_leader(FLAGS_group, response.redirect());
continue;
}
g_latency_recorder << cntl.latency_us();
if (FLAGS_log_each_request) {
LOG(INFO) << "Received response from " << leader
<< " value=" << response.value()
<< " latency=" << cntl.latency_us();
bthread_usleep(1000L * 1000L);
}
}
return NULL;
}
int main(int argc, char* argv[]) {
GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
butil::AtExitManager exit_manager;
// Register configuration of target group to RouteTable
if (braft::rtb::update_configuration(FLAGS_group, FLAGS_conf) != 0) {
LOG(ERROR) << "Fail to register configuration " << FLAGS_conf
<< " of group " << FLAGS_group;
return -1;
}
std::vector<bthread_t> tids;
std::vector<pthread_t> pids;
if (!FLAGS_use_bthread) {
pids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (pthread_create(&pids[i], NULL, sender, NULL) != 0) {
LOG(ERROR) << "Fail to create pthread";
return -1;
}
}
} else {
tids.resize(FLAGS_thread_num);
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (bthread_start_background(&tids[i], NULL, sender, NULL) != 0) {
LOG(ERROR) << "Fail to create bthread";
return -1;
}
}
}
while (!brpc::IsAskedToQuit()) {
sleep(1);
LOG_IF(INFO, !FLAGS_log_each_request)
<< "Sending Request to " << FLAGS_group
<< " (" << FLAGS_conf << ')'
<< " at qps=" << g_latency_recorder.qps(1)
<< " latency=" << g_latency_recorder.latency(1);
}
LOG(INFO) << "Counter client is going to quit";
for (int i = 0; i < FLAGS_thread_num; ++i) {
if (!FLAGS_use_bthread) {
pthread_join(pids[i], NULL);
} else {
bthread_join(tids[i], NULL);
}
}
return 0;
}