forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload_integration_test.cc
378 lines (321 loc) · 17.4 KB
/
overload_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
#include <unordered_map>
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/overload/v3/overload.pb.h"
#include "test/integration/base_overload_integration_test.h"
#include "test/integration/http_protocol_integration.h"
#include "test/integration/ssl_utility.h"
#include "absl/strings/str_cat.h"
using testing::InvokeWithoutArgs;
namespace Envoy {
using testing::HasSubstr;
class OverloadIntegrationTest : public BaseOverloadIntegrationTest,
public HttpProtocolIntegrationTest {
protected:
void
initializeOverloadManager(const envoy::config::overload::v3::OverloadAction& overload_action) {
setupOverloadManagerConfig(overload_action);
config_helper_.addConfigModifier([this](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
*bootstrap.mutable_overload_manager() = this->overload_manager_config_;
});
initialize();
updateResource(0);
}
};
INSTANTIATE_TEST_SUITE_P(Protocols, OverloadIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams(
{Http::CodecClient::Type::HTTP1, Http::CodecClient::Type::HTTP2,
Http::CodecClient::Type::HTTP3},
{FakeHttpConnection::Type::HTTP1, FakeHttpConnection::Type::HTTP2})),
HttpProtocolIntegrationTest::protocolTestParamsToString);
TEST_P(OverloadIntegrationTest, CloseStreamsWhenOverloaded) {
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::OverloadAction>(R"EOF(
name: "envoy.overload_actions.stop_accepting_requests"
triggers:
- name: "envoy.resource_monitors.testonly.fake_resource_monitor"
threshold:
value: 0.9
)EOF"));
// Put envoy in overloaded state and check that it drops new requests.
// Test both header-only and header+body requests since the code paths are slightly different.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.stop_accepting_requests.active", 1);
Http::TestRequestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeRequestWithBody(request_headers, 10);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(response->complete());
EXPECT_EQ("503", response->headers().getStatusValue());
EXPECT_EQ("envoy overloaded", response->body());
codec_client_->close();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
response = codec_client_->makeHeaderOnlyRequest(request_headers);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(response->complete());
EXPECT_EQ("503", response->headers().getStatusValue());
EXPECT_EQ("envoy overloaded", response->body());
codec_client_->close();
// Deactivate overload state and check that new requests are accepted.
updateResource(0.8);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.stop_accepting_requests.active", 0);
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
response = sendRequestAndWaitForResponse(request_headers, 0, default_response_headers_, 0);
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ(0U, response->body().size());
}
TEST_P(OverloadIntegrationTest, DisableKeepaliveWhenOverloaded) {
if (downstreamProtocol() != Http::CodecType::HTTP1) {
return; // only relevant for downstream HTTP1.x connections
}
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::OverloadAction>(R"EOF(
name: "envoy.overload_actions.disable_http_keepalive"
triggers:
- name: "envoy.resource_monitors.testonly.fake_resource_monitor"
threshold:
value: 0.8
)EOF"));
// Put envoy in overloaded state and check that it disables keepalive
updateResource(0.8);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.disable_http_keepalive.active", 1);
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
auto response = sendRequestAndWaitForResponse(request_headers, 1, default_response_headers_, 1);
ASSERT_TRUE(codec_client_->waitForDisconnect());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ("close", response->headers().getConnectionValue());
// Deactivate overload state and check that keepalive is not disabled
updateResource(0.7);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.disable_http_keepalive.active", 0);
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
response = sendRequestAndWaitForResponse(request_headers, 1, default_response_headers_, 1);
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ(nullptr, response->headers().Connection());
}
TEST_P(OverloadIntegrationTest, StopAcceptingConnectionsWhenOverloaded) {
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::OverloadAction>(R"EOF(
name: "envoy.overload_actions.stop_accepting_connections"
triggers:
- name: "envoy.resource_monitors.testonly.fake_resource_monitor"
threshold:
value: 0.95
)EOF"));
// Put envoy in overloaded state and check that it doesn't accept the new client connection.
updateResource(0.95);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.stop_accepting_connections.active",
1);
IntegrationStreamDecoderPtr response;
if (downstreamProtocol() == Http::CodecClient::Type::HTTP3) {
// For HTTP/3, excess connections are force-rejected.
codec_client_ =
makeRawHttpConnection(makeClientConnection((lookupPort("http"))), absl::nullopt);
EXPECT_TRUE(codec_client_->disconnected());
} else {
// For HTTP/2 and below, excess connection won't be accepted, but will hang out
// in a pending state and resume below.
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
response = codec_client_->makeRequestWithBody(default_request_headers_, 10);
EXPECT_FALSE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_,
std::chrono::milliseconds(1000)));
}
// Reduce load a little to allow the connection to be accepted.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.stop_accepting_connections.active",
0);
if (downstreamProtocol() == Http::CodecClient::Type::HTTP3) {
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
response = codec_client_->makeRequestWithBody(default_request_headers_, 10);
}
EXPECT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
EXPECT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, 10));
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "202"}}, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(response->complete());
EXPECT_EQ("202", response->headers().getStatusValue());
codec_client_->close();
}
class OverloadScaledTimerIntegrationTest : public OverloadIntegrationTest {
protected:
void initializeOverloadManager(
const envoy::config::overload::v3::ScaleTimersOverloadActionConfig& config) {
envoy::config::overload::v3::OverloadAction overload_action =
TestUtility::parseYaml<envoy::config::overload::v3::OverloadAction>(R"EOF(
name: "envoy.overload_actions.reduce_timeouts"
triggers:
- name: "envoy.resource_monitors.testonly.fake_resource_monitor"
scaled:
scaling_threshold: 0.5
saturation_threshold: 0.9
)EOF");
overload_action.mutable_typed_config()->PackFrom(config);
OverloadIntegrationTest::initializeOverloadManager(overload_action);
}
};
INSTANTIATE_TEST_SUITE_P(Protocols, OverloadScaledTimerIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
TEST_P(OverloadScaledTimerIntegrationTest, CloseIdleHttpConnections) {
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::ScaleTimersOverloadActionConfig>(R"EOF(
timer_scale_factors:
- timer: HTTP_DOWNSTREAM_CONNECTION_IDLE
min_timeout: 5s
)EOF"));
const Http::TestRequestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
// Create an HTTP connection and complete a request.
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeRequestWithBody(request_headers, 10);
ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, 10));
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
ASSERT_TRUE(response->waitForEndStream());
// At this point, the connection should be idle but still open.
ASSERT_TRUE(codec_client_->connected());
// Set the load so the timer is reduced but not to the minimum value.
updateResource(0.8);
test_server_->waitForGaugeGe("overload.envoy.overload_actions.reduce_timeouts.scale_percent", 50);
// Advancing past the minimum time shouldn't close the connection.
timeSystem().advanceTimeWait(std::chrono::seconds(5));
// Increase load so that the minimum time has now elapsed.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.reduce_timeouts.scale_percent",
100);
// Wait for the proxy to notice and take action for the overload.
test_server_->waitForCounterGe("http.config_test.downstream_cx_idle_timeout", 1);
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
if (GetParam().downstream_protocol == Http::CodecType::HTTP1) {
// For HTTP1, Envoy will start draining but will wait to close the
// connection. If a new stream comes in, it will set the connection header
// to "close" on the response and close the connection after.
auto response = codec_client_->makeRequestWithBody(request_headers, 10);
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
ASSERT_TRUE(upstream_request_->waitForData(*dispatcher_, 10));
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ(response->headers().getConnectionValue(), "close");
} else {
ASSERT_TRUE(codec_client_->waitForDisconnect());
EXPECT_TRUE(codec_client_->sawGoAway());
}
codec_client_->close();
}
TEST_P(OverloadScaledTimerIntegrationTest, CloseIdleHttpStream) {
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::ScaleTimersOverloadActionConfig>(R"EOF(
timer_scale_factors:
- timer: HTTP_DOWNSTREAM_STREAM_IDLE
min_timeout: 5s
)EOF"));
const Http::TestRequestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
// Create an HTTP connection and start a request.
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeRequestWithBody(request_headers, 10);
ASSERT_TRUE(fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_));
ASSERT_TRUE(fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_));
ASSERT_TRUE(upstream_request_->waitForHeadersComplete());
// At this point, Envoy is waiting for the upstream to respond, but that won't
// happen before it hits the stream timeout.
// Set the load so the timer is reduced but not to the minimum value.
updateResource(0.8);
test_server_->waitForGaugeGe("overload.envoy.overload_actions.reduce_timeouts.scale_percent", 50);
// Advancing past the minimum time shouldn't end the stream.
timeSystem().advanceTimeWait(std::chrono::seconds(5));
// Increase load so that the minimum time has now elapsed.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.reduce_timeouts.scale_percent",
100);
// Wait for the proxy to notice and take action for the overload.
test_server_->waitForCounterGe("http.config_test.downstream_rq_idle_timeout", 1);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_EQ(response->headers().getStatusValue(), "504");
EXPECT_THAT(response->body(), HasSubstr("stream timeout"));
}
TEST_P(OverloadScaledTimerIntegrationTest, TlsHandshakeTimeout) {
// Set up the Envoy to expect a TLS connection, with a 20 second timeout that can scale down to 5
// seconds.
config_helper_.addSslConfig();
config_helper_.addConfigModifier([&](envoy::config::bootstrap::v3::Bootstrap& bootstrap) -> void {
auto* filter_chain =
bootstrap.mutable_static_resources()->mutable_listeners(0)->mutable_filter_chains(0);
auto* connect_timeout = filter_chain->mutable_transport_socket_connect_timeout();
connect_timeout->set_seconds(20);
connect_timeout->set_nanos(0);
});
initializeOverloadManager(
TestUtility::parseYaml<envoy::config::overload::v3::ScaleTimersOverloadActionConfig>(R"EOF(
timer_scale_factors:
- timer: TRANSPORT_SOCKET_CONNECT
min_timeout: 5s
)EOF"));
// Set up a delinquent transport socket that causes the dispatcher to exit on every read & write
// instead of actually doing anything useful.
auto bad_transport_socket = std::make_unique<NiceMock<Network::MockTransportSocket>>();
Network::TransportSocketCallbacks* transport_callbacks;
EXPECT_CALL(*bad_transport_socket, setTransportSocketCallbacks)
.WillOnce(SaveArgAddress(&transport_callbacks));
ON_CALL(*bad_transport_socket, doRead).WillByDefault(InvokeWithoutArgs([&] {
Buffer::OwnedImpl buffer;
transport_callbacks->connection().dispatcher().exit();
// Read some amount of data; what's more important is whether the socket was remote-closed. That
// needs to be propagated to the socket.
return Network::IoResult{transport_callbacks->ioHandle().read(buffer, 2 * 1024).return_value_ ==
0
? Network::PostIoAction::Close
: Network::PostIoAction::KeepOpen,
0, false};
}));
ON_CALL(*bad_transport_socket, doWrite).WillByDefault(InvokeWithoutArgs([&] {
transport_callbacks->connection().dispatcher().exit();
return Network::IoResult{Network::PostIoAction::KeepOpen, 0, false};
}));
ConnectionStatusCallbacks connect_callbacks;
Network::Address::InstanceConstSharedPtr address =
Ssl::getSslAddress(version_, lookupPort("http"));
auto bad_ssl_client =
dispatcher_->createClientConnection(address, Network::Address::InstanceConstSharedPtr(),
std::move(bad_transport_socket), nullptr, nullptr);
bad_ssl_client->addConnectionCallbacks(connect_callbacks);
bad_ssl_client->enableHalfClose(true);
bad_ssl_client->connect();
// Run the dispatcher until it exits due to a read/write.
dispatcher_->run(Event::Dispatcher::RunType::RunUntilExit);
// At this point, the connection should be idle but the SSL handshake isn't done.
EXPECT_FALSE(connect_callbacks.connected());
// Set the load so the timer is reduced but not to the minimum value.
updateResource(0.8);
test_server_->waitForGaugeGe("overload.envoy.overload_actions.reduce_timeouts.scale_percent", 50);
// Advancing past the minimum time shouldn't close the connection, but it shouldn't complete it
// either.
timeSystem().advanceTimeWait(std::chrono::seconds(5));
EXPECT_FALSE(connect_callbacks.connected());
// At this point, Envoy has been waiting for the (bad) client to finish the TLS handshake for 5
// seconds. Increase the load so that the minimum time has now elapsed. This should cause Envoy to
// close the connection on its end.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.reduce_timeouts.scale_percent",
100);
// The bad client will continue attempting to read, eventually noticing the remote close and
// closing the connection.
while (!connect_callbacks.closed()) {
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
}
// The transport-level connection was never completed, and the connection was closed.
EXPECT_FALSE(connect_callbacks.connected());
EXPECT_TRUE(connect_callbacks.closed());
}
} // namespace Envoy