forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cds_integration_test.cc
553 lines (482 loc) · 27.3 KB
/
cds_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#include "envoy/config/cluster/v3/cluster.pb.h"
#include "envoy/grpc/status.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "envoy/stats/scope.h"
#include "source/common/config/protobuf_link_hacks.h"
#include "source/common/protobuf/protobuf.h"
#include "source/common/protobuf/utility.h"
#include "test/common/grpc/grpc_client_integration.h"
#include "test/config/v2_link_hacks.h"
#include "test/integration/http_integration.h"
#include "test/integration/utility.h"
#include "test/test_common/network_utility.h"
#include "test/test_common/resources.h"
#include "test/test_common/simulated_time_system.h"
#include "test/test_common/utility.h"
#include "absl/synchronization/notification.h"
#include "gtest/gtest.h"
using testing::AssertionResult;
namespace Envoy {
namespace {
const char ClusterName1[] = "cluster_1";
const char ClusterName2[] = "cluster_2";
const int UpstreamIndex1 = 1;
const int UpstreamIndex2 = 2;
class CdsIntegrationTest : public Grpc::DeltaSotwIntegrationParamTest, public HttpIntegrationTest {
public:
CdsIntegrationTest()
: HttpIntegrationTest(Http::CodecType::HTTP2, ipVersion(),
ConfigHelper::discoveredClustersBootstrap(
sotwOrDelta() == Grpc::SotwOrDelta::Sotw ||
sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw
? "GRPC"
: "DELTA_GRPC")),
cluster_creator_(&ConfigHelper::buildStaticCluster) {
if (sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw ||
sotwOrDelta() == Grpc::SotwOrDelta::UnifiedDelta) {
config_helper_.addRuntimeOverride("envoy.reloadable_features.unified_mux", "true");
}
use_lds_ = false;
sotw_or_delta_ = sotwOrDelta();
}
void TearDown() override {
if (!test_skipped_) {
cleanUpXdsConnection();
}
}
// Overridden to insert this stuff into the initialize() at the very beginning of
// HttpIntegrationTest::testRouterHeaderOnlyRequestAndResponse().
void initialize() override {
use_lds_ = false;
test_skipped_ = false;
// Controls how many addFakeUpstream() will happen in
// BaseIntegrationTest::createUpstreams() (which is part of initialize()).
// Make sure this number matches the size of the 'clusters' repeated field in the bootstrap
// config that you use!
setUpstreamCount(1); // the CDS cluster
setUpstreamProtocol(Http::CodecType::HTTP2); // CDS uses gRPC uses HTTP2.
// HttpIntegrationTest::initialize() does many things:
// 1) It appends to fake_upstreams_ as many as you asked for via setUpstreamCount().
// 2) It updates your bootstrap config with the ports your fake upstreams are actually listening
// on (since you're supposed to leave them as 0).
// 3) It creates and starts an IntegrationTestServer - the thing that wraps the almost-actual
// Envoy used in the tests.
// 4) Bringing up the server usually entails waiting to ensure that any listeners specified in
// the bootstrap config have come up, and registering them in a port map (see lookupPort()).
// However, this test needs to defer all of that to later.
defer_listener_finalization_ = true;
HttpIntegrationTest::initialize();
// Create the regular (i.e. not an xDS server) upstreams. We create them manually here after
// initialize() because finalize() expects all fake_upstreams_ to correspond to a static
// cluster in the bootstrap config - which we don't want since we're testing dynamic CDS!
addFakeUpstream(upstream_codec_type_);
addFakeUpstream(upstream_codec_type_);
cluster1_ = cluster_creator_(
ClusterName1, fake_upstreams_[UpstreamIndex1]->localAddress()->ip()->port(),
Network::Test::getLoopbackAddressString(ipVersion()), "ROUND_ROBIN");
cluster2_ = cluster_creator_(
ClusterName2, fake_upstreams_[UpstreamIndex2]->localAddress()->ip()->port(),
Network::Test::getLoopbackAddressString(ipVersion()), "ROUND_ROBIN");
// Let Envoy establish its connection to the CDS server.
acceptXdsConnection();
// Do the initial compareDiscoveryRequest / sendDiscoveryResponse for cluster_1.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "", {}, {}, {}, true));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster,
{cluster1_}, {cluster1_}, {}, "55");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse describing cluster_1 that we sent.
// 2 because the statically specified CDS server itself counts as a cluster.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 2);
// Wait for our statically specified listener to become ready, and register its port in the
// test framework's downstream listener port map.
test_server_->waitUntilListenersReady();
registerTestServerPorts({"http"});
}
// Regression test to catch the code declaring a gRPC service method for {SotW,delta}
// when the user's bootstrap config asks for the other type.
void verifyGrpcServiceMethod() {
EXPECT_TRUE(xds_stream_->waitForHeadersComplete());
Envoy::Http::LowerCaseString path_string(":path");
std::string expected_method(
sotwOrDelta() == Grpc::SotwOrDelta::Sotw || sotwOrDelta() == Grpc::SotwOrDelta::UnifiedSotw
? "/envoy.service.cluster.v3.ClusterDiscoveryService/StreamClusters"
: "/envoy.service.cluster.v3.ClusterDiscoveryService/DeltaClusters");
EXPECT_EQ(xds_stream_->headers().get(path_string)[0]->value(), expected_method);
}
void acceptXdsConnection() {
AssertionResult result = // xds_connection_ is filled with the new FakeHttpConnection.
fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, xds_connection_);
RELEASE_ASSERT(result, result.message());
result = xds_connection_->waitForNewStream(*dispatcher_, xds_stream_);
RELEASE_ASSERT(result, result.message());
xds_stream_->startGrpcStream();
verifyGrpcServiceMethod();
}
envoy::config::cluster::v3::Cluster cluster1_;
envoy::config::cluster::v3::Cluster cluster2_;
// True if we decided not to run the test after all.
bool test_skipped_{true};
Http::CodecType upstream_codec_type_{Http::CodecType::HTTP2};
std::function<envoy::config::cluster::v3::Cluster(const std::string&, int, const std::string&,
const std::string&)>
cluster_creator_;
};
INSTANTIATE_TEST_SUITE_P(IpVersionsClientTypeDelta, CdsIntegrationTest,
DELTA_SOTW_GRPC_CLIENT_INTEGRATION_PARAMS);
// 1) Envoy starts up with no static clusters (other than the CDS-over-gRPC server).
// 2) Envoy is told of a cluster via CDS.
// 3) We send Envoy a request, which we verify is properly proxied to and served by that cluster.
// 4) Envoy is told that cluster is gone.
// 5) We send Envoy a request, which should 503.
// 6) Envoy is told that the cluster is back.
// 7) We send Envoy a request, which we verify is properly proxied to and served by that cluster.
TEST_P(CdsIntegrationTest, CdsClusterUpDownUp) {
// Calls our initialize(), which includes establishing a listener, route, and cluster.
config_helper_.addConfigModifier(configureProxyStatus());
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
// Tell Envoy that cluster_1 is gone.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster, {}, {},
{ClusterName1}, "42");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse that says cluster_1 is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
// Now that cluster_1 is gone, the listener (with its routing to cluster_1) should 503.
BufferingStreamDecoderPtr response = IntegrationUtil::makeSingleRequest(
lookupPort("http"), "GET", "/cluster1", "", downstream_protocol_, version_, "foo.com");
ASSERT_TRUE(response->complete());
EXPECT_EQ("503", response->headers().getStatusValue());
EXPECT_EQ(response->headers().getProxyStatusValue(),
"envoy; error=destination_unavailable; details=\"cluster_not_found; NC\"");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Tell Envoy that cluster_1 is back.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "42", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster,
{cluster1_}, {cluster1_}, {}, "413");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse describing cluster_1 that we sent. Again, 2 includes CDS server.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 2);
// Does *not* call our initialize().
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
cleanupUpstreamAndDownstream();
}
// Make sure that clusters won't create new connections on teardown.
TEST_P(CdsIntegrationTest, CdsClusterTeardownWhileConnecting) {
initialize();
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
test_server_->waitForCounterExists("cluster.cluster_1.upstream_cx_total");
Stats::CounterSharedPtr cx_counter = test_server_->counter("cluster.cluster_1.upstream_cx_total");
// Confirm no upstream connection is attempted so far.
EXPECT_EQ(0, cx_counter->value());
// Make the upstreams stop working, to ensure the connection was not
// established.
fake_upstreams_[1]->dispatcher()->exit();
fake_upstreams_[2]->dispatcher()->exit();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto encoder_decoder = codec_client_->startRequest(Http::TestRequestHeaderMapImpl{
{":method", "GET"}, {":path", "/cluster1"}, {":scheme", "http"}, {":authority", "host"}});
// Tell Envoy that cluster_1 is gone.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster, {}, {},
{ClusterName1}, "42");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse that says cluster_1 is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
codec_client_->sendReset(encoder_decoder.first);
cleanupUpstreamAndDownstream();
// Either 0 or 1 upstream connection is attempted but no more.
EXPECT_LE(cx_counter->value(), 1);
}
// Test the fast addition and removal of clusters when they use ThreadAwareLb.
TEST_P(CdsIntegrationTest, CdsClusterWithThreadAwareLbCycleUpDownUp) {
// Calls our initialize(), which includes establishing a listener, route, and cluster.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
// Tell Envoy that cluster_1 is gone.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster, {}, {},
{ClusterName1}, "42");
// Make sure that Envoy's ClusterManager has made use of the DiscoveryResponse that says cluster_1
// is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
// Update cluster1_ to use MAGLEV load balancer policy.
cluster1_ = ConfigHelper::buildStaticCluster(
ClusterName1, fake_upstreams_[UpstreamIndex1]->localAddress()->ip()->port(),
Network::Test::getLoopbackAddressString(ipVersion()), "MAGLEV");
// Cyclically add and remove cluster with ThreadAwareLb.
for (int i = 42; i < 142; i += 2) {
EXPECT_TRUE(
compareDiscoveryRequest(Config::TypeUrl::get().Cluster, absl::StrCat(i), {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_}, {cluster1_}, {}, absl::StrCat(i + 1));
EXPECT_TRUE(
compareDiscoveryRequest(Config::TypeUrl::get().Cluster, absl::StrCat(i + 1), {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {}, {}, {ClusterName1}, absl::StrCat(i + 2));
}
cleanupUpstreamAndDownstream();
}
// Tests adding a cluster, adding another, then removing the first.
TEST_P(CdsIntegrationTest, TwoClusters) {
// Calls our initialize(), which includes establishing a listener, route, and cluster.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Tell Envoy that cluster_2 is here.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster2_}, {cluster2_}, {}, "42");
// The '3' includes the fake CDS server.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// A request for cluster_2 should be fine.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex2, "/cluster2");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Tell Envoy that cluster_1 is gone.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "42", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster,
{cluster2_}, {}, {ClusterName1}, "43");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse that says cluster_1 is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
// Even with cluster_1 gone, a request for cluster_2 should be fine.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex2, "/cluster2");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Tell Envoy that cluster_1 is back.
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "43", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster2_}, {cluster1_}, {}, "413");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse describing cluster_1 that we sent. Again, 3 includes CDS server.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// Does *not* call our initialize().
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
cleanupUpstreamAndDownstream();
}
// Test internal redirect to a cluster removed during the backend think time.
TEST_P(CdsIntegrationTest, TwoClustersAndRedirects) {
setDownstreamProtocol(Http::CodecType::HTTP1);
config_helper_.addConfigModifier(
[](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) {
auto* route = hcm.mutable_route_config()->mutable_virtual_hosts(0)->mutable_routes(1);
route->mutable_route()
->mutable_internal_redirect_policy()
->mutable_redirect_response_codes()
->Add(302);
});
// Tell Envoy that cluster_2 is here.
initialize();
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(
Config::TypeUrl::get().Cluster, {cluster1_, cluster2_}, {cluster2_}, {}, "42");
// The '3' includes the fake CDS server.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// Tell Envoy that cluster_1 is gone.
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster,
{cluster2_}, {}, {ClusterName1}, "43");
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
default_request_headers_.setPath("/cluster2");
default_request_headers_.setContentLength("4");
auto encoder_decoder = codec_client_->startRequest(default_request_headers_);
Buffer::OwnedImpl data("body");
encoder_decoder.first.encodeData(data, true);
auto& response = encoder_decoder.second;
ASSERT_TRUE(fake_upstreams_[UpstreamIndex2]->waitForHttpConnection(*dispatcher_,
fake_upstream_connection_));
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForEndStream(*dispatcher_));
Http::TestResponseHeaderMapImpl redirect_response{
{":status", "302"}, {"content-length", "0"}, {"location", "http://host/cluster1"}};
// Send a response to the original request redirecting to the deleted cluster.
upstream_request_->encodeHeaders(redirect_response, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ("503", response->headers().getStatusValue());
}
// Tests that when Envoy's delta xDS stream dis/reconnects, Envoy can inform the server of the
// resources it already has: the reconnected stream need not start with a state-of-the-world update.
TEST_P(CdsIntegrationTest, VersionsRememberedAfterReconnect) {
SKIP_IF_XDS_IS(Grpc::SotwOrDelta::Sotw);
SKIP_IF_XDS_IS(Grpc::SotwOrDelta::UnifiedSotw);
// Calls our initialize(), which includes establishing a listener, route, and cluster.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// Close the connection carrying Envoy's xDS gRPC stream...
AssertionResult result = xds_connection_->close();
RELEASE_ASSERT(result, result.message());
result = xds_connection_->waitForDisconnect();
RELEASE_ASSERT(result, result.message());
xds_connection_.reset();
// ...and reconnect it.
acceptXdsConnection();
// Upon reconnecting, the Envoy should tell us its current resource versions.
envoy::service::discovery::v3::DeltaDiscoveryRequest request;
result = xds_stream_->waitForGrpcMessage(*dispatcher_, request);
RELEASE_ASSERT(result, result.message());
const auto& initial_resource_versions = request.initial_resource_versions();
EXPECT_EQ("55", initial_resource_versions.at(std::string(ClusterName1)));
EXPECT_EQ(1, initial_resource_versions.size());
// Tell Envoy that cluster_2 is here. This update does *not* need to include cluster_1,
// which Envoy should already know about despite the disconnect.
sendDeltaDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster,
{cluster2_}, {}, "42");
// The '3' includes the fake CDS server.
test_server_->waitForGaugeGe("cluster_manager.active_clusters", 3);
// A request for cluster_1 should be fine.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex1, "/cluster1");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// A request for cluster_2 should be fine.
testRouterHeaderOnlyRequestAndResponse(nullptr, UpstreamIndex2, "/cluster2");
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
}
// This test verifies that Envoy can delete a cluster with a lot of idle connections.
// The original problem was recursive closure of idle connections that can run out
// of stack when there are a lot of idle connections.
TEST_P(CdsIntegrationTest, CdsClusterDownWithLotsOfIdleConnections) {
constexpr int num_requests = 2000;
// Make upstream H/1 so it creates connection for each request
upstream_codec_type_ = Http::CodecType::HTTP1;
// Relax default circuit breaker limits and timeouts so Envoy can accumulate a lot of idle
// connections
cluster_creator_ = &ConfigHelper::buildH1ClusterWithHighCircuitBreakersLimits;
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
hcm.mutable_route_config()
->mutable_virtual_hosts(0)
->mutable_routes(0)
->mutable_route()
->mutable_timeout()
->set_seconds(600);
hcm.mutable_route_config()
->mutable_virtual_hosts(0)
->mutable_routes(0)
->mutable_route()
->mutable_idle_timeout()
->set_seconds(600);
});
initialize();
std::vector<IntegrationStreamDecoderPtr> responses;
std::vector<FakeHttpConnectionPtr> upstream_connections;
std::vector<FakeStreamPtr> upstream_requests;
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
// The first loop establishes a lot of open connections with active requests to upstream
for (int i = 0; i < num_requests; ++i) {
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/cluster1"},
{":scheme", "http"},
{":authority", "host"},
{"x-lyft-user-id", absl::StrCat(i)}};
auto response = codec_client_->makeHeaderOnlyRequest(request_headers);
responses.push_back(std::move(response));
FakeHttpConnectionPtr fake_upstream_connection;
waitForNextUpstreamConnection({UpstreamIndex1}, TestUtility::DefaultTimeout,
fake_upstream_connection);
// Wait for the next stream on the upstream connection.
FakeStreamPtr upstream_request;
AssertionResult result =
fake_upstream_connection->waitForNewStream(*dispatcher_, upstream_request);
RELEASE_ASSERT(result, result.message());
// Wait for the stream to be completely received.
result = upstream_request->waitForEndStream(*dispatcher_);
RELEASE_ASSERT(result, result.message());
upstream_connections.push_back(std::move(fake_upstream_connection));
upstream_requests.push_back(std::move(upstream_request));
}
// This loop completes all requests making the all upstream connections idle
for (int i = 0; i < num_requests; ++i) {
// Send response headers, and end_stream if there is no response body.
upstream_requests[i]->encodeHeaders(default_response_headers_, true);
// Wait for the response to be read by the codec client.
RELEASE_ASSERT(responses[i]->waitForEndStream(), "unexpected timeout");
ASSERT_TRUE(responses[i]->complete());
EXPECT_EQ("200", responses[i]->headers().getStatusValue());
}
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
// Tell Envoy that cluster_1 is gone. Envoy will try to close all idle connections
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster, {}, {},
{ClusterName1}, "42");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse that says cluster_1 is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
// If we made it this far then everything is ok.
for (int i = 0; i < num_requests; ++i) {
AssertionResult result = upstream_connections[i]->close();
RELEASE_ASSERT(result, result.message());
result = upstream_connections[i]->waitForDisconnect();
RELEASE_ASSERT(result, result.message());
}
upstream_connections.clear();
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
}
// This test verifies that Envoy can delete a cluster with a lot of connections in the connecting
// state and associated pending requests. The recursion guard in the
// ConnPoolImplBase::closeIdleConnectionsForDrainingPool() would fire if it was called recursively.
//
// Test is currently disabled as there is presently no reliable way of making upstream connections
// hang in connecting state.
TEST_P(CdsIntegrationTest, DISABLED_CdsClusterDownWithLotsOfConnectingConnections) {
// Use low number of pending connections to prevent bumping into the default
// limit of 128, since the upstream will be prevented below from
// accepting connections.
constexpr int num_requests = 64;
// Make upstream H/1 so it creates connection for each request
upstream_codec_type_ = Http::CodecType::HTTP1;
cluster_creator_ = &ConfigHelper::buildH1ClusterWithHighCircuitBreakersLimits;
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
hcm.mutable_route_config()
->mutable_virtual_hosts(0)
->mutable_routes(0)
->mutable_route()
->mutable_timeout()
->set_seconds(600);
hcm.mutable_route_config()
->mutable_virtual_hosts(0)
->mutable_routes(0)
->mutable_route()
->mutable_idle_timeout()
->set_seconds(600);
});
initialize();
test_server_->waitForCounterGe("cluster_manager.cluster_added", 1);
std::vector<IntegrationStreamDecoderPtr> responses;
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
// Stop upstream at UpstreamIndex1 dispatcher, to prevent it from accepting TCP connections.
// This will cause Envoy's connections to that upstream hang in the connecting state.
fake_upstreams_[UpstreamIndex1]->dispatcher()->exit();
for (int i = 0; i < num_requests; ++i) {
Http::TestRequestHeaderMapImpl request_headers{{":method", "GET"},
{":path", "/cluster1"},
{":scheme", "http"},
{":authority", "host"},
{"x-lyft-user-id", absl::StrCat(i)}};
auto response = codec_client_->makeHeaderOnlyRequest(request_headers);
responses.push_back(std::move(response));
}
// Wait for Envoy to try to establish all expected connections
test_server_->waitForCounterEq("cluster.cluster_1.upstream_cx_total", num_requests);
// Tell Envoy that cluster_1 is gone. Envoy will try to close all pending connections
EXPECT_TRUE(compareDiscoveryRequest(Config::TypeUrl::get().Cluster, "55", {}, {}, {}));
sendDiscoveryResponse<envoy::config::cluster::v3::Cluster>(Config::TypeUrl::get().Cluster, {}, {},
{ClusterName1}, "42");
// We can continue the test once we're sure that Envoy's ClusterManager has made use of
// the DiscoveryResponse that says cluster_1 is gone.
test_server_->waitForCounterGe("cluster_manager.cluster_removed", 1);
cleanupUpstreamAndDownstream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
// If we got here it means that the recursion guard in the
// ConnPoolImplBase::closeIdleConnectionsForDrainingPool() did not fire, which is what this test
// validates.
}
} // namespace
} // namespace Envoy