Debian Codegraph @

test/cpp/end2end/xds/xds_outlier_detection_end2end_test.cc

raw
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 #include <chrono>
16 #include <string>
17 #include <thread>
18 #include <vector>
19
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "src/core/ext/filters/client_channel/backup_poller.h"
24 #include "src/proto/grpc/testing/xds/v3/cluster.grpc.pb.h"
25 #include "src/proto/grpc/testing/xds/v3/fault.grpc.pb.h"
26 #include "src/proto/grpc/testing/xds/v3/outlier_detection.grpc.pb.h"
27 #include "src/proto/grpc/testing/xds/v3/router.grpc.pb.h"
28 #include "test/cpp/end2end/xds/xds_end2end_test_lib.h"
29
30 namespace grpc {
31 namespace testing {
32 namespace {
33
34 class OutlierDetectionTest : public XdsEnd2endTest {
35 protected:
36 std::string CreateMetadataValueThatHashesToBackend(int index) {
37 return absl::StrCat(ipv6_only_ ? "[::1]" : "127.0.0.1", ":",
38 backends_[index]->port(), "_0");
39 }
40 };
41
42 INSTANTIATE_TEST_SUITE_P(XdsTest, OutlierDetectionTest,
43 ::testing::Values(XdsTestType()), &XdsTestType::Name);
44 // TODO(donnadionne): add non-xds test a new
45 // test/cpp/end2end/outlier_detection_end2end_test.cc
46
47 // Tests SuccessRateEjectionAndUnejection:
48 // 1. Use ring hash policy that hashes using a header value to ensure rpcs
49 // go to all backends.
50 // 2. Cause a single error on 1 backend and wait for 1 outlier detection
51 // interval to pass.
52 // 3. We should skip exactly 1 backend due to ejection and all the loads
53 // sticky to that backend should go to 1 other backend.
54 // 4. Let the ejection period pass and verify we can go back to both backends
55 // after the uneject.
56 TEST_P(OutlierDetectionTest, SuccessRateEjectionAndUnejection) {
57 CreateAndStartBackends(2);
58 auto cluster = default_cluster_;
59 cluster.set_lb_policy(Cluster::RING_HASH);
60 // Setup outlier failure percentage parameters.
61 // Any failure will cause an potential ejection with the probability of 100%
62 // (to eliminate flakiness of the test).
63 auto* outlier_detection = cluster.mutable_outlier_detection();
64 SetProtoDuration(grpc_core::Duration::Seconds(1),
65 outlier_detection->mutable_interval());
66 SetProtoDuration(grpc_core::Duration::Seconds(1),
67 outlier_detection->mutable_base_ejection_time());
68 outlier_detection->mutable_success_rate_stdev_factor()->set_value(100);
69 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
70 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
71 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
72 balancer_->ads_service()->SetCdsResource(cluster);
73 auto new_route_config = default_route_config_;
74 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
75 auto* hash_policy = route->mutable_route()->add_hash_policy();
76 hash_policy->mutable_header()->set_header_name("address_hash");
77 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
78 new_route_config);
79 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
80 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
81 // Note each type of RPC will contains a header value that will always be
82 // hashed to a specific backend as the header value matches the value used
83 // to create the entry in the ring.
84 std::vector<std::pair<std::string, std::string>> metadata = {
85 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
86 std::vector<std::pair<std::string, std::string>> metadata1 = {
87 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
88 const auto rpc_options = RpcOptions().set_metadata(metadata);
89 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
90 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
91 WaitForBackendOptions(), rpc_options);
92 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
93 WaitForBackendOptions(), rpc_options1);
94 // Trigger an error to backend 0.
95 // The success rate enforcement_percentage is 100%, so this will cause
96 // the backend to be ejected when the ejection timer fires.
97 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
98 RpcOptions()
99 .set_metadata(std::move(metadata))
100 .set_server_expected_error(StatusCode::CANCELLED));
101 // Wait for traffic aimed at backend 0 to start going to backend 1.
102 // This tells us that backend 0 has been ejected.
103 // It should take no more than one ejection timer interval.
104 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
105 WaitForBackendOptions().set_timeout_ms(
106 3000 * grpc_test_slowdown_factor()),
107 rpc_options);
108 // Now wait for traffic aimed at backend 0 to switch back to backend 0.
109 // This tells us that backend 0 has been unejected.
110 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
111 WaitForBackendOptions().set_timeout_ms(
112 3000 * grpc_test_slowdown_factor()),
113 rpc_options);
114 }
115
116 // We don't eject more than max_ejection_percent (default 10%) of the backends
117 // beyond the first one.
118 TEST_P(OutlierDetectionTest, SuccessRateMaxPercent) {
119 CreateAndStartBackends(4);
120 auto cluster = default_cluster_;
121 cluster.set_lb_policy(Cluster::RING_HASH);
122 // Setup outlier failure percentage parameters.
123 // Any failure will cause an potential ejection with the probability of 100%
124 // (to eliminate flakiness of the test).
125 auto* outlier_detection = cluster.mutable_outlier_detection();
126 SetProtoDuration(grpc_core::Duration::Seconds(1),
127 outlier_detection->mutable_interval());
128 outlier_detection->mutable_success_rate_stdev_factor()->set_value(100);
129 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
130 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
131 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
132 balancer_->ads_service()->SetCdsResource(cluster);
133 auto new_route_config = default_route_config_;
134 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
135 auto* hash_policy = route->mutable_route()->add_hash_policy();
136 hash_policy->mutable_header()->set_header_name("address_hash");
137 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
138 new_route_config);
139 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
140 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
141 // Note each type of RPC will contains a header value that will always be
142 // hashed to a specific backend as the header value matches the value used
143 // to create the entry in the ring.
144 std::vector<std::pair<std::string, std::string>> metadata = {
145 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
146 std::vector<std::pair<std::string, std::string>> metadata1 = {
147 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
148 std::vector<std::pair<std::string, std::string>> metadata2 = {
149 {"address_hash", CreateMetadataValueThatHashesToBackend(2)}};
150 std::vector<std::pair<std::string, std::string>> metadata3 = {
151 {"address_hash", CreateMetadataValueThatHashesToBackend(3)}};
152 const auto rpc_options = RpcOptions().set_metadata(metadata);
153 const auto rpc_options1 = RpcOptions().set_metadata(metadata1);
154 const auto rpc_options2 = RpcOptions().set_metadata(metadata2);
155 const auto rpc_options3 = RpcOptions().set_metadata(metadata3);
156 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
157 WaitForBackendOptions(), rpc_options);
158 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
159 WaitForBackendOptions(), rpc_options1);
160 WaitForBackend(DEBUG_LOCATION, 2, /*check_status=*/nullptr,
161 WaitForBackendOptions(), rpc_options2);
162 WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr,
163 WaitForBackendOptions(), rpc_options3);
164 // Cause 2 errors and wait until one ejection happens.
165 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
166 RpcOptions()
167 .set_metadata(std::move(metadata))
168 .set_server_expected_error(StatusCode::CANCELLED));
169 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
170 RpcOptions()
171 .set_metadata(std::move(metadata1))
172 .set_server_expected_error(StatusCode::CANCELLED));
173 absl::Time deadline =
174 absl::Now() + absl::Seconds(3) * grpc_test_slowdown_factor();
175 while (true) {
176 ResetBackendCounters();
177 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options);
178 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options1);
179 if (!SeenAllBackends(0, 2)) {
180 break;
181 }
182 EXPECT_LE(absl::Now(), deadline);
183 if (absl::Now() >= deadline) break;
184 }
185 // 1 backend should be ejected, trafficed picked up by another backend.
186 // No other backend should be ejected.
187 ResetBackendCounters();
188 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
189 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
190 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options2);
191 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options3);
192 size_t empty_load_backend_count = 0;
193 size_t double_load_backend_count = 0;
194 size_t regular_load_backend_count = 0;
195 for (size_t i = 0; i < backends_.size(); ++i) {
196 if (backends_[i]->backend_service()->request_count() == 0) {
197 ++empty_load_backend_count;
198 } else if (backends_[i]->backend_service()->request_count() == 200) {
199 ++double_load_backend_count;
200 } else if (backends_[i]->backend_service()->request_count() == 100) {
201 ++regular_load_backend_count;
202 } else {
203 GPR_ASSERT(1);
204 }
205 }
206 EXPECT_EQ(1, empty_load_backend_count);
207 EXPECT_EQ(1, double_load_backend_count);
208 EXPECT_EQ(2, regular_load_backend_count);
209 }
210
211 // Success rate stdev_factor is honored, a higher value would ensure ejection
212 // does not occur.
213 TEST_P(OutlierDetectionTest, SuccessRateStdevFactor) {
214 CreateAndStartBackends(2);
215 auto cluster = default_cluster_;
216 cluster.set_lb_policy(Cluster::RING_HASH);
217 // Setup outlier failure percentage parameters.
218 // Any failure will cause an potential ejection with the probability of 100%
219 // (to eliminate flakiness of the test).
220 auto* outlier_detection = cluster.mutable_outlier_detection();
221 SetProtoDuration(grpc_core::Duration::Seconds(1),
222 outlier_detection->mutable_interval());
223 SetProtoDuration(grpc_core::Duration::Seconds(1),
224 outlier_detection->mutable_base_ejection_time());
225 // We know a stdev factor of 100 will ensure the ejection occurs, so setting
226 // it to something higher like 1000 to test that ejection will not occur.
227 // Note this parameter is the only difference between this test and
228 // SuccessRateEjectionAndUnejection (ejection portion, value set to 100) and
229 // this one value changes means the difference between not ejecting in this
230 // test and ejecting in the other test.
231 outlier_detection->mutable_success_rate_stdev_factor()->set_value(1000);
232 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
233 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
234 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
235 balancer_->ads_service()->SetCdsResource(cluster);
236 auto new_route_config = default_route_config_;
237 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
238 auto* hash_policy = route->mutable_route()->add_hash_policy();
239 hash_policy->mutable_header()->set_header_name("address_hash");
240 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
241 new_route_config);
242 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
243 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
244 // Note each type of RPC will contains a header value that will always be
245 // hashed to a specific backend as the header value matches the value used
246 // to create the entry in the ring.
247 std::vector<std::pair<std::string, std::string>> metadata = {
248 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
249 std::vector<std::pair<std::string, std::string>> metadata1 = {
250 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
251 const auto rpc_options = RpcOptions().set_metadata(metadata);
252 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
253 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
254 WaitForBackendOptions(), rpc_options);
255 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
256 WaitForBackendOptions(), rpc_options1);
257 // Cause an error and wait for 1 outlier detection interval to pass
258 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
259 RpcOptions()
260 .set_metadata(std::move(metadata))
261 .set_server_expected_error(StatusCode::CANCELLED));
262 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
263 3000 * grpc_test_slowdown_factor()));
264 ResetBackendCounters();
265 // 1 backend experenced failure, but since the stdev_factor is high, no
266 // backend will be noticed as an outlier so no ejection.
267 // Both backends are still getting the RPCs intended for them.
268 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
269 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
270 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
271 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
272 }
273
274 // Success rate enforcement percentage is honored, setting it to 0 so guarantee
275 // the randomized number between 1 to 100 will always be great, so nothing will
276 // be ejected.
277 TEST_P(OutlierDetectionTest, SuccessRateEnforcementPercentage) {
278 CreateAndStartBackends(2);
279 auto cluster = default_cluster_;
280 cluster.set_lb_policy(Cluster::RING_HASH);
281 auto* outlier_detection = cluster.mutable_outlier_detection();
282 SetProtoDuration(grpc_core::Duration::Seconds(1),
283 outlier_detection->mutable_interval());
284 SetProtoDuration(grpc_core::Duration::Seconds(1),
285 outlier_detection->mutable_base_ejection_time());
286 outlier_detection->mutable_success_rate_stdev_factor()->set_value(100);
287 // Setting enforcing_success_rate to 0 to ensure we will never eject.
288 // Note this parameter is the only difference between this test and
289 // SuccessRateEjectionAndUnejection (ejection portion, value set to 100) and
290 // this one value changes means the difference between guaranteed not ejecting
291 // in this test and guaranteed ejecting in the other test.
292 outlier_detection->mutable_enforcing_success_rate()->set_value(0);
293 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
294 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
295 balancer_->ads_service()->SetCdsResource(cluster);
296 auto new_route_config = default_route_config_;
297 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
298 auto* hash_policy = route->mutable_route()->add_hash_policy();
299 hash_policy->mutable_header()->set_header_name("address_hash");
300 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
301 new_route_config);
302 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
303 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
304 // Note each type of RPC will contains a header value that will always be
305 // hashed to a specific backend as the header value matches the value used
306 // to create the entry in the ring.
307 std::vector<std::pair<std::string, std::string>> metadata = {
308 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
309 std::vector<std::pair<std::string, std::string>> metadata1 = {
310 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
311 const auto rpc_options = RpcOptions().set_metadata(metadata);
312 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
313 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
314 WaitForBackendOptions(), rpc_options);
315 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
316 WaitForBackendOptions(), rpc_options1);
317 // Cause an error and wait for 1 outlier detection interval to pass
318 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
319 RpcOptions()
320 .set_metadata(std::move(metadata))
321 .set_server_expected_error(StatusCode::CANCELLED));
322 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
323 3000 * grpc_test_slowdown_factor()));
324 ResetBackendCounters();
325 // 1 backend experenced failure, but since the enforcement percentage is 0, no
326 // backend will be ejected.
327 // Both backends are still getting the RPCs intended for them.
328 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
329 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
330 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
331 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
332 }
333
334 // Success rate does not eject if there are less than minimum_hosts backends
335 // Set success_rate_minimum_hosts to 3 when we only have 2 backends
336 TEST_P(OutlierDetectionTest, SuccessRateMinimumHosts) {
337 CreateAndStartBackends(2);
338 auto cluster = default_cluster_;
339 cluster.set_lb_policy(Cluster::RING_HASH);
340 // Setup outlier failure percentage parameters.
341 // Any failure will cause an potential ejection with the probability of 100%
342 // (to eliminate flakiness of the test).
343 auto* outlier_detection = cluster.mutable_outlier_detection();
344 SetProtoDuration(grpc_core::Duration::Seconds(1),
345 outlier_detection->mutable_interval());
346 outlier_detection->mutable_success_rate_stdev_factor()->set_value(100);
347 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
348 // Set success_rate_minimum_hosts to 3 when we only have 2 backends
349 // Note this parameter is the only difference between this test and
350 // SuccessRateEjectionAndUnejection (ejection portion, value set to 1) and
351 // this one value changes means the difference between not ejecting in this
352 // test and ejecting in the other test.
353 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(3);
354 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
355 balancer_->ads_service()->SetCdsResource(cluster);
356 auto new_route_config = default_route_config_;
357 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
358 auto* hash_policy = route->mutable_route()->add_hash_policy();
359 hash_policy->mutable_header()->set_header_name("address_hash");
360 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
361 new_route_config);
362 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
363 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
364 // Note each type of RPC will contains a header value that will always be
365 // hashed to a specific backend as the header value matches the value used
366 // to create the entry in the ring.
367 std::vector<std::pair<std::string, std::string>> metadata = {
368 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
369 std::vector<std::pair<std::string, std::string>> metadata1 = {
370 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
371 const auto rpc_options = RpcOptions().set_metadata(metadata);
372 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
373 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
374 WaitForBackendOptions(), rpc_options);
375 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
376 WaitForBackendOptions(), rpc_options1);
377 // Cause an error and wait for 1 outlier detection interval to pass
378 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
379 RpcOptions()
380 .set_metadata(std::move(metadata))
381 .set_server_expected_error(StatusCode::CANCELLED));
382 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
383 3000 * grpc_test_slowdown_factor()));
384 ResetBackendCounters();
385 // All traffic still reaching the original backends and no backends are
386 // ejected.
387 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
388 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
389 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
390 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
391 }
392
393 // Success rate does not eject if there are less than request_volume requests
394 // Set success_rate_request_volume to 4 when we only send 3 RPC in the
395 // interval.
396 TEST_P(OutlierDetectionTest, SuccessRateRequestVolume) {
397 CreateAndStartBackends(2);
398 auto cluster = default_cluster_;
399 cluster.set_lb_policy(Cluster::RING_HASH);
400 // Setup outlier failure percentage parameters.
401 // Any failure will cause an potential ejection with the probability of 100%
402 // (to eliminate flakiness of the test).
403 auto* outlier_detection = cluster.mutable_outlier_detection();
404 SetProtoDuration(grpc_core::Duration::Seconds(1),
405 outlier_detection->mutable_interval());
406 outlier_detection->mutable_success_rate_stdev_factor()->set_value(100);
407 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
408 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
409 // Set success_rate_request_volume to 4 when we only send 3 RPC in the
410 // interval.
411 // Note this parameter is the only difference between this test and
412 // SuccessRateEjectionAndUnejection (ejection portion, value set to 1) and
413 // this one value changes means the difference between not ejecting in this
414 // test and ejecting in the other test.
415 outlier_detection->mutable_success_rate_request_volume()->set_value(4);
416 balancer_->ads_service()->SetCdsResource(cluster);
417 auto new_route_config = default_route_config_;
418 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
419 auto* hash_policy = route->mutable_route()->add_hash_policy();
420 hash_policy->mutable_header()->set_header_name("address_hash");
421 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
422 new_route_config);
423 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
424 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
425 // Note each type of RPC will contains a header value that will always be
426 // hashed to a specific backend as the header value matches the value used
427 // to create the entry in the ring.
428 std::vector<std::pair<std::string, std::string>> metadata = {
429 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
430 std::vector<std::pair<std::string, std::string>> metadata1 = {
431 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
432 const auto rpc_options = RpcOptions().set_metadata(metadata);
433 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
434 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
435 WaitForBackendOptions(), rpc_options);
436 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
437 WaitForBackendOptions(), rpc_options1);
438 // Cause an error and wait for 1 outlier detection interval to pass
439 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
440 RpcOptions()
441 .set_metadata(std::move(metadata))
442 .set_server_expected_error(StatusCode::CANCELLED));
443 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
444 3000 * grpc_test_slowdown_factor()));
445 ResetBackendCounters();
446 // All traffic still reaching the original backends and no backends are
447 // ejected.
448 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
449 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
450 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
451 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
452 }
453
454 // Tests FailurePercentageEjectionAndUnejection:
455 // 1. Use ring hash policy that hashes using a header value to ensure RPCs
456 // go to all backends.
457 // 2. Cause a single error on 1 backend and wait for 1 outlier detection
458 // interval to pass.
459 // 3. We should skip exactly 1 backend due to ejection and all the loads
460 // sticky to that backend should go to 1 other backend.
461 // 4. Let the ejection period pass and verify that traffic will again go both
462 // backends as we have unejected the backend.
463 TEST_P(OutlierDetectionTest, FailurePercentageEjectionAndUnejection) {
464 CreateAndStartBackends(2);
465 auto cluster = default_cluster_;
466 cluster.set_lb_policy(Cluster::RING_HASH);
467 // Setup outlier failure percentage parameters.
468 // Any failure will cause an potential ejection with the probability of 100%
469 // (to eliminate flakiness of the test).
470 auto* outlier_detection = cluster.mutable_outlier_detection();
471 SetProtoDuration(grpc_core::Duration::Seconds(1),
472 outlier_detection->mutable_interval());
473 SetProtoDuration(grpc_core::Duration::Seconds(3),
474 outlier_detection->mutable_base_ejection_time());
475 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
476 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
477 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
478 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
479 balancer_->ads_service()->SetCdsResource(cluster);
480 auto new_route_config = default_route_config_;
481 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
482 auto* hash_policy = route->mutable_route()->add_hash_policy();
483 hash_policy->mutable_header()->set_header_name("address_hash");
484 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
485 new_route_config);
486 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
487 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
488 // Note each type of RPC will contains a header value that will always be
489 // hashed to a specific backend as the header value matches the value used
490 // to create the entry in the ring.
491 std::vector<std::pair<std::string, std::string>> metadata = {
492 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
493 std::vector<std::pair<std::string, std::string>> metadata1 = {
494 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
495 const auto rpc_options = RpcOptions().set_metadata(metadata);
496 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
497 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
498 WaitForBackendOptions(), rpc_options);
499 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
500 WaitForBackendOptions(), rpc_options1);
501 // Cause an error and wait for traffic aimed at backend 0 to start going to
502 // backend 1.
503 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
504 RpcOptions()
505 .set_metadata(std::move(metadata))
506 .set_server_expected_error(StatusCode::CANCELLED));
507 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
508 WaitForBackendOptions().set_timeout_ms(
509 3000 * grpc_test_slowdown_factor()),
510 rpc_options);
511 // 1 backend is ejected all traffic going to the ejected backend should now
512 // all be going to the other backend.
513 // failure percentage enforcement_percentage of 100% is honored as this test
514 // will consistently reject 1 backend.
515 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
516 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
517 // Now wait for traffic aimed at backend 0 to switch back to backend 0.
518 // This tells us that backend 0 has been unejected.
519 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
520 WaitForBackendOptions().set_timeout_ms(
521 30000 * grpc_test_slowdown_factor()),
522 rpc_options);
523 // Verify that rpcs go to their expectedly hashed backends.
524 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
525 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
526 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
527 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
528 }
529
530 // We don't eject more than max_ejection_percent (default 10%) of the backends
531 // beyond the first one.
532 TEST_P(OutlierDetectionTest, FailurePercentageMaxPercentage) {
533 CreateAndStartBackends(4);
534 auto cluster = default_cluster_;
535 cluster.set_lb_policy(Cluster::RING_HASH);
536 // Setup outlier failure percentage parameters.
537 // Any failure will cause an potential ejection with the probability of 100%
538 // (to eliminate flakiness of the test).
539 auto* outlier_detection = cluster.mutable_outlier_detection();
540 SetProtoDuration(grpc_core::Duration::Seconds(1),
541 outlier_detection->mutable_interval());
542 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
543 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
544 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
545 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
546 balancer_->ads_service()->SetCdsResource(cluster);
547 auto new_route_config = default_route_config_;
548 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
549 auto* hash_policy = route->mutable_route()->add_hash_policy();
550 hash_policy->mutable_header()->set_header_name("address_hash");
551 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
552 new_route_config);
553 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
554 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
555 // Note each type of RPC will contains a header value that will always be
556 // hashed to a specific backend as the header value matches the value used
557 // to create the entry in the ring.
558 std::vector<std::pair<std::string, std::string>> metadata = {
559 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
560 std::vector<std::pair<std::string, std::string>> metadata1 = {
561 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
562 std::vector<std::pair<std::string, std::string>> metadata2 = {
563 {"address_hash", CreateMetadataValueThatHashesToBackend(2)}};
564 std::vector<std::pair<std::string, std::string>> metadata3 = {
565 {"address_hash", CreateMetadataValueThatHashesToBackend(3)}};
566 const auto rpc_options = RpcOptions().set_metadata(metadata);
567 const auto rpc_options1 = RpcOptions().set_metadata(metadata1);
568 const auto rpc_options2 = RpcOptions().set_metadata(metadata2);
569 const auto rpc_options3 = RpcOptions().set_metadata(metadata3);
570 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
571 WaitForBackendOptions(), rpc_options);
572 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
573 WaitForBackendOptions(), rpc_options1);
574 WaitForBackend(DEBUG_LOCATION, 2, /*check_status=*/nullptr,
575 WaitForBackendOptions(), rpc_options2);
576 WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr,
577 WaitForBackendOptions(), rpc_options3);
578 // Cause 2 errors and wait until one ejection happens.
579 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
580 RpcOptions()
581 .set_metadata(std::move(metadata))
582 .set_server_expected_error(StatusCode::CANCELLED));
583 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
584 RpcOptions()
585 .set_metadata(std::move(metadata1))
586 .set_server_expected_error(StatusCode::CANCELLED));
587 absl::Time deadline =
588 absl::Now() + absl::Seconds(3) * grpc_test_slowdown_factor();
589 while (true) {
590 ResetBackendCounters();
591 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options);
592 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options1);
593 if (!SeenAllBackends(0, 2)) {
594 break;
595 }
596 EXPECT_LE(absl::Now(), deadline);
597 if (absl::Now() >= deadline) break;
598 }
599 // 1 backend should be ejected, trafficed picked up by another backend.
600 // No other backend should be ejected.
601 ResetBackendCounters();
602 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
603 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
604 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options2);
605 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options3);
606 size_t empty_load_backend_count = 0;
607 size_t double_load_backend_count = 0;
608 size_t regular_load_backend_count = 0;
609 for (size_t i = 0; i < backends_.size(); ++i) {
610 if (backends_[i]->backend_service()->request_count() == 0) {
611 ++empty_load_backend_count;
612 } else if (backends_[i]->backend_service()->request_count() == 200) {
613 ++double_load_backend_count;
614 } else if (backends_[i]->backend_service()->request_count() == 100) {
615 ++regular_load_backend_count;
616 } else {
617 GPR_ASSERT(1);
618 }
619 }
620 EXPECT_EQ(1, empty_load_backend_count);
621 EXPECT_EQ(1, double_load_backend_count);
622 EXPECT_EQ(2, regular_load_backend_count);
623 }
624
625 // Failure percentage threshold is honored, a higher value would ensure ejection
626 // does not occur
627 TEST_P(OutlierDetectionTest, FailurePercentageThreshold) {
628 CreateAndStartBackends(2);
629 auto cluster = default_cluster_;
630 cluster.set_lb_policy(Cluster::RING_HASH);
631 auto* outlier_detection = cluster.mutable_outlier_detection();
632 SetProtoDuration(grpc_core::Duration::Seconds(1),
633 outlier_detection->mutable_interval());
634 SetProtoDuration(grpc_core::Duration::Seconds(1),
635 outlier_detection->mutable_base_ejection_time());
636 // Setup outlier failure percentage parameter to 50
637 // Note this parameter is the only difference between this test and
638 // FailurePercentageEjectionAndUnejection (ejection portion, value set to 0)
639 // and this one value changes means the difference between not ejecting in
640 // this test and ejecting in the other test.
641 outlier_detection->mutable_failure_percentage_threshold()->set_value(50);
642 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
643 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
644 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
645 balancer_->ads_service()->SetCdsResource(cluster);
646 auto new_route_config = default_route_config_;
647 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
648 auto* hash_policy = route->mutable_route()->add_hash_policy();
649 hash_policy->mutable_header()->set_header_name("address_hash");
650 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
651 new_route_config);
652 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
653 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
654 // Note each type of RPC will contains a header value that will always be
655 // hashed to a specific backend as the header value matches the value used
656 // to create the entry in the ring.
657 std::vector<std::pair<std::string, std::string>> metadata = {
658 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
659 std::vector<std::pair<std::string, std::string>> metadata1 = {
660 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
661 const auto rpc_options = RpcOptions().set_metadata(metadata);
662 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
663 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
664 WaitForBackendOptions(), rpc_options);
665 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
666 WaitForBackendOptions(), rpc_options1);
667 // Cause an error and wait for 1 outlier detection interval to pass to cause
668 // the backend to be ejected.
669 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
670 RpcOptions()
671 .set_metadata(std::move(metadata))
672 .set_server_expected_error(StatusCode::CANCELLED));
673 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
674 3000 * grpc_test_slowdown_factor()));
675 ResetBackendCounters();
676 // 1 backend experenced 1 failure, but since the threshold is 50 % no
677 // backend will be noticed as an outlier so no ejection.
678 // Both backends are still getting the RPCs intended for them.
679 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
680 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
681 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
682 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
683 }
684
685 // Failure percentage enforcement percentage is honored, setting it to 0 so
686 // guarantee the randomized number between 1 to 100 will always be great, so
687 // nothing will be ejected.
688 TEST_P(OutlierDetectionTest, FailurePercentageEnforcementPercentage) {
689 CreateAndStartBackends(2);
690 auto cluster = default_cluster_;
691 cluster.set_lb_policy(Cluster::RING_HASH);
692 auto* outlier_detection = cluster.mutable_outlier_detection();
693 SetProtoDuration(grpc_core::Duration::Seconds(1),
694 outlier_detection->mutable_interval());
695 SetProtoDuration(grpc_core::Duration::Seconds(1),
696 outlier_detection->mutable_base_ejection_time());
697 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
698 // Setting enforcing_success_rate to 0 to ensure we will never eject.
699 // Note this parameter is the only difference between this test and
700 // FailurePercentageEjectionAndUnejection (ejection portion, value set to 100)
701 // and this one value changes means the difference between guaranteed not
702 // ejecting in this test and guaranteed ejecting in the other test.
703 outlier_detection->mutable_enforcing_failure_percentage()->set_value(0);
704 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
705 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
706 balancer_->ads_service()->SetCdsResource(cluster);
707 auto new_route_config = default_route_config_;
708 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
709 auto* hash_policy = route->mutable_route()->add_hash_policy();
710 hash_policy->mutable_header()->set_header_name("address_hash");
711 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
712 new_route_config);
713 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
714 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
715 // Note each type of RPC will contains a header value that will always be
716 // hashed to a specific backend as the header value matches the value used
717 // to create the entry in the ring.
718 std::vector<std::pair<std::string, std::string>> metadata = {
719 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
720 std::vector<std::pair<std::string, std::string>> metadata1 = {
721 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
722 const auto rpc_options = RpcOptions().set_metadata(metadata);
723 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
724 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
725 WaitForBackendOptions(), rpc_options);
726 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
727 WaitForBackendOptions(), rpc_options1);
728 // Cause an error and wait for 1 outlier detection interval to pass to cause
729 // the backend to be ejected.
730 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
731 RpcOptions()
732 .set_metadata(std::move(metadata))
733 .set_server_expected_error(StatusCode::CANCELLED));
734 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
735 3000 * grpc_test_slowdown_factor()));
736 ResetBackendCounters();
737 // 1 backend experenced failure, but since the enforcement percentage is 0, no
738 // backend will be ejected.
739 // Both backends are still getting the RPCs intended for them.
740 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
741 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
742 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
743 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
744 }
745
746 // Failure percentage does not eject if there are less than minimum_hosts
747 // backends Set success_rate_minimum_hosts to 3 when we only have 2 backends
748 TEST_P(OutlierDetectionTest, FailurePercentageMinimumHosts) {
749 CreateAndStartBackends(2);
750 auto cluster = default_cluster_;
751 cluster.set_lb_policy(Cluster::RING_HASH);
752 // Setup outlier failure percentage parameters.
753 // Any failure will cause an potential ejection with the probability of 100%
754 // (to eliminate flakiness of the test).
755 auto* outlier_detection = cluster.mutable_outlier_detection();
756 SetProtoDuration(grpc_core::Duration::Seconds(1),
757 outlier_detection->mutable_interval());
758 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
759 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
760 // Set failure_percentage_minimum_hosts to 3 when we only have 2 backends
761 // Note this parameter is the only difference between this test and
762 // FailurePercentageEjectionAndUnejection (ejection portion, value set to 1)
763 // and this one value changes means the difference between not ejecting in
764 // this test and ejecting in the other test.
765 cluster.mutable_outlier_detection()
766 ->mutable_failure_percentage_minimum_hosts()
767 ->set_value(3);
768 cluster.mutable_outlier_detection()
769 ->mutable_failure_percentage_request_volume()
770 ->set_value(1);
771 balancer_->ads_service()->SetCdsResource(cluster);
772 auto new_route_config = default_route_config_;
773 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
774 auto* hash_policy = route->mutable_route()->add_hash_policy();
775 hash_policy->mutable_header()->set_header_name("address_hash");
776 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
777 new_route_config);
778 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
779 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
780 // Note each type of RPC will contains a header value that will always be
781 // hashed to a specific backend as the header value matches the value used
782 // to create the entry in the ring.
783 std::vector<std::pair<std::string, std::string>> metadata = {
784 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
785 std::vector<std::pair<std::string, std::string>> metadata1 = {
786 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
787 const auto rpc_options = RpcOptions().set_metadata(metadata);
788 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
789 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
790 WaitForBackendOptions(), rpc_options);
791 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
792 WaitForBackendOptions(), rpc_options1);
793 // Cause an error and wait for 1 outlier detection interval to pass to cause
794 // the backend to be ejected.
795 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
796 RpcOptions()
797 .set_metadata(std::move(metadata))
798 .set_server_expected_error(StatusCode::CANCELLED));
799 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
800 3000 * grpc_test_slowdown_factor()));
801 ResetBackendCounters();
802 // All traffic still reaching the original backends and no backends are
803 // ejected.
804 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
805 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
806 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
807 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
808 }
809
810 // Failure percentage does not eject if there are less than request_volume
811 // requests
812 // Set success_rate_request_volume to 4 when we only send 3 RPC in the
813 // interval.
814 TEST_P(OutlierDetectionTest, FailurePercentageRequestVolume) {
815 CreateAndStartBackends(2);
816 auto cluster = default_cluster_;
817 cluster.set_lb_policy(Cluster::RING_HASH);
818 // Setup outlier failure percentage parameters.
819 // Any failure will cause an potential ejection with the probability of 100%
820 // (to eliminate flakiness of the test).
821 auto* outlier_detection = cluster.mutable_outlier_detection();
822 SetProtoDuration(grpc_core::Duration::Seconds(1),
823 outlier_detection->mutable_interval());
824 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
825 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
826 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
827 // Set failure_percentage_request_volume to 4 when we only send 3 RPC in the
828 // interval.
829 // // Note this parameter is the only difference between this test and
830 // FailurePercentageEjectionAndUnejection (ejection portion, value set to 1)
831 // and this one value changes means the difference between not ejecting in
832 // this test and ejecting in the other test.
833 outlier_detection->mutable_failure_percentage_request_volume()->set_value(4);
834 balancer_->ads_service()->SetCdsResource(cluster);
835 auto new_route_config = default_route_config_;
836 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
837 auto* hash_policy = route->mutable_route()->add_hash_policy();
838 hash_policy->mutable_header()->set_header_name("address_hash");
839 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
840 new_route_config);
841 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
842 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
843 // Note each type of RPC will contains a header value that will always be
844 // hashed to a specific backend as the header value matches the value used
845 // to create the entry in the ring.
846 std::vector<std::pair<std::string, std::string>> metadata = {
847 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
848 std::vector<std::pair<std::string, std::string>> metadata1 = {
849 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
850 const auto rpc_options = RpcOptions().set_metadata(metadata);
851 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
852 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
853 WaitForBackendOptions(), rpc_options);
854 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
855 WaitForBackendOptions(), rpc_options1);
856 // Cause an error and wait for 1 outlier detection interval to pass to cause
857 // the backend to be ejected.
858 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
859 RpcOptions()
860 .set_metadata(std::move(metadata))
861 .set_server_expected_error(StatusCode::CANCELLED));
862 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
863 3000 * grpc_test_slowdown_factor()));
864 ResetBackendCounters();
865 // All traffic still reaching the original backends and no backends are
866 // ejected.
867 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
868 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
869 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
870 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
871 }
872
873 // Tests SuccessRate and FailurePercentage both configured
874 // Configure max_ejection_percent to 50% which means max 2/4 backends can be
875 // ejected.
876 // Configure success rate to eject 1 and failure percentage to eject 2.
877 // Verify that maximum 2 backends are ejected, not 3!
878 TEST_P(OutlierDetectionTest, SuccessRateAndFailurePercentage) {
879 CreateAndStartBackends(4);
880 auto cluster = default_cluster_;
881 cluster.set_lb_policy(Cluster::RING_HASH);
882 // Setup outlier failure percentage parameters.
883 // Any failure will cause an potential ejection with the probability of 100%
884 // (to eliminate flakiness of the test).
885 auto* outlier_detection = cluster.mutable_outlier_detection();
886 SetProtoDuration(grpc_core::Duration::Seconds(1),
887 outlier_detection->mutable_interval());
888 outlier_detection->mutable_max_ejection_percent()->set_value(50);
889 // This stdev of 500 will ensure the number of ok RPC and error RPC we send
890 // will make 1 outlier out of the 4 backends.
891 outlier_detection->mutable_success_rate_stdev_factor()->set_value(500);
892 outlier_detection->mutable_enforcing_success_rate()->set_value(100);
893 outlier_detection->mutable_success_rate_minimum_hosts()->set_value(1);
894 outlier_detection->mutable_success_rate_request_volume()->set_value(1);
895 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
896 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
897 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
898 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
899 balancer_->ads_service()->SetCdsResource(cluster);
900 auto new_route_config = default_route_config_;
901 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
902 auto* hash_policy = route->mutable_route()->add_hash_policy();
903 hash_policy->mutable_header()->set_header_name("address_hash");
904 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
905 new_route_config);
906 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
907 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
908 // Note each type of RPC will contains a header value that will always be
909 // hashed to a specific backend as the header value matches the value used
910 // to create the entry in the ring.
911 std::vector<std::pair<std::string, std::string>> metadata = {
912 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
913 std::vector<std::pair<std::string, std::string>> metadata1 = {
914 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
915 std::vector<std::pair<std::string, std::string>> metadata2 = {
916 {"address_hash", CreateMetadataValueThatHashesToBackend(2)}};
917 std::vector<std::pair<std::string, std::string>> metadata3 = {
918 {"address_hash", CreateMetadataValueThatHashesToBackend(3)}};
919 const auto rpc_options = RpcOptions().set_metadata(metadata);
920 const auto rpc_options1 = RpcOptions().set_metadata(metadata1);
921 const auto rpc_options2 = RpcOptions().set_metadata(metadata2);
922 const auto rpc_options3 = RpcOptions().set_metadata(metadata3);
923 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
924 WaitForBackendOptions(), rpc_options);
925 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
926 WaitForBackendOptions(), rpc_options1);
927 WaitForBackend(DEBUG_LOCATION, 2, /*check_status=*/nullptr,
928 WaitForBackendOptions(), rpc_options2);
929 WaitForBackend(DEBUG_LOCATION, 3, /*check_status=*/nullptr,
930 WaitForBackendOptions(), rpc_options3);
931 // Cause 2 errors on 1 backend and 1 error on 2 backends and wait for 2
932 // backends to be ejected. The 2 errors to the 1 backend will make exactly 1
933 // outlier from the success rate algorithm; all 4 errors will make 3 outliers
934 // from the failure pecentage algorithm because the threahold is set to 0. I
935 // have verified through debug logs we eject 1 backend because of success
936 // rate, 1 backend because of failure percentage; but as we attempt to eject
937 // another backend because of failure percentage we will stop as we have
938 // reached our 50% limit.
939 CheckRpcSendFailure(
940 DEBUG_LOCATION, StatusCode::CANCELLED, "",
941 RpcOptions().set_metadata(metadata).set_server_expected_error(
942 StatusCode::CANCELLED));
943 CheckRpcSendFailure(
944 DEBUG_LOCATION, StatusCode::CANCELLED, "",
945 RpcOptions().set_metadata(metadata).set_server_expected_error(
946 StatusCode::CANCELLED));
947 CheckRpcSendFailure(
948 DEBUG_LOCATION, StatusCode::CANCELLED, "",
949 RpcOptions().set_metadata(metadata1).set_server_expected_error(
950 StatusCode::CANCELLED));
951 CheckRpcSendFailure(
952 DEBUG_LOCATION, StatusCode::CANCELLED, "",
953 RpcOptions().set_metadata(metadata2).set_server_expected_error(
954 StatusCode::CANCELLED));
955 absl::Time deadline =
956 absl::Now() + absl::Seconds(3) * grpc_test_slowdown_factor();
957 std::vector<size_t> idx = {0, 1, 2, 3};
958 while (true) {
959 ResetBackendCounters();
960 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options);
961 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options1);
962 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options2);
963 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options3);
964 if (std::count_if(idx.begin(), idx.end(),
965 [this](size_t i) { return SeenBackend(i); }) == 2) {
966 break;
967 }
968 EXPECT_LE(absl::Now(), deadline);
969 if (absl::Now() >= deadline) break;
970 }
971 ResetBackendCounters();
972 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
973 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
974 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options2);
975 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options3);
976 size_t empty_load_backend_count = 0;
977 size_t double_load_backend_count = 0;
978 for (size_t i = 0; i < backends_.size(); ++i) {
979 if (backends_[i]->backend_service()->request_count() == 0) {
980 ++empty_load_backend_count;
981 } else if (backends_[i]->backend_service()->request_count() >= 100) {
982 // The extra load could go to 2 remaining backends or just 1 of them.
983 ++double_load_backend_count;
984 } else if (backends_[i]->backend_service()->request_count() > 300) {
985 GPR_ASSERT(1);
986 }
987 }
988 EXPECT_EQ(2, empty_load_backend_count);
989 EXPECT_EQ(2, double_load_backend_count);
990 }
991
992 // Tests SuccessRate and FailurePercentage both unconfigured;
993 // This is the case where according to the gRFC we need to instruct the picker
994 // not to do counting or even start the timer. The result of not counting is
995 // that there will be no ejection taking place since we can't do any
996 // calculations.
997 TEST_P(OutlierDetectionTest, SuccessRateAndFailurePercentageBothDisabled) {
998 CreateAndStartBackends(2);
999 auto cluster = default_cluster_;
1000 cluster.set_lb_policy(Cluster::RING_HASH);
1001 auto* outlier_detection = cluster.mutable_outlier_detection();
1002 SetProtoDuration(grpc_core::Duration::Seconds(1),
1003 outlier_detection->mutable_interval());
1004 SetProtoDuration(grpc_core::Duration::Seconds(1),
1005 outlier_detection->mutable_base_ejection_time());
1006 balancer_->ads_service()->SetCdsResource(cluster);
1007 auto new_route_config = default_route_config_;
1008 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1009 auto* hash_policy = route->mutable_route()->add_hash_policy();
1010 hash_policy->mutable_header()->set_header_name("address_hash");
1011 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
1012 new_route_config);
1013 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
1014 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1015 // Note each type of RPC will contains a header value that will always be
1016 // hashed to a specific backend as the header value matches the value used
1017 // to create the entry in the ring.
1018 std::vector<std::pair<std::string, std::string>> metadata = {
1019 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
1020 std::vector<std::pair<std::string, std::string>> metadata1 = {
1021 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
1022 const auto rpc_options = RpcOptions().set_metadata(metadata);
1023 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
1024 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
1025 WaitForBackendOptions(), rpc_options);
1026 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
1027 WaitForBackendOptions(), rpc_options1);
1028 // Cause an error and wait for 1 outlier detection interval to pass
1029 CheckRpcSendFailure(DEBUG_LOCATION, StatusCode::CANCELLED, "",
1030 RpcOptions()
1031 .set_metadata(std::move(metadata))
1032 .set_server_expected_error(StatusCode::CANCELLED));
1033 gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(
1034 3000 * grpc_test_slowdown_factor()));
1035 ResetBackendCounters();
1036 // 1 backend experenced failure, but since there is no counting there is no
1037 // ejection. Both backends are still getting the RPCs intended for them.
1038 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options);
1039 CheckRpcSendOk(DEBUG_LOCATION, 100, rpc_options1);
1040 EXPECT_EQ(100, backends_[0]->backend_service()->request_count());
1041 EXPECT_EQ(100, backends_[1]->backend_service()->request_count());
1042 }
1043
1044 // Tests that we uneject any ejected addresses when the OD policy is
1045 // disabled.
1046 TEST_P(OutlierDetectionTest, DisableOutlierDetectionWhileAddressesAreEjected) {
1047 CreateAndStartBackends(2);
1048 auto cluster = default_cluster_;
1049 cluster.set_lb_policy(Cluster::RING_HASH);
1050 // Setup outlier failure percentage parameters.
1051 // Any failure will cause an potential ejection with the probability of 100%
1052 // (to eliminate flakiness of the test).
1053 auto* outlier_detection = cluster.mutable_outlier_detection();
1054 SetProtoDuration(grpc_core::Duration::Seconds(1),
1055 outlier_detection->mutable_interval());
1056 SetProtoDuration(grpc_core::Duration::Seconds(3),
1057 outlier_detection->mutable_base_ejection_time());
1058 outlier_detection->mutable_failure_percentage_threshold()->set_value(0);
1059 outlier_detection->mutable_enforcing_failure_percentage()->set_value(100);
1060 outlier_detection->mutable_failure_percentage_minimum_hosts()->set_value(1);
1061 outlier_detection->mutable_failure_percentage_request_volume()->set_value(1);
1062 balancer_->ads_service()->SetCdsResource(cluster);
1063 auto new_route_config = default_route_config_;
1064 auto* route = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
1065 auto* hash_policy = route->mutable_route()->add_hash_policy();
1066 hash_policy->mutable_header()->set_header_name("address_hash");
1067 SetListenerAndRouteConfiguration(balancer_.get(), default_listener_,
1068 new_route_config);
1069 EdsResourceArgs args({{"locality0", CreateEndpointsForBackends()}});
1070 balancer_->ads_service()->SetEdsResource(BuildEdsResource(args));
1071 // Note each type of RPC will contains a header value that will always be
1072 // hashed to a specific backend as the header value matches the value used
1073 // to create the entry in the ring.
1074 std::vector<std::pair<std::string, std::string>> metadata = {
1075 {"address_hash", CreateMetadataValueThatHashesToBackend(0)}};
1076 std::vector<std::pair<std::string, std::string>> metadata1 = {
1077 {"address_hash", CreateMetadataValueThatHashesToBackend(1)}};
1078 const auto rpc_options = RpcOptions().set_metadata(metadata);
1079 const auto rpc_options1 = RpcOptions().set_metadata(std::move(metadata1));
1080 WaitForBackend(DEBUG_LOCATION, 0, /*check_status=*/nullptr,
1081 WaitForBackendOptions(), rpc_options);
1082 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
1083 WaitForBackendOptions(), rpc_options1);
1084 // Cause an error and wait for traffic aimed at backend 0 to start going to
1085 // backend 1.
1086 CheckRpcSendFailure(
1087 DEBUG_LOCATION, StatusCode::CANCELLED, "",
1088 RpcOptions().set_metadata(metadata).set_server_expected_error(
1089 StatusCode::CANCELLED));
1090 WaitForBackend(DEBUG_LOCATION, 1, /*check_status=*/nullptr,
1091 WaitForBackendOptions().set_timeout_ms(
1092 3000 * grpc_test_slowdown_factor()),
1093 rpc_options);
1094 // 1 backend is ejected all traffic going to the ejected backend should now
1095 // all be going to the other backend.
1096 // failure percentage enforcement_percentage of 100% is honored as this test
1097 // will consistently reject 1 backend.
1098 CheckRpcSendOk(DEBUG_LOCATION, 1, rpc_options);
1099 EXPECT_EQ(1, backends_[1]->backend_service()->request_count());
1100 // Send an update that disables outlier detection.
1101 cluster.clear_outlier_detection();
1102 balancer_->ads_service()->SetCdsResource(cluster);
1103 // Wait for the backend to start being used again.
1104 WaitForBackend(
1105 DEBUG_LOCATION, 0,
1106 [](const RpcResult& result) {
1107 EXPECT_EQ(result.status.error_code(), StatusCode::CANCELLED)
1108 << "Error: " << result.status.error_message();
1109 },
1110 WaitForBackendOptions(),
1111 RpcOptions()
1112 .set_metadata(std::move(metadata))
1113 .set_server_expected_error(StatusCode::CANCELLED));
1114 }
1115
1116 } // namespace
1117 } // namespace testing
1118 } // namespace grpc
1119
1120 int main(int argc, char** argv) {
1121 grpc::testing::TestEnvironment env(&argc, argv);
1122 ::testing::InitGoogleTest(&argc, argv);
1123 // Make the backup poller poll very frequently in order to pick up
1124 // updates from all the subchannels's FDs.
1125 GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);
1126 #if TARGET_OS_IPHONE
1127 // Workaround Apple CFStream bug
1128 grpc_core::SetEnv("grpc_cfstream", "0");
1129 #endif
1130 grpc_init();
1131 const auto result = RUN_ALL_TESTS();
1132 grpc_shutdown();
1133 return result;
1134 }