From 962a8f28f9c448dbb84769109d69be21103f42f9 Mon Sep 17 00:00:00 2001 From: Alexander Polcyn Date: Wed, 1 Apr 2020 13:00:12 -0700 Subject: [PATCH] Add a flag to set a grpclb+pick_first service config in CreateTestChannel; use CreateTestChannel in qps_json_driver --- test/cpp/qps/driver.cc | 26 ++++++++++-------------- test/cpp/util/create_test_channel.cc | 30 ++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/test/cpp/qps/driver.cc b/test/cpp/qps/driver.cc index 94c2feffc19..03a4477b4d8 100644 --- a/test/cpp/qps/driver.cc +++ b/test/cpp/qps/driver.cc @@ -313,11 +313,10 @@ std::unique_ptr RunScenario( gpr_log(GPR_INFO, "Starting server on %s (worker #%" PRIuPTR ")", workers[i].c_str(), i); if (!run_inproc) { - servers[i].stub = WorkerService::NewStub(grpc::CreateChannel( - workers[i], GetCredentialsProvider()->GetChannelCredentials( - GetCredType(workers[i], per_worker_credential_types, - credential_type), - &channel_args))); + servers[i].stub = WorkerService::NewStub(grpc::CreateTestChannel( + workers[i], + GetCredType(workers[i], per_worker_credential_types, credential_type), + nullptr /* call creds */, {} /* interceptor creators */)); } else { servers[i].stub = WorkerService::NewStub( local_workers[i]->InProcessChannel(channel_args)); @@ -373,11 +372,10 @@ std::unique_ptr RunScenario( gpr_log(GPR_INFO, "Starting client on %s (worker #%" PRIuPTR ")", worker.c_str(), i + num_servers); if (!run_inproc) { - clients[i].stub = WorkerService::NewStub(grpc::CreateChannel( + clients[i].stub = WorkerService::NewStub(grpc::CreateTestChannel( worker, - GetCredentialsProvider()->GetChannelCredentials( - GetCredType(worker, per_worker_credential_types, credential_type), - &channel_args))); + GetCredType(worker, per_worker_credential_types, credential_type), + nullptr /* call creds */, {} /* interceptor creators */)); } else { clients[i].stub = WorkerService::NewStub( local_workers[i + num_servers]->InProcessChannel(channel_args)); @@ -588,13 +586,11 @@ bool RunQuit( return false; } - ChannelArguments channel_args; for (size_t i = 0; i < workers.size(); i++) { - auto stub = WorkerService::NewStub(grpc::CreateChannel( - workers[i], GetCredentialsProvider()->GetChannelCredentials( - GetCredType(workers[i], per_worker_credential_types, - credential_type), - &channel_args))); + auto stub = WorkerService::NewStub(grpc::CreateTestChannel( + workers[i], + GetCredType(workers[i], per_worker_credential_types, credential_type), + nullptr /* call creds */, {} /* interceptor creators */)); Void dummy; grpc::ClientContext ctx; ctx.set_wait_for_ready(true); diff --git a/test/cpp/util/create_test_channel.cc b/test/cpp/util/create_test_channel.cc index a46a39c87c1..c4ba24749a6 100644 --- a/test/cpp/util/create_test_channel.cc +++ b/test/cpp/util/create_test_channel.cc @@ -18,12 +18,21 @@ #include "test/cpp/util/create_test_channel.h" +#include + #include #include #include #include "test/cpp/util/test_credentials_provider.h" +DEFINE_string( + grpc_test_use_grpclb_with_child_policy, "", + "If non-empty, set a static service config on channels created by " + "grpc::CreateTestChannel, that configures the grpclb LB policy " + "with a child policy being the value of this flag (e.g. round_robin " + "or pick_first)."); + namespace grpc { namespace { @@ -49,6 +58,16 @@ void AddProdSslType() { new SslCredentialProvider)); } +void MaybeSetCustomChannelArgs(grpc::ChannelArguments* args) { + if (FLAGS_grpc_test_use_grpclb_with_child_policy.size() > 0) { + args->SetString("grpc.service_config", + "{\"loadBalancingConfig\":[{\"grpclb\":{\"childPolicy\":[{" + "\"" + + FLAGS_grpc_test_use_grpclb_with_child_policy + + "\":{}}]}}]}"); + } +} + } // namespace // When cred_type is 'ssl', if server is empty, override_hostname is used to @@ -111,6 +130,7 @@ std::shared_ptr CreateTestChannel( const grpc::string& server, const grpc::string& credential_type, const std::shared_ptr& creds) { ChannelArguments channel_args; + MaybeSetCustomChannelArgs(&channel_args); std::shared_ptr channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(credential_type, &channel_args); @@ -129,14 +149,15 @@ std::shared_ptr CreateTestChannel( std::unique_ptr> interceptor_creators) { ChannelArguments channel_args(args); + MaybeSetCustomChannelArgs(&channel_args); std::shared_ptr channel_creds; if (cred_type.empty()) { if (interceptor_creators.empty()) { return ::grpc::CreateCustomChannel(server, InsecureChannelCredentials(), - args); + channel_args); } else { return experimental::CreateCustomChannelWithInterceptors( - server, InsecureChannelCredentials(), args, + server, InsecureChannelCredentials(), channel_args, std::move(interceptor_creators)); } } else if (cred_type == testing::kTlsCredentialsType) { // cred_type == "ssl" @@ -173,10 +194,10 @@ std::shared_ptr CreateTestChannel( GPR_ASSERT(channel_creds != nullptr); if (interceptor_creators.empty()) { - return ::grpc::CreateCustomChannel(server, channel_creds, args); + return ::grpc::CreateCustomChannel(server, channel_creds, channel_args); } else { return experimental::CreateCustomChannelWithInterceptors( - server, channel_creds, args, std::move(interceptor_creators)); + server, channel_creds, channel_args, std::move(interceptor_creators)); } } } @@ -217,6 +238,7 @@ std::shared_ptr CreateTestChannel( std::unique_ptr> interceptor_creators) { ChannelArguments channel_args; + MaybeSetCustomChannelArgs(&channel_args); std::shared_ptr channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(credential_type, &channel_args);