3 * Copyright 2020 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include <condition_variable>
31 #include "absl/algorithm/container.h"
32 #include "absl/flags/flag.h"
33 #include "absl/strings/str_split.h"
35 #include <grpcpp/ext/admin_services.h>
36 #include <grpcpp/ext/proto_server_reflection_plugin.h>
37 #include <grpcpp/grpcpp.h>
38 #include <grpcpp/server.h>
39 #include <grpcpp/server_builder.h>
40 #include <grpcpp/server_context.h>
42 #include "src/core/lib/channel/status_util.h"
43 #include "src/core/lib/gpr/env.h"
44 #include "src/proto/grpc/testing/empty.pb.h"
45 #include "src/proto/grpc/testing/messages.pb.h"
46 #include "src/proto/grpc/testing/test.grpc.pb.h"
47 #include "test/core/util/test_config.h"
48 #include "test/cpp/util/test_config.h"
50 ABSL_FLAG(bool, fail_on_failed_rpc, false,
51 "Fail client if any RPCs fail after first successful RPC.");
52 ABSL_FLAG(int32_t, num_channels, 1, "Number of channels.");
53 ABSL_FLAG(bool, print_response, false, "Write RPC response to stdout.");
54 ABSL_FLAG(int32_t, qps, 1, "Qps per channel.");
55 // TODO(Capstan): Consider using absl::Duration
56 ABSL_FLAG(int32_t, rpc_timeout_sec, 30, "Per RPC timeout seconds.");
57 ABSL_FLAG(std::string, server, "localhost:50051", "Address of server.");
58 ABSL_FLAG(int32_t, stats_port, 50052,
59 "Port to expose peer distribution stats service.");
60 ABSL_FLAG(std::string, rpc, "UnaryCall",
61 "a comma separated list of rpc methods.");
62 ABSL_FLAG(std::string, metadata, "", "metadata to send with the RPC.");
63 ABSL_FLAG(std::string, expect_status, "OK",
64 "RPC status for the test RPC to be considered successful");
66 bool, secure_mode, false,
67 "If true, XdsCredentials are used, InsecureChannelCredentials otherwise");
70 using grpc::ClientAsyncResponseReader;
71 using grpc::ClientContext;
72 using grpc::CompletionQueue;
74 using grpc::ServerBuilder;
75 using grpc::ServerContext;
77 using grpc::testing::ClientConfigureRequest;
78 using grpc::testing::ClientConfigureRequest_RpcType_Name;
79 using grpc::testing::ClientConfigureResponse;
80 using grpc::testing::Empty;
81 using grpc::testing::LoadBalancerAccumulatedStatsRequest;
82 using grpc::testing::LoadBalancerAccumulatedStatsResponse;
83 using grpc::testing::LoadBalancerStatsRequest;
84 using grpc::testing::LoadBalancerStatsResponse;
85 using grpc::testing::LoadBalancerStatsService;
86 using grpc::testing::SimpleRequest;
87 using grpc::testing::SimpleResponse;
88 using grpc::testing::TestService;
89 using grpc::testing::XdsUpdateClientConfigureService;
91 class XdsStatsWatcher;
93 struct StatsWatchers {
94 // Unique ID for each outgoing RPC
95 int global_request_id = 0;
96 // Unique ID for each outgoing RPC by RPC method type
97 std::map<int, int> global_request_id_by_type;
98 // Stores a set of watchers that should be notified upon outgoing RPC
100 std::set<XdsStatsWatcher*> watchers;
101 // Global watcher for accumululated stats.
102 XdsStatsWatcher* global_watcher;
103 // Mutex for global_request_id and watchers
106 // Whether at least one RPC has succeeded, indicating xDS resolution completed.
107 std::atomic<bool> one_rpc_succeeded(false);
108 // RPC configuration detailing how RPC should be sent.
110 ClientConfigureRequest::RpcType type;
111 std::vector<std::pair<std::string, std::string>> metadata;
114 struct RpcConfigurationsQueue {
115 // A queue of RPC configurations detailing how RPCs should be sent.
116 std::deque<std::vector<RpcConfig>> rpc_configs_queue;
117 // Mutex for rpc_configs_queue
118 std::mutex mu_rpc_configs_queue;
120 struct AsyncClientCall {
121 Empty empty_response;
122 SimpleResponse simple_response;
123 ClientContext context;
125 int saved_request_id;
126 ClientConfigureRequest::RpcType rpc_type;
127 std::unique_ptr<ClientAsyncResponseReader<Empty>> empty_response_reader;
128 std::unique_ptr<ClientAsyncResponseReader<SimpleResponse>>
129 simple_response_reader;
132 /** Records the remote peer distribution for a given range of RPCs. */
133 class XdsStatsWatcher {
135 XdsStatsWatcher(int start_id, int end_id)
136 : start_id_(start_id), end_id_(end_id), rpcs_needed_(end_id - start_id) {}
138 // Upon the completion of an RPC, we will look at the request_id, the
139 // rpc_type, and the peer the RPC was sent to in order to count
140 // this RPC into the right stats bin.
141 void RpcCompleted(AsyncClientCall* call, const std::string& peer) {
142 // We count RPCs for global watcher or if the request_id falls into the
143 // watcher's interested range of request ids.
144 if ((start_id_ == 0 && end_id_ == 0) ||
145 (start_id_ <= call->saved_request_id &&
146 call->saved_request_id < end_id_)) {
148 std::lock_guard<std::mutex> lock(m_);
151 ++no_remote_peer_by_type_[call->rpc_type];
153 // RPC is counted into both per-peer bin and per-method-per-peer bin.
154 rpcs_by_peer_[peer]++;
155 rpcs_by_type_[call->rpc_type][peer]++;
158 // Report accumulated stats.
159 auto& stats_per_method = *accumulated_stats_.mutable_stats_per_method();
161 stats_per_method[ClientConfigureRequest_RpcType_Name(
163 auto& result = *method_stat.mutable_result();
164 grpc_status_code code =
165 static_cast<grpc_status_code>(call->status.error_code());
166 auto& num_rpcs = result[code];
168 auto rpcs_started = method_stat.rpcs_started();
169 method_stat.set_rpcs_started(++rpcs_started);
175 void WaitForRpcStatsResponse(LoadBalancerStatsResponse* response,
177 std::unique_lock<std::mutex> lock(m_);
178 cv_.wait_for(lock, std::chrono::seconds(timeout_sec),
179 [this] { return rpcs_needed_ == 0; });
180 response->mutable_rpcs_by_peer()->insert(rpcs_by_peer_.begin(),
181 rpcs_by_peer_.end());
182 auto& response_rpcs_by_method = *response->mutable_rpcs_by_method();
183 for (const auto& rpc_by_type : rpcs_by_type_) {
184 std::string method_name;
185 if (rpc_by_type.first == ClientConfigureRequest::EMPTY_CALL) {
186 method_name = "EmptyCall";
187 } else if (rpc_by_type.first == ClientConfigureRequest::UNARY_CALL) {
188 method_name = "UnaryCall";
192 // TODO(@donnadionne): When the test runner changes to accept EMPTY_CALL
193 // and UNARY_CALL we will just use the name of the enum instead of the
194 // method_name variable.
195 auto& response_rpc_by_method = response_rpcs_by_method[method_name];
196 auto& response_rpcs_by_peer =
197 *response_rpc_by_method.mutable_rpcs_by_peer();
198 for (const auto& rpc_by_peer : rpc_by_type.second) {
199 auto& response_rpc_by_peer = response_rpcs_by_peer[rpc_by_peer.first];
200 response_rpc_by_peer = rpc_by_peer.second;
203 response->set_num_failures(no_remote_peer_ + rpcs_needed_);
206 void GetCurrentRpcStats(LoadBalancerAccumulatedStatsResponse* response,
207 StatsWatchers* stats_watchers) {
208 std::unique_lock<std::mutex> lock(m_);
209 response->CopyFrom(accumulated_stats_);
210 // TODO(@donnadionne): delete deprecated stats below when the test is no
211 // longer using them.
212 auto& response_rpcs_started_by_method =
213 *response->mutable_num_rpcs_started_by_method();
214 auto& response_rpcs_succeeded_by_method =
215 *response->mutable_num_rpcs_succeeded_by_method();
216 auto& response_rpcs_failed_by_method =
217 *response->mutable_num_rpcs_failed_by_method();
218 for (const auto& rpc_by_type : rpcs_by_type_) {
219 auto total_succeeded = 0;
220 for (const auto& rpc_by_peer : rpc_by_type.second) {
221 total_succeeded += rpc_by_peer.second;
223 response_rpcs_succeeded_by_method[ClientConfigureRequest_RpcType_Name(
224 rpc_by_type.first)] = total_succeeded;
225 response_rpcs_started_by_method[ClientConfigureRequest_RpcType_Name(
226 rpc_by_type.first)] =
227 stats_watchers->global_request_id_by_type[rpc_by_type.first];
228 response_rpcs_failed_by_method[ClientConfigureRequest_RpcType_Name(
229 rpc_by_type.first)] = no_remote_peer_by_type_[rpc_by_type.first];
237 int no_remote_peer_ = 0;
238 std::map<int, int> no_remote_peer_by_type_;
239 // A map of stats keyed by peer name.
240 std::map<std::string, int> rpcs_by_peer_;
241 // A two-level map of stats keyed at top level by RPC method and second level
243 std::map<int, std::map<std::string, int>> rpcs_by_type_;
244 // Storing accumulated stats in the response proto format.
245 LoadBalancerAccumulatedStatsResponse accumulated_stats_;
247 std::condition_variable cv_;
252 TestClient(const std::shared_ptr<Channel>& channel,
253 StatsWatchers* stats_watchers)
254 : stub_(TestService::NewStub(channel)), stats_watchers_(stats_watchers) {}
256 void AsyncUnaryCall(const RpcConfig& config) {
257 SimpleResponse response;
258 int saved_request_id;
260 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
261 saved_request_id = ++stats_watchers_->global_request_id;
263 ->global_request_id_by_type[ClientConfigureRequest::UNARY_CALL];
265 std::chrono::system_clock::time_point deadline =
266 std::chrono::system_clock::now() +
267 std::chrono::seconds(config.timeout_sec != 0
269 : absl::GetFlag(FLAGS_rpc_timeout_sec));
270 AsyncClientCall* call = new AsyncClientCall;
271 for (const auto& data : config.metadata) {
272 call->context.AddMetadata(data.first, data.second);
273 // TODO(@donnadionne): move deadline to separate proto.
274 if (data.first == "rpc-behavior" && data.second == "keep-open") {
276 std::chrono::system_clock::now() + std::chrono::seconds(INT_MAX);
279 call->context.set_deadline(deadline);
280 call->saved_request_id = saved_request_id;
281 call->rpc_type = ClientConfigureRequest::UNARY_CALL;
282 call->simple_response_reader = stub_->PrepareAsyncUnaryCall(
283 &call->context, SimpleRequest::default_instance(), &cq_);
284 call->simple_response_reader->StartCall();
285 call->simple_response_reader->Finish(&call->simple_response, &call->status,
289 void AsyncEmptyCall(const RpcConfig& config) {
291 int saved_request_id;
293 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
294 saved_request_id = ++stats_watchers_->global_request_id;
296 ->global_request_id_by_type[ClientConfigureRequest::EMPTY_CALL];
298 std::chrono::system_clock::time_point deadline =
299 std::chrono::system_clock::now() +
300 std::chrono::seconds(config.timeout_sec != 0
302 : absl::GetFlag(FLAGS_rpc_timeout_sec));
303 AsyncClientCall* call = new AsyncClientCall;
304 for (const auto& data : config.metadata) {
305 call->context.AddMetadata(data.first, data.second);
306 // TODO(@donnadionne): move deadline to separate proto.
307 if (data.first == "rpc-behavior" && data.second == "keep-open") {
309 std::chrono::system_clock::now() + std::chrono::seconds(INT_MAX);
312 call->context.set_deadline(deadline);
313 call->saved_request_id = saved_request_id;
314 call->rpc_type = ClientConfigureRequest::EMPTY_CALL;
315 call->empty_response_reader = stub_->PrepareAsyncEmptyCall(
316 &call->context, Empty::default_instance(), &cq_);
317 call->empty_response_reader->StartCall();
318 call->empty_response_reader->Finish(&call->empty_response, &call->status,
322 void AsyncCompleteRpc() {
325 while (cq_.Next(&got_tag, &ok)) {
326 AsyncClientCall* call = static_cast<AsyncClientCall*>(got_tag);
329 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
330 auto server_initial_metadata = call->context.GetServerInitialMetadata();
331 auto metadata_hostname =
332 call->context.GetServerInitialMetadata().find("hostname");
333 std::string hostname =
334 metadata_hostname != call->context.GetServerInitialMetadata().end()
335 ? std::string(metadata_hostname->second.data(),
336 metadata_hostname->second.length())
337 : call->simple_response.hostname();
338 for (auto watcher : stats_watchers_->watchers) {
339 watcher->RpcCompleted(call, hostname);
343 if (!RpcStatusCheckSuccess(call)) {
344 if (absl::GetFlag(FLAGS_print_response) ||
345 absl::GetFlag(FLAGS_fail_on_failed_rpc)) {
346 std::cout << "RPC failed: " << call->status.error_code() << ": "
347 << call->status.error_message() << std::endl;
349 if (absl::GetFlag(FLAGS_fail_on_failed_rpc) &&
350 one_rpc_succeeded.load()) {
354 if (absl::GetFlag(FLAGS_print_response)) {
355 auto metadata_hostname =
356 call->context.GetServerInitialMetadata().find("hostname");
357 std::string hostname =
359 call->context.GetServerInitialMetadata().end()
360 ? std::string(metadata_hostname->second.data(),
361 metadata_hostname->second.length())
362 : call->simple_response.hostname();
363 std::cout << "Greeting: Hello world, this is " << hostname
364 << ", from " << call->context.peer() << std::endl;
366 one_rpc_succeeded = true;
374 static bool RpcStatusCheckSuccess(AsyncClientCall* call) {
375 // Determine RPC success based on expected status.
376 grpc_status_code code;
377 GPR_ASSERT(grpc_status_code_from_string(
378 absl::GetFlag(FLAGS_expect_status).c_str(), &code));
379 return code == static_cast<grpc_status_code>(call->status.error_code());
382 std::unique_ptr<TestService::Stub> stub_;
383 StatsWatchers* stats_watchers_;
387 class LoadBalancerStatsServiceImpl : public LoadBalancerStatsService::Service {
389 explicit LoadBalancerStatsServiceImpl(StatsWatchers* stats_watchers)
390 : stats_watchers_(stats_watchers) {}
392 Status GetClientStats(ServerContext* /*context*/,
393 const LoadBalancerStatsRequest* request,
394 LoadBalancerStatsResponse* response) override {
397 XdsStatsWatcher* watcher;
399 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
400 start_id = stats_watchers_->global_request_id + 1;
401 end_id = start_id + request->num_rpcs();
402 watcher = new XdsStatsWatcher(start_id, end_id);
403 stats_watchers_->watchers.insert(watcher);
405 watcher->WaitForRpcStatsResponse(response, request->timeout_sec());
407 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
408 stats_watchers_->watchers.erase(watcher);
414 Status GetClientAccumulatedStats(
415 ServerContext* /*context*/,
416 const LoadBalancerAccumulatedStatsRequest* /*request*/,
417 LoadBalancerAccumulatedStatsResponse* response) override {
418 std::lock_guard<std::mutex> lock(stats_watchers_->mu);
419 stats_watchers_->global_watcher->GetCurrentRpcStats(response,
425 StatsWatchers* stats_watchers_;
428 class XdsUpdateClientConfigureServiceImpl
429 : public XdsUpdateClientConfigureService::Service {
431 explicit XdsUpdateClientConfigureServiceImpl(
432 RpcConfigurationsQueue* rpc_configs_queue)
433 : rpc_configs_queue_(rpc_configs_queue) {}
435 Status Configure(ServerContext* /*context*/,
436 const ClientConfigureRequest* request,
437 ClientConfigureResponse* /*response*/) override {
438 std::map<int, std::vector<std::pair<std::string, std::string>>>
440 for (const auto& data : request->metadata()) {
441 metadata_map[data.type()].push_back({data.key(), data.value()});
443 std::vector<RpcConfig> configs;
444 for (const auto& rpc : request->types()) {
446 config.timeout_sec = request->timeout_sec();
447 config.type = static_cast<ClientConfigureRequest::RpcType>(rpc);
448 auto metadata_iter = metadata_map.find(rpc);
449 if (metadata_iter != metadata_map.end()) {
450 config.metadata = metadata_iter->second;
452 configs.push_back(std::move(config));
455 std::lock_guard<std::mutex> lock(
456 rpc_configs_queue_->mu_rpc_configs_queue);
457 rpc_configs_queue_->rpc_configs_queue.emplace_back(std::move(configs));
463 RpcConfigurationsQueue* rpc_configs_queue_;
466 void RunTestLoop(std::chrono::duration<double> duration_per_query,
467 StatsWatchers* stats_watchers,
468 RpcConfigurationsQueue* rpc_configs_queue) {
469 grpc::ChannelArguments channel_args;
470 channel_args.SetInt(GRPC_ARG_ENABLE_RETRIES, 1);
472 grpc::CreateCustomChannel(absl::GetFlag(FLAGS_server),
473 absl::GetFlag(FLAGS_secure_mode)
474 ? grpc::experimental::XdsCredentials(
475 grpc::InsecureChannelCredentials())
476 : grpc::InsecureChannelCredentials(),
479 std::chrono::time_point<std::chrono::system_clock> start =
480 std::chrono::system_clock::now();
481 std::chrono::duration<double> elapsed;
483 std::thread thread = std::thread(&TestClient::AsyncCompleteRpc, &client);
485 std::vector<RpcConfig> configs;
488 std::lock_guard<std::mutex> lockk(
489 rpc_configs_queue->mu_rpc_configs_queue);
490 if (!rpc_configs_queue->rpc_configs_queue.empty()) {
491 configs = std::move(rpc_configs_queue->rpc_configs_queue.front());
492 rpc_configs_queue->rpc_configs_queue.pop_front();
496 elapsed = std::chrono::system_clock::now() - start;
497 if (elapsed > duration_per_query) {
498 start = std::chrono::system_clock::now();
499 for (const auto& config : configs) {
500 if (config.type == ClientConfigureRequest::EMPTY_CALL) {
501 client.AsyncEmptyCall(config);
502 } else if (config.type == ClientConfigureRequest::UNARY_CALL) {
503 client.AsyncUnaryCall(config);
510 GPR_UNREACHABLE_CODE(thread.join());
513 void RunServer(const int port, StatsWatchers* stats_watchers,
514 RpcConfigurationsQueue* rpc_configs_queue) {
515 GPR_ASSERT(port != 0);
516 std::ostringstream server_address;
517 server_address << "0.0.0.0:" << port;
519 LoadBalancerStatsServiceImpl stats_service(stats_watchers);
520 XdsUpdateClientConfigureServiceImpl client_config_service(rpc_configs_queue);
522 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
523 ServerBuilder builder;
524 builder.RegisterService(&stats_service);
525 builder.RegisterService(&client_config_service);
526 grpc::AddAdminServices(&builder);
527 builder.AddListeningPort(server_address.str(),
528 grpc::InsecureServerCredentials());
529 std::unique_ptr<Server> server(builder.BuildAndStart());
530 gpr_log(GPR_DEBUG, "Server listening on %s", server_address.str().c_str());
535 void BuildRpcConfigsFromFlags(RpcConfigurationsQueue* rpc_configs_queue) {
536 // Store Metadata like
537 // "EmptyCall:key1:value1,UnaryCall:key1:value1,UnaryCall:key2:value2" into a
538 // map where the key is the RPC method and value is a vector of key:value
539 // pairs. {EmptyCall, [{key1,value1}],
540 // UnaryCall, [{key1,value1}, {key2,value2}]}
541 std::vector<std::string> rpc_metadata =
542 absl::StrSplit(absl::GetFlag(FLAGS_metadata), ',', absl::SkipEmpty());
543 std::map<int, std::vector<std::pair<std::string, std::string>>> metadata_map;
544 for (auto& data : rpc_metadata) {
545 std::vector<std::string> metadata =
546 absl::StrSplit(data, ':', absl::SkipEmpty());
547 GPR_ASSERT(metadata.size() == 3);
548 if (metadata[0] == "EmptyCall") {
549 metadata_map[ClientConfigureRequest::EMPTY_CALL].push_back(
550 {metadata[1], metadata[2]});
551 } else if (metadata[0] == "UnaryCall") {
552 metadata_map[ClientConfigureRequest::UNARY_CALL].push_back(
553 {metadata[1], metadata[2]});
558 std::vector<RpcConfig> configs;
559 std::vector<std::string> rpc_methods =
560 absl::StrSplit(absl::GetFlag(FLAGS_rpc), ',', absl::SkipEmpty());
561 for (const std::string& rpc_method : rpc_methods) {
563 if (rpc_method == "EmptyCall") {
564 config.type = ClientConfigureRequest::EMPTY_CALL;
565 } else if (rpc_method == "UnaryCall") {
566 config.type = ClientConfigureRequest::UNARY_CALL;
570 auto metadata_iter = metadata_map.find(config.type);
571 if (metadata_iter != metadata_map.end()) {
572 config.metadata = metadata_iter->second;
574 configs.push_back(std::move(config));
577 std::lock_guard<std::mutex> lock(rpc_configs_queue->mu_rpc_configs_queue);
578 rpc_configs_queue->rpc_configs_queue.emplace_back(std::move(configs));
582 int main(int argc, char** argv) {
583 grpc::testing::TestEnvironment env(argc, argv);
584 grpc::testing::InitTest(&argc, &argv, true);
585 // Validate the expect_status flag.
586 grpc_status_code code;
587 GPR_ASSERT(grpc_status_code_from_string(
588 absl::GetFlag(FLAGS_expect_status).c_str(), &code));
589 StatsWatchers stats_watchers;
590 RpcConfigurationsQueue rpc_config_queue;
593 std::lock_guard<std::mutex> lock(stats_watchers.mu);
594 stats_watchers.global_watcher = new XdsStatsWatcher(0, 0);
595 stats_watchers.watchers.insert(stats_watchers.global_watcher);
598 BuildRpcConfigsFromFlags(&rpc_config_queue);
600 std::chrono::duration<double> duration_per_query =
601 std::chrono::nanoseconds(std::chrono::seconds(1)) /
602 absl::GetFlag(FLAGS_qps);
604 std::vector<std::thread> test_threads;
605 test_threads.reserve(absl::GetFlag(FLAGS_num_channels));
606 for (int i = 0; i < absl::GetFlag(FLAGS_num_channels); i++) {
607 test_threads.emplace_back(std::thread(&RunTestLoop, duration_per_query,
608 &stats_watchers, &rpc_config_queue));
611 RunServer(absl::GetFlag(FLAGS_stats_port), &stats_watchers,
614 for (auto it = test_threads.begin(); it != test_threads.end(); it++) {