| 1 |
// Copyright 2017 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
|
| 16 |
#include <numeric> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include <gmock/gmock.h> |
| 21 |
#include <gtest/gtest.h> |
| 22 |
|
| 23 |
#include "absl/strings/match.h" |
| 24 |
#include "absl/strings/str_cat.h" |
| 25 |
|
| 26 |
#include "src/core/ext/filters/client_channel/backup_poller.h" |
| 27 |
#include "src/core/lib/address_utils/sockaddr_utils.h" |
| 28 |
#include "test/cpp/end2end/connection_attempt_injector.h" |
| 29 |
#include "test/cpp/end2end/xds/xds_end2end_test_lib.h" |
| 30 |
|
| 31 |
namespace grpc { |
| 32 |
namespace testing { |
| 33 |
namespace { |
| 34 |
|
| 35 |
using ::envoy::config::cluster::v3::CircuitBreakers; |
| 36 |
using ::envoy::config::cluster::v3::RoutingPriority; |
| 37 |
using ::envoy::config::endpoint::v3::HealthStatus; |
| 38 |
using ::envoy::type::v3::FractionalPercent; |
| 39 |
|
| 40 |
using ClientStats = LrsServiceImpl::ClientStats; |
| 41 |
|
| 42 |
constexpr char kLbDropType[] = "lb"; |
| 43 |
constexpr char kThrottleDropType[] = "throttle"; |
| 44 |
constexpr char kStatusMessageDropPrefix[] = "EDS-configured drop: "; |
| 45 |
|
| 46 |
// |
| 47 |
// CDS tests |
| 48 |
// |
| 49 |
|
| 50 |
using CdsTest = XdsEnd2endTest; |
| 51 |
|
| 52 |
INSTANTIATE_TEST_SUITE_P(XdsTest, CdsTest, ::testing::Values(XdsTestType()), |
| 53 |
&XdsTestType::Name); |
| 54 |
|
| 55 |
// Tests that CDS client should send an ACK upon correct CDS response. |
| 56 |
TEST_P(CdsTest, Vanilla) { |
| 57 |
(void)SendRpc(); |
| 58 |
auto response_state = balancer_->ads_service()->cds_response_state(); |
| 59 |
ASSERT_TRUE(response_state.has_value()); |
| 60 |
EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED); |
| 61 |
} |
| 62 |
|
| 63 |
// Testing just one example of an invalid resource here. |
| 64 |
// Unit tests for XdsClusterResourceType have exhaustive tests for all |
| 65 |
// of the invalid cases. |
| 66 |
TEST_P(CdsTest, InvalidClusterResource) { |
| 67 |
auto cluster = default_cluster_; |
| 68 |
cluster.set_type(Cluster::STATIC); |
| 69 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 70 |
const auto response_state = WaitForCdsNack(DEBUG_LOCATION); |
| 71 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 72 |
EXPECT_EQ(response_state->error_message, |
| 73 |
"xDS response validation errors: [" |
| 74 |
"resource index 0: cluster_name: " |
| 75 |
"INVALID_ARGUMENT: errors validating Cluster resource: [" |
| 76 |
"field:type error:unknown discovery type]]"); |
| 77 |
} |
| 78 |
|
| 79 |
// Tests that we don't trigger does-not-exist callbacks for a resource |
| 80 |
// that was previously valid but is updated to be invalid. |
| 81 |
TEST_P(CdsTest, InvalidClusterStillExistsIfPreviouslyCached) { |
| 82 |
CreateAndStartBackends(1); |
| 83 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 84 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 85 |
// Check that everything works. |
| 86 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 87 |
// Now send an update changing the Cluster to be invalid. |
| 88 |
auto cluster = default_cluster_; |
| 89 |
cluster.set_type(Cluster::STATIC); |
| 90 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 91 |
const auto response_state = |
| 92 |
WaitForCdsNack(DEBUG_LOCATION, RpcOptions(), StatusCode::OK); |
| 93 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 94 |
EXPECT_EQ(response_state->error_message, |
| 95 |
"xDS response validation errors: [" |
| 96 |
"resource index 0: cluster_name: " |
| 97 |
"INVALID_ARGUMENT: errors validating Cluster resource: [" |
| 98 |
"field:type error:unknown discovery type]]"); |
| 99 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 100 |
} |
| 101 |
|
| 102 |
// Tests round robin is not implacted by the endpoint weight, and that the |
| 103 |
// localities in a locality map are picked according to their weights. |
| 104 |
TEST_P(CdsTest, EndpointWeightDoesNotImpactWeightedRoundRobin) { |
| 105 |
CreateAndStartBackends(2); |
| 106 |
const int kLocalityWeight0 = 2; |
| 107 |
const int kLocalityWeight1 = 8; |
| 108 |
const int kTotalLocalityWeight = kLocalityWeight0 + kLocalityWeight1; |
| 109 |
const double kLocalityWeightRate0 = |
| 110 |
static_cast<double>(kLocalityWeight0) / kTotalLocalityWeight; |
| 111 |
const double kLocalityWeightRate1 = |
| 112 |
static_cast<double>(kLocalityWeight1) / kTotalLocalityWeight; |
| 113 |
const double kErrorTolerance = 0.05; |
| 114 |
const size_t kNumRpcs = |
| 115 |
ComputeIdealNumRpcs(kLocalityWeightRate0, kErrorTolerance); |
| 116 |
// ADS response contains 2 localities, each of which contains 1 backend. |
| 117 |
EdsResourceArgs args({ |
| 118 |
{"locality0", |
| 119 |
{CreateEndpoint(0, HealthStatus::UNKNOWN, 8)}, |
| 120 |
kLocalityWeight0}, |
| 121 |
{"locality1", |
| 122 |
{CreateEndpoint(1, HealthStatus::UNKNOWN, 2)}, |
| 123 |
kLocalityWeight1}, |
| 124 |
}); |
| 125 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 126 |
// Wait for both backends to be ready. |
| 127 |
WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 128 |
// Send kNumRpcs RPCs. |
| 129 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 130 |
// The locality picking rates should be roughly equal to the expectation. |
| 131 |
const double locality_picked_rate_0 = |
| 132 |
static_cast<double>(backends_[0]->backend_service()->request_count()) / |
| 133 |
kNumRpcs; |
| 134 |
const double locality_picked_rate_1 = |
| 135 |
static_cast<double>(backends_[1]->backend_service()->request_count()) / |
| 136 |
kNumRpcs; |
| 137 |
EXPECT_THAT(locality_picked_rate_0, |
| 138 |
::testing::DoubleNear(kLocalityWeightRate0, kErrorTolerance)); |
| 139 |
EXPECT_THAT(locality_picked_rate_1, |
| 140 |
::testing::DoubleNear(kLocalityWeightRate1, kErrorTolerance)); |
| 141 |
} |
| 142 |
|
| 143 |
// In most of our tests, we use different names for different resource |
| 144 |
// types, to make sure that there are no cut-and-paste errors in the code |
| 145 |
// that cause us to look at data for the wrong resource type. So we add |
| 146 |
// this test to make sure that the EDS resource name defaults to the |
| 147 |
// cluster name if not specified in the CDS resource. |
| 148 |
TEST_P(CdsTest, EdsServiceNameDefaultsToClusterName) { |
| 149 |
CreateAndStartBackends(1); |
| 150 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 151 |
balancer_->ads_service()->SetEdsResource( |
| 152 |
BuildEdsResource(args, kDefaultClusterName)); |
| 153 |
Cluster cluster = default_cluster_; |
| 154 |
cluster.mutable_eds_cluster_config()->clear_service_name(); |
| 155 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 156 |
CheckRpcSendOk(DEBUG_LOCATION, /*times=*/1, |
| 157 |
RpcOptions().set_timeout_ms(5000)); |
| 158 |
} |
| 159 |
|
| 160 |
// Tests switching over from one cluster to another. |
| 161 |
TEST_P(CdsTest, ChangeClusters) { |
| 162 |
CreateAndStartBackends(2); |
| 163 |
const char* kNewClusterName = "new_cluster_name"; |
| 164 |
const char* kNewEdsServiceName = "new_eds_service_name"; |
| 165 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 166 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 167 |
// We need to wait for all backends to come online. |
| 168 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 169 |
// Populate new EDS resource. |
| 170 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 171 |
balancer_->ads_service()->SetEdsResource( |
| 172 |
BuildEdsResource(args, kNewEdsServiceName)); |
| 173 |
// Populate new CDS resource. |
| 174 |
Cluster new_cluster = default_cluster_; |
| 175 |
new_cluster.set_name(kNewClusterName); |
| 176 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 177 |
kNewEdsServiceName); |
| 178 |
balancer_->ads_service()->SetCdsResource(new_cluster); |
| 179 |
// Change RDS resource to point to new cluster. |
| 180 |
RouteConfiguration new_route_config = default_route_config_; |
| 181 |
new_route_config.mutable_virtual_hosts(0) |
| 182 |
->mutable_routes(0) |
| 183 |
->mutable_route() |
| 184 |
->set_cluster(kNewClusterName); |
| 185 |
SetListenerAndRouteConfiguration(balancer_.get(), default_listener_, |
| 186 |
new_route_config); |
| 187 |
// Wait for all new backends to be used. |
| 188 |
WaitForAllBackends(DEBUG_LOCATION, 1, 2); |
| 189 |
} |
| 190 |
|
| 191 |
TEST_P(CdsTest, CircuitBreaking) { |
| 192 |
CreateAndStartBackends(1); |
| 193 |
constexpr size_t kMaxConcurrentRequests = 10; |
| 194 |
// Populate new EDS resources. |
| 195 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 196 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 197 |
// Update CDS resource to set max concurrent request. |
| 198 |
CircuitBreakers circuit_breaks; |
| 199 |
Cluster cluster = default_cluster_; |
| 200 |
auto* threshold = cluster.mutable_circuit_breakers()->add_thresholds(); |
| 201 |
threshold->set_priority(RoutingPriority::DEFAULT); |
| 202 |
threshold->mutable_max_requests()->set_value(kMaxConcurrentRequests); |
| 203 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 204 |
// Send exactly max_concurrent_requests long RPCs. |
| 205 |
LongRunningRpc rpcs[kMaxConcurrentRequests]; |
| 206 |
for (size_t i = 0; i < kMaxConcurrentRequests; ++i) { |
| 207 |
rpcs[i].StartRpc(stub_.get()); |
| 208 |
} |
| 209 |
// Wait for all RPCs to be in flight. |
| 210 |
while (backends_[0]->backend_service()->RpcsWaitingForClientCancel() < |
| 211 |
kMaxConcurrentRequests) { |
| 212 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 213 |
gpr_time_from_micros(1 * 1000, GPR_TIMESPAN))); |
| 214 |
} |
| 215 |
// Sending a RPC now should fail, the error message should tell us |
| 216 |
// we hit the max concurrent requests limit and got dropped. |
| 217 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 218 |
"circuit breaker drop"); |
| 219 |
// Cancel one RPC to allow another one through. |
| 220 |
rpcs[0].CancelRpc(); |
| 221 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 222 |
// Clean up. |
| 223 |
for (size_t i = 1; i < kMaxConcurrentRequests; ++i) { |
| 224 |
rpcs[i].CancelRpc(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
TEST_P(CdsTest, CircuitBreakingMultipleChannelsShareCallCounter) { |
| 229 |
CreateAndStartBackends(1); |
| 230 |
constexpr size_t kMaxConcurrentRequests = 10; |
| 231 |
// Populate new EDS resources. |
| 232 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 233 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 234 |
// Update CDS resource to set max concurrent request. |
| 235 |
CircuitBreakers circuit_breaks; |
| 236 |
Cluster cluster = default_cluster_; |
| 237 |
auto* threshold = cluster.mutable_circuit_breakers()->add_thresholds(); |
| 238 |
threshold->set_priority(RoutingPriority::DEFAULT); |
| 239 |
threshold->mutable_max_requests()->set_value(kMaxConcurrentRequests); |
| 240 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 241 |
auto channel2 = CreateChannel(); |
| 242 |
auto stub2 = grpc::testing::EchoTestService::NewStub(channel2); |
| 243 |
// Send exactly max_concurrent_requests long RPCs, alternating between |
| 244 |
// the two channels. |
| 245 |
LongRunningRpc rpcs[kMaxConcurrentRequests]; |
| 246 |
for (size_t i = 0; i < kMaxConcurrentRequests; ++i) { |
| 247 |
rpcs[i].StartRpc(i % 2 == 0 ? stub_.get() : stub2.get()); |
| 248 |
} |
| 249 |
// Wait for all RPCs to be in flight. |
| 250 |
while (backends_[0]->backend_service()->RpcsWaitingForClientCancel() < |
| 251 |
kMaxConcurrentRequests) { |
| 252 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 253 |
gpr_time_from_micros(1 * 1000, GPR_TIMESPAN))); |
| 254 |
} |
| 255 |
// Sending a RPC now should fail, the error message should tell us |
| 256 |
// we hit the max concurrent requests limit and got dropped. |
| 257 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 258 |
"circuit breaker drop"); |
| 259 |
// Cancel one RPC to allow another one through |
| 260 |
rpcs[0].CancelRpc(); |
| 261 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 262 |
// Clean up. |
| 263 |
for (size_t i = 1; i < kMaxConcurrentRequests; ++i) { |
| 264 |
rpcs[i].CancelRpc(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
TEST_P(CdsTest, ClusterChangeAfterAdsCallFails) { |
| 269 |
CreateAndStartBackends(2); |
| 270 |
const char* kNewEdsResourceName = "new_eds_resource_name"; |
| 271 |
// Populate EDS resources. |
| 272 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 273 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 274 |
// Check that the channel is working. |
| 275 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 276 |
// Stop and restart the balancer. |
| 277 |
balancer_->Shutdown(); |
| 278 |
balancer_->Start(); |
| 279 |
// Create new EDS resource. |
| 280 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 281 |
balancer_->ads_service()->SetEdsResource( |
| 282 |
BuildEdsResource(args, kNewEdsResourceName)); |
| 283 |
// Change CDS resource to point to new EDS resource. |
| 284 |
auto cluster = default_cluster_; |
| 285 |
cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName); |
| 286 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 287 |
// Make sure client sees the change. |
| 288 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 289 |
} |
| 290 |
|
| 291 |
// |
| 292 |
// CDS deletion tests |
| 293 |
// |
| 294 |
|
| 295 |
class CdsDeletionTest : public XdsEnd2endTest { |
| 296 |
protected: |
| 297 |
void SetUp() override {} // Individual tests call InitClient(). |
| 298 |
}; |
| 299 |
|
| 300 |
INSTANTIATE_TEST_SUITE_P(XdsTest, CdsDeletionTest, |
| 301 |
::testing::Values(XdsTestType()), &XdsTestType::Name); |
| 302 |
|
| 303 |
// Tests that we go into TRANSIENT_FAILURE if the Cluster is deleted. |
| 304 |
TEST_P(CdsDeletionTest, ClusterDeleted) { |
| 305 |
InitClient(); |
| 306 |
CreateAndStartBackends(1); |
| 307 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 308 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 309 |
// We need to wait for all backends to come online. |
| 310 |
WaitForAllBackends(DEBUG_LOCATION); |
| 311 |
// Unset CDS resource. |
| 312 |
balancer_->ads_service()->UnsetResource(kCdsTypeUrl, kDefaultClusterName); |
| 313 |
// Wait for RPCs to start failing. |
| 314 |
SendRpcsUntil(DEBUG_LOCATION, [](const RpcResult& result) { |
| 315 |
if (result.status.ok()) return true; // Keep going. |
| 316 |
EXPECT_EQ(StatusCode::UNAVAILABLE, result.status.error_code()); |
| 317 |
EXPECT_EQ(absl::StrCat("CDS resource \"", kDefaultClusterName, |
| 318 |
"\" does not exist"), |
| 319 |
result.status.error_message()); |
| 320 |
return false; |
| 321 |
}); |
| 322 |
// Make sure we ACK'ed the update. |
| 323 |
auto response_state = balancer_->ads_service()->cds_response_state(); |
| 324 |
ASSERT_TRUE(response_state.has_value()); |
| 325 |
EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED); |
| 326 |
} |
| 327 |
|
| 328 |
// Tests that we ignore Cluster deletions if configured to do so. |
| 329 |
TEST_P(CdsDeletionTest, ClusterDeletionIgnored) { |
| 330 |
InitClient(BootstrapBuilder().SetIgnoreResourceDeletion()); |
| 331 |
CreateAndStartBackends(2); |
| 332 |
// Bring up client pointing to backend 0 and wait for it to connect. |
| 333 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 334 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 335 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1); |
| 336 |
// Make sure we ACKed the CDS update. |
| 337 |
auto response_state = balancer_->ads_service()->cds_response_state(); |
| 338 |
ASSERT_TRUE(response_state.has_value()); |
| 339 |
EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED); |
| 340 |
// Unset CDS resource and wait for client to ACK the update. |
| 341 |
balancer_->ads_service()->UnsetResource(kCdsTypeUrl, kDefaultClusterName); |
| 342 |
const auto deadline = absl::Now() + absl::Seconds(30); |
| 343 |
while (true) { |
| 344 |
ASSERT_LT(absl::Now(), deadline) << "timed out waiting for CDS ACK"; |
| 345 |
response_state = balancer_->ads_service()->cds_response_state(); |
| 346 |
if (response_state.has_value()) break; |
| 347 |
} |
| 348 |
EXPECT_EQ(response_state->state, AdsServiceImpl::ResponseState::ACKED); |
| 349 |
// Make sure we can still send RPCs. |
| 350 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 351 |
// Now recreate the CDS resource pointing to a new EDS resource that |
| 352 |
// specified backend 1, and make sure the client uses it. |
| 353 |
const char* kNewEdsResourceName = "new_eds_resource_name"; |
| 354 |
auto cluster = default_cluster_; |
| 355 |
cluster.mutable_eds_cluster_config()->set_service_name(kNewEdsResourceName); |
| 356 |
balancer_->ads_service()->SetCdsResource(cluster); |
| 357 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 358 |
balancer_->ads_service()->SetEdsResource( |
| 359 |
BuildEdsResource(args, kNewEdsResourceName)); |
| 360 |
// Wait for client to start using backend 1. |
| 361 |
WaitForAllBackends(DEBUG_LOCATION, 1, 2); |
| 362 |
} |
| 363 |
|
| 364 |
// |
| 365 |
// EDS tests |
| 366 |
// |
| 367 |
|
| 368 |
using EdsTest = XdsEnd2endTest; |
| 369 |
|
| 370 |
INSTANTIATE_TEST_SUITE_P( |
| 371 |
XdsTest, EdsTest, |
| 372 |
::testing::Values(XdsTestType(), XdsTestType().set_enable_load_reporting()), |
| 373 |
&XdsTestType::Name); |
| 374 |
|
| 375 |
// Tests that the balancer sends the correct response to the client, and the |
| 376 |
// client sends RPCs to the backends using the default child policy. |
| 377 |
TEST_P(EdsTest, Vanilla) { |
| 378 |
CreateAndStartBackends(3); |
| 379 |
const size_t kNumRpcsPerAddress = 100; |
| 380 |
EdsResourceArgs args({ |
| 381 |
{"locality0", CreateEndpointsForBackends()}, |
| 382 |
}); |
| 383 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 384 |
// Make sure that trying to connect works without a call. |
| 385 |
channel_->GetState(true /* try_to_connect */); |
| 386 |
// We need to wait for all backends to come online. |
| 387 |
WaitForAllBackends(DEBUG_LOCATION); |
| 388 |
// Send kNumRpcsPerAddress RPCs per server. |
| 389 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsPerAddress * backends_.size()); |
| 390 |
// Each backend should have gotten 100 requests. |
| 391 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 392 |
EXPECT_EQ(kNumRpcsPerAddress, |
| 393 |
backends_[i]->backend_service()->request_count()); |
| 394 |
} |
| 395 |
// Check LB policy name for the channel. |
| 396 |
EXPECT_EQ("xds_cluster_manager_experimental", |
| 397 |
channel_->GetLoadBalancingPolicyName()); |
| 398 |
} |
| 399 |
|
| 400 |
TEST_P(EdsTest, IgnoresUnhealthyEndpoints) { |
| 401 |
CreateAndStartBackends(2); |
| 402 |
const size_t kNumRpcsPerAddress = 100; |
| 403 |
auto endpoints = CreateEndpointsForBackends(); |
| 404 |
endpoints.push_back(MakeNonExistantEndpoint()); |
| 405 |
endpoints.back().health_status = HealthStatus::DRAINING; |
| 406 |
EdsResourceArgs args({ |
| 407 |
{"locality0", std::move(endpoints), kDefaultLocalityWeight, |
| 408 |
kDefaultLocalityPriority}, |
| 409 |
}); |
| 410 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 411 |
// Make sure that trying to connect works without a call. |
| 412 |
channel_->GetState(true /* try_to_connect */); |
| 413 |
// We need to wait for all backends to come online. |
| 414 |
WaitForAllBackends(DEBUG_LOCATION); |
| 415 |
// Send kNumRpcsPerAddress RPCs per server. |
| 416 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsPerAddress * backends_.size()); |
| 417 |
// Each backend should have gotten 100 requests. |
| 418 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 419 |
EXPECT_EQ(kNumRpcsPerAddress, |
| 420 |
backends_[i]->backend_service()->request_count()); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
TEST_P(EdsTest, OneLocalityWithNoEndpoints) { |
| 425 |
CreateAndStartBackends(1); |
| 426 |
// Initial EDS resource has one locality with no endpoints. |
| 427 |
EdsResourceArgs::Locality empty_locality("locality0", {}); |
| 428 |
EdsResourceArgs args({std::move(empty_locality)}); |
| 429 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 430 |
// RPCs should fail. |
| 431 |
constexpr char kErrorMessage[] = |
| 432 |
"no children in weighted_target policy: " |
| 433 |
"EDS resource eds_service_name contains empty localities: " |
| 434 |
"\\[\\{region=\"xds_default_locality_region\", " |
| 435 |
"zone=\"xds_default_locality_zone\", sub_zone=\"locality0\"\\}\\]"; |
| 436 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, kErrorMessage); |
| 437 |
// Send EDS resource that has an endpoint. |
| 438 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends()}}); |
| 439 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 440 |
// RPCs should eventually succeed. |
| 441 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1, [&](const RpcResult& result) { |
| 442 |
if (!result.status.ok()) { |
| 443 |
EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE); |
| 444 |
EXPECT_THAT(result.status.error_message(), |
| 445 |
::testing::MatchesRegex(kErrorMessage)); |
| 446 |
} |
| 447 |
}); |
| 448 |
} |
| 449 |
|
| 450 |
TEST_P(EdsTest, NoLocalities) { |
| 451 |
CreateAndStartBackends(1); |
| 452 |
// Initial EDS resource has no localities. |
| 453 |
EdsResourceArgs args; |
| 454 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 455 |
// RPCs should fail. |
| 456 |
constexpr char kErrorMessage[] = |
| 457 |
"no children in weighted_target policy: EDS resource eds_service_name " |
| 458 |
"contains no localities"; |
| 459 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, kErrorMessage); |
| 460 |
// Send EDS resource that has an endpoint. |
| 461 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends()}}); |
| 462 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 463 |
// RPCs should eventually succeed. |
| 464 |
WaitForAllBackends(DEBUG_LOCATION, 0, 1, [&](const RpcResult& result) { |
| 465 |
if (!result.status.ok()) { |
| 466 |
EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE); |
| 467 |
EXPECT_THAT(result.status.error_message(), |
| 468 |
::testing::MatchesRegex(kErrorMessage)); |
| 469 |
} |
| 470 |
}); |
| 471 |
} |
| 472 |
|
| 473 |
// Tests that RPCs will fail with UNAVAILABLE instead of DEADLINE_EXCEEDED if |
| 474 |
// all the servers are unreachable. |
| 475 |
TEST_P(EdsTest, AllServersUnreachableFailFast) { |
| 476 |
// Set Rpc timeout to 5 seconds to ensure there is enough time |
| 477 |
// for communication with the xDS server to take place upon test start up. |
| 478 |
const uint32_t kRpcTimeoutMs = 5000; |
| 479 |
const size_t kNumUnreachableServers = 5; |
| 480 |
std::vector<EdsResourceArgs::Endpoint> endpoints; |
| 481 |
for (size_t i = 0; i < kNumUnreachableServers; ++i) { |
| 482 |
endpoints.emplace_back(MakeNonExistantEndpoint()); |
| 483 |
} |
| 484 |
EdsResourceArgs args({{"locality0", std::move(endpoints)}}); |
| 485 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 486 |
// The error shouldn't be DEADLINE_EXCEEDED because timeout is set to 5 |
| 487 |
// seconds, and we should disocver in that time that the target backend is |
| 488 |
// down. |
| 489 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 490 |
"connections to all backends failing; last error: " |
| 491 |
"(UNKNOWN: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 492 |
"Failed to connect to remote host: Connection refused|" |
| 493 |
"UNAVAILABLE: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 494 |
"Failed to connect to remote host: FD shutdown)", |
| 495 |
RpcOptions().set_timeout_ms(kRpcTimeoutMs)); |
| 496 |
} |
| 497 |
|
| 498 |
// Tests that RPCs fail when the backends are down, and will succeed again |
| 499 |
// after the backends are restarted. |
| 500 |
TEST_P(EdsTest, BackendsRestart) { |
| 501 |
CreateAndStartBackends(3); |
| 502 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 503 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 504 |
WaitForAllBackends(DEBUG_LOCATION); |
| 505 |
// Stop backends. RPCs should fail. |
| 506 |
ShutdownAllBackends(); |
| 507 |
// Wait for channel to transition out of READY, so that we know it has |
| 508 |
// noticed that all of the subchannels have failed. Note that it may |
| 509 |
// be reporting either CONNECTING or TRANSIENT_FAILURE at this point. |
| 510 |
EXPECT_TRUE(channel_->WaitForStateChange( |
| 511 |
GRPC_CHANNEL_READY, grpc_timeout_seconds_to_deadline(5))); |
| 512 |
EXPECT_THAT(channel_->GetState(false), |
| 513 |
::testing::AnyOf(::testing::Eq(GRPC_CHANNEL_TRANSIENT_FAILURE), |
| 514 |
::testing::Eq(GRPC_CHANNEL_CONNECTING))); |
| 515 |
// RPCs should fail. |
| 516 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 517 |
"connections to all backends failing; last error: " |
| 518 |
"(UNKNOWN: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 519 |
"Failed to connect to remote host: Connection refused|" |
| 520 |
"UNAVAILABLE: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 521 |
"Failed to connect to remote host: FD shutdown)"); |
| 522 |
// Restart all backends. RPCs should start succeeding again. |
| 523 |
StartAllBackends(); |
| 524 |
CheckRpcSendOk(DEBUG_LOCATION, 1, |
| 525 |
RpcOptions().set_timeout_ms(2000).set_wait_for_ready(true)); |
| 526 |
} |
| 527 |
|
| 528 |
TEST_P(EdsTest, IgnoresDuplicateUpdates) { |
| 529 |
CreateAndStartBackends(1); |
| 530 |
const size_t kNumRpcsPerAddress = 100; |
| 531 |
EdsResourceArgs args({ |
| 532 |
{"locality0", CreateEndpointsForBackends()}, |
| 533 |
}); |
| 534 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 535 |
// Wait for all backends to come online. |
| 536 |
WaitForAllBackends(DEBUG_LOCATION); |
| 537 |
// Send kNumRpcsPerAddress RPCs per server, but send an EDS update in |
| 538 |
// between. If the update is not ignored, this will cause the |
| 539 |
// round_robin policy to see an update, which will randomly reset its |
| 540 |
// position in the address list. |
| 541 |
for (size_t i = 0; i < kNumRpcsPerAddress; ++i) { |
| 542 |
CheckRpcSendOk(DEBUG_LOCATION, 2); |
| 543 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 544 |
CheckRpcSendOk(DEBUG_LOCATION, 2); |
| 545 |
} |
| 546 |
// Each backend should have gotten the right number of requests. |
| 547 |
for (size_t i = 1; i < backends_.size(); ++i) { |
| 548 |
EXPECT_EQ(kNumRpcsPerAddress, |
| 549 |
backends_[i]->backend_service()->request_count()); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
// Testing just one example of an invalid resource here. |
| 554 |
// Unit tests for XdsEndpointResourceType have exhaustive tests for all |
| 555 |
// of the invalid cases. |
| 556 |
TEST_P(EdsTest, NacksInvalidResource) { |
| 557 |
EdsResourceArgs args({ |
| 558 |
{"locality0", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 1}, |
| 559 |
}); |
| 560 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 561 |
const auto response_state = WaitForEdsNack(DEBUG_LOCATION); |
| 562 |
ASSERT_TRUE(response_state.has_value()) << "timed out waiting for NACK"; |
| 563 |
EXPECT_EQ(response_state->error_message, |
| 564 |
"xDS response validation errors: [" |
| 565 |
"resource index 0: eds_service_name: " |
| 566 |
"INVALID_ARGUMENT: errors parsing EDS resource: [" |
| 567 |
"field:endpoints error:priority 0 empty]]"); |
| 568 |
} |
| 569 |
|
| 570 |
// Tests that if the balancer is down, the RPCs will still be sent to the |
| 571 |
// backends according to the last balancer response, until a new balancer is |
| 572 |
// reachable. |
| 573 |
TEST_P(EdsTest, KeepUsingLastDataIfBalancerGoesDown) { |
| 574 |
CreateAndStartBackends(2); |
| 575 |
// Set up EDS resource pointing to backend 0. |
| 576 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 577 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 578 |
// Start the client and make sure it sees the backend. |
| 579 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 580 |
// Stop the balancer, and verify that RPCs continue to flow to backend 0. |
| 581 |
balancer_->Shutdown(); |
| 582 |
auto deadline = grpc_timeout_seconds_to_deadline(5); |
| 583 |
do { |
| 584 |
CheckRpcSendOk(DEBUG_LOCATION); |
| 585 |
} while (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) < 0); |
| 586 |
// Check the EDS resource to point to backend 1 and bring the balancer |
| 587 |
// back up. |
| 588 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(1, 2)}}); |
| 589 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 590 |
balancer_->Start(); |
| 591 |
// Wait for client to see backend 1. |
| 592 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 593 |
} |
| 594 |
|
| 595 |
// Tests that the localities in a locality map are picked according to their |
| 596 |
// weights. |
| 597 |
TEST_P(EdsTest, WeightedRoundRobin) { |
| 598 |
CreateAndStartBackends(2); |
| 599 |
const int kLocalityWeight0 = 2; |
| 600 |
const int kLocalityWeight1 = 8; |
| 601 |
const int kTotalLocalityWeight = kLocalityWeight0 + kLocalityWeight1; |
| 602 |
const double kLocalityWeightRate0 = |
| 603 |
static_cast<double>(kLocalityWeight0) / kTotalLocalityWeight; |
| 604 |
const double kLocalityWeightRate1 = |
| 605 |
static_cast<double>(kLocalityWeight1) / kTotalLocalityWeight; |
| 606 |
const double kErrorTolerance = 0.05; |
| 607 |
const size_t kNumRpcs = |
| 608 |
ComputeIdealNumRpcs(kLocalityWeightRate0, kErrorTolerance); |
| 609 |
// ADS response contains 2 localities, each of which contains 1 backend. |
| 610 |
EdsResourceArgs args({ |
| 611 |
{"locality0", CreateEndpointsForBackends(0, 1), kLocalityWeight0}, |
| 612 |
{"locality1", CreateEndpointsForBackends(1, 2), kLocalityWeight1}, |
| 613 |
}); |
| 614 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 615 |
// Wait for both backends to be ready. |
| 616 |
WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 617 |
// Send kNumRpcs RPCs. |
| 618 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 619 |
// The locality picking rates should be roughly equal to the expectation. |
| 620 |
const double locality_picked_rate_0 = |
| 621 |
static_cast<double>(backends_[0]->backend_service()->request_count()) / |
| 622 |
kNumRpcs; |
| 623 |
const double locality_picked_rate_1 = |
| 624 |
static_cast<double>(backends_[1]->backend_service()->request_count()) / |
| 625 |
kNumRpcs; |
| 626 |
EXPECT_THAT(locality_picked_rate_0, |
| 627 |
::testing::DoubleNear(kLocalityWeightRate0, kErrorTolerance)); |
| 628 |
EXPECT_THAT(locality_picked_rate_1, |
| 629 |
::testing::DoubleNear(kLocalityWeightRate1, kErrorTolerance)); |
| 630 |
} |
| 631 |
|
| 632 |
// Tests that we don't suffer from integer overflow in locality weights. |
| 633 |
TEST_P(EdsTest, NoIntegerOverflowInLocalityWeights) { |
| 634 |
CreateAndStartBackends(2); |
| 635 |
const uint32_t kLocalityWeight1 = std::numeric_limits<uint32_t>::max() / 3; |
| 636 |
const uint32_t kLocalityWeight0 = |
| 637 |
std::numeric_limits<uint32_t>::max() - kLocalityWeight1; |
| 638 |
const uint64_t kTotalLocalityWeight = |
| 639 |
static_cast<uint64_t>(kLocalityWeight0) + |
| 640 |
static_cast<uint64_t>(kLocalityWeight1); |
| 641 |
const double kLocalityWeightRate0 = |
| 642 |
static_cast<double>(kLocalityWeight0) / kTotalLocalityWeight; |
| 643 |
const double kLocalityWeightRate1 = |
| 644 |
static_cast<double>(kLocalityWeight1) / kTotalLocalityWeight; |
| 645 |
const double kErrorTolerance = 0.05; |
| 646 |
const size_t kNumRpcs = |
| 647 |
ComputeIdealNumRpcs(kLocalityWeightRate0, kErrorTolerance); |
| 648 |
// ADS response contains 2 localities, each of which contains 1 backend. |
| 649 |
EdsResourceArgs args({ |
| 650 |
{"locality0", CreateEndpointsForBackends(0, 1), kLocalityWeight0}, |
| 651 |
{"locality1", CreateEndpointsForBackends(1, 2), kLocalityWeight1}, |
| 652 |
}); |
| 653 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 654 |
// Wait for both backends to be ready. |
| 655 |
WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 656 |
// Send kNumRpcs RPCs. |
| 657 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 658 |
// The locality picking rates should be roughly equal to the expectation. |
| 659 |
const double locality_picked_rate_0 = |
| 660 |
static_cast<double>(backends_[0]->backend_service()->request_count()) / |
| 661 |
kNumRpcs; |
| 662 |
const double locality_picked_rate_1 = |
| 663 |
static_cast<double>(backends_[1]->backend_service()->request_count()) / |
| 664 |
kNumRpcs; |
| 665 |
EXPECT_THAT(locality_picked_rate_0, |
| 666 |
::testing::DoubleNear(kLocalityWeightRate0, kErrorTolerance)); |
| 667 |
EXPECT_THAT(locality_picked_rate_1, |
| 668 |
::testing::DoubleNear(kLocalityWeightRate1, kErrorTolerance)); |
| 669 |
} |
| 670 |
|
| 671 |
// Tests that we correctly handle a locality containing no endpoints. |
| 672 |
TEST_P(EdsTest, LocalityContainingNoEndpoints) { |
| 673 |
CreateAndStartBackends(2); |
| 674 |
const size_t kNumRpcs = 5000; |
| 675 |
// EDS response contains 2 localities, one with no endpoints. |
| 676 |
EdsResourceArgs args({ |
| 677 |
{"locality0", CreateEndpointsForBackends()}, |
| 678 |
{"locality1", {}}, |
| 679 |
}); |
| 680 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 681 |
// Wait for both backends to be ready. |
| 682 |
WaitForAllBackends(DEBUG_LOCATION); |
| 683 |
// Send kNumRpcs RPCs. |
| 684 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 685 |
// All traffic should go to the reachable locality. |
| 686 |
EXPECT_EQ(backends_[0]->backend_service()->request_count(), |
| 687 |
kNumRpcs / backends_.size()); |
| 688 |
EXPECT_EQ(backends_[1]->backend_service()->request_count(), |
| 689 |
kNumRpcs / backends_.size()); |
| 690 |
} |
| 691 |
|
| 692 |
// Tests that the locality map can work properly even when it contains a large |
| 693 |
// number of localities. |
| 694 |
TEST_P(EdsTest, ManyLocalitiesStressTest) { |
| 695 |
const size_t kNumLocalities = 50; |
| 696 |
CreateAndStartBackends(kNumLocalities + 1); |
| 697 |
const uint32_t kRpcTimeoutMs = 5000; |
| 698 |
// The first ADS response contains kNumLocalities localities, each of which |
| 699 |
// contains its own backend. |
| 700 |
EdsResourceArgs args; |
| 701 |
for (size_t i = 0; i < kNumLocalities; ++i) { |
| 702 |
std::string name = absl::StrCat("locality", i); |
| 703 |
EdsResourceArgs::Locality locality(name, |
| 704 |
CreateEndpointsForBackends(i, i + 1)); |
| 705 |
args.locality_list.emplace_back(std::move(locality)); |
| 706 |
} |
| 707 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 708 |
// Wait until all backends are ready. |
| 709 |
WaitForAllBackends(DEBUG_LOCATION, 0, kNumLocalities, |
| 710 |
/*check_status=*/nullptr, |
| 711 |
WaitForBackendOptions().set_reset_counters(false), |
| 712 |
RpcOptions().set_timeout_ms(kRpcTimeoutMs)); |
| 713 |
// The second ADS response contains 1 locality, which contains backend 50. |
| 714 |
args = |
| 715 |
EdsResourceArgs({{"locality0", CreateEndpointsForBackends( |
| 716 |
kNumLocalities, kNumLocalities + 1)}}); |
| 717 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 718 |
// Wait until backend 50 is ready. |
| 719 |
WaitForBackend(DEBUG_LOCATION, kNumLocalities); |
| 720 |
} |
| 721 |
|
| 722 |
// Tests that the localities in a locality map are picked correctly after |
| 723 |
// update (addition, modification, deletion). |
| 724 |
TEST_P(EdsTest, LocalityMapUpdateChurn) { |
| 725 |
CreateAndStartBackends(4); |
| 726 |
const size_t kNumRpcs = 3000; |
| 727 |
// The locality weight for the first 3 localities. |
| 728 |
const std::vector<int> kLocalityWeights0 = {2, 3, 4}; |
| 729 |
const double kTotalLocalityWeight0 = |
| 730 |
std::accumulate(kLocalityWeights0.begin(), kLocalityWeights0.end(), 0); |
| 731 |
std::vector<double> locality_weight_rate_0; |
| 732 |
locality_weight_rate_0.reserve(kLocalityWeights0.size()); |
| 733 |
for (int weight : kLocalityWeights0) { |
| 734 |
locality_weight_rate_0.push_back(weight / kTotalLocalityWeight0); |
| 735 |
} |
| 736 |
// Delete the first locality, keep the second locality, change the third |
| 737 |
// locality's weight from 4 to 2, and add a new locality with weight 6. |
| 738 |
const std::vector<int> kLocalityWeights1 = {3, 2, 6}; |
| 739 |
const double kTotalLocalityWeight1 = |
| 740 |
std::accumulate(kLocalityWeights1.begin(), kLocalityWeights1.end(), 0); |
| 741 |
std::vector<double> locality_weight_rate_1 = { |
| 742 |
0 /* placeholder for locality 0 */}; |
| 743 |
for (int weight : kLocalityWeights1) { |
| 744 |
locality_weight_rate_1.push_back(weight / kTotalLocalityWeight1); |
| 745 |
} |
| 746 |
EdsResourceArgs args({ |
| 747 |
{"locality0", CreateEndpointsForBackends(0, 1), 2}, |
| 748 |
{"locality1", CreateEndpointsForBackends(1, 2), 3}, |
| 749 |
{"locality2", CreateEndpointsForBackends(2, 3), 4}, |
| 750 |
}); |
| 751 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 752 |
// Wait for the first 3 backends to be ready. |
| 753 |
WaitForAllBackends(DEBUG_LOCATION, 0, 3); |
| 754 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 755 |
// Send kNumRpcs RPCs. |
| 756 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 757 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 758 |
// The picking rates of the first 3 backends should be roughly equal to the |
| 759 |
// expectation. |
| 760 |
std::vector<double> locality_picked_rates; |
| 761 |
for (size_t i = 0; i < 3; ++i) { |
| 762 |
locality_picked_rates.push_back( |
| 763 |
static_cast<double>(backends_[i]->backend_service()->request_count()) / |
| 764 |
kNumRpcs); |
| 765 |
} |
| 766 |
const double kErrorTolerance = 0.2; |
| 767 |
for (size_t i = 0; i < 3; ++i) { |
| 768 |
gpr_log(GPR_INFO, "Locality %" PRIuPTR " rate %f", i, |
| 769 |
locality_picked_rates[i]); |
| 770 |
EXPECT_THAT( |
| 771 |
locality_picked_rates[i], |
| 772 |
::testing::AllOf( |
| 773 |
::testing::Ge(locality_weight_rate_0[i] * (1 - kErrorTolerance)), |
| 774 |
::testing::Le(locality_weight_rate_0[i] * (1 + kErrorTolerance)))); |
| 775 |
} |
| 776 |
args = EdsResourceArgs({ |
| 777 |
{"locality1", CreateEndpointsForBackends(1, 2), 3}, |
| 778 |
{"locality2", CreateEndpointsForBackends(2, 3), 2}, |
| 779 |
{"locality3", CreateEndpointsForBackends(3, 4), 6}, |
| 780 |
}); |
| 781 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 782 |
// Backend 3 hasn't received any request. |
| 783 |
EXPECT_EQ(0U, backends_[3]->backend_service()->request_count()); |
| 784 |
// Wait until the locality update has been processed, as signaled by backend |
| 785 |
// 3 receiving a request. |
| 786 |
WaitForAllBackends(DEBUG_LOCATION, 3, 4); |
| 787 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 788 |
// Send kNumRpcs RPCs. |
| 789 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 790 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 791 |
// Backend 0 no longer receives any request. |
| 792 |
EXPECT_EQ(0U, backends_[0]->backend_service()->request_count()); |
| 793 |
// The picking rates of the last 3 backends should be roughly equal to the |
| 794 |
// expectation. |
| 795 |
locality_picked_rates = {0 /* placeholder for backend 0 */}; |
| 796 |
for (size_t i = 1; i < 4; ++i) { |
| 797 |
locality_picked_rates.push_back( |
| 798 |
static_cast<double>(backends_[i]->backend_service()->request_count()) / |
| 799 |
kNumRpcs); |
| 800 |
} |
| 801 |
for (size_t i = 1; i < 4; ++i) { |
| 802 |
gpr_log(GPR_INFO, "Locality %" PRIuPTR " rate %f", i, |
| 803 |
locality_picked_rates[i]); |
| 804 |
EXPECT_THAT( |
| 805 |
locality_picked_rates[i], |
| 806 |
::testing::AllOf( |
| 807 |
::testing::Ge(locality_weight_rate_1[i] * (1 - kErrorTolerance)), |
| 808 |
::testing::Le(locality_weight_rate_1[i] * (1 + kErrorTolerance)))); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
// Tests that we don't fail RPCs when replacing all of the localities in |
| 813 |
// a given priority. |
| 814 |
TEST_P(EdsTest, ReplaceAllLocalitiesInPriority) { |
| 815 |
CreateAndStartBackends(2); |
| 816 |
// Initial EDS update has backend 0. |
| 817 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 1)}}); |
| 818 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 819 |
// Wait for the first backend to be ready. |
| 820 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 821 |
// Send EDS update that replaces the locality and switches to backend 1. |
| 822 |
args = EdsResourceArgs({{"locality1", CreateEndpointsForBackends(1, 2)}}); |
| 823 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 824 |
// When the client sees the update, RPCs should start going to backend 1. |
| 825 |
// No RPCs should fail during this change. |
| 826 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 827 |
} |
| 828 |
|
| 829 |
TEST_P(EdsTest, ConsistentWeightedTargetUpdates) { |
| 830 |
CreateAndStartBackends(4); |
| 831 |
// Initial update has two localities. |
| 832 |
EdsResourceArgs args({ |
| 833 |
{"locality0", CreateEndpointsForBackends(1, 2)}, |
| 834 |
{"locality1", CreateEndpointsForBackends(2, 3)}, |
| 835 |
}); |
| 836 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 837 |
WaitForAllBackends(DEBUG_LOCATION, 1, 3); |
| 838 |
// Next update removes locality1. |
| 839 |
// Also add backend 0 to locality0, so that we can tell when the |
| 840 |
// update has been seen. |
| 841 |
args = EdsResourceArgs({ |
| 842 |
{"locality0", CreateEndpointsForBackends(0, 2)}, |
| 843 |
}); |
| 844 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 845 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 846 |
// Next update re-adds locality1. |
| 847 |
// Also add backend 3 to locality1, so that we can tell when the |
| 848 |
// update has been seen. |
| 849 |
args = EdsResourceArgs({ |
| 850 |
{"locality0", CreateEndpointsForBackends(0, 2)}, |
| 851 |
{"locality1", CreateEndpointsForBackends(2, 4)}, |
| 852 |
}); |
| 853 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 854 |
WaitForBackend(DEBUG_LOCATION, 3); |
| 855 |
} |
| 856 |
|
| 857 |
// Tests that RPCs are dropped according to the drop config. |
| 858 |
TEST_P(EdsTest, Drops) { |
| 859 |
CreateAndStartBackends(1); |
| 860 |
const uint32_t kDropPerMillionForLb = 100000; |
| 861 |
const uint32_t kDropPerMillionForThrottle = 200000; |
| 862 |
const double kDropRateForLb = kDropPerMillionForLb / 1000000.0; |
| 863 |
const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0; |
| 864 |
const double kDropRateForLbAndThrottle = |
| 865 |
kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle; |
| 866 |
const double kErrorTolerance = 0.05; |
| 867 |
const size_t kNumRpcs = |
| 868 |
ComputeIdealNumRpcs(kDropRateForLbAndThrottle, kErrorTolerance); |
| 869 |
// The ADS response contains two drop categories. |
| 870 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 871 |
args.drop_categories = {{kLbDropType, kDropPerMillionForLb}, |
| 872 |
{kThrottleDropType, kDropPerMillionForThrottle}}; |
| 873 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 874 |
// Send kNumRpcs RPCs and count the drops. |
| 875 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 876 |
DEBUG_LOCATION, kNumRpcs, StatusCode::UNAVAILABLE, |
| 877 |
kStatusMessageDropPrefix); |
| 878 |
// The drop rate should be roughly equal to the expectation. |
| 879 |
const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs; |
| 880 |
EXPECT_THAT(seen_drop_rate, ::testing::DoubleNear(kDropRateForLbAndThrottle, |
| 881 |
kErrorTolerance)); |
| 882 |
} |
| 883 |
|
| 884 |
// Tests that drop config is converted correctly from per hundred. |
| 885 |
TEST_P(EdsTest, DropPerHundred) { |
| 886 |
CreateAndStartBackends(1); |
| 887 |
const uint32_t kDropPerHundredForLb = 10; |
| 888 |
const double kDropRateForLb = kDropPerHundredForLb / 100.0; |
| 889 |
const double kErrorTolerance = 0.05; |
| 890 |
const size_t kNumRpcs = ComputeIdealNumRpcs(kDropRateForLb, kErrorTolerance); |
| 891 |
// The ADS response contains one drop category. |
| 892 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 893 |
args.drop_categories = {{kLbDropType, kDropPerHundredForLb}}; |
| 894 |
args.drop_denominator = FractionalPercent::HUNDRED; |
| 895 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 896 |
// Send kNumRpcs RPCs and count the drops. |
| 897 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 898 |
DEBUG_LOCATION, kNumRpcs, StatusCode::UNAVAILABLE, |
| 899 |
kStatusMessageDropPrefix); |
| 900 |
// The drop rate should be roughly equal to the expectation. |
| 901 |
const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs; |
| 902 |
EXPECT_THAT(seen_drop_rate, |
| 903 |
::testing::DoubleNear(kDropRateForLb, kErrorTolerance)); |
| 904 |
} |
| 905 |
|
| 906 |
// Tests that drop config is converted correctly from per ten thousand. |
| 907 |
TEST_P(EdsTest, DropPerTenThousand) { |
| 908 |
CreateAndStartBackends(1); |
| 909 |
const uint32_t kDropPerTenThousandForLb = 1000; |
| 910 |
const double kDropRateForLb = kDropPerTenThousandForLb / 10000.0; |
| 911 |
const double kErrorTolerance = 0.05; |
| 912 |
const size_t kNumRpcs = ComputeIdealNumRpcs(kDropRateForLb, kErrorTolerance); |
| 913 |
// The ADS response contains one drop category. |
| 914 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 915 |
args.drop_categories = {{kLbDropType, kDropPerTenThousandForLb}}; |
| 916 |
args.drop_denominator = FractionalPercent::TEN_THOUSAND; |
| 917 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 918 |
// Send kNumRpcs RPCs and count the drops. |
| 919 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 920 |
DEBUG_LOCATION, kNumRpcs, StatusCode::UNAVAILABLE, |
| 921 |
kStatusMessageDropPrefix); |
| 922 |
// The drop rate should be roughly equal to the expectation. |
| 923 |
const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs; |
| 924 |
EXPECT_THAT(seen_drop_rate, |
| 925 |
::testing::DoubleNear(kDropRateForLb, kErrorTolerance)); |
| 926 |
} |
| 927 |
|
| 928 |
// Tests that drop is working correctly after update. |
| 929 |
TEST_P(EdsTest, DropConfigUpdate) { |
| 930 |
CreateAndStartBackends(1); |
| 931 |
const uint32_t kDropPerMillionForLb = 100000; |
| 932 |
const uint32_t kDropPerMillionForThrottle = 200000; |
| 933 |
const double kErrorTolerance = 0.05; |
| 934 |
const double kDropRateForLb = kDropPerMillionForLb / 1000000.0; |
| 935 |
const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0; |
| 936 |
const double kDropRateForLbAndThrottle = |
| 937 |
kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle; |
| 938 |
const size_t kNumRpcsLbOnly = |
| 939 |
ComputeIdealNumRpcs(kDropRateForLb, kErrorTolerance); |
| 940 |
const size_t kNumRpcsBoth = |
| 941 |
ComputeIdealNumRpcs(kDropRateForLbAndThrottle, kErrorTolerance); |
| 942 |
// The first ADS response contains one drop category. |
| 943 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 944 |
args.drop_categories = {{kLbDropType, kDropPerMillionForLb}}; |
| 945 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 946 |
// Send kNumRpcsLbOnly RPCs and count the drops. |
| 947 |
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); |
| 948 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 949 |
DEBUG_LOCATION, kNumRpcsLbOnly, StatusCode::UNAVAILABLE, |
| 950 |
kStatusMessageDropPrefix); |
| 951 |
gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH =========="); |
| 952 |
// The drop rate should be roughly equal to the expectation. |
| 953 |
double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcsLbOnly; |
| 954 |
gpr_log(GPR_INFO, "First batch drop rate %f", seen_drop_rate); |
| 955 |
EXPECT_THAT(seen_drop_rate, |
| 956 |
::testing::DoubleNear(kDropRateForLb, kErrorTolerance)); |
| 957 |
// The second ADS response contains two drop categories, send an update EDS |
| 958 |
// response. |
| 959 |
args.drop_categories = {{kLbDropType, kDropPerMillionForLb}, |
| 960 |
{kThrottleDropType, kDropPerMillionForThrottle}}; |
| 961 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 962 |
// Wait until the drop rate increases to the middle of the two configs, |
| 963 |
// which implies that the update has been in effect. |
| 964 |
const double kDropRateThreshold = |
| 965 |
(kDropRateForLb + kDropRateForLbAndThrottle) / 2; |
| 966 |
size_t num_rpcs = kNumRpcsBoth; |
| 967 |
SendRpcsUntil( |
| 968 |
DEBUG_LOCATION, |
| 969 |
[&](const RpcResult& result) { |
| 970 |
++num_rpcs; |
| 971 |
if (result.status.ok()) { |
| 972 |
EXPECT_EQ(result.response.message(), kRequestMessage); |
| 973 |
} else { |
| 974 |
EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE); |
| 975 |
EXPECT_THAT(result.status.error_message(), |
| 976 |
::testing::StartsWith(kStatusMessageDropPrefix)); |
| 977 |
++num_drops; |
| 978 |
} |
| 979 |
seen_drop_rate = static_cast<double>(num_drops) / num_rpcs; |
| 980 |
return seen_drop_rate < kDropRateThreshold; |
| 981 |
}, |
| 982 |
/*timeout_ms=*/40000); |
| 983 |
// Send kNumRpcsBoth RPCs and count the drops. |
| 984 |
gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH =========="); |
| 985 |
num_drops = SendRpcsAndCountFailuresWithMessage(DEBUG_LOCATION, kNumRpcsBoth, |
| 986 |
StatusCode::UNAVAILABLE, |
| 987 |
kStatusMessageDropPrefix); |
| 988 |
gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH =========="); |
| 989 |
// The new drop rate should be roughly equal to the expectation. |
| 990 |
seen_drop_rate = static_cast<double>(num_drops) / kNumRpcsBoth; |
| 991 |
gpr_log(GPR_INFO, "Second batch drop rate %f", seen_drop_rate); |
| 992 |
EXPECT_THAT(seen_drop_rate, ::testing::DoubleNear(kDropRateForLbAndThrottle, |
| 993 |
kErrorTolerance)); |
| 994 |
} |
| 995 |
|
| 996 |
// Tests that all the RPCs are dropped if any drop category drops 100%. |
| 997 |
TEST_P(EdsTest, DropAll) { |
| 998 |
const size_t kNumRpcs = 1000; |
| 999 |
const uint32_t kDropPerMillionForLb = 100000; |
| 1000 |
const uint32_t kDropPerMillionForThrottle = 1000000; |
| 1001 |
// The ADS response contains two drop categories. |
| 1002 |
EdsResourceArgs args; |
| 1003 |
args.drop_categories = {{kLbDropType, kDropPerMillionForLb}, |
| 1004 |
{kThrottleDropType, kDropPerMillionForThrottle}}; |
| 1005 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1006 |
// Send kNumRpcs RPCs and all of them are dropped. |
| 1007 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 1008 |
DEBUG_LOCATION, kNumRpcs, StatusCode::UNAVAILABLE, |
| 1009 |
kStatusMessageDropPrefix); |
| 1010 |
EXPECT_EQ(num_drops, kNumRpcs); |
| 1011 |
} |
| 1012 |
|
| 1013 |
// |
| 1014 |
// EDS failover tests |
| 1015 |
// |
| 1016 |
|
| 1017 |
class FailoverTest : public XdsEnd2endTest { |
| 1018 |
public: |
| 1019 |
void SetUp() override { |
| 1020 |
XdsEnd2endTest::SetUp(); |
| 1021 |
ResetStub(/*failover_timeout_ms=*/500); |
| 1022 |
} |
| 1023 |
}; |
| 1024 |
|
| 1025 |
INSTANTIATE_TEST_SUITE_P( |
| 1026 |
XdsTest, FailoverTest, |
| 1027 |
::testing::Values(XdsTestType(), XdsTestType().set_enable_load_reporting()), |
| 1028 |
&XdsTestType::Name); |
| 1029 |
|
| 1030 |
// Localities with the highest priority are used when multiple priority exist. |
| 1031 |
TEST_P(FailoverTest, ChooseHighestPriority) { |
| 1032 |
CreateAndStartBackends(4); |
| 1033 |
EdsResourceArgs args({ |
| 1034 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1035 |
1}, |
| 1036 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1037 |
2}, |
| 1038 |
{"locality2", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1039 |
3}, |
| 1040 |
{"locality3", CreateEndpointsForBackends(3, 4), kDefaultLocalityWeight, |
| 1041 |
0}, |
| 1042 |
}); |
| 1043 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1044 |
WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr, |
| 1045 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1046 |
for (size_t i = 0; i < 3; ++i) { |
| 1047 |
EXPECT_EQ(0U, backends_[i]->backend_service()->request_count()); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
// Does not choose priority with no endpoints. |
| 1052 |
TEST_P(FailoverTest, DoesNotUsePriorityWithNoEndpoints) { |
| 1053 |
CreateAndStartBackends(3); |
| 1054 |
EdsResourceArgs args({ |
| 1055 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1056 |
1}, |
| 1057 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1058 |
2}, |
| 1059 |
{"locality2", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1060 |
3}, |
| 1061 |
{"locality3", {}, kDefaultLocalityWeight, 0}, |
| 1062 |
}); |
| 1063 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1064 |
WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr, |
| 1065 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1066 |
for (size_t i = 1; i < 3; ++i) { |
| 1067 |
EXPECT_EQ(0U, backends_[i]->backend_service()->request_count()); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Does not choose locality with no endpoints. |
| 1072 |
TEST_P(FailoverTest, DoesNotUseLocalityWithNoEndpoints) { |
| 1073 |
CreateAndStartBackends(1); |
| 1074 |
EdsResourceArgs args({ |
| 1075 |
{"locality0", {}, kDefaultLocalityWeight, 0}, |
| 1076 |
{"locality1", CreateEndpointsForBackends(), kDefaultLocalityWeight, 0}, |
| 1077 |
}); |
| 1078 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1079 |
// Wait for all backends to be used. |
| 1080 |
WaitForAllBackends(DEBUG_LOCATION); |
| 1081 |
} |
| 1082 |
|
| 1083 |
// If the higher priority localities are not reachable, failover to the |
| 1084 |
// highest priority among the rest. |
| 1085 |
TEST_P(FailoverTest, Failover) { |
| 1086 |
CreateAndStartBackends(2); |
| 1087 |
EdsResourceArgs args({ |
| 1088 |
{"locality0", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 1}, |
| 1089 |
{"locality1", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1090 |
2}, |
| 1091 |
{"locality2", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1092 |
3}, |
| 1093 |
{"locality3", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 0}, |
| 1094 |
}); |
| 1095 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1096 |
WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr, |
| 1097 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1098 |
EXPECT_EQ(0U, backends_[1]->backend_service()->request_count()); |
| 1099 |
} |
| 1100 |
|
| 1101 |
// Reports CONNECTING when failing over to a lower priority. |
| 1102 |
TEST_P(FailoverTest, ReportsConnectingDuringFailover) { |
| 1103 |
CreateAndStartBackends(1); |
| 1104 |
// Priority 0 will be unreachable, so we'll use priority 1. |
| 1105 |
EdsResourceArgs args({ |
| 1106 |
{"locality0", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 0}, |
| 1107 |
{"locality1", CreateEndpointsForBackends(), kDefaultLocalityWeight, 1}, |
| 1108 |
}); |
| 1109 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1110 |
ConnectionAttemptInjector injector; |
| 1111 |
auto hold = injector.AddHold(backends_[0]->port()); |
| 1112 |
// Start an RPC in the background, which should cause the channel to |
| 1113 |
// try to connect. |
| 1114 |
LongRunningRpc rpc; |
| 1115 |
rpc.StartRpc(stub_.get(), RpcOptions()); |
| 1116 |
// Wait for connection attempt to start to the backend. |
| 1117 |
hold->Wait(); |
| 1118 |
// Channel state should be CONNECTING here, and any RPC should be |
| 1119 |
// queued. |
| 1120 |
EXPECT_EQ(channel_->GetState(false), GRPC_CHANNEL_CONNECTING); |
| 1121 |
// Allow the connection attempt to complete. |
| 1122 |
hold->Resume(); |
| 1123 |
// Now the RPC should complete successfully. |
| 1124 |
gpr_log(GPR_INFO, "=== WAITING FOR RPC TO FINISH ==="); |
| 1125 |
Status status = rpc.GetStatus(); |
| 1126 |
gpr_log(GPR_INFO, "=== RPC FINISHED ==="); |
| 1127 |
EXPECT_TRUE(status.ok()) << "code=" << status.error_code() |
| 1128 |
<< " message=" << status.error_message(); |
| 1129 |
} |
| 1130 |
|
| 1131 |
// If a locality with higher priority than the current one becomes ready, |
| 1132 |
// switch to it. |
| 1133 |
TEST_P(FailoverTest, SwitchBackToHigherPriority) { |
| 1134 |
CreateAndStartBackends(4); |
| 1135 |
const size_t kNumRpcs = 100; |
| 1136 |
EdsResourceArgs args({ |
| 1137 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1138 |
1}, |
| 1139 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1140 |
2}, |
| 1141 |
{"locality2", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1142 |
3}, |
| 1143 |
{"locality3", CreateEndpointsForBackends(3, 4), kDefaultLocalityWeight, |
| 1144 |
0}, |
| 1145 |
}); |
| 1146 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1147 |
WaitForBackend(DEBUG_LOCATION, 3); |
| 1148 |
backends_[3]->StopListeningAndSendGoaways(); |
| 1149 |
backends_[0]->StopListeningAndSendGoaways(); |
| 1150 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 1151 |
ShutdownBackend(0); |
| 1152 |
StartBackend(0); |
| 1153 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 1154 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 1155 |
EXPECT_EQ(kNumRpcs, backends_[0]->backend_service()->request_count()); |
| 1156 |
} |
| 1157 |
|
| 1158 |
// The first update only contains unavailable priorities. The second update |
| 1159 |
// contains available priorities. |
| 1160 |
TEST_P(FailoverTest, UpdateInitialUnavailable) { |
| 1161 |
CreateAndStartBackends(2); |
| 1162 |
EdsResourceArgs args({ |
| 1163 |
{"locality0", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 0}, |
| 1164 |
{"locality1", {MakeNonExistantEndpoint()}, kDefaultLocalityWeight, 1}, |
| 1165 |
}); |
| 1166 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1167 |
constexpr char kErrorMessageRegex[] = |
| 1168 |
"connections to all backends failing; last error: " |
| 1169 |
"(UNKNOWN: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 1170 |
"Failed to connect to remote host: Connection refused|" |
| 1171 |
"UNAVAILABLE: (ipv6:%5B::1%5D|ipv4:127.0.0.1):[0-9]+: " |
| 1172 |
"Failed to connect to remote host: FD shutdown)"; |
| 1173 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::UNAVAILABLE, |
| 1174 |
kErrorMessageRegex); |
| 1175 |
args = EdsResourceArgs({ |
| 1176 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1177 |
0}, |
| 1178 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1179 |
1}, |
| 1180 |
}); |
| 1181 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1182 |
WaitForBackend(DEBUG_LOCATION, 0, [&](const RpcResult& result) { |
| 1183 |
if (!result.status.ok()) { |
| 1184 |
EXPECT_EQ(result.status.error_code(), StatusCode::UNAVAILABLE); |
| 1185 |
EXPECT_THAT(result.status.error_message(), |
| 1186 |
::testing::MatchesRegex(kErrorMessageRegex)); |
| 1187 |
} |
| 1188 |
}); |
| 1189 |
} |
| 1190 |
|
| 1191 |
// Tests that after the localities' priorities are updated, we still choose |
| 1192 |
// the highest READY priority with the updated localities. |
| 1193 |
TEST_P(FailoverTest, UpdatePriority) { |
| 1194 |
CreateAndStartBackends(4); |
| 1195 |
const size_t kNumRpcs = 100; |
| 1196 |
EdsResourceArgs args({ |
| 1197 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1198 |
1}, |
| 1199 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1200 |
2}, |
| 1201 |
{"locality2", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1202 |
3}, |
| 1203 |
{"locality3", CreateEndpointsForBackends(3, 4), kDefaultLocalityWeight, |
| 1204 |
0}, |
| 1205 |
}); |
| 1206 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1207 |
WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr, |
| 1208 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1209 |
EXPECT_EQ(0U, backends_[0]->backend_service()->request_count()); |
| 1210 |
EXPECT_EQ(0U, backends_[1]->backend_service()->request_count()); |
| 1211 |
EXPECT_EQ(0U, backends_[2]->backend_service()->request_count()); |
| 1212 |
args = EdsResourceArgs({ |
| 1213 |
{"locality0", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1214 |
2}, |
| 1215 |
{"locality1", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1216 |
0}, |
| 1217 |
{"locality2", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1218 |
1}, |
| 1219 |
{"locality3", CreateEndpointsForBackends(3, 4), kDefaultLocalityWeight, |
| 1220 |
3}, |
| 1221 |
}); |
| 1222 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1223 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 1224 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcs); |
| 1225 |
EXPECT_EQ(kNumRpcs, backends_[1]->backend_service()->request_count()); |
| 1226 |
} |
| 1227 |
|
| 1228 |
// Moves all localities in the current priority to a higher priority. |
| 1229 |
TEST_P(FailoverTest, MoveAllLocalitiesInCurrentPriorityToHigherPriority) { |
| 1230 |
CreateAndStartBackends(3); |
| 1231 |
auto non_existant_endpoint = MakeNonExistantEndpoint(); |
| 1232 |
// First update: |
| 1233 |
// - Priority 0 is locality 0, containing an unreachable backend. |
| 1234 |
// - Priority 1 is locality 1, containing backends 0 and 1. |
| 1235 |
EdsResourceArgs args({ |
| 1236 |
{"locality0", {non_existant_endpoint}, kDefaultLocalityWeight, 0}, |
| 1237 |
{"locality1", CreateEndpointsForBackends(0, 2), kDefaultLocalityWeight, |
| 1238 |
1}, |
| 1239 |
}); |
| 1240 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1241 |
// When we get the first update, all backends in priority 0 are down, |
| 1242 |
// so we will create priority 1. Backends 0 and 1 should have traffic, |
| 1243 |
// but backend 2 should not. |
| 1244 |
WaitForAllBackends(DEBUG_LOCATION, 0, 2, /*check_status=*/nullptr, |
| 1245 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1246 |
EXPECT_EQ(0UL, backends_[2]->backend_service()->request_count()); |
| 1247 |
// Second update: |
| 1248 |
// - Priority 0 contains both localities 0 and 1. |
| 1249 |
// - Priority 1 is not present. |
| 1250 |
// - We add backend 2 to locality 1, just so we have a way to know |
| 1251 |
// when the update has been seen by the client. |
| 1252 |
args = EdsResourceArgs({ |
| 1253 |
{"locality0", {non_existant_endpoint}, kDefaultLocalityWeight, 0}, |
| 1254 |
{"locality1", CreateEndpointsForBackends(0, 3), kDefaultLocalityWeight, |
| 1255 |
0}, |
| 1256 |
}); |
| 1257 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1258 |
// When backend 2 gets traffic, we know the second update has been seen. |
| 1259 |
WaitForBackend(DEBUG_LOCATION, 2); |
| 1260 |
// The xDS server got at least 1 response. |
| 1261 |
EXPECT_TRUE(balancer_->ads_service()->eds_response_state().has_value()); |
| 1262 |
} |
| 1263 |
|
| 1264 |
// This tests a bug triggered by the xds_cluster_resolver policy reusing |
| 1265 |
// a child name for the priority policy when that child name was still |
| 1266 |
// present but deactivated. |
| 1267 |
TEST_P(FailoverTest, PriorityChildNameChurn) { |
| 1268 |
CreateAndStartBackends(4); |
| 1269 |
auto non_existant_endpoint = MakeNonExistantEndpoint(); |
| 1270 |
// Initial update: |
| 1271 |
// - P0:locality0, child number 0 (unreachable) |
| 1272 |
// - P1:locality1, child number 1 |
| 1273 |
// - P2:locality2, child number 2 |
| 1274 |
EdsResourceArgs args({ |
| 1275 |
{"locality0", {non_existant_endpoint}, kDefaultLocalityWeight, 0}, |
| 1276 |
{"locality1", CreateEndpointsForBackends(0, 1), kDefaultLocalityWeight, |
| 1277 |
1}, |
| 1278 |
{"locality2", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1279 |
2}, |
| 1280 |
}); |
| 1281 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1282 |
WaitForBackend(DEBUG_LOCATION, 0); |
| 1283 |
// Next update: |
| 1284 |
// - P0:locality0, child number 0 (still unreachable) |
| 1285 |
// - P1:locality2, child number 2 (moved from P2 to P1) |
| 1286 |
// - P2:locality3, child number 3 (new child) |
| 1287 |
// Child number 1 will be deactivated. |
| 1288 |
args = EdsResourceArgs({ |
| 1289 |
{"locality0", {non_existant_endpoint}, kDefaultLocalityWeight, 0}, |
| 1290 |
{"locality2", CreateEndpointsForBackends(1, 2), kDefaultLocalityWeight, |
| 1291 |
1}, |
| 1292 |
{"locality3", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1293 |
2}, |
| 1294 |
}); |
| 1295 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1296 |
WaitForBackend(DEBUG_LOCATION, 1); |
| 1297 |
// Next update: |
| 1298 |
// - P0:locality0, child number 0 (still unreachable) |
| 1299 |
// - P1:locality4, child number 4 (new child number -- should not reuse #1) |
| 1300 |
// - P2:locality3, child number 3 |
| 1301 |
// Child number 1 will be deactivated. |
| 1302 |
args = EdsResourceArgs({ |
| 1303 |
{"locality0", {non_existant_endpoint}, kDefaultLocalityWeight, 0}, |
| 1304 |
{"locality4", CreateEndpointsForBackends(3, 4), kDefaultLocalityWeight, |
| 1305 |
1}, |
| 1306 |
{"locality3", CreateEndpointsForBackends(2, 3), kDefaultLocalityWeight, |
| 1307 |
2}, |
| 1308 |
}); |
| 1309 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1310 |
WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr, |
| 1311 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1312 |
// P2 should not have gotten any traffic in this change. |
| 1313 |
EXPECT_EQ(0UL, backends_[2]->backend_service()->request_count()); |
| 1314 |
} |
| 1315 |
|
| 1316 |
// |
| 1317 |
// EDS client load reporting tests |
| 1318 |
// |
| 1319 |
|
| 1320 |
using ClientLoadReportingTest = XdsEnd2endTest; |
| 1321 |
|
| 1322 |
INSTANTIATE_TEST_SUITE_P( |
| 1323 |
XdsTest, ClientLoadReportingTest, |
| 1324 |
::testing::Values(XdsTestType().set_enable_load_reporting()), |
| 1325 |
&XdsTestType::Name); |
| 1326 |
|
| 1327 |
// Tests that the load report received at the balancer is correct. |
| 1328 |
TEST_P(ClientLoadReportingTest, Vanilla) { |
| 1329 |
CreateAndStartBackends(4); |
| 1330 |
const size_t kNumRpcsPerAddress = 10; |
| 1331 |
const size_t kNumFailuresPerAddress = 3; |
| 1332 |
EdsResourceArgs args({ |
| 1333 |
{"locality0", CreateEndpointsForBackends(0, 2)}, |
| 1334 |
{"locality1", CreateEndpointsForBackends(2, 4)}, |
| 1335 |
}); |
| 1336 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1337 |
// Wait until all backends are ready. |
| 1338 |
size_t num_warmup_rpcs = |
| 1339 |
WaitForAllBackends(DEBUG_LOCATION, 0, 4, /*check_status=*/nullptr, |
| 1340 |
WaitForBackendOptions().set_reset_counters(false)); |
| 1341 |
// Send kNumRpcsPerAddress RPCs per server. |
| 1342 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsPerAddress * backends_.size()); |
| 1343 |
for (size_t i = 0; i < kNumFailuresPerAddress * backends_.size(); ++i) { |
| 1344 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::FAILED_PRECONDITION, "", |
| 1345 |
RpcOptions().set_server_fail(true)); |
| 1346 |
} |
| 1347 |
const size_t total_successful_rpcs_sent = |
| 1348 |
(kNumRpcsPerAddress * backends_.size()) + num_warmup_rpcs; |
| 1349 |
const size_t total_failed_rpcs_sent = |
| 1350 |
kNumFailuresPerAddress * backends_.size(); |
| 1351 |
// Check that the backends got the right number of requests. |
| 1352 |
size_t total_rpcs_sent = 0; |
| 1353 |
for (const auto& backend : backends_) { |
| 1354 |
total_rpcs_sent += backend->backend_service()->request_count(); |
| 1355 |
} |
| 1356 |
EXPECT_EQ(total_rpcs_sent, |
| 1357 |
total_successful_rpcs_sent + total_failed_rpcs_sent); |
| 1358 |
// The load report received at the balancer should be correct. |
| 1359 |
std::vector<ClientStats> load_report = |
| 1360 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1361 |
ASSERT_EQ(load_report.size(), 1UL); |
| 1362 |
ClientStats& client_stats = load_report.front(); |
| 1363 |
EXPECT_EQ(client_stats.cluster_name(), kDefaultClusterName); |
| 1364 |
EXPECT_EQ(client_stats.eds_service_name(), kDefaultEdsServiceName); |
| 1365 |
EXPECT_EQ(total_successful_rpcs_sent, |
| 1366 |
client_stats.total_successful_requests()); |
| 1367 |
EXPECT_EQ(0U, client_stats.total_requests_in_progress()); |
| 1368 |
EXPECT_EQ(total_rpcs_sent, client_stats.total_issued_requests()); |
| 1369 |
EXPECT_EQ(total_failed_rpcs_sent, client_stats.total_error_requests()); |
| 1370 |
EXPECT_EQ(0U, client_stats.total_dropped_requests()); |
| 1371 |
ASSERT_THAT( |
| 1372 |
client_stats.locality_stats(), |
| 1373 |
::testing::ElementsAre(::testing::Pair("locality0", ::testing::_), |
| 1374 |
::testing::Pair("locality1", ::testing::_))); |
| 1375 |
size_t num_successful_rpcs = 0; |
| 1376 |
size_t num_failed_rpcs = 0; |
| 1377 |
for (const auto& p : client_stats.locality_stats()) { |
| 1378 |
EXPECT_EQ(p.second.total_requests_in_progress, 0U); |
| 1379 |
EXPECT_EQ( |
| 1380 |
p.second.total_issued_requests, |
| 1381 |
p.second.total_successful_requests + p.second.total_error_requests); |
| 1382 |
num_successful_rpcs += p.second.total_successful_requests; |
| 1383 |
num_failed_rpcs += p.second.total_error_requests; |
| 1384 |
} |
| 1385 |
EXPECT_EQ(num_successful_rpcs, total_successful_rpcs_sent); |
| 1386 |
EXPECT_EQ(num_failed_rpcs, total_failed_rpcs_sent); |
| 1387 |
EXPECT_EQ(num_successful_rpcs + num_failed_rpcs, total_rpcs_sent); |
| 1388 |
// The LRS service got a single request, and sent a single response. |
| 1389 |
EXPECT_EQ(1U, balancer_->lrs_service()->request_count()); |
| 1390 |
EXPECT_EQ(1U, balancer_->lrs_service()->response_count()); |
| 1391 |
} |
| 1392 |
|
| 1393 |
// Tests send_all_clusters. |
| 1394 |
TEST_P(ClientLoadReportingTest, SendAllClusters) { |
| 1395 |
CreateAndStartBackends(2); |
| 1396 |
balancer_->lrs_service()->set_send_all_clusters(true); |
| 1397 |
const size_t kNumRpcsPerAddress = 10; |
| 1398 |
const size_t kNumFailuresPerAddress = 3; |
| 1399 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 1400 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1401 |
// Wait until all backends are ready. |
| 1402 |
size_t num_warmup_rpcs = WaitForAllBackends(DEBUG_LOCATION); |
| 1403 |
// Send kNumRpcsPerAddress RPCs per server. |
| 1404 |
CheckRpcSendOk(DEBUG_LOCATION, kNumRpcsPerAddress * backends_.size()); |
| 1405 |
for (size_t i = 0; i < kNumFailuresPerAddress * backends_.size(); ++i) { |
| 1406 |
CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::FAILED_PRECONDITION, "", |
| 1407 |
RpcOptions().set_server_fail(true)); |
| 1408 |
} |
| 1409 |
// Check that each backend got the right number of requests. |
| 1410 |
for (size_t i = 0; i < backends_.size(); ++i) { |
| 1411 |
EXPECT_EQ(kNumRpcsPerAddress + kNumFailuresPerAddress, |
| 1412 |
backends_[i]->backend_service()->request_count()); |
| 1413 |
} |
| 1414 |
// The load report received at the balancer should be correct. |
| 1415 |
std::vector<ClientStats> load_report = |
| 1416 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1417 |
ASSERT_EQ(load_report.size(), 1UL); |
| 1418 |
ClientStats& client_stats = load_report.front(); |
| 1419 |
EXPECT_EQ(kNumRpcsPerAddress * backends_.size() + num_warmup_rpcs, |
| 1420 |
client_stats.total_successful_requests()); |
| 1421 |
EXPECT_EQ(0U, client_stats.total_requests_in_progress()); |
| 1422 |
EXPECT_EQ((kNumRpcsPerAddress + kNumFailuresPerAddress) * backends_.size() + |
| 1423 |
num_warmup_rpcs, |
| 1424 |
client_stats.total_issued_requests()); |
| 1425 |
EXPECT_EQ(kNumFailuresPerAddress * backends_.size(), |
| 1426 |
client_stats.total_error_requests()); |
| 1427 |
EXPECT_EQ(0U, client_stats.total_dropped_requests()); |
| 1428 |
// The LRS service got a single request, and sent a single response. |
| 1429 |
EXPECT_EQ(1U, balancer_->lrs_service()->request_count()); |
| 1430 |
EXPECT_EQ(1U, balancer_->lrs_service()->response_count()); |
| 1431 |
} |
| 1432 |
|
| 1433 |
// Tests that we don't include stats for clusters that are not requested |
| 1434 |
// by the LRS server. |
| 1435 |
TEST_P(ClientLoadReportingTest, HonorsClustersRequestedByLrsServer) { |
| 1436 |
CreateAndStartBackends(1); |
| 1437 |
balancer_->lrs_service()->set_cluster_names({"bogus"}); |
| 1438 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 1439 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1440 |
// Wait until all backends are ready. |
| 1441 |
WaitForAllBackends(DEBUG_LOCATION); |
| 1442 |
// The load report received at the balancer should be correct. |
| 1443 |
std::vector<ClientStats> load_report = |
| 1444 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1445 |
ASSERT_EQ(load_report.size(), 0UL); |
| 1446 |
// The LRS service got a single request, and sent a single response. |
| 1447 |
EXPECT_EQ(1U, balancer_->lrs_service()->request_count()); |
| 1448 |
EXPECT_EQ(1U, balancer_->lrs_service()->response_count()); |
| 1449 |
} |
| 1450 |
|
| 1451 |
// Tests that if the balancer restarts, the client load report contains the |
| 1452 |
// stats before and after the restart correctly. |
| 1453 |
TEST_P(ClientLoadReportingTest, BalancerRestart) { |
| 1454 |
CreateAndStartBackends(4); |
| 1455 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends(0, 2)}}); |
| 1456 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1457 |
// Wait until all backends returned by the balancer are ready. |
| 1458 |
size_t num_rpcs = WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 1459 |
std::vector<ClientStats> load_report = |
| 1460 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1461 |
ASSERT_EQ(load_report.size(), 1UL); |
| 1462 |
ClientStats client_stats = std::move(load_report.front()); |
| 1463 |
EXPECT_EQ(num_rpcs, client_stats.total_successful_requests()); |
| 1464 |
EXPECT_EQ(0U, client_stats.total_requests_in_progress()); |
| 1465 |
EXPECT_EQ(0U, client_stats.total_error_requests()); |
| 1466 |
EXPECT_EQ(0U, client_stats.total_dropped_requests()); |
| 1467 |
// Shut down the balancer. |
| 1468 |
balancer_->Shutdown(); |
| 1469 |
// We should continue using the last EDS response we received from the |
| 1470 |
// balancer before it was shut down. |
| 1471 |
// Note: We need to use WaitForAllBackends() here instead of just |
| 1472 |
// CheckRpcSendOk(kNumBackendsFirstPass), because when the balancer |
| 1473 |
// shuts down, the XdsClient will generate an error to the |
| 1474 |
// ListenerWatcher, which will cause the xds resolver to send a |
| 1475 |
// no-op update to the LB policy. When this update gets down to the |
| 1476 |
// round_robin child policy for the locality, it will generate a new |
| 1477 |
// subchannel list, which resets the start index randomly. So we need |
| 1478 |
// to be a little more permissive here to avoid spurious failures. |
| 1479 |
ResetBackendCounters(); |
| 1480 |
num_rpcs = WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 1481 |
// Now restart the balancer, this time pointing to the new backends. |
| 1482 |
balancer_->Start(); |
| 1483 |
args = EdsResourceArgs({{"locality0", CreateEndpointsForBackends(2, 4)}}); |
| 1484 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1485 |
// Wait for queries to start going to one of the new backends. |
| 1486 |
// This tells us that we're now using the new serverlist. |
| 1487 |
num_rpcs += WaitForAllBackends(DEBUG_LOCATION, 2, 4); |
| 1488 |
// Send one RPC per backend. |
| 1489 |
CheckRpcSendOk(DEBUG_LOCATION, 2); |
| 1490 |
num_rpcs += 2; |
| 1491 |
// Check client stats. |
| 1492 |
load_report = balancer_->lrs_service()->WaitForLoadReport(); |
| 1493 |
ASSERT_EQ(load_report.size(), 1UL); |
| 1494 |
client_stats = std::move(load_report.front()); |
| 1495 |
EXPECT_EQ(num_rpcs, client_stats.total_successful_requests()); |
| 1496 |
EXPECT_EQ(0U, client_stats.total_requests_in_progress()); |
| 1497 |
EXPECT_EQ(0U, client_stats.total_error_requests()); |
| 1498 |
EXPECT_EQ(0U, client_stats.total_dropped_requests()); |
| 1499 |
} |
| 1500 |
|
| 1501 |
// Tests load reporting when switching over from one cluster to another. |
| 1502 |
TEST_P(ClientLoadReportingTest, ChangeClusters) { |
| 1503 |
CreateAndStartBackends(4); |
| 1504 |
const char* kNewClusterName = "new_cluster_name"; |
| 1505 |
const char* kNewEdsServiceName = "new_eds_service_name"; |
| 1506 |
balancer_->lrs_service()->set_cluster_names( |
| 1507 |
{kDefaultClusterName, kNewClusterName}); |
| 1508 |
// cluster kDefaultClusterName -> locality0 -> backends 0 and 1 |
| 1509 |
EdsResourceArgs args({ |
| 1510 |
{"locality0", CreateEndpointsForBackends(0, 2)}, |
| 1511 |
}); |
| 1512 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1513 |
// cluster kNewClusterName -> locality1 -> backends 2 and 3 |
| 1514 |
EdsResourceArgs args2({ |
| 1515 |
{"locality1", CreateEndpointsForBackends(2, 4)}, |
| 1516 |
}); |
| 1517 |
balancer_->ads_service()->SetEdsResource( |
| 1518 |
BuildEdsResource(args2, kNewEdsServiceName)); |
| 1519 |
// CDS resource for kNewClusterName. |
| 1520 |
Cluster new_cluster = default_cluster_; |
| 1521 |
new_cluster.set_name(kNewClusterName); |
| 1522 |
new_cluster.mutable_eds_cluster_config()->set_service_name( |
| 1523 |
kNewEdsServiceName); |
| 1524 |
balancer_->ads_service()->SetCdsResource(new_cluster); |
| 1525 |
// Wait for all backends to come online. |
| 1526 |
size_t num_rpcs = WaitForAllBackends(DEBUG_LOCATION, 0, 2); |
| 1527 |
// The load report received at the balancer should be correct. |
| 1528 |
std::vector<ClientStats> load_report = |
| 1529 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1530 |
EXPECT_THAT( |
| 1531 |
load_report, |
| 1532 |
::testing::ElementsAre(::testing::AllOf( |
| 1533 |
::testing::Property(&ClientStats::cluster_name, kDefaultClusterName), |
| 1534 |
::testing::Property(&ClientStats::eds_service_name, |
| 1535 |
kDefaultEdsServiceName), |
| 1536 |
::testing::Property( |
| 1537 |
&ClientStats::locality_stats, |
| 1538 |
::testing::ElementsAre(::testing::Pair( |
| 1539 |
"locality0", |
| 1540 |
::testing::AllOf( |
| 1541 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1542 |
total_successful_requests, |
| 1543 |
num_rpcs), |
| 1544 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1545 |
total_requests_in_progress, |
| 1546 |
0UL), |
| 1547 |
::testing::Field( |
| 1548 |
&ClientStats::LocalityStats::total_error_requests, |
| 1549 |
0UL), |
| 1550 |
::testing::Field( |
| 1551 |
&ClientStats::LocalityStats::total_issued_requests, |
| 1552 |
num_rpcs))))), |
| 1553 |
::testing::Property(&ClientStats::total_dropped_requests, 0UL)))); |
| 1554 |
// Change RDS resource to point to new cluster. |
| 1555 |
RouteConfiguration new_route_config = default_route_config_; |
| 1556 |
new_route_config.mutable_virtual_hosts(0) |
| 1557 |
->mutable_routes(0) |
| 1558 |
->mutable_route() |
| 1559 |
->set_cluster(kNewClusterName); |
| 1560 |
SetListenerAndRouteConfiguration(balancer_.get(), default_listener_, |
| 1561 |
new_route_config); |
| 1562 |
// Wait for all new backends to be used. |
| 1563 |
num_rpcs = WaitForAllBackends(DEBUG_LOCATION, 2, 4); |
| 1564 |
// The load report received at the balancer should be correct. |
| 1565 |
load_report = balancer_->lrs_service()->WaitForLoadReport(); |
| 1566 |
EXPECT_THAT( |
| 1567 |
load_report, |
| 1568 |
::testing::ElementsAre( |
| 1569 |
::testing::AllOf( |
| 1570 |
::testing::Property(&ClientStats::cluster_name, |
| 1571 |
kDefaultClusterName), |
| 1572 |
::testing::Property(&ClientStats::eds_service_name, |
| 1573 |
kDefaultEdsServiceName), |
| 1574 |
::testing::Property( |
| 1575 |
&ClientStats::locality_stats, |
| 1576 |
::testing::ElementsAre(::testing::Pair( |
| 1577 |
"locality0", |
| 1578 |
::testing::AllOf( |
| 1579 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1580 |
total_successful_requests, |
| 1581 |
::testing::Lt(num_rpcs)), |
| 1582 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1583 |
total_requests_in_progress, |
| 1584 |
0UL), |
| 1585 |
::testing::Field( |
| 1586 |
&ClientStats::LocalityStats::total_error_requests, |
| 1587 |
0UL), |
| 1588 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1589 |
total_issued_requests, |
| 1590 |
::testing::Le(num_rpcs)))))), |
| 1591 |
::testing::Property(&ClientStats::total_dropped_requests, 0UL)), |
| 1592 |
::testing::AllOf( |
| 1593 |
::testing::Property(&ClientStats::cluster_name, kNewClusterName), |
| 1594 |
::testing::Property(&ClientStats::eds_service_name, |
| 1595 |
kNewEdsServiceName), |
| 1596 |
::testing::Property( |
| 1597 |
&ClientStats::locality_stats, |
| 1598 |
::testing::ElementsAre(::testing::Pair( |
| 1599 |
"locality1", |
| 1600 |
::testing::AllOf( |
| 1601 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1602 |
total_successful_requests, |
| 1603 |
::testing::Le(num_rpcs)), |
| 1604 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1605 |
total_requests_in_progress, |
| 1606 |
0UL), |
| 1607 |
::testing::Field( |
| 1608 |
&ClientStats::LocalityStats::total_error_requests, |
| 1609 |
0UL), |
| 1610 |
::testing::Field(&ClientStats::LocalityStats:: |
| 1611 |
total_issued_requests, |
| 1612 |
::testing::Le(num_rpcs)))))), |
| 1613 |
::testing::Property(&ClientStats::total_dropped_requests, 0UL)))); |
| 1614 |
size_t total_ok = 0; |
| 1615 |
for (const ClientStats& client_stats : load_report) { |
| 1616 |
total_ok += client_stats.total_successful_requests(); |
| 1617 |
} |
| 1618 |
EXPECT_EQ(total_ok, num_rpcs); |
| 1619 |
// The LRS service got a single request, and sent a single response. |
| 1620 |
EXPECT_EQ(1U, balancer_->lrs_service()->request_count()); |
| 1621 |
EXPECT_EQ(1U, balancer_->lrs_service()->response_count()); |
| 1622 |
} |
| 1623 |
|
| 1624 |
// Tests that the drop stats are correctly reported by client load reporting. |
| 1625 |
TEST_P(ClientLoadReportingTest, DropStats) { |
| 1626 |
CreateAndStartBackends(1); |
| 1627 |
const uint32_t kDropPerMillionForLb = 100000; |
| 1628 |
const uint32_t kDropPerMillionForThrottle = 200000; |
| 1629 |
const double kErrorTolerance = 0.05; |
| 1630 |
const double kDropRateForLb = kDropPerMillionForLb / 1000000.0; |
| 1631 |
const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0; |
| 1632 |
const double kDropRateForLbAndThrottle = |
| 1633 |
kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle; |
| 1634 |
const size_t kNumRpcs = |
| 1635 |
ComputeIdealNumRpcs(kDropRateForLbAndThrottle, kErrorTolerance); |
| 1636 |
// The ADS response contains two drop categories. |
| 1637 |
EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}}); |
| 1638 |
args.drop_categories = {{kLbDropType, kDropPerMillionForLb}, |
| 1639 |
{kThrottleDropType, kDropPerMillionForThrottle}}; |
| 1640 |
balancer_->ads_service()->SetEdsResource(BuildEdsResource(args)); |
| 1641 |
// Send kNumRpcs RPCs and count the drops. |
| 1642 |
size_t num_drops = SendRpcsAndCountFailuresWithMessage( |
| 1643 |
DEBUG_LOCATION, kNumRpcs, StatusCode::UNAVAILABLE, |
| 1644 |
kStatusMessageDropPrefix); |
| 1645 |
// The drop rate should be roughly equal to the expectation. |
| 1646 |
const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs; |
| 1647 |
EXPECT_THAT(seen_drop_rate, ::testing::DoubleNear(kDropRateForLbAndThrottle, |
| 1648 |
kErrorTolerance)); |
| 1649 |
// Check client stats. |
| 1650 |
ClientStats client_stats; |
| 1651 |
do { |
| 1652 |
std::vector<ClientStats> load_reports = |
| 1653 |
balancer_->lrs_service()->WaitForLoadReport(); |
| 1654 |
for (const auto& load_report : load_reports) { |
| 1655 |
client_stats += load_report; |
| 1656 |
} |
| 1657 |
} while (client_stats.total_issued_requests() + |
| 1658 |
client_stats.total_dropped_requests() < |
| 1659 |
kNumRpcs); |
| 1660 |
EXPECT_EQ(num_drops, client_stats.total_dropped_requests()); |
| 1661 |
EXPECT_THAT(static_cast<double>(client_stats.dropped_requests(kLbDropType)) / |
| 1662 |
kNumRpcs, |
| 1663 |
::testing::DoubleNear(kDropRateForLb, kErrorTolerance)); |
| 1664 |
EXPECT_THAT( |
| 1665 |
static_cast<double>(client_stats.dropped_requests(kThrottleDropType)) / |
| 1666 |
(kNumRpcs * (1 - kDropRateForLb)), |
| 1667 |
::testing::DoubleNear(kDropRateForThrottle, kErrorTolerance)); |
| 1668 |
} |
| 1669 |
|
| 1670 |
} // namespace |
| 1671 |
} // namespace testing |
| 1672 |
} // namespace grpc |
| 1673 |
|
| 1674 |
int main(int argc, char** argv) { |
| 1675 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 1676 |
::testing::InitGoogleTest(&argc, argv); |
| 1677 |
// Make the backup poller poll very frequently in order to pick up |
| 1678 |
// updates from all the subchannels's FDs. |
| 1679 |
GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1); |
| 1680 |
#if TARGET_OS_IPHONE |
| 1681 |
// Workaround Apple CFStream bug |
| 1682 |
grpc_core::SetEnv("grpc_cfstream", "0"); |
| 1683 |
#endif |
| 1684 |
grpc_init(); |
| 1685 |
grpc::testing::ConnectionAttemptInjector::Init(); |
| 1686 |
const auto result = RUN_ALL_TESTS(); |
| 1687 |
grpc_shutdown(); |
| 1688 |
return result; |
| 1689 |
} |