forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoverload_integration_test.cc
361 lines (299 loc) · 15.3 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
#include <unordered_map>
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/config/overload/v3/overload.pb.h"
#include "envoy/server/resource_monitor.h"
#include "envoy/server/resource_monitor_config.h"
#include "test/common/config/dummy_config.pb.h"
#include "test/integration/http_protocol_integration.h"
#include "test/test_common/registry.h"
#include "absl/strings/str_cat.h"
namespace Envoy {
using testing::HasSubstr;
class FakeResourceMonitorFactory;
class FakeResourceMonitor : public Server::ResourceMonitor {
public:
FakeResourceMonitor(Event::Dispatcher& dispatcher, FakeResourceMonitorFactory& factory)
: dispatcher_(dispatcher), factory_(factory), pressure_(0.0) {}
~FakeResourceMonitor() override;
void updateResourceUsage(Callbacks& callbacks) override;
void setResourcePressure(double pressure) {
dispatcher_.post([this, pressure] { pressure_ = pressure; });
}
private:
Event::Dispatcher& dispatcher_;
FakeResourceMonitorFactory& factory_;
double pressure_;
};
class FakeResourceMonitorFactory : public Server::Configuration::ResourceMonitorFactory {
public:
FakeResourceMonitor* monitor() const { return monitor_; }
Server::ResourceMonitorPtr
createResourceMonitor(const Protobuf::Message& config,
Server::Configuration::ResourceMonitorFactoryContext& context) override;
ProtobufTypes::MessagePtr createEmptyConfigProto() override {
return std::make_unique<test::common::config::DummyConfig>();
}
std::string name() const override {
return "envoy.resource_monitors.testonly.fake_resource_monitor";
}
void onMonitorDestroyed(FakeResourceMonitor* monitor);
private:
FakeResourceMonitor* monitor_{nullptr};
};
FakeResourceMonitor::~FakeResourceMonitor() { factory_.onMonitorDestroyed(this); }
void FakeResourceMonitor::updateResourceUsage(Callbacks& callbacks) {
Server::ResourceUsage usage;
usage.resource_pressure_ = pressure_;
callbacks.onSuccess(usage);
}
void FakeResourceMonitorFactory::onMonitorDestroyed(FakeResourceMonitor* monitor) {
ASSERT(monitor_ == monitor);
monitor_ = nullptr;
}
Server::ResourceMonitorPtr FakeResourceMonitorFactory::createResourceMonitor(
const Protobuf::Message&, Server::Configuration::ResourceMonitorFactoryContext& context) {
auto monitor = std::make_unique<FakeResourceMonitor>(context.dispatcher(), *this);
monitor_ = monitor.get();
return monitor;
}
class OverloadIntegrationTest : public HttpProtocolIntegrationTest {
protected:
void
initializeOverloadManager(const envoy::config::overload::v3::OverloadAction& overload_action) {
const std::string overload_config = R"EOF(
refresh_interval:
seconds: 0
nanos: 1000000
resource_monitors:
- name: "envoy.resource_monitors.testonly.fake_resource_monitor"
typed_config:
"@type": type.googleapis.com/google.protobuf.Empty
)EOF";
envoy::config::overload::v3::OverloadManager overload_manager_config =
TestUtility::parseYaml<envoy::config::overload::v3::OverloadManager>(overload_config);
*overload_manager_config.add_actions() = overload_action;
config_helper_.addConfigModifier(
[overload_manager_config](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
*bootstrap.mutable_overload_manager() = overload_manager_config;
});
initialize();
updateResource(0);
}
void updateResource(double pressure) {
auto* monitor = fake_resource_monitor_factory_.monitor();
ASSERT(monitor != nullptr);
monitor->setResourcePressure(pressure);
}
FakeResourceMonitorFactory fake_resource_monitor_factory_;
Registry::InjectFactory<Server::Configuration::ResourceMonitorFactory> inject_factory_{
fake_resource_monitor_factory_};
};
INSTANTIATE_TEST_SUITE_P(Protocols, OverloadIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
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);
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);
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::CodecClient::Type::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);
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
Http::TestRequestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
auto response = codec_client_->makeRequestWithBody(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);
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);
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);
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::CodecClient::Type::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));
response->waitForEndStream();
EXPECT_EQ(response->headers().getConnectionValue(), "close");
} else {
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.
FakeStreamPtr http_stream;
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_, http_stream));
ASSERT_TRUE(http_stream->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);
response->waitForEndStream();
EXPECT_EQ(response->headers().getStatusValue(), "408");
EXPECT_THAT(response->body(), HasSubstr("stream timeout"));
}
} // namespace Envoy