| 1 |
// |
| 2 |
// Copyright 2017 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include <deque> |
| 18 |
#include <memory> |
| 19 |
#include <mutex> |
| 20 |
#include <set> |
| 21 |
#include <sstream> |
| 22 |
#include <string> |
| 23 |
#include <thread> |
| 24 |
|
| 25 |
#include <gmock/gmock.h> |
| 26 |
#include <gtest/gtest.h> |
| 27 |
|
| 28 |
#include "absl/memory/memory.h" |
| 29 |
#include "absl/strings/str_cat.h" |
| 30 |
#include "absl/strings/str_format.h" |
| 31 |
|
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/support/alloc.h> |
| 34 |
#include <grpc/support/log.h> |
| 35 |
#include <grpc/support/time.h> |
| 36 |
#include <grpcpp/channel.h> |
| 37 |
#include <grpcpp/client_context.h> |
| 38 |
#include <grpcpp/create_channel.h> |
| 39 |
#include <grpcpp/impl/codegen/sync.h> |
| 40 |
#include <grpcpp/server.h> |
| 41 |
#include <grpcpp/server_builder.h> |
| 42 |
|
| 43 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 44 |
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h" |
| 45 |
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h" |
| 46 |
#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h" |
| 47 |
#include "src/core/lib/address_utils/parse_address.h" |
| 48 |
#include "src/core/lib/channel/channel_args.h" |
| 49 |
#include "src/core/lib/gprpp/env.h" |
| 50 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 51 |
#include "src/core/lib/iomgr/sockaddr.h" |
| 52 |
#include "src/core/lib/resolver/server_address.h" |
| 53 |
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
| 54 |
#include "src/core/lib/service_config/service_config_impl.h" |
| 55 |
#include "src/cpp/client/secure_credentials.h" |
| 56 |
#include "src/cpp/server/secure_server_credentials.h" |
| 57 |
#include "src/proto/grpc/lb/v1/load_balancer.grpc.pb.h" |
| 58 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 59 |
#include "test/core/util/port.h" |
| 60 |
#include "test/core/util/resolve_localhost_ip46.h" |
| 61 |
#include "test/core/util/test_config.h" |
| 62 |
#include "test/cpp/end2end/counted_service.h" |
| 63 |
#include "test/cpp/end2end/test_service_impl.h" |
| 64 |
#include "test/cpp/util/test_config.h" |
| 65 |
|
| 66 |
// TODO(dgq): Other scenarios in need of testing: |
| 67 |
// - Send a serverlist with faulty ip:port addresses (port > 2^16, etc). |
| 68 |
// - Test reception of invalid serverlist |
| 69 |
// - Test against a non-LB server. |
| 70 |
// - Random LB server closing the stream unexpectedly. |
| 71 |
// |
| 72 |
// Findings from end to end testing to be covered here: |
| 73 |
// - Handling of LB servers restart, including reconnection after backing-off |
| 74 |
// retries. |
| 75 |
// - Destruction of load balanced channel (and therefore of grpclb instance) |
| 76 |
// while: |
| 77 |
// 1) the internal LB call is still active. This should work by virtue |
| 78 |
// of the weak reference the LB call holds. The call should be terminated as |
| 79 |
// part of the grpclb shutdown process. |
| 80 |
// 2) the retry timer is active. Again, the weak reference it holds should |
| 81 |
// prevent a premature call to \a glb_destroy. |
| 82 |
|
| 83 |
using std::chrono::system_clock; |
| 84 |
|
| 85 |
using grpc::lb::v1::LoadBalancer; |
| 86 |
using grpc::lb::v1::LoadBalanceRequest; |
| 87 |
using grpc::lb::v1::LoadBalanceResponse; |
| 88 |
|
| 89 |
namespace grpc { |
| 90 |
namespace testing { |
| 91 |
namespace { |
| 92 |
|
| 93 |
constexpr char kDefaultServiceConfig[] = |
| 94 |
"{\n" |
| 95 |
" \"loadBalancingConfig\":[\n" |
| 96 |
" { \"grpclb\":{} }\n" |
| 97 |
" ]\n" |
| 98 |
"}"; |
| 99 |
|
| 100 |
using BackendService = CountedService<TestServiceImpl>; |
| 101 |
using BalancerService = CountedService<LoadBalancer::Service>; |
| 102 |
|
| 103 |
const char g_kCallCredsMdKey[] = "Balancer should not ..."; |
| 104 |
const char g_kCallCredsMdValue[] = "... receive me"; |
| 105 |
|
| 106 |
// A test user agent string sent by the client only to the grpclb loadbalancer. |
| 107 |
// The backend should not see this user-agent string. |
| 108 |
constexpr char kGrpclbSpecificUserAgentString[] = "grpc-grpclb-test-user-agent"; |
| 109 |
|
| 110 |
class BackendServiceImpl : public BackendService { |
| 111 |
public: |
| 112 |
BackendServiceImpl() {} |
| 113 |
|
| 114 |
Status Echo(ServerContext* context, const EchoRequest* request, |
| 115 |
EchoResponse* response) override { |
| 116 |
// The backend should not see a test user agent configured at the client |
| 117 |
// using GRPC_ARG_GRPCLB_CHANNEL_ARGS. |
| 118 |
auto it = context->client_metadata().find("user-agent"); |
| 119 |
if (it != context->client_metadata().end()) { |
| 120 |
EXPECT_FALSE(it->second.starts_with(kGrpclbSpecificUserAgentString)); |
| 121 |
} |
| 122 |
// Backend should receive the call credentials metadata. |
| 123 |
auto call_credentials_entry = |
| 124 |
context->client_metadata().find(g_kCallCredsMdKey); |
| 125 |
EXPECT_NE(call_credentials_entry, context->client_metadata().end()); |
| 126 |
if (call_credentials_entry != context->client_metadata().end()) { |
| 127 |
EXPECT_EQ(call_credentials_entry->second, g_kCallCredsMdValue); |
| 128 |
} |
| 129 |
IncreaseRequestCount(); |
| 130 |
const auto status = TestServiceImpl::Echo(context, request, response); |
| 131 |
IncreaseResponseCount(); |
| 132 |
AddClient(context->peer()); |
| 133 |
return status; |
| 134 |
} |
| 135 |
|
| 136 |
void Start() {} |
| 137 |
|
| 138 |
void Shutdown() {} |
| 139 |
|
| 140 |
std::set<std::string> clients() { |
| 141 |
grpc::internal::MutexLock lock(&clients_mu_); |
| 142 |
return clients_; |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
void AddClient(const std::string& client) { |
| 147 |
grpc::internal::MutexLock lock(&clients_mu_); |
| 148 |
clients_.insert(client); |
| 149 |
} |
| 150 |
|
| 151 |
grpc::internal::Mutex clients_mu_; |
| 152 |
std::set<std::string> clients_ ABSL_GUARDED_BY(&clients_mu_); |
| 153 |
}; |
| 154 |
|
| 155 |
std::string Ip4ToPackedString(const char* ip_str) { |
| 156 |
struct in_addr ip4; |
| 157 |
GPR_ASSERT(inet_pton(AF_INET, ip_str, &ip4) == 1); |
| 158 |
return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4)); |
| 159 |
} |
| 160 |
|
| 161 |
std::string Ip6ToPackedString(const char* ip_str) { |
| 162 |
struct in6_addr ip6; |
| 163 |
GPR_ASSERT(inet_pton(AF_INET6, ip_str, &ip6) == 1); |
| 164 |
return std::string(reinterpret_cast<const char*>(&ip6), sizeof(ip6)); |
| 165 |
} |
| 166 |
|
| 167 |
struct ClientStats { |
| 168 |
size_t num_calls_started = 0; |
| 169 |
size_t num_calls_finished = 0; |
| 170 |
size_t num_calls_finished_with_client_failed_to_send = 0; |
| 171 |
size_t num_calls_finished_known_received = 0; |
| 172 |
std::map<std::string, size_t> drop_token_counts; |
| 173 |
|
| 174 |
ClientStats& operator+=(const ClientStats& other) { |
| 175 |
num_calls_started += other.num_calls_started; |
| 176 |
num_calls_finished += other.num_calls_finished; |
| 177 |
num_calls_finished_with_client_failed_to_send += |
| 178 |
other.num_calls_finished_with_client_failed_to_send; |
| 179 |
num_calls_finished_known_received += |
| 180 |
other.num_calls_finished_known_received; |
| 181 |
for (const auto& p : other.drop_token_counts) { |
| 182 |
drop_token_counts[p.first] += p.second; |
| 183 |
} |
| 184 |
return *this; |
| 185 |
} |
| 186 |
|
| 187 |
void Reset() { |
| 188 |
num_calls_started = 0; |
| 189 |
num_calls_finished = 0; |
| 190 |
num_calls_finished_with_client_failed_to_send = 0; |
| 191 |
num_calls_finished_known_received = 0; |
| 192 |
drop_token_counts.clear(); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
class BalancerServiceImpl : public BalancerService { |
| 197 |
public: |
| 198 |
using Stream = ServerReaderWriter<LoadBalanceResponse, LoadBalanceRequest>; |
| 199 |
using ResponseDelayPair = std::pair<LoadBalanceResponse, int>; |
| 200 |
|
| 201 |
explicit BalancerServiceImpl(int client_load_reporting_interval_seconds) |
| 202 |
: client_load_reporting_interval_seconds_( |
| 203 |
client_load_reporting_interval_seconds) {} |
| 204 |
|
| 205 |
Status BalanceLoad(ServerContext* context, Stream* stream) override { |
| 206 |
gpr_log(GPR_INFO, "LB[%p]: BalanceLoad", this); |
| 207 |
{ |
| 208 |
grpc::internal::MutexLock lock(&mu_); |
| 209 |
if (serverlist_done_) goto done; |
| 210 |
} |
| 211 |
{ |
| 212 |
// The loadbalancer should see a test user agent because it was |
| 213 |
// specifically configured at the client using |
| 214 |
// GRPC_ARG_GRPCLB_CHANNEL_ARGS |
| 215 |
auto it = context->client_metadata().find("user-agent"); |
| 216 |
EXPECT_TRUE(it != context->client_metadata().end()); |
| 217 |
if (it != context->client_metadata().end()) { |
| 218 |
EXPECT_THAT(std::string(it->second.data(), it->second.length()), |
| 219 |
::testing::StartsWith(kGrpclbSpecificUserAgentString)); |
| 220 |
} |
| 221 |
// Balancer shouldn't receive the call credentials metadata. |
| 222 |
EXPECT_EQ(context->client_metadata().find(g_kCallCredsMdKey), |
| 223 |
context->client_metadata().end()); |
| 224 |
LoadBalanceRequest request; |
| 225 |
std::vector<ResponseDelayPair> responses_and_delays; |
| 226 |
|
| 227 |
if (!stream->Read(&request)) { |
| 228 |
goto done; |
| 229 |
} else { |
| 230 |
if (request.has_initial_request()) { |
| 231 |
grpc::internal::MutexLock lock(&mu_); |
| 232 |
service_names_.push_back(request.initial_request().name()); |
| 233 |
} |
| 234 |
} |
| 235 |
IncreaseRequestCount(); |
| 236 |
gpr_log(GPR_INFO, "LB[%p]: received initial message '%s'", this, |
| 237 |
request.DebugString().c_str()); |
| 238 |
|
| 239 |
// TODO(juanlishen): Initial response should always be the first response. |
| 240 |
if (client_load_reporting_interval_seconds_ > 0) { |
| 241 |
LoadBalanceResponse initial_response; |
| 242 |
initial_response.mutable_initial_response() |
| 243 |
->mutable_client_stats_report_interval() |
| 244 |
->set_seconds(client_load_reporting_interval_seconds_); |
| 245 |
stream->Write(initial_response); |
| 246 |
} |
| 247 |
|
| 248 |
{ |
| 249 |
grpc::internal::MutexLock lock(&mu_); |
| 250 |
responses_and_delays = responses_and_delays_; |
| 251 |
} |
| 252 |
for (const auto& response_and_delay : responses_and_delays) { |
| 253 |
SendResponse(stream, response_and_delay.first, |
| 254 |
response_and_delay.second); |
| 255 |
} |
| 256 |
{ |
| 257 |
grpc::internal::MutexLock lock(&mu_); |
| 258 |
while (!serverlist_done_) { |
| 259 |
serverlist_cond_.Wait(&mu_); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
if (client_load_reporting_interval_seconds_ > 0) { |
| 264 |
request.Clear(); |
| 265 |
while (stream->Read(&request)) { |
| 266 |
gpr_log(GPR_INFO, "LB[%p]: received client load report message '%s'", |
| 267 |
this, request.DebugString().c_str()); |
| 268 |
GPR_ASSERT(request.has_client_stats()); |
| 269 |
ClientStats load_report; |
| 270 |
load_report.num_calls_started = |
| 271 |
request.client_stats().num_calls_started(); |
| 272 |
load_report.num_calls_finished = |
| 273 |
request.client_stats().num_calls_finished(); |
| 274 |
load_report.num_calls_finished_with_client_failed_to_send = |
| 275 |
request.client_stats() |
| 276 |
.num_calls_finished_with_client_failed_to_send(); |
| 277 |
load_report.num_calls_finished_known_received = |
| 278 |
request.client_stats().num_calls_finished_known_received(); |
| 279 |
for (const auto& drop_token_count : |
| 280 |
request.client_stats().calls_finished_with_drop()) { |
| 281 |
load_report |
| 282 |
.drop_token_counts[drop_token_count.load_balance_token()] = |
| 283 |
drop_token_count.num_calls(); |
| 284 |
} |
| 285 |
// We need to acquire the lock here in order to prevent the notify_one |
| 286 |
// below from firing before its corresponding wait is executed. |
| 287 |
grpc::internal::MutexLock lock(&mu_); |
| 288 |
load_report_queue_.emplace_back(std::move(load_report)); |
| 289 |
load_report_cond_.Signal(); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
done: |
| 294 |
gpr_log(GPR_INFO, "LB[%p]: done", this); |
| 295 |
return Status::OK; |
| 296 |
} |
| 297 |
|
| 298 |
void add_response(const LoadBalanceResponse& response, int send_after_ms) { |
| 299 |
grpc::internal::MutexLock lock(&mu_); |
| 300 |
responses_and_delays_.push_back(std::make_pair(response, send_after_ms)); |
| 301 |
} |
| 302 |
|
| 303 |
void Start() { |
| 304 |
grpc::internal::MutexLock lock(&mu_); |
| 305 |
serverlist_done_ = false; |
| 306 |
responses_and_delays_.clear(); |
| 307 |
load_report_queue_.clear(); |
| 308 |
} |
| 309 |
|
| 310 |
void Shutdown() { |
| 311 |
NotifyDoneWithServerlists(); |
| 312 |
gpr_log(GPR_INFO, "LB[%p]: shut down", this); |
| 313 |
} |
| 314 |
|
| 315 |
ClientStats WaitForLoadReport() { |
| 316 |
grpc::internal::MutexLock lock(&mu_); |
| 317 |
if (load_report_queue_.empty()) { |
| 318 |
while (load_report_queue_.empty()) { |
| 319 |
load_report_cond_.Wait(&mu_); |
| 320 |
} |
| 321 |
} |
| 322 |
ClientStats load_report = std::move(load_report_queue_.front()); |
| 323 |
load_report_queue_.pop_front(); |
| 324 |
return load_report; |
| 325 |
} |
| 326 |
|
| 327 |
void NotifyDoneWithServerlists() { |
| 328 |
grpc::internal::MutexLock lock(&mu_); |
| 329 |
if (!serverlist_done_) { |
| 330 |
serverlist_done_ = true; |
| 331 |
serverlist_cond_.SignalAll(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
std::vector<std::string> service_names() { |
| 336 |
grpc::internal::MutexLock lock(&mu_); |
| 337 |
return service_names_; |
| 338 |
} |
| 339 |
|
| 340 |
private: |
| 341 |
void SendResponse(Stream* stream, const LoadBalanceResponse& response, |
| 342 |
int delay_ms) { |
| 343 |
gpr_log(GPR_INFO, "LB[%p]: sleeping for %d ms...", this, delay_ms); |
| 344 |
if (delay_ms > 0) { |
| 345 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(delay_ms)); |
| 346 |
} |
| 347 |
gpr_log(GPR_INFO, "LB[%p]: Woke up! Sending response '%s'", this, |
| 348 |
response.DebugString().c_str()); |
| 349 |
IncreaseResponseCount(); |
| 350 |
stream->Write(response); |
| 351 |
} |
| 352 |
|
| 353 |
const int client_load_reporting_interval_seconds_; |
| 354 |
std::vector<ResponseDelayPair> responses_and_delays_; |
| 355 |
std::vector<std::string> service_names_; |
| 356 |
|
| 357 |
grpc::internal::Mutex mu_; |
| 358 |
grpc::internal::CondVar serverlist_cond_; |
| 359 |
bool serverlist_done_ ABSL_GUARDED_BY(mu_) = false; |
| 360 |
grpc::internal::CondVar load_report_cond_; |
| 361 |
std::deque<ClientStats> load_report_queue_ ABSL_GUARDED_BY(mu_); |
| 362 |
}; |
| 363 |
|
| 364 |
class GrpclbEnd2endTest : public ::testing::Test { |
| 365 |
protected: |
| 366 |
GrpclbEnd2endTest(size_t num_backends, size_t num_balancers, |
| 367 |
int client_load_reporting_interval_seconds) |
| 368 |
: server_host_("localhost"), |
| 369 |
num_backends_(num_backends), |
| 370 |
num_balancers_(num_balancers), |
| 371 |
client_load_reporting_interval_seconds_( |
| 372 |
client_load_reporting_interval_seconds) {} |
| 373 |
|
| 374 |
static void SetUpTestCase() { |
| 375 |
// Make the backup poller poll very frequently in order to pick up |
| 376 |
// updates from all the subchannels's FDs. |
| 377 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1); |
| 378 |
#if TARGET_OS_IPHONE |
| 379 |
// Workaround Apple CFStream bug |
| 380 |
grpc_core::SetEnv("grpc_cfstream", "0"); |
| 381 |
#endif |
| 382 |
grpc_init(); |
| 383 |
} |
| 384 |
|
| 385 |
static void TearDownTestCase() { grpc_shutdown(); } |
| 386 |
|
| 387 |
void SetUp() override { |
| 388 |
bool localhost_resolves_to_ipv4 = false; |
| 389 |
bool localhost_resolves_to_ipv6 = false; |
| 390 |
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4, |
| 391 |
&localhost_resolves_to_ipv6); |
| 392 |
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6; |
| 393 |
response_generator_ = |
| 394 |
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>(); |
| 395 |
// Start the backends. |
| 396 |
for (size_t i = 0; i < num_backends_; ++i) { |
| 397 |
backends_.emplace_back(new ServerThread<BackendServiceImpl>("backend")); |
| 398 |
backends_.back()->Start(server_host_); |
| 399 |
} |
| 400 |
// Start the load balancers. |
| 401 |
for (size_t i = 0; i < num_balancers_; ++i) { |
| 402 |
balancers_.emplace_back(new ServerThread<BalancerServiceImpl>( |
| 403 |
"balancer", client_load_reporting_interval_seconds_)); |
| 404 |
balancers_.back()->Start(server_host_); |
| 405 |
} |
| 406 |
ResetStub(); |
| 407 |
} |
| 408 |
|
| 409 |
void TearDown() override { |
| 410 |
ShutdownAllBackends(); |
| 411 |
for (auto& balancer : balancers_) balancer->Shutdown(); |
| 412 |
} |
| 413 |
|
| 414 |
void StartAllBackends() { |
| 415 |
for (auto& backend : backends_) backend->Start(server_host_); |
| 416 |
} |
| 417 |
|
| 418 |
void StartBackend(size_t index) { backends_[index]->Start(server_host_); } |
| 419 |
|
| 420 |
void ShutdownAllBackends() { |
| 421 |
for (auto& backend : backends_) backend->Shutdown(); |
| 422 |
} |
| 423 |
|
| 424 |
void ShutdownBackend(size_t index) { backends_[index]->Shutdown(); } |
| 425 |
|
| 426 |
void ResetStub(int fallback_timeout = 0, |
| 427 |
const std::string& expected_targets = "", |
| 428 |
int subchannel_cache_delay_ms = 0) { |
| 429 |
// Send a separate user agent string for the grpclb load balancer alone. |
| 430 |
grpc_core::ChannelArgs grpclb_channel_args; |
| 431 |
// Set a special user agent string for the grpclb load balancer. It |
| 432 |
// will be verified at the load balancer. |
| 433 |
grpclb_channel_args = grpclb_channel_args.Set( |
| 434 |
GRPC_ARG_PRIMARY_USER_AGENT_STRING, kGrpclbSpecificUserAgentString); |
| 435 |
ChannelArguments args; |
| 436 |
if (fallback_timeout > 0) args.SetGrpclbFallbackTimeout(fallback_timeout); |
| 437 |
args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR, |
| 438 |
response_generator_.get()); |
| 439 |
if (!expected_targets.empty()) { |
| 440 |
args.SetString(GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS, expected_targets); |
| 441 |
grpclb_channel_args = grpclb_channel_args.Set( |
| 442 |
GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS, expected_targets); |
| 443 |
} |
| 444 |
if (subchannel_cache_delay_ms > 0) { |
| 445 |
args.SetInt(GRPC_ARG_GRPCLB_SUBCHANNEL_CACHE_INTERVAL_MS, |
| 446 |
subchannel_cache_delay_ms * grpc_test_slowdown_factor()); |
| 447 |
} |
| 448 |
static const grpc_arg_pointer_vtable channel_args_vtable = { |
| 449 |
// copy |
| 450 |
[](void* p) -> void* { |
| 451 |
return grpc_channel_args_copy(static_cast<grpc_channel_args*>(p)); |
| 452 |
}, |
| 453 |
// destroy |
| 454 |
[](void* p) { |
| 455 |
grpc_channel_args_destroy(static_cast<grpc_channel_args*>(p)); |
| 456 |
}, |
| 457 |
// compare |
| 458 |
[](void* p1, void* p2) { |
| 459 |
return grpc_channel_args_compare(static_cast<grpc_channel_args*>(p1), |
| 460 |
static_cast<grpc_channel_args*>(p2)); |
| 461 |
}, |
| 462 |
}; |
| 463 |
// Specify channel args for the channel to the load balancer. |
| 464 |
args.SetPointerWithVtable( |
| 465 |
GRPC_ARG_EXPERIMENTAL_GRPCLB_CHANNEL_ARGS, |
| 466 |
const_cast<grpc_channel_args*>(grpclb_channel_args.ToC().get()), |
| 467 |
&channel_args_vtable); |
| 468 |
std::ostringstream uri; |
| 469 |
uri << "fake:///" << kApplicationTargetName_; |
| 470 |
// TODO(dgq): templatize tests to run everything using both secure and |
| 471 |
// insecure channel credentials. |
| 472 |
grpc_channel_credentials* channel_creds = |
| 473 |
grpc_fake_transport_security_credentials_create(); |
| 474 |
grpc_call_credentials* call_creds = grpc_md_only_test_credentials_create( |
| 475 |
g_kCallCredsMdKey, g_kCallCredsMdValue); |
| 476 |
std::shared_ptr<ChannelCredentials> creds( |
| 477 |
new SecureChannelCredentials(grpc_composite_channel_credentials_create( |
| 478 |
channel_creds, call_creds, nullptr))); |
| 479 |
call_creds->Unref(); |
| 480 |
channel_creds->Unref(); |
| 481 |
channel_ = grpc::CreateCustomChannel(uri.str(), creds, args); |
| 482 |
stub_ = grpc::testing::EchoTestService::NewStub(channel_); |
| 483 |
} |
| 484 |
|
| 485 |
void ResetBackendCounters() { |
| 486 |
for (auto& backend : backends_) backend->service_.ResetCounters(); |
| 487 |
} |
| 488 |
|
| 489 |
ClientStats WaitForLoadReports() { |
| 490 |
ClientStats client_stats; |
| 491 |
for (auto& balancer : balancers_) { |
| 492 |
client_stats += balancer->service_.WaitForLoadReport(); |
| 493 |
} |
| 494 |
return client_stats; |
| 495 |
} |
| 496 |
|
| 497 |
bool SeenAllBackends(size_t start_index = 0, size_t stop_index = 0) { |
| 498 |
if (stop_index == 0) stop_index = backends_.size(); |
| 499 |
for (size_t i = start_index; i < stop_index; ++i) { |
| 500 |
if (backends_[i]->service_.request_count() == 0) return false; |
| 501 |
} |
| 502 |
return true; |
| 503 |
} |
| 504 |
|
| 505 |
void SendRpcAndCount(int* num_total, int* num_ok, int* num_failure, |
| 506 |
int* num_drops) { |
| 507 |
const Status status = SendRpc(); |
| 508 |
if (status.ok()) { |
| 509 |
++*num_ok; |
| 510 |
} else { |
| 511 |
if (status.error_message() == "drop directed by grpclb balancer") { |
| 512 |
++*num_drops; |
| 513 |
} else { |
| 514 |
++*num_failure; |
| 515 |
} |
| 516 |
} |
| 517 |
++*num_total; |
| 518 |
} |
| 519 |
|
| 520 |
std::tuple<int, int, int> WaitForAllBackends(int num_requests_multiple_of = 1, |
| 521 |
size_t start_index = 0, |
| 522 |
size_t stop_index = 0) { |
| 523 |
int num_ok = 0; |
| 524 |
int num_failure = 0; |
| 525 |
int num_drops = 0; |
| 526 |
int num_total = 0; |
| 527 |
while (!SeenAllBackends(start_index, stop_index)) { |
| 528 |
SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops); |
| 529 |
} |
| 530 |
while (num_total % num_requests_multiple_of != 0) { |
| 531 |
SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops); |
| 532 |
} |
| 533 |
ResetBackendCounters(); |
| 534 |
gpr_log(GPR_INFO, |
| 535 |
"Performed %d warm up requests (a multiple of %d) against the " |
| 536 |
"backends. %d succeeded, %d failed, %d dropped.", |
| 537 |
num_total, num_requests_multiple_of, num_ok, num_failure, |
| 538 |
num_drops); |
| 539 |
return std::make_tuple(num_ok, num_failure, num_drops); |
| 540 |
} |
| 541 |
|
| 542 |
void WaitForBackend(size_t backend_idx) { |
| 543 |
do { |
| 544 |
(void)SendRpc(); |
| 545 |
} while (backends_[backend_idx]->service_.request_count() == 0); |
| 546 |
ResetBackendCounters(); |
| 547 |
} |
| 548 |
|
| 549 |
struct AddressData { |
| 550 |
int port; |
| 551 |
std::string balancer_name; |
| 552 |
}; |
| 553 |
|
| 554 |
grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList( |
| 555 |
const std::vector<AddressData>& address_data) { |
| 556 |
grpc_core::ServerAddressList addresses; |
| 557 |
for (const auto& addr : address_data) { |
| 558 |
absl::StatusOr<grpc_core::URI> lb_uri = |
| 559 |
grpc_core::URI::Parse(absl::StrCat( |
| 560 |
ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", addr.port)); |
| 561 |
GPR_ASSERT(lb_uri.ok()); |
| 562 |
grpc_resolved_address address; |
| 563 |
GPR_ASSERT(grpc_parse_uri(*lb_uri, &address)); |
| 564 |
addresses.emplace_back( |
| 565 |
address.addr, address.len, |
| 566 |
grpc_core::ChannelArgs().Set(GRPC_ARG_DEFAULT_AUTHORITY, |
| 567 |
addr.balancer_name)); |
| 568 |
} |
| 569 |
return addresses; |
| 570 |
} |
| 571 |
|
| 572 |
grpc_core::Resolver::Result MakeResolverResult( |
| 573 |
const std::vector<AddressData>& balancer_address_data, |
| 574 |
const std::vector<AddressData>& backend_address_data = {}, |
| 575 |
const char* service_config_json = kDefaultServiceConfig) { |
| 576 |
grpc_core::Resolver::Result result; |
| 577 |
result.addresses = |
| 578 |
CreateLbAddressesFromAddressDataList(backend_address_data); |
| 579 |
result.service_config = grpc_core::ServiceConfigImpl::Create( |
| 580 |
grpc_core::ChannelArgs(), service_config_json); |
| 581 |
GPR_ASSERT(result.service_config.ok()); |
| 582 |
grpc_core::ServerAddressList balancer_addresses = |
| 583 |
CreateLbAddressesFromAddressDataList(balancer_address_data); |
| 584 |
result.args = grpc_core::SetGrpcLbBalancerAddresses( |
| 585 |
grpc_core::ChannelArgs(), std::move(balancer_addresses)); |
| 586 |
return result; |
| 587 |
} |
| 588 |
|
| 589 |
void SetNextResolutionAllBalancers( |
| 590 |
const char* service_config_json = kDefaultServiceConfig) { |
| 591 |
std::vector<AddressData> addresses; |
| 592 |
for (size_t i = 0; i < balancers_.size(); ++i) { |
| 593 |
addresses.emplace_back(AddressData{balancers_[i]->port_, ""}); |
| 594 |
} |
| 595 |
SetNextResolution(addresses, {}, service_config_json); |
| 596 |
} |
| 597 |
|
| 598 |
void SetNextResolution( |
| 599 |
const std::vector<AddressData>& balancer_address_data, |
| 600 |
const std::vector<AddressData>& backend_address_data = {}, |
| 601 |
const char* service_config_json = kDefaultServiceConfig) { |
| 602 |
grpc_core::ExecCtx exec_ctx; |
| 603 |
grpc_core::Resolver::Result result = MakeResolverResult( |
| 604 |
balancer_address_data, backend_address_data, service_config_json); |
| 605 |
response_generator_->SetResponse(std::move(result)); |
| 606 |
} |
| 607 |
|
| 608 |
void SetNextReresolutionResponse( |
| 609 |
const std::vector<AddressData>& balancer_address_data, |
| 610 |
const std::vector<AddressData>& backend_address_data = {}, |
| 611 |
const char* service_config_json = kDefaultServiceConfig) { |
| 612 |
grpc_core::ExecCtx exec_ctx; |
| 613 |
grpc_core::Resolver::Result result = MakeResolverResult( |
| 614 |
balancer_address_data, backend_address_data, service_config_json); |
| 615 |
response_generator_->SetReresolutionResponse(std::move(result)); |
| 616 |
} |
| 617 |
|
| 618 |
std::vector<int> GetBackendPorts(size_t start_index = 0, |
| 619 |
size_t stop_index = 0) const { |
| 620 |
if (stop_index == 0) stop_index = backends_.size(); |
| 621 |
std::vector<int> backend_ports; |
| 622 |
for (size_t i = start_index; i < stop_index; ++i) { |
| 623 |
backend_ports.push_back(backends_[i]->port_); |
| 624 |
} |
| 625 |
return backend_ports; |
| 626 |
} |
| 627 |
|
| 628 |
void ScheduleResponseForBalancer(size_t i, |
| 629 |
const LoadBalanceResponse& response, |
| 630 |
int delay_ms) { |
| 631 |
balancers_[i]->service_.add_response(response, delay_ms); |
| 632 |
} |
| 633 |
|
| 634 |
LoadBalanceResponse BuildResponseForBackends( |
| 635 |
const std::vector<int>& backend_ports, |
| 636 |
const std::map<std::string, size_t>& drop_token_counts) { |
| 637 |
LoadBalanceResponse response; |
| 638 |
for (const auto& drop_token_count : drop_token_counts) { |
| 639 |
for (size_t i = 0; i < drop_token_count.second; ++i) { |
| 640 |
auto* server = response.mutable_server_list()->add_servers(); |
| 641 |
server->set_drop(true); |
| 642 |
server->set_load_balance_token(drop_token_count.first); |
| 643 |
} |
| 644 |
} |
| 645 |
for (const int& backend_port : backend_ports) { |
| 646 |
auto* server = response.mutable_server_list()->add_servers(); |
| 647 |
server->set_ip_address(ipv6_only_ ? Ip6ToPackedString("::1") |
| 648 |
: Ip4ToPackedString("127.0.0.1")); |
| 649 |
server->set_port(backend_port); |
| 650 |
static int token_count = 0; |
| 651 |
server->set_load_balance_token( |
| 652 |
absl::StrFormat("token%03d", ++token_count)); |
| 653 |
} |
| 654 |
return response; |
| 655 |
} |
| 656 |
|
| 657 |
Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000, |
| 658 |
bool wait_for_ready = false, |
| 659 |
const Status& expected_status = Status::OK) { |
| 660 |
const bool local_response = (response == nullptr); |
| 661 |
if (local_response) response = new EchoResponse; |
| 662 |
EchoRequest request; |
| 663 |
request.set_message(kRequestMessage_); |
| 664 |
if (!expected_status.ok()) { |
| 665 |
auto* error = request.mutable_param()->mutable_expected_error(); |
| 666 |
error->set_code(expected_status.error_code()); |
| 667 |
error->set_error_message(expected_status.error_message()); |
| 668 |
} |
| 669 |
ClientContext context; |
| 670 |
context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms)); |
| 671 |
if (wait_for_ready) context.set_wait_for_ready(true); |
| 672 |
Status status = stub_->Echo(&context, request, response); |
| 673 |
if (local_response) delete response; |
| 674 |
return status; |
| 675 |
} |
| 676 |
|
| 677 |
void CheckRpcSendOk(const size_t times = 1, const int timeout_ms = 1000, |
| 678 |
bool wait_for_ready = false) { |
| 679 |
for (size_t i = 0; i < times; ++i) { |
| 680 |
EchoResponse response; |
| 681 |
const Status status = SendRpc(&response, timeout_ms, wait_for_ready); |
| 682 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 683 |
<< " message=" << status.error_message(); |
| 684 |
EXPECT_EQ(response.message(), kRequestMessage_); |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
void CheckRpcSendFailure() { |
| 689 |
const Status status = SendRpc(); |
| 690 |
EXPECT_FALSE(status.ok()); |
| 691 |
} |
| 692 |
|
| 693 |
template <typename T> |
| 694 |
struct ServerThread { |
| 695 |
template <typename... Args> |
| 696 |
explicit ServerThread(const std::string& type, Args&&... args) |
| 697 |
: port_(grpc_pick_unused_port_or_die()), |
| 698 |
type_(type), |
| 699 |
service_(std::forward<Args>(args)...) {} |
| 700 |
|
| 701 |
void Start(const std::string& server_host) { |
| 702 |
gpr_log(GPR_INFO, "starting %s server on port %d", type_.c_str(), port_); |
| 703 |
GPR_ASSERT(!running_); |
| 704 |
running_ = true; |
| 705 |
service_.Start(); |
| 706 |
grpc::internal::Mutex mu; |
| 707 |
// We need to acquire the lock here in order to prevent the notify_one |
| 708 |
// by ServerThread::Serve from firing before the wait below is hit. |
| 709 |
grpc::internal::MutexLock lock(&mu); |
| 710 |
grpc::internal::CondVar cond; |
| 711 |
thread_ = std::make_unique<std::thread>( |
| 712 |
std::bind(&ServerThread::Serve, this, server_host, &mu, &cond)); |
| 713 |
cond.Wait(&mu); |
| 714 |
gpr_log(GPR_INFO, "%s server startup complete", type_.c_str()); |
| 715 |
} |
| 716 |
|
| 717 |
void Serve(const std::string& server_host, grpc::internal::Mutex* mu, |
| 718 |
grpc::internal::CondVar* cond) { |
| 719 |
// We need to acquire the lock here in order to prevent the notify_one |
| 720 |
// below from firing before its corresponding wait is executed. |
| 721 |
grpc::internal::MutexLock lock(mu); |
| 722 |
std::ostringstream server_address; |
| 723 |
server_address << server_host << ":" << port_; |
| 724 |
ServerBuilder builder; |
| 725 |
std::shared_ptr<ServerCredentials> creds(new SecureServerCredentials( |
| 726 |
grpc_fake_transport_security_server_credentials_create())); |
| 727 |
builder.AddListeningPort(server_address.str(), creds); |
| 728 |
builder.RegisterService(&service_); |
| 729 |
server_ = builder.BuildAndStart(); |
| 730 |
cond->Signal(); |
| 731 |
} |
| 732 |
|
| 733 |
void Shutdown() { |
| 734 |
if (!running_) return; |
| 735 |
gpr_log(GPR_INFO, "%s about to shutdown", type_.c_str()); |
| 736 |
service_.Shutdown(); |
| 737 |
server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0)); |
| 738 |
thread_->join(); |
| 739 |
gpr_log(GPR_INFO, "%s shutdown completed", type_.c_str()); |
| 740 |
running_ = false; |
| 741 |
} |
| 742 |
|
| 743 |
const int port_; |
| 744 |
std::string type_; |
| 745 |
T service_; |
| 746 |
std::unique_ptr<Server> server_; |
| 747 |
std::unique_ptr<std::thread> thread_; |
| 748 |
bool running_ = false; |
| 749 |
}; |
| 750 |
|
| 751 |
const std::string server_host_; |
| 752 |
const size_t num_backends_; |
| 753 |
const size_t num_balancers_; |
| 754 |
const int client_load_reporting_interval_seconds_; |
| 755 |
bool ipv6_only_ = false; |
| 756 |
std::shared_ptr<Channel> channel_; |
| 757 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 758 |
std::vector<std::unique_ptr<ServerThread<BackendServiceImpl>>> backends_; |
| 759 |
std::vector<std::unique_ptr<ServerThread<BalancerServiceImpl>>> balancers_; |
| 760 |
grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator> |
| 761 |
response_generator_; |
| 762 |
const std::string kRequestMessage_ = "Live long and prosper."; |
| 763 |
const std::string kApplicationTargetName_ = "application_target_name"; |
| 764 |
}; |
| 765 |
|
| 766 |
class SingleBalancerTest : public GrpclbEnd2endTest { |
| 767 |
public: |
| 768 |
SingleBalancerTest() : GrpclbEnd2endTest(4, 1, 0) {} |
| 769 |
}; |
| 770 |
|
| 771 |
TEST_F(SingleBalancerTest, Vanilla) { |
| 772 |
SetNextResolutionAllBalancers(); |
| 773 |
const size_t kNumRpcsPerAddress = 100; |
| 774 |
ScheduleResponseForBalancer( |
| 775 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 776 |
// Make sure that trying to connect works without a call. |
| 777 |
channel_->GetState(true /* try_to_connect */); |
| 778 |
// We need to wait for all backends to come online. |
| 779 |
WaitForAllBackends(); |
| 780 |
// Send kNumRpcsPerAddress RPCs per server. |
| 781 |
CheckRpcSendOk(kNumRpcsPerAddress * num_backends_); |
| 782 |
|
| 783 |
// Each backend should have gotten 100 requests. |
| 784 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 785 |
EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count()); |
| 786 |
} |
| 787 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 788 |
// The balancer got a single request. |
| 789 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 790 |
// and sent a single response. |
| 791 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 792 |
|
| 793 |
// Check LB policy name for the channel. |
| 794 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 795 |
} |
| 796 |
|
| 797 |
TEST_F(SingleBalancerTest, SubchannelCaching) { |
| 798 |
ResetStub(/*fallback_timeout=*/0, /*expected_targets=*/"", |
| 799 |
/*subchannel_cache_delay_ms=*/1500); |
| 800 |
SetNextResolutionAllBalancers(); |
| 801 |
// Initially send all backends. |
| 802 |
ScheduleResponseForBalancer( |
| 803 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 804 |
// Then remove backends 0 and 1. |
| 805 |
ScheduleResponseForBalancer( |
| 806 |
0, BuildResponseForBackends(GetBackendPorts(2), {}), 1000); |
| 807 |
// Now re-add backend 1. |
| 808 |
ScheduleResponseForBalancer( |
| 809 |
0, BuildResponseForBackends(GetBackendPorts(1), {}), 1000); |
| 810 |
// Wait for all backends to come online. |
| 811 |
WaitForAllBackends(); |
| 812 |
// Send RPCs for long enough to get all responses. |
| 813 |
gpr_timespec deadline = grpc_timeout_milliseconds_to_deadline(3000); |
| 814 |
do { |
| 815 |
CheckRpcSendOk(); |
| 816 |
} while (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) < 0); |
| 817 |
// Backend 0 should have received less traffic than the others. |
| 818 |
// Backend 1 would have received less traffic than 2 and 3. |
| 819 |
gpr_log(GPR_INFO, "BACKEND 0: %" PRIuPTR " requests", |
| 820 |
backends_[0]->service_.request_count()); |
| 821 |
EXPECT_GT(backends_[0]->service_.request_count(), 0); |
| 822 |
for (size_t i = 1; i < backends_.size(); ++i) { |
| 823 |
gpr_log(GPR_INFO, "BACKEND %" PRIuPTR ": %" PRIuPTR " requests", i, |
| 824 |
backends_[i]->service_.request_count()); |
| 825 |
EXPECT_GT(backends_[i]->service_.request_count(), |
| 826 |
backends_[0]->service_.request_count()) |
| 827 |
<< "backend " << i; |
| 828 |
if (i >= 2) { |
| 829 |
EXPECT_GT(backends_[i]->service_.request_count(), |
| 830 |
backends_[1]->service_.request_count()) |
| 831 |
<< "backend " << i; |
| 832 |
} |
| 833 |
} |
| 834 |
// Backend 1 should never have lost its connection from the client. |
| 835 |
EXPECT_EQ(1UL, backends_[1]->service_.clients().size()); |
| 836 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 837 |
// The balancer got a single request. |
| 838 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 839 |
// And sent 3 responses. |
| 840 |
EXPECT_EQ(3U, balancers_[0]->service_.response_count()); |
| 841 |
} |
| 842 |
|
| 843 |
TEST_F(SingleBalancerTest, ReturnServerStatus) { |
| 844 |
SetNextResolutionAllBalancers(); |
| 845 |
ScheduleResponseForBalancer( |
| 846 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 847 |
// We need to wait for all backends to come online. |
| 848 |
WaitForAllBackends(); |
| 849 |
// Send a request that the backend will fail, and make sure we get |
| 850 |
// back the right status. |
| 851 |
Status expected(StatusCode::INVALID_ARGUMENT, "He's dead, Jim!"); |
| 852 |
Status actual = SendRpc(/*response=*/nullptr, /*timeout_ms=*/1000, |
| 853 |
/*wait_for_ready=*/false, expected); |
| 854 |
EXPECT_EQ(actual.error_code(), expected.error_code()); |
| 855 |
EXPECT_EQ(actual.error_message(), expected.error_message()); |
| 856 |
} |
| 857 |
|
| 858 |
TEST_F(SingleBalancerTest, SelectGrpclbWithMigrationServiceConfig) { |
| 859 |
SetNextResolutionAllBalancers( |
| 860 |
"{\n" |
| 861 |
" \"loadBalancingConfig\":[\n" |
| 862 |
" { \"does_not_exist\":{} },\n" |
| 863 |
" { \"grpclb\":{} }\n" |
| 864 |
" ]\n" |
| 865 |
"}"); |
| 866 |
ScheduleResponseForBalancer( |
| 867 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 868 |
CheckRpcSendOk(1, 1000 /* timeout_ms */, true /* wait_for_ready */); |
| 869 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 870 |
// The balancer got a single request. |
| 871 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 872 |
// and sent a single response. |
| 873 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 874 |
// Check LB policy name for the channel. |
| 875 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 876 |
} |
| 877 |
|
| 878 |
TEST_F(SingleBalancerTest, |
| 879 |
SelectGrpclbWithMigrationServiceConfigAndNoAddresses) { |
| 880 |
const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor(); |
| 881 |
ResetStub(kFallbackTimeoutMs); |
| 882 |
SetNextResolution({}, {}, |
| 883 |
"{\n" |
| 884 |
" \"loadBalancingConfig\":[\n" |
| 885 |
" { \"does_not_exist\":{} },\n" |
| 886 |
" { \"grpclb\":{} }\n" |
| 887 |
" ]\n" |
| 888 |
"}"); |
| 889 |
// Try to connect. |
| 890 |
EXPECT_EQ(GRPC_CHANNEL_IDLE, channel_->GetState(true)); |
| 891 |
// Should go into state TRANSIENT_FAILURE when we enter fallback mode. |
| 892 |
const gpr_timespec deadline = grpc_timeout_seconds_to_deadline(1); |
| 893 |
grpc_connectivity_state state; |
| 894 |
while ((state = channel_->GetState(false)) != |
| 895 |
GRPC_CHANNEL_TRANSIENT_FAILURE) { |
| 896 |
ASSERT_TRUE(channel_->WaitForStateChange(state, deadline)); |
| 897 |
} |
| 898 |
// Check LB policy name for the channel. |
| 899 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 900 |
} |
| 901 |
|
| 902 |
TEST_F(SingleBalancerTest, UsePickFirstChildPolicy) { |
| 903 |
SetNextResolutionAllBalancers( |
| 904 |
"{\n" |
| 905 |
" \"loadBalancingConfig\":[\n" |
| 906 |
" { \"grpclb\":{\n" |
| 907 |
" \"childPolicy\":[\n" |
| 908 |
" { \"pick_first\":{} }\n" |
| 909 |
" ]\n" |
| 910 |
" } }\n" |
| 911 |
" ]\n" |
| 912 |
"}"); |
| 913 |
ScheduleResponseForBalancer( |
| 914 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 915 |
const size_t kNumRpcs = num_backends_ * 2; |
| 916 |
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */); |
| 917 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 918 |
// Check that all requests went to the first backend. This verifies |
| 919 |
// that we used pick_first instead of round_robin as the child policy. |
| 920 |
EXPECT_EQ(backends_[0]->service_.request_count(), kNumRpcs); |
| 921 |
for (size_t i = 1; i < backends_.size(); ++i) { |
| 922 |
EXPECT_EQ(backends_[i]->service_.request_count(), 0UL); |
| 923 |
} |
| 924 |
// The balancer got a single request. |
| 925 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 926 |
// and sent a single response. |
| 927 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 928 |
// Check LB policy name for the channel. |
| 929 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 930 |
} |
| 931 |
|
| 932 |
TEST_F(SingleBalancerTest, SwapChildPolicy) { |
| 933 |
SetNextResolutionAllBalancers( |
| 934 |
"{\n" |
| 935 |
" \"loadBalancingConfig\":[\n" |
| 936 |
" { \"grpclb\":{\n" |
| 937 |
" \"childPolicy\":[\n" |
| 938 |
" { \"pick_first\":{} }\n" |
| 939 |
" ]\n" |
| 940 |
" } }\n" |
| 941 |
" ]\n" |
| 942 |
"}"); |
| 943 |
ScheduleResponseForBalancer( |
| 944 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 945 |
const size_t kNumRpcs = num_backends_ * 2; |
| 946 |
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */); |
| 947 |
// Check that all requests went to the first backend. This verifies |
| 948 |
// that we used pick_first instead of round_robin as the child policy. |
| 949 |
EXPECT_EQ(backends_[0]->service_.request_count(), kNumRpcs); |
| 950 |
for (size_t i = 1; i < backends_.size(); ++i) { |
| 951 |
EXPECT_EQ(backends_[i]->service_.request_count(), 0UL); |
| 952 |
} |
| 953 |
// Send new resolution that removes child policy from service config. |
| 954 |
SetNextResolutionAllBalancers(); |
| 955 |
WaitForAllBackends(); |
| 956 |
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */); |
| 957 |
// Check that every backend saw the same number of requests. This verifies |
| 958 |
// that we used round_robin. |
| 959 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 960 |
EXPECT_EQ(backends_[i]->service_.request_count(), 2UL); |
| 961 |
} |
| 962 |
// Done. |
| 963 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 964 |
// The balancer got a single request. |
| 965 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 966 |
// and sent a single response. |
| 967 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 968 |
// Check LB policy name for the channel. |
| 969 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 970 |
} |
| 971 |
|
| 972 |
TEST_F(SingleBalancerTest, SameBackendListedMultipleTimes) { |
| 973 |
SetNextResolutionAllBalancers(); |
| 974 |
// Same backend listed twice. |
| 975 |
std::vector<int> ports; |
| 976 |
ports.push_back(backends_[0]->port_); |
| 977 |
ports.push_back(backends_[0]->port_); |
| 978 |
const size_t kNumRpcsPerAddress = 10; |
| 979 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0); |
| 980 |
// We need to wait for the backend to come online. |
| 981 |
WaitForBackend(0); |
| 982 |
// Send kNumRpcsPerAddress RPCs per server. |
| 983 |
CheckRpcSendOk(kNumRpcsPerAddress * ports.size()); |
| 984 |
// Backend should have gotten 20 requests. |
| 985 |
EXPECT_EQ(kNumRpcsPerAddress * 2, backends_[0]->service_.request_count()); |
| 986 |
// And they should have come from a single client port, because of |
| 987 |
// subchannel sharing. |
| 988 |
EXPECT_EQ(1UL, backends_[0]->service_.clients().size()); |
| 989 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 990 |
} |
| 991 |
|
| 992 |
TEST_F(SingleBalancerTest, SecureNaming) { |
| 993 |
ResetStub(0, kApplicationTargetName_ + ";lb"); |
| 994 |
SetNextResolution({AddressData{balancers_[0]->port_, "lb"}}); |
| 995 |
const size_t kNumRpcsPerAddress = 100; |
| 996 |
ScheduleResponseForBalancer( |
| 997 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 998 |
// Make sure that trying to connect works without a call. |
| 999 |
channel_->GetState(true /* try_to_connect */); |
| 1000 |
// We need to wait for all backends to come online. |
| 1001 |
WaitForAllBackends(); |
| 1002 |
// Send kNumRpcsPerAddress RPCs per server. |
| 1003 |
CheckRpcSendOk(kNumRpcsPerAddress * num_backends_); |
| 1004 |
|
| 1005 |
// Each backend should have gotten 100 requests. |
| 1006 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 1007 |
EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count()); |
| 1008 |
} |
| 1009 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1010 |
// The balancer got a single request. |
| 1011 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1012 |
// and sent a single response. |
| 1013 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1014 |
// Check LB policy name for the channel. |
| 1015 |
EXPECT_EQ("grpclb", channel_->GetLoadBalancingPolicyName()); |
| 1016 |
} |
| 1017 |
|
| 1018 |
TEST_F(SingleBalancerTest, SecureNamingDeathTest) { |
| 1019 |
GTEST_FLAG_SET(death_test_style, "threadsafe"); |
| 1020 |
// Make sure that we blow up (via abort() from the security connector) when |
| 1021 |
// the name from the balancer doesn't match expectations. |
| 1022 |
ASSERT_DEATH_IF_SUPPORTED( |
| 1023 |
{ |
| 1024 |
ResetStub(0, kApplicationTargetName_ + ";lb"); |
| 1025 |
SetNextResolution({AddressData{balancers_[0]->port_, "woops"}}); |
| 1026 |
channel_->WaitForConnected(grpc_timeout_seconds_to_deadline(1)); |
| 1027 |
}, |
| 1028 |
""); |
| 1029 |
} |
| 1030 |
|
| 1031 |
TEST_F(SingleBalancerTest, InitiallyEmptyServerlist) { |
| 1032 |
SetNextResolutionAllBalancers(); |
| 1033 |
const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor(); |
| 1034 |
const int kCallDeadlineMs = kServerlistDelayMs * 10; |
| 1035 |
// First response is an empty serverlist, sent right away. |
| 1036 |
ScheduleResponseForBalancer(0, LoadBalanceResponse(), 0); |
| 1037 |
// Send non-empty serverlist only after kServerlistDelayMs |
| 1038 |
ScheduleResponseForBalancer( |
| 1039 |
0, BuildResponseForBackends(GetBackendPorts(), {}), kServerlistDelayMs); |
| 1040 |
const auto t0 = system_clock::now(); |
| 1041 |
// Client will block: LB will initially send empty serverlist. |
| 1042 |
CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */); |
| 1043 |
const auto ellapsed_ms = |
| 1044 |
std::chrono::duration_cast<std::chrono::milliseconds>( |
| 1045 |
system_clock::now() - t0); |
| 1046 |
// but eventually, the LB sends a serverlist update that allows the call to |
| 1047 |
// proceed. The call delay must be larger than the delay in sending the |
| 1048 |
// populated serverlist but under the call's deadline (which is enforced by |
| 1049 |
// the call's deadline). |
| 1050 |
EXPECT_GT(ellapsed_ms.count(), kServerlistDelayMs); |
| 1051 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1052 |
// The balancer got a single request. |
| 1053 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1054 |
// and sent two responses. |
| 1055 |
EXPECT_EQ(2U, balancers_[0]->service_.response_count()); |
| 1056 |
} |
| 1057 |
|
| 1058 |
TEST_F(SingleBalancerTest, AllServersUnreachableFailFast) { |
| 1059 |
SetNextResolutionAllBalancers(); |
| 1060 |
const size_t kNumUnreachableServers = 5; |
| 1061 |
std::vector<int> ports; |
| 1062 |
for (size_t i = 0; i < kNumUnreachableServers; ++i) { |
| 1063 |
ports.push_back(grpc_pick_unused_port_or_die()); |
| 1064 |
} |
| 1065 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0); |
| 1066 |
const Status status = SendRpc(); |
| 1067 |
// The error shouldn't be DEADLINE_EXCEEDED. |
| 1068 |
EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code()); |
| 1069 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1070 |
// The balancer got a single request. |
| 1071 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1072 |
// and sent a single response. |
| 1073 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1074 |
} |
| 1075 |
|
| 1076 |
TEST_F(SingleBalancerTest, Fallback) { |
| 1077 |
SetNextResolutionAllBalancers(); |
| 1078 |
const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor(); |
| 1079 |
const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor(); |
| 1080 |
const size_t kNumBackendsInResolution = backends_.size() / 2; |
| 1081 |
|
| 1082 |
ResetStub(kFallbackTimeoutMs); |
| 1083 |
std::vector<AddressData> balancer_addresses; |
| 1084 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1085 |
std::vector<AddressData> backend_addresses; |
| 1086 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1087 |
backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""}); |
| 1088 |
} |
| 1089 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1090 |
|
| 1091 |
// Send non-empty serverlist only after kServerlistDelayMs. |
| 1092 |
ScheduleResponseForBalancer( |
| 1093 |
0, |
| 1094 |
BuildResponseForBackends( |
| 1095 |
GetBackendPorts(kNumBackendsInResolution /* start_index */), {}), |
| 1096 |
kServerlistDelayMs); |
| 1097 |
|
| 1098 |
// Wait until all the fallback backends are reachable. |
| 1099 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1100 |
WaitForBackend(i); |
| 1101 |
} |
| 1102 |
|
| 1103 |
// The first request. |
| 1104 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1105 |
CheckRpcSendOk(kNumBackendsInResolution); |
| 1106 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1107 |
|
| 1108 |
// Fallback is used: each backend returned by the resolver should have |
| 1109 |
// gotten one request. |
| 1110 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1111 |
EXPECT_EQ(1U, backends_[i]->service_.request_count()); |
| 1112 |
} |
| 1113 |
for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) { |
| 1114 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1115 |
} |
| 1116 |
|
| 1117 |
// Wait until the serverlist reception has been processed and all backends |
| 1118 |
// in the serverlist are reachable. |
| 1119 |
for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) { |
| 1120 |
WaitForBackend(i); |
| 1121 |
} |
| 1122 |
|
| 1123 |
// Send out the second request. |
| 1124 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 1125 |
CheckRpcSendOk(backends_.size() - kNumBackendsInResolution); |
| 1126 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 1127 |
|
| 1128 |
// Serverlist is used: each backend returned by the balancer should |
| 1129 |
// have gotten one request. |
| 1130 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1131 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1132 |
} |
| 1133 |
for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) { |
| 1134 |
EXPECT_EQ(1U, backends_[i]->service_.request_count()); |
| 1135 |
} |
| 1136 |
|
| 1137 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1138 |
// The balancer got a single request. |
| 1139 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1140 |
// and sent a single response. |
| 1141 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1142 |
} |
| 1143 |
|
| 1144 |
TEST_F(SingleBalancerTest, FallbackUpdate) { |
| 1145 |
SetNextResolutionAllBalancers(); |
| 1146 |
const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor(); |
| 1147 |
const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor(); |
| 1148 |
const size_t kNumBackendsInResolution = backends_.size() / 3; |
| 1149 |
const size_t kNumBackendsInResolutionUpdate = backends_.size() / 3; |
| 1150 |
|
| 1151 |
ResetStub(kFallbackTimeoutMs); |
| 1152 |
std::vector<AddressData> balancer_addresses; |
| 1153 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1154 |
std::vector<AddressData> backend_addresses; |
| 1155 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1156 |
backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""}); |
| 1157 |
} |
| 1158 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1159 |
|
| 1160 |
// Send non-empty serverlist only after kServerlistDelayMs. |
| 1161 |
ScheduleResponseForBalancer( |
| 1162 |
0, |
| 1163 |
BuildResponseForBackends( |
| 1164 |
GetBackendPorts(kNumBackendsInResolution + |
| 1165 |
kNumBackendsInResolutionUpdate /* start_index */), |
| 1166 |
{}), |
| 1167 |
kServerlistDelayMs); |
| 1168 |
|
| 1169 |
// Wait until all the fallback backends are reachable. |
| 1170 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1171 |
WaitForBackend(i); |
| 1172 |
} |
| 1173 |
|
| 1174 |
// The first request. |
| 1175 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1176 |
CheckRpcSendOk(kNumBackendsInResolution); |
| 1177 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1178 |
|
| 1179 |
// Fallback is used: each backend returned by the resolver should have |
| 1180 |
// gotten one request. |
| 1181 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1182 |
EXPECT_EQ(1U, backends_[i]->service_.request_count()); |
| 1183 |
} |
| 1184 |
for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) { |
| 1185 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1186 |
} |
| 1187 |
|
| 1188 |
balancer_addresses.clear(); |
| 1189 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1190 |
backend_addresses.clear(); |
| 1191 |
for (size_t i = kNumBackendsInResolution; |
| 1192 |
i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) { |
| 1193 |
backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""}); |
| 1194 |
} |
| 1195 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1196 |
|
| 1197 |
// Wait until the resolution update has been processed and all the new |
| 1198 |
// fallback backends are reachable. |
| 1199 |
for (size_t i = kNumBackendsInResolution; |
| 1200 |
i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) { |
| 1201 |
WaitForBackend(i); |
| 1202 |
} |
| 1203 |
|
| 1204 |
// Send out the second request. |
| 1205 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 1206 |
CheckRpcSendOk(kNumBackendsInResolutionUpdate); |
| 1207 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 1208 |
|
| 1209 |
// The resolution update is used: each backend in the resolution update should |
| 1210 |
// have gotten one request. |
| 1211 |
for (size_t i = 0; i < kNumBackendsInResolution; ++i) { |
| 1212 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1213 |
} |
| 1214 |
for (size_t i = kNumBackendsInResolution; |
| 1215 |
i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) { |
| 1216 |
EXPECT_EQ(1U, backends_[i]->service_.request_count()); |
| 1217 |
} |
| 1218 |
for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate; |
| 1219 |
i < backends_.size(); ++i) { |
| 1220 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1221 |
} |
| 1222 |
|
| 1223 |
// Wait until the serverlist reception has been processed and all backends |
| 1224 |
// in the serverlist are reachable. |
| 1225 |
for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate; |
| 1226 |
i < backends_.size(); ++i) { |
| 1227 |
WaitForBackend(i); |
| 1228 |
} |
| 1229 |
|
| 1230 |
// Send out the third request. |
| 1231 |
gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH =========="); |
| 1232 |
CheckRpcSendOk(backends_.size() - kNumBackendsInResolution - |
| 1233 |
kNumBackendsInResolutionUpdate); |
| 1234 |
gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH =========="); |
| 1235 |
|
| 1236 |
// Serverlist is used: each backend returned by the balancer should |
| 1237 |
// have gotten one request. |
| 1238 |
for (size_t i = 0; |
| 1239 |
i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) { |
| 1240 |
EXPECT_EQ(0U, backends_[i]->service_.request_count()); |
| 1241 |
} |
| 1242 |
for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate; |
| 1243 |
i < backends_.size(); ++i) { |
| 1244 |
EXPECT_EQ(1U, backends_[i]->service_.request_count()); |
| 1245 |
} |
| 1246 |
|
| 1247 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1248 |
// The balancer got a single request. |
| 1249 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1250 |
// and sent a single response. |
| 1251 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1252 |
} |
| 1253 |
|
| 1254 |
TEST_F(SingleBalancerTest, |
| 1255 |
FallbackAfterStartup_LoseContactWithBalancerThenBackends) { |
| 1256 |
// First two backends are fallback, last two are pointed to by balancer. |
| 1257 |
const size_t kNumFallbackBackends = 2; |
| 1258 |
const size_t kNumBalancerBackends = backends_.size() - kNumFallbackBackends; |
| 1259 |
std::vector<AddressData> backend_addresses; |
| 1260 |
for (size_t i = 0; i < kNumFallbackBackends; ++i) { |
| 1261 |
backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""}); |
| 1262 |
} |
| 1263 |
std::vector<AddressData> balancer_addresses; |
| 1264 |
for (size_t i = 0; i < balancers_.size(); ++i) { |
| 1265 |
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""}); |
| 1266 |
} |
| 1267 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1268 |
ScheduleResponseForBalancer( |
| 1269 |
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}), |
| 1270 |
0); |
| 1271 |
// Try to connect. |
| 1272 |
channel_->GetState(true /* try_to_connect */); |
| 1273 |
WaitForAllBackends(1 /* num_requests_multiple_of */, |
| 1274 |
kNumFallbackBackends /* start_index */); |
| 1275 |
// Stop balancer. RPCs should continue going to backends from balancer. |
| 1276 |
balancers_[0]->Shutdown(); |
| 1277 |
CheckRpcSendOk(100 * kNumBalancerBackends); |
| 1278 |
for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) { |
| 1279 |
EXPECT_EQ(100UL, backends_[i]->service_.request_count()); |
| 1280 |
} |
| 1281 |
// Stop backends from balancer. This should put us in fallback mode. |
| 1282 |
for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) { |
| 1283 |
ShutdownBackend(i); |
| 1284 |
} |
| 1285 |
WaitForAllBackends(1 /* num_requests_multiple_of */, 0 /* start_index */, |
| 1286 |
kNumFallbackBackends /* stop_index */); |
| 1287 |
// Restart the backends from the balancer. We should *not* start |
| 1288 |
// sending traffic back to them at this point (although the behavior |
| 1289 |
// in xds may be different). |
| 1290 |
for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) { |
| 1291 |
StartBackend(i); |
| 1292 |
} |
| 1293 |
CheckRpcSendOk(100 * kNumBalancerBackends); |
| 1294 |
for (size_t i = 0; i < kNumFallbackBackends; ++i) { |
| 1295 |
EXPECT_EQ(100UL, backends_[i]->service_.request_count()); |
| 1296 |
} |
| 1297 |
// Now start the balancer again. This should cause us to exit |
| 1298 |
// fallback mode. |
| 1299 |
balancers_[0]->Start(server_host_); |
| 1300 |
ScheduleResponseForBalancer( |
| 1301 |
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}), |
| 1302 |
0); |
| 1303 |
WaitForAllBackends(1 /* num_requests_multiple_of */, |
| 1304 |
kNumFallbackBackends /* start_index */); |
| 1305 |
} |
| 1306 |
|
| 1307 |
TEST_F(SingleBalancerTest, |
| 1308 |
FallbackAfterStartup_LoseContactWithBackendsThenBalancer) { |
| 1309 |
// First two backends are fallback, last two are pointed to by balancer. |
| 1310 |
const size_t kNumFallbackBackends = 2; |
| 1311 |
const size_t kNumBalancerBackends = backends_.size() - kNumFallbackBackends; |
| 1312 |
std::vector<AddressData> backend_addresses; |
| 1313 |
for (size_t i = 0; i < kNumFallbackBackends; ++i) { |
| 1314 |
backend_addresses.emplace_back(AddressData{backends_[i]->port_, ""}); |
| 1315 |
} |
| 1316 |
std::vector<AddressData> balancer_addresses; |
| 1317 |
for (size_t i = 0; i < balancers_.size(); ++i) { |
| 1318 |
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""}); |
| 1319 |
} |
| 1320 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1321 |
ScheduleResponseForBalancer( |
| 1322 |
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}), |
| 1323 |
0); |
| 1324 |
// Try to connect. |
| 1325 |
channel_->GetState(true /* try_to_connect */); |
| 1326 |
WaitForAllBackends(1 /* num_requests_multiple_of */, |
| 1327 |
kNumFallbackBackends /* start_index */); |
| 1328 |
// Stop backends from balancer. Since we are still in contact with |
| 1329 |
// the balancer at this point, RPCs should be failing. |
| 1330 |
for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) { |
| 1331 |
ShutdownBackend(i); |
| 1332 |
} |
| 1333 |
CheckRpcSendFailure(); |
| 1334 |
// Stop balancer. This should put us in fallback mode. |
| 1335 |
balancers_[0]->Shutdown(); |
| 1336 |
WaitForAllBackends(1 /* num_requests_multiple_of */, 0 /* start_index */, |
| 1337 |
kNumFallbackBackends /* stop_index */); |
| 1338 |
// Restart the backends from the balancer. We should *not* start |
| 1339 |
// sending traffic back to them at this point (although the behavior |
| 1340 |
// in xds may be different). |
| 1341 |
for (size_t i = kNumFallbackBackends; i < backends_.size(); ++i) { |
| 1342 |
StartBackend(i); |
| 1343 |
} |
| 1344 |
CheckRpcSendOk(100 * kNumBalancerBackends); |
| 1345 |
for (size_t i = 0; i < kNumFallbackBackends; ++i) { |
| 1346 |
EXPECT_EQ(100UL, backends_[i]->service_.request_count()); |
| 1347 |
} |
| 1348 |
// Now start the balancer again. This should cause us to exit |
| 1349 |
// fallback mode. |
| 1350 |
balancers_[0]->Start(server_host_); |
| 1351 |
ScheduleResponseForBalancer( |
| 1352 |
0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}), |
| 1353 |
0); |
| 1354 |
WaitForAllBackends(1 /* num_requests_multiple_of */, |
| 1355 |
kNumFallbackBackends /* start_index */); |
| 1356 |
} |
| 1357 |
|
| 1358 |
TEST_F(SingleBalancerTest, FallbackEarlyWhenBalancerChannelFails) { |
| 1359 |
const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor(); |
| 1360 |
ResetStub(kFallbackTimeoutMs); |
| 1361 |
// Return an unreachable balancer and one fallback backend. |
| 1362 |
std::vector<AddressData> balancer_addresses; |
| 1363 |
balancer_addresses.emplace_back( |
| 1364 |
AddressData{grpc_pick_unused_port_or_die(), ""}); |
| 1365 |
std::vector<AddressData> backend_addresses; |
| 1366 |
backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""}); |
| 1367 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1368 |
// Send RPC with deadline less than the fallback timeout and make sure it |
| 1369 |
// succeeds. |
| 1370 |
CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000, |
| 1371 |
/* wait_for_ready */ false); |
| 1372 |
} |
| 1373 |
|
| 1374 |
TEST_F(SingleBalancerTest, FallbackEarlyWhenBalancerCallFails) { |
| 1375 |
const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor(); |
| 1376 |
ResetStub(kFallbackTimeoutMs); |
| 1377 |
// Return one balancer and one fallback backend. |
| 1378 |
std::vector<AddressData> balancer_addresses; |
| 1379 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1380 |
std::vector<AddressData> backend_addresses; |
| 1381 |
backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""}); |
| 1382 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1383 |
// Balancer drops call without sending a serverlist. |
| 1384 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1385 |
// Send RPC with deadline less than the fallback timeout and make sure it |
| 1386 |
// succeeds. |
| 1387 |
CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000, |
| 1388 |
/* wait_for_ready */ false); |
| 1389 |
} |
| 1390 |
|
| 1391 |
TEST_F(SingleBalancerTest, FallbackControlledByBalancer_BeforeFirstServerlist) { |
| 1392 |
const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor(); |
| 1393 |
ResetStub(kFallbackTimeoutMs); |
| 1394 |
// Return one balancer and one fallback backend. |
| 1395 |
std::vector<AddressData> balancer_addresses; |
| 1396 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1397 |
std::vector<AddressData> backend_addresses; |
| 1398 |
backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""}); |
| 1399 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1400 |
// Balancer explicitly tells client to fallback. |
| 1401 |
LoadBalanceResponse resp; |
| 1402 |
resp.mutable_fallback_response(); |
| 1403 |
ScheduleResponseForBalancer(0, resp, 0); |
| 1404 |
// Send RPC with deadline less than the fallback timeout and make sure it |
| 1405 |
// succeeds. |
| 1406 |
CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000, |
| 1407 |
/* wait_for_ready */ false); |
| 1408 |
} |
| 1409 |
|
| 1410 |
TEST_F(SingleBalancerTest, FallbackControlledByBalancer_AfterFirstServerlist) { |
| 1411 |
// Return one balancer and one fallback backend (backend 0). |
| 1412 |
std::vector<AddressData> balancer_addresses; |
| 1413 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1414 |
std::vector<AddressData> backend_addresses; |
| 1415 |
backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""}); |
| 1416 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1417 |
// Balancer initially sends serverlist, then tells client to fall back, |
| 1418 |
// then sends the serverlist again. |
| 1419 |
// The serverlist points to backend 1. |
| 1420 |
LoadBalanceResponse serverlist_resp = |
| 1421 |
BuildResponseForBackends({backends_[1]->port_}, {}); |
| 1422 |
LoadBalanceResponse fallback_resp; |
| 1423 |
fallback_resp.mutable_fallback_response(); |
| 1424 |
ScheduleResponseForBalancer(0, serverlist_resp, 0); |
| 1425 |
ScheduleResponseForBalancer(0, fallback_resp, 100); |
| 1426 |
ScheduleResponseForBalancer(0, serverlist_resp, 100); |
| 1427 |
// Requests initially go to backend 1, then go to backend 0 in |
| 1428 |
// fallback mode, then go back to backend 1 when we exit fallback. |
| 1429 |
WaitForBackend(1); |
| 1430 |
WaitForBackend(0); |
| 1431 |
WaitForBackend(1); |
| 1432 |
} |
| 1433 |
|
| 1434 |
TEST_F(SingleBalancerTest, BackendsRestart) { |
| 1435 |
SetNextResolutionAllBalancers(); |
| 1436 |
const size_t kNumRpcsPerAddress = 100; |
| 1437 |
ScheduleResponseForBalancer( |
| 1438 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 1439 |
// Make sure that trying to connect works without a call. |
| 1440 |
channel_->GetState(true /* try_to_connect */); |
| 1441 |
// Send kNumRpcsPerAddress RPCs per server. |
| 1442 |
CheckRpcSendOk(kNumRpcsPerAddress * num_backends_); |
| 1443 |
// Stop backends. RPCs should fail. |
| 1444 |
ShutdownAllBackends(); |
| 1445 |
CheckRpcSendFailure(); |
| 1446 |
// Restart backends. RPCs should start succeeding again. |
| 1447 |
StartAllBackends(); |
| 1448 |
CheckRpcSendOk(1 /* times */, 2000 /* timeout_ms */, |
| 1449 |
true /* wait_for_ready */); |
| 1450 |
// The balancer got a single request. |
| 1451 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1452 |
// and sent a single response. |
| 1453 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1454 |
} |
| 1455 |
|
| 1456 |
TEST_F(SingleBalancerTest, ServiceNameFromLbPolicyConfig) { |
| 1457 |
constexpr char kServiceConfigWithTarget[] = |
| 1458 |
"{\n" |
| 1459 |
" \"loadBalancingConfig\":[\n" |
| 1460 |
" { \"grpclb\":{\n" |
| 1461 |
" \"serviceName\":\"test_service\"\n" |
| 1462 |
" }}\n" |
| 1463 |
" ]\n" |
| 1464 |
"}"; |
| 1465 |
|
| 1466 |
SetNextResolutionAllBalancers(kServiceConfigWithTarget); |
| 1467 |
ScheduleResponseForBalancer( |
| 1468 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 1469 |
// Make sure that trying to connect works without a call. |
| 1470 |
channel_->GetState(true /* try_to_connect */); |
| 1471 |
// We need to wait for all backends to come online. |
| 1472 |
WaitForAllBackends(); |
| 1473 |
EXPECT_EQ(balancers_[0]->service_.service_names().back(), "test_service"); |
| 1474 |
} |
| 1475 |
|
| 1476 |
class UpdatesTest : public GrpclbEnd2endTest { |
| 1477 |
public: |
| 1478 |
UpdatesTest() : GrpclbEnd2endTest(4, 3, 0) {} |
| 1479 |
}; |
| 1480 |
|
| 1481 |
TEST_F(UpdatesTest, UpdateBalancersButKeepUsingOriginalBalancer) { |
| 1482 |
SetNextResolutionAllBalancers(); |
| 1483 |
const std::vector<int> first_backend{GetBackendPorts()[0]}; |
| 1484 |
const std::vector<int> second_backend{GetBackendPorts()[1]}; |
| 1485 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}), |
| 1486 |
0); |
| 1487 |
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}), |
| 1488 |
0); |
| 1489 |
|
| 1490 |
// Wait until the first backend is ready. |
| 1491 |
WaitForBackend(0); |
| 1492 |
|
| 1493 |
// Send 10 requests. |
| 1494 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1495 |
CheckRpcSendOk(10); |
| 1496 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1497 |
|
| 1498 |
// All 10 requests should have gone to the first backend. |
| 1499 |
EXPECT_EQ(10U, backends_[0]->service_.request_count()); |
| 1500 |
|
| 1501 |
// Balancer 0 got a single request. |
| 1502 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1503 |
// and sent a single response. |
| 1504 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1505 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1506 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1507 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1508 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1509 |
|
| 1510 |
std::vector<AddressData> addresses; |
| 1511 |
addresses.emplace_back(AddressData{balancers_[1]->port_, ""}); |
| 1512 |
gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 =========="); |
| 1513 |
SetNextResolution(addresses); |
| 1514 |
gpr_log(GPR_INFO, "========= UPDATE 1 DONE =========="); |
| 1515 |
|
| 1516 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1517 |
gpr_timespec deadline = gpr_time_add( |
| 1518 |
gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN)); |
| 1519 |
// Send 10 seconds worth of RPCs |
| 1520 |
do { |
| 1521 |
CheckRpcSendOk(); |
| 1522 |
} while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); |
| 1523 |
// The current LB call is still working, so grpclb continued using it to the |
| 1524 |
// first balancer, which doesn't assign the second backend. |
| 1525 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1526 |
|
| 1527 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1528 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1529 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1530 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1531 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1532 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1533 |
} |
| 1534 |
|
| 1535 |
// Send an update with the same set of LBs as the one in SetUp() in order to |
| 1536 |
// verify that the LB channel inside grpclb keeps the initial connection (which |
| 1537 |
// by definition is also present in the update). |
| 1538 |
TEST_F(UpdatesTest, UpdateBalancersRepeated) { |
| 1539 |
SetNextResolutionAllBalancers(); |
| 1540 |
const std::vector<int> first_backend{GetBackendPorts()[0]}; |
| 1541 |
const std::vector<int> second_backend{GetBackendPorts()[0]}; |
| 1542 |
|
| 1543 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}), |
| 1544 |
0); |
| 1545 |
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}), |
| 1546 |
0); |
| 1547 |
|
| 1548 |
// Wait until the first backend is ready. |
| 1549 |
WaitForBackend(0); |
| 1550 |
|
| 1551 |
// Send 10 requests. |
| 1552 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1553 |
CheckRpcSendOk(10); |
| 1554 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1555 |
|
| 1556 |
// All 10 requests should have gone to the first backend. |
| 1557 |
EXPECT_EQ(10U, backends_[0]->service_.request_count()); |
| 1558 |
|
| 1559 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1560 |
// Balancer 0 got a single request. |
| 1561 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1562 |
// and sent a single response. |
| 1563 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1564 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1565 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1566 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1567 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1568 |
|
| 1569 |
std::vector<AddressData> addresses; |
| 1570 |
addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1571 |
addresses.emplace_back(AddressData{balancers_[1]->port_, ""}); |
| 1572 |
addresses.emplace_back(AddressData{balancers_[2]->port_, ""}); |
| 1573 |
gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 =========="); |
| 1574 |
SetNextResolution(addresses); |
| 1575 |
gpr_log(GPR_INFO, "========= UPDATE 1 DONE =========="); |
| 1576 |
|
| 1577 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1578 |
gpr_timespec deadline = gpr_time_add( |
| 1579 |
gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN)); |
| 1580 |
// Send 10 seconds worth of RPCs |
| 1581 |
do { |
| 1582 |
CheckRpcSendOk(); |
| 1583 |
} while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); |
| 1584 |
// grpclb continued using the original LB call to the first balancer, which |
| 1585 |
// doesn't assign the second backend. |
| 1586 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1587 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1588 |
|
| 1589 |
addresses.clear(); |
| 1590 |
addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1591 |
addresses.emplace_back(AddressData{balancers_[1]->port_, ""}); |
| 1592 |
gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 2 =========="); |
| 1593 |
SetNextResolution(addresses); |
| 1594 |
gpr_log(GPR_INFO, "========= UPDATE 2 DONE =========="); |
| 1595 |
|
| 1596 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1597 |
deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 1598 |
gpr_time_from_millis(10000, GPR_TIMESPAN)); |
| 1599 |
// Send 10 seconds worth of RPCs |
| 1600 |
do { |
| 1601 |
CheckRpcSendOk(); |
| 1602 |
} while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0); |
| 1603 |
// grpclb continued using the original LB call to the first balancer, which |
| 1604 |
// doesn't assign the second backend. |
| 1605 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1606 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1607 |
} |
| 1608 |
|
| 1609 |
TEST_F(UpdatesTest, UpdateBalancersDeadUpdate) { |
| 1610 |
std::vector<AddressData> addresses; |
| 1611 |
addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1612 |
SetNextResolution(addresses); |
| 1613 |
const std::vector<int> first_backend{GetBackendPorts()[0]}; |
| 1614 |
const std::vector<int> second_backend{GetBackendPorts()[1]}; |
| 1615 |
|
| 1616 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}), |
| 1617 |
0); |
| 1618 |
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}), |
| 1619 |
0); |
| 1620 |
|
| 1621 |
// Start servers and send 10 RPCs per server. |
| 1622 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1623 |
CheckRpcSendOk(10); |
| 1624 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1625 |
// All 10 requests should have gone to the first backend. |
| 1626 |
EXPECT_EQ(10U, backends_[0]->service_.request_count()); |
| 1627 |
|
| 1628 |
// Kill balancer 0 |
| 1629 |
gpr_log(GPR_INFO, "********** ABOUT TO KILL BALANCER 0 *************"); |
| 1630 |
balancers_[0]->Shutdown(); |
| 1631 |
gpr_log(GPR_INFO, "********** KILLED BALANCER 0 *************"); |
| 1632 |
|
| 1633 |
// This is serviced by the existing RR policy |
| 1634 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 1635 |
CheckRpcSendOk(10); |
| 1636 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 1637 |
// All 10 requests should again have gone to the first backend. |
| 1638 |
EXPECT_EQ(20U, backends_[0]->service_.request_count()); |
| 1639 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1640 |
|
| 1641 |
// Balancer 0 got a single request. |
| 1642 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1643 |
// and sent a single response. |
| 1644 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1645 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1646 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1647 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1648 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1649 |
|
| 1650 |
addresses.clear(); |
| 1651 |
addresses.emplace_back(AddressData{balancers_[1]->port_, ""}); |
| 1652 |
gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 =========="); |
| 1653 |
SetNextResolution(addresses); |
| 1654 |
gpr_log(GPR_INFO, "========= UPDATE 1 DONE =========="); |
| 1655 |
|
| 1656 |
// Wait until update has been processed, as signaled by the second backend |
| 1657 |
// receiving a request. In the meantime, the client continues to be serviced |
| 1658 |
// (by the first backend) without interruption. |
| 1659 |
EXPECT_EQ(0U, backends_[1]->service_.request_count()); |
| 1660 |
WaitForBackend(1); |
| 1661 |
|
| 1662 |
// This is serviced by the updated RR policy |
| 1663 |
backends_[1]->service_.ResetCounters(); |
| 1664 |
gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH =========="); |
| 1665 |
CheckRpcSendOk(10); |
| 1666 |
gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH =========="); |
| 1667 |
// All 10 requests should have gone to the second backend. |
| 1668 |
EXPECT_EQ(10U, backends_[1]->service_.request_count()); |
| 1669 |
|
| 1670 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1671 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1672 |
// The second balancer, published as part of the first update, may end up |
| 1673 |
// getting two requests (that is, 1 <= #req <= 2) if the LB call retry timer |
| 1674 |
// firing races with the arrival of the update containing the second |
| 1675 |
// balancer. |
| 1676 |
EXPECT_GE(balancers_[1]->service_.request_count(), 1U); |
| 1677 |
EXPECT_GE(balancers_[1]->service_.response_count(), 1U); |
| 1678 |
EXPECT_LE(balancers_[1]->service_.request_count(), 2U); |
| 1679 |
EXPECT_LE(balancers_[1]->service_.response_count(), 2U); |
| 1680 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1681 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1682 |
} |
| 1683 |
|
| 1684 |
TEST_F(UpdatesTest, ReresolveDeadBackend) { |
| 1685 |
ResetStub(500); |
| 1686 |
// The first resolution contains the addresses of a balancer that never |
| 1687 |
// responds, and a fallback backend. |
| 1688 |
std::vector<AddressData> balancer_addresses; |
| 1689 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1690 |
std::vector<AddressData> backend_addresses; |
| 1691 |
backend_addresses.emplace_back(AddressData{backends_[0]->port_, ""}); |
| 1692 |
SetNextResolution(balancer_addresses, backend_addresses); |
| 1693 |
// Ask channel to connect to trigger resolver creation. |
| 1694 |
channel_->GetState(true); |
| 1695 |
// The re-resolution result will contain the addresses of the same balancer |
| 1696 |
// and a new fallback backend. |
| 1697 |
balancer_addresses.clear(); |
| 1698 |
balancer_addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1699 |
backend_addresses.clear(); |
| 1700 |
backend_addresses.emplace_back(AddressData{backends_[1]->port_, ""}); |
| 1701 |
SetNextReresolutionResponse(balancer_addresses, backend_addresses); |
| 1702 |
|
| 1703 |
// Start servers and send 10 RPCs per server. |
| 1704 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1705 |
CheckRpcSendOk(10); |
| 1706 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1707 |
// All 10 requests should have gone to the fallback backend. |
| 1708 |
EXPECT_EQ(10U, backends_[0]->service_.request_count()); |
| 1709 |
|
| 1710 |
// Kill backend 0. |
| 1711 |
gpr_log(GPR_INFO, "********** ABOUT TO KILL BACKEND 0 *************"); |
| 1712 |
backends_[0]->Shutdown(); |
| 1713 |
gpr_log(GPR_INFO, "********** KILLED BACKEND 0 *************"); |
| 1714 |
|
| 1715 |
// Wait until re-resolution has finished, as signaled by the second backend |
| 1716 |
// receiving a request. |
| 1717 |
WaitForBackend(1); |
| 1718 |
|
| 1719 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 1720 |
CheckRpcSendOk(10); |
| 1721 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 1722 |
// All 10 requests should have gone to the second backend. |
| 1723 |
EXPECT_EQ(10U, backends_[1]->service_.request_count()); |
| 1724 |
|
| 1725 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1726 |
balancers_[1]->service_.NotifyDoneWithServerlists(); |
| 1727 |
balancers_[2]->service_.NotifyDoneWithServerlists(); |
| 1728 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1729 |
EXPECT_EQ(0U, balancers_[0]->service_.response_count()); |
| 1730 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1731 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1732 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1733 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1734 |
} |
| 1735 |
|
| 1736 |
// TODO(juanlishen): Should be removed when the first response is always the |
| 1737 |
// initial response. Currently, if client load reporting is not enabled, the |
| 1738 |
// balancer doesn't send initial response. When the backend shuts down, an |
| 1739 |
// unexpected re-resolution will happen. This test configuration is a workaround |
| 1740 |
// for test ReresolveDeadBalancer. |
| 1741 |
class UpdatesWithClientLoadReportingTest : public GrpclbEnd2endTest { |
| 1742 |
public: |
| 1743 |
UpdatesWithClientLoadReportingTest() : GrpclbEnd2endTest(4, 3, 2) {} |
| 1744 |
}; |
| 1745 |
|
| 1746 |
TEST_F(UpdatesWithClientLoadReportingTest, ReresolveDeadBalancer) { |
| 1747 |
const std::vector<int> first_backend{GetBackendPorts()[0]}; |
| 1748 |
const std::vector<int> second_backend{GetBackendPorts()[1]}; |
| 1749 |
ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}), |
| 1750 |
0); |
| 1751 |
ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}), |
| 1752 |
0); |
| 1753 |
|
| 1754 |
// Ask channel to connect to trigger resolver creation. |
| 1755 |
channel_->GetState(true); |
| 1756 |
std::vector<AddressData> addresses; |
| 1757 |
addresses.emplace_back(AddressData{balancers_[0]->port_, ""}); |
| 1758 |
SetNextResolution(addresses); |
| 1759 |
addresses.clear(); |
| 1760 |
addresses.emplace_back(AddressData{balancers_[1]->port_, ""}); |
| 1761 |
SetNextReresolutionResponse(addresses); |
| 1762 |
|
| 1763 |
// Start servers and send 10 RPCs per server. |
| 1764 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 1765 |
CheckRpcSendOk(10); |
| 1766 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 1767 |
// All 10 requests should have gone to the first backend. |
| 1768 |
EXPECT_EQ(10U, backends_[0]->service_.request_count()); |
| 1769 |
|
| 1770 |
// Kill backend 0. |
| 1771 |
gpr_log(GPR_INFO, "********** ABOUT TO KILL BACKEND 0 *************"); |
| 1772 |
backends_[0]->Shutdown(); |
| 1773 |
gpr_log(GPR_INFO, "********** KILLED BACKEND 0 *************"); |
| 1774 |
|
| 1775 |
CheckRpcSendFailure(); |
| 1776 |
|
| 1777 |
// Balancer 0 got a single request. |
| 1778 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1779 |
// and sent a single response. |
| 1780 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1781 |
EXPECT_EQ(0U, balancers_[1]->service_.request_count()); |
| 1782 |
EXPECT_EQ(0U, balancers_[1]->service_.response_count()); |
| 1783 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1784 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1785 |
|
| 1786 |
// Kill balancer 0. |
| 1787 |
gpr_log(GPR_INFO, "********** ABOUT TO KILL BALANCER 0 *************"); |
| 1788 |
balancers_[0]->Shutdown(); |
| 1789 |
gpr_log(GPR_INFO, "********** KILLED BALANCER 0 *************"); |
| 1790 |
|
| 1791 |
// Wait until re-resolution has finished, as signaled by the second backend |
| 1792 |
// receiving a request. |
| 1793 |
WaitForBackend(1); |
| 1794 |
|
| 1795 |
// This is serviced by the new serverlist. |
| 1796 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 1797 |
CheckRpcSendOk(10); |
| 1798 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 1799 |
// All 10 requests should have gone to the second backend. |
| 1800 |
EXPECT_EQ(10U, backends_[1]->service_.request_count()); |
| 1801 |
|
| 1802 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1803 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1804 |
// After balancer 0 is killed, we restart an LB call immediately (because we |
| 1805 |
// disconnect to a previously connected balancer). Although we will cancel |
| 1806 |
// this call when the re-resolution update is done and another LB call restart |
| 1807 |
// is needed, this old call may still succeed reaching the LB server if |
| 1808 |
// re-resolution is slow. So balancer 1 may have received 2 requests and sent |
| 1809 |
// 2 responses. |
| 1810 |
EXPECT_GE(balancers_[1]->service_.request_count(), 1U); |
| 1811 |
EXPECT_GE(balancers_[1]->service_.response_count(), 1U); |
| 1812 |
EXPECT_LE(balancers_[1]->service_.request_count(), 2U); |
| 1813 |
EXPECT_LE(balancers_[1]->service_.response_count(), 2U); |
| 1814 |
EXPECT_EQ(0U, balancers_[2]->service_.request_count()); |
| 1815 |
EXPECT_EQ(0U, balancers_[2]->service_.response_count()); |
| 1816 |
} |
| 1817 |
|
| 1818 |
TEST_F(SingleBalancerTest, Drop) { |
| 1819 |
SetNextResolutionAllBalancers(); |
| 1820 |
const size_t kNumRpcsPerAddress = 100; |
| 1821 |
const int num_of_drop_by_rate_limiting_addresses = 1; |
| 1822 |
const int num_of_drop_by_load_balancing_addresses = 2; |
| 1823 |
const int num_of_drop_addresses = num_of_drop_by_rate_limiting_addresses + |
| 1824 |
num_of_drop_by_load_balancing_addresses; |
| 1825 |
const int num_total_addresses = num_backends_ + num_of_drop_addresses; |
| 1826 |
ScheduleResponseForBalancer( |
| 1827 |
0, |
| 1828 |
BuildResponseForBackends( |
| 1829 |
GetBackendPorts(), |
| 1830 |
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, |
| 1831 |
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), |
| 1832 |
0); |
| 1833 |
// Wait until all backends are ready. |
| 1834 |
WaitForAllBackends(); |
| 1835 |
// Send kNumRpcsPerAddress RPCs for each server and drop address. |
| 1836 |
size_t num_drops = 0; |
| 1837 |
for (size_t i = 0; i < kNumRpcsPerAddress * num_total_addresses; ++i) { |
| 1838 |
EchoResponse response; |
| 1839 |
const Status status = SendRpc(&response); |
| 1840 |
if (!status.ok() && |
| 1841 |
status.error_message() == "drop directed by grpclb balancer") { |
| 1842 |
++num_drops; |
| 1843 |
} else { |
| 1844 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 1845 |
<< " message=" << status.error_message(); |
| 1846 |
EXPECT_EQ(response.message(), kRequestMessage_); |
| 1847 |
} |
| 1848 |
} |
| 1849 |
EXPECT_EQ(kNumRpcsPerAddress * num_of_drop_addresses, num_drops); |
| 1850 |
// Each backend should have gotten 100 requests. |
| 1851 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 1852 |
EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count()); |
| 1853 |
} |
| 1854 |
// The balancer got a single request. |
| 1855 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1856 |
// and sent a single response. |
| 1857 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1858 |
} |
| 1859 |
|
| 1860 |
TEST_F(SingleBalancerTest, DropAllFirst) { |
| 1861 |
SetNextResolutionAllBalancers(); |
| 1862 |
// All registered addresses are marked as "drop". |
| 1863 |
const int num_of_drop_by_rate_limiting_addresses = 1; |
| 1864 |
const int num_of_drop_by_load_balancing_addresses = 1; |
| 1865 |
ScheduleResponseForBalancer( |
| 1866 |
0, |
| 1867 |
BuildResponseForBackends( |
| 1868 |
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, |
| 1869 |
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), |
| 1870 |
0); |
| 1871 |
const Status status = SendRpc(nullptr, 1000, true); |
| 1872 |
EXPECT_FALSE(status.ok()); |
| 1873 |
EXPECT_EQ(status.error_message(), "drop directed by grpclb balancer"); |
| 1874 |
} |
| 1875 |
|
| 1876 |
TEST_F(SingleBalancerTest, DropAll) { |
| 1877 |
SetNextResolutionAllBalancers(); |
| 1878 |
ScheduleResponseForBalancer( |
| 1879 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 1880 |
const int num_of_drop_by_rate_limiting_addresses = 1; |
| 1881 |
const int num_of_drop_by_load_balancing_addresses = 1; |
| 1882 |
ScheduleResponseForBalancer( |
| 1883 |
0, |
| 1884 |
BuildResponseForBackends( |
| 1885 |
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, |
| 1886 |
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), |
| 1887 |
1000); |
| 1888 |
|
| 1889 |
// First call succeeds. |
| 1890 |
CheckRpcSendOk(); |
| 1891 |
// But eventually, the update with only dropped servers is processed and calls |
| 1892 |
// fail. |
| 1893 |
Status status; |
| 1894 |
do { |
| 1895 |
status = SendRpc(nullptr, 1000, true); |
| 1896 |
} while (status.ok()); |
| 1897 |
EXPECT_FALSE(status.ok()); |
| 1898 |
EXPECT_EQ(status.error_message(), "drop directed by grpclb balancer"); |
| 1899 |
} |
| 1900 |
|
| 1901 |
class SingleBalancerWithClientLoadReportingTest : public GrpclbEnd2endTest { |
| 1902 |
public: |
| 1903 |
SingleBalancerWithClientLoadReportingTest() : GrpclbEnd2endTest(4, 1, 3) {} |
| 1904 |
}; |
| 1905 |
|
| 1906 |
TEST_F(SingleBalancerWithClientLoadReportingTest, Vanilla) { |
| 1907 |
SetNextResolutionAllBalancers(); |
| 1908 |
const size_t kNumRpcsPerAddress = 100; |
| 1909 |
ScheduleResponseForBalancer( |
| 1910 |
0, BuildResponseForBackends(GetBackendPorts(), {}), 0); |
| 1911 |
// Wait until all backends are ready. |
| 1912 |
int num_ok = 0; |
| 1913 |
int num_failure = 0; |
| 1914 |
int num_drops = 0; |
| 1915 |
std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends(); |
| 1916 |
// Send kNumRpcsPerAddress RPCs per server. |
| 1917 |
CheckRpcSendOk(kNumRpcsPerAddress * num_backends_); |
| 1918 |
// Each backend should have gotten 100 requests. |
| 1919 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 1920 |
EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count()); |
| 1921 |
} |
| 1922 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1923 |
// The balancer got a single request. |
| 1924 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 1925 |
// and sent a single response. |
| 1926 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 1927 |
|
| 1928 |
ClientStats client_stats; |
| 1929 |
do { |
| 1930 |
client_stats += WaitForLoadReports(); |
| 1931 |
} while (client_stats.num_calls_finished != |
| 1932 |
kNumRpcsPerAddress * num_backends_ + num_ok); |
| 1933 |
EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok, |
| 1934 |
client_stats.num_calls_started); |
| 1935 |
EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok, |
| 1936 |
client_stats.num_calls_finished); |
| 1937 |
EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send); |
| 1938 |
EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + (num_ok + num_drops), |
| 1939 |
client_stats.num_calls_finished_known_received); |
| 1940 |
EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre()); |
| 1941 |
} |
| 1942 |
|
| 1943 |
TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) { |
| 1944 |
SetNextResolutionAllBalancers(); |
| 1945 |
const size_t kNumBackendsFirstPass = 2; |
| 1946 |
const size_t kNumBackendsSecondPass = |
| 1947 |
backends_.size() - kNumBackendsFirstPass; |
| 1948 |
// Balancer returns backends starting at index 1. |
| 1949 |
ScheduleResponseForBalancer( |
| 1950 |
0, |
| 1951 |
BuildResponseForBackends(GetBackendPorts(0, kNumBackendsFirstPass), {}), |
| 1952 |
0); |
| 1953 |
// Wait until all backends returned by the balancer are ready. |
| 1954 |
int num_ok = 0; |
| 1955 |
int num_failure = 0; |
| 1956 |
int num_drops = 0; |
| 1957 |
std::tie(num_ok, num_failure, num_drops) = |
| 1958 |
WaitForAllBackends(/* num_requests_multiple_of */ 1, /* start_index */ 0, |
| 1959 |
/* stop_index */ kNumBackendsFirstPass); |
| 1960 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1961 |
ClientStats client_stats = WaitForLoadReports(); |
| 1962 |
EXPECT_EQ(static_cast<size_t>(num_ok), client_stats.num_calls_started); |
| 1963 |
EXPECT_EQ(static_cast<size_t>(num_ok), client_stats.num_calls_finished); |
| 1964 |
EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send); |
| 1965 |
EXPECT_EQ(static_cast<size_t>(num_ok), |
| 1966 |
client_stats.num_calls_finished_known_received); |
| 1967 |
EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre()); |
| 1968 |
// Shut down the balancer. |
| 1969 |
balancers_[0]->Shutdown(); |
| 1970 |
// Send 10 more requests per backend. This will continue using the |
| 1971 |
// last serverlist we received from the balancer before it was shut down. |
| 1972 |
ResetBackendCounters(); |
| 1973 |
CheckRpcSendOk(kNumBackendsFirstPass); |
| 1974 |
// Each backend should have gotten 1 request. |
| 1975 |
for (size_t i = 0; i < kNumBackendsFirstPass; ++i) { |
| 1976 |
EXPECT_EQ(1UL, backends_[i]->service_.request_count()); |
| 1977 |
} |
| 1978 |
// Now restart the balancer, this time pointing to all backends. |
| 1979 |
balancers_[0]->Start(server_host_); |
| 1980 |
ScheduleResponseForBalancer( |
| 1981 |
0, BuildResponseForBackends(GetBackendPorts(kNumBackendsFirstPass), {}), |
| 1982 |
0); |
| 1983 |
// Wait for queries to start going to one of the new backends. |
| 1984 |
// This tells us that we're now using the new serverlist. |
| 1985 |
do { |
| 1986 |
CheckRpcSendOk(); |
| 1987 |
} while (backends_[2]->service_.request_count() == 0 && |
| 1988 |
backends_[3]->service_.request_count() == 0); |
| 1989 |
// Send one RPC per backend. |
| 1990 |
CheckRpcSendOk(kNumBackendsSecondPass); |
| 1991 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 1992 |
// Check client stats. |
| 1993 |
client_stats = WaitForLoadReports(); |
| 1994 |
EXPECT_EQ(kNumBackendsSecondPass + 1, client_stats.num_calls_started); |
| 1995 |
EXPECT_EQ(kNumBackendsSecondPass + 1, client_stats.num_calls_finished); |
| 1996 |
EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send); |
| 1997 |
EXPECT_EQ(kNumBackendsSecondPass + 1, |
| 1998 |
client_stats.num_calls_finished_known_received); |
| 1999 |
EXPECT_THAT(client_stats.drop_token_counts, ::testing::ElementsAre()); |
| 2000 |
} |
| 2001 |
|
| 2002 |
TEST_F(SingleBalancerWithClientLoadReportingTest, Drop) { |
| 2003 |
SetNextResolutionAllBalancers(); |
| 2004 |
const size_t kNumRpcsPerAddress = 3; |
| 2005 |
const int num_of_drop_by_rate_limiting_addresses = 2; |
| 2006 |
const int num_of_drop_by_load_balancing_addresses = 1; |
| 2007 |
const int num_of_drop_addresses = num_of_drop_by_rate_limiting_addresses + |
| 2008 |
num_of_drop_by_load_balancing_addresses; |
| 2009 |
const int num_total_addresses = num_backends_ + num_of_drop_addresses; |
| 2010 |
ScheduleResponseForBalancer( |
| 2011 |
0, |
| 2012 |
BuildResponseForBackends( |
| 2013 |
GetBackendPorts(), |
| 2014 |
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, |
| 2015 |
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), |
| 2016 |
0); |
| 2017 |
// Wait until all backends are ready. |
| 2018 |
int num_warmup_ok = 0; |
| 2019 |
int num_warmup_failure = 0; |
| 2020 |
int num_warmup_drops = 0; |
| 2021 |
std::tie(num_warmup_ok, num_warmup_failure, num_warmup_drops) = |
| 2022 |
WaitForAllBackends(num_total_addresses /* num_requests_multiple_of */); |
| 2023 |
const int num_total_warmup_requests = |
| 2024 |
num_warmup_ok + num_warmup_failure + num_warmup_drops; |
| 2025 |
size_t num_drops = 0; |
| 2026 |
for (size_t i = 0; i < kNumRpcsPerAddress * num_total_addresses; ++i) { |
| 2027 |
EchoResponse response; |
| 2028 |
const Status status = SendRpc(&response); |
| 2029 |
if (!status.ok() && |
| 2030 |
status.error_message() == "drop directed by grpclb balancer") { |
| 2031 |
++num_drops; |
| 2032 |
} else { |
| 2033 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 2034 |
<< " message=" << status.error_message(); |
| 2035 |
EXPECT_EQ(response.message(), kRequestMessage_); |
| 2036 |
} |
| 2037 |
} |
| 2038 |
EXPECT_EQ(kNumRpcsPerAddress * num_of_drop_addresses, num_drops); |
| 2039 |
// Each backend should have gotten 100 requests. |
| 2040 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 2041 |
EXPECT_EQ(kNumRpcsPerAddress, backends_[i]->service_.request_count()); |
| 2042 |
} |
| 2043 |
balancers_[0]->service_.NotifyDoneWithServerlists(); |
| 2044 |
// The balancer got a single request. |
| 2045 |
EXPECT_EQ(1U, balancers_[0]->service_.request_count()); |
| 2046 |
// and sent a single response. |
| 2047 |
EXPECT_EQ(1U, balancers_[0]->service_.response_count()); |
| 2048 |
|
| 2049 |
const ClientStats client_stats = WaitForLoadReports(); |
| 2050 |
EXPECT_EQ( |
| 2051 |
kNumRpcsPerAddress * num_total_addresses + num_total_warmup_requests, |
| 2052 |
client_stats.num_calls_started); |
| 2053 |
EXPECT_EQ( |
| 2054 |
kNumRpcsPerAddress * num_total_addresses + num_total_warmup_requests, |
| 2055 |
client_stats.num_calls_finished); |
| 2056 |
EXPECT_EQ(0U, client_stats.num_calls_finished_with_client_failed_to_send); |
| 2057 |
EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_warmup_ok, |
| 2058 |
client_stats.num_calls_finished_known_received); |
| 2059 |
// The number of warmup request is a multiple of the number of addresses. |
| 2060 |
// Therefore, all addresses in the scheduled balancer response are hit the |
| 2061 |
// same number of times. |
| 2062 |
const int num_times_drop_addresses_hit = |
| 2063 |
num_warmup_drops / num_of_drop_addresses; |
| 2064 |
EXPECT_THAT( |
| 2065 |
client_stats.drop_token_counts, |
| 2066 |
::testing::ElementsAre( |
| 2067 |
::testing::Pair("load_balancing", |
| 2068 |
(kNumRpcsPerAddress + num_times_drop_addresses_hit)), |
| 2069 |
::testing::Pair( |
| 2070 |
"rate_limiting", |
| 2071 |
(kNumRpcsPerAddress + num_times_drop_addresses_hit) * 2))); |
| 2072 |
} |
| 2073 |
|
| 2074 |
} // namespace |
| 2075 |
} // namespace testing |
| 2076 |
} // namespace grpc |
| 2077 |
|
| 2078 |
int main(int argc, char** argv) { |
| 2079 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 2080 |
::testing::InitGoogleTest(&argc, argv); |
| 2081 |
const auto result = RUN_ALL_TESTS(); |
| 2082 |
return result; |
| 2083 |
} |