| 33 | bvar::LatencyRecorder g_latency_recorder("counter_client"); |
| 34 | |
| 35 | static void* sender(void* arg) { |
| 36 | while (!brpc::IsAskedToQuit()) { |
| 37 | braft::PeerId leader; |
| 38 | // Select leader of the target group from RouteTable |
| 39 | if (braft::rtb::select_leader(FLAGS_group, &leader) != 0) { |
| 40 | // Leader is unknown in RouteTable. Ask RouteTable to refresh leader |
| 41 | // by sending RPCs. |
| 42 | butil::Status st = braft::rtb::refresh_leader( |
| 43 | FLAGS_group, FLAGS_timeout_ms); |
| 44 | if (!st.ok()) { |
| 45 | // Not sure about the leader, sleep for a while and the ask again. |
| 46 | LOG(WARNING) << "Fail to refresh_leader : " << st; |
| 47 | bthread_usleep(FLAGS_timeout_ms * 1000L); |
| 48 | } |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | // Now we known who is the leader, construct Stub and then sending |
| 53 | // rpc |
| 54 | brpc::Channel channel; |
| 55 | if (channel.Init(leader.addr, NULL) != 0) { |
| 56 | LOG(ERROR) << "Fail to init channel to " << leader; |
| 57 | bthread_usleep(FLAGS_timeout_ms * 1000L); |
| 58 | continue; |
| 59 | } |
| 60 | example::CounterService_Stub stub(&channel); |
| 61 | |
| 62 | brpc::Controller cntl; |
| 63 | cntl.set_timeout_ms(FLAGS_timeout_ms); |
| 64 | // Randomly select which request we want send; |
| 65 | example::CounterResponse response; |
| 66 | |
| 67 | if (butil::fast_rand_less_than(100) < (size_t)FLAGS_add_percentage) { |
| 68 | example::FetchAddRequest request; |
| 69 | request.set_value(FLAGS_added_by); |
| 70 | stub.fetch_add(&cntl, &request, &response, NULL); |
| 71 | } else { |
| 72 | example::GetRequest request; |
| 73 | stub.get(&cntl, &request, &response, NULL); |
| 74 | } |
| 75 | if (cntl.Failed()) { |
| 76 | LOG(WARNING) << "Fail to send request to " << leader |
| 77 | << " : " << cntl.ErrorText(); |
| 78 | // Clear leadership since this RPC failed. |
| 79 | braft::rtb::update_leader(FLAGS_group, braft::PeerId()); |
| 80 | bthread_usleep(FLAGS_timeout_ms * 1000L); |
| 81 | continue; |
| 82 | } |
| 83 | if (!response.success()) { |
| 84 | LOG(WARNING) << "Fail to send request to " << leader |
| 85 | << ", redirecting to " |
| 86 | << (response.has_redirect() |
| 87 | ? response.redirect() : "nowhere"); |
| 88 | // Update route table since we have redirect information |
| 89 | braft::rtb::update_leader(FLAGS_group, response.redirect()); |
| 90 | continue; |
| 91 | } |
| 92 | g_latency_recorder << cntl.latency_us(); |
nothing calls this directly
no test coverage detected