forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload_integration_test.cc
164 lines (135 loc) · 6.96 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
#include "test/integration/http_protocol_integration.h"
#include "absl/strings/str_cat.h"
namespace Envoy {
class OverloadIntegrationTest : public HttpProtocolIntegrationTest {
protected:
OverloadIntegrationTest()
: injected_resource_filename_(TestEnvironment::temporaryPath("injected_resource")),
file_updater_(injected_resource_filename_) {}
void initialize() override {
config_helper_.addConfigModifier([this](envoy::config::bootstrap::v2::Bootstrap& bootstrap) {
const std::string overload_config = fmt::format(R"EOF(
refresh_interval:
seconds: 0
nanos: 1000000
resource_monitors:
- name: "envoy.resource_monitors.injected_resource"
config:
filename: "{}"
actions:
- name: "envoy.overload_actions.stop_accepting_requests"
triggers:
- name: "envoy.resource_monitors.injected_resource"
threshold:
value: 0.9
- name: "envoy.overload_actions.disable_http_keepalive"
triggers:
- name: "envoy.resource_monitors.injected_resource"
threshold:
value: 0.8
- name: "envoy.overload_actions.stop_accepting_connections"
triggers:
- name: "envoy.resource_monitors.injected_resource"
threshold:
value: 0.95
)EOF",
injected_resource_filename_);
*bootstrap.mutable_overload_manager() =
TestUtility::parseYaml<envoy::config::overload::v2alpha::OverloadManager>(
overload_config);
});
updateResource(0);
HttpIntegrationTest::initialize();
}
void updateResource(double pressure) { file_updater_.update(absl::StrCat(pressure)); }
const std::string injected_resource_filename_;
AtomicFileUpdater file_updater_;
};
INSTANTIATE_TEST_SUITE_P(Protocols, OverloadIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
TEST_P(OverloadIntegrationTest, CloseStreamsWhenOverloaded) {
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
// 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::TestHeaderMapImpl 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().Status()->value().getStringView());
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().Status()->value().getStringView());
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().Status()->value().getStringView());
EXPECT_EQ(0U, response->body().size());
}
TEST_P(OverloadIntegrationTest, DisableKeepaliveWhenOverloaded) {
if (downstreamProtocol() != Http::CodecClient::Type::HTTP1) {
return; // only relevant for downstream HTTP1.x connections
}
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
// 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::TestHeaderMapImpl request_headers{
{":method", "GET"}, {":path", "/test/long/url"}, {":scheme", "http"}, {":authority", "host"}};
auto response = sendRequestAndWaitForResponse(request_headers, 1, default_response_headers_, 1);
codec_client_->waitForDisconnect();
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().Status()->value().getStringView());
EXPECT_EQ("close", response->headers().Connection()->value().getStringView());
// 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().Status()->value().getStringView());
EXPECT_EQ(nullptr, response->headers().Connection());
}
TEST_P(OverloadIntegrationTest, StopAcceptingConnectionsWhenOverloaded) {
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
// 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::TestHeaderMapImpl 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 but then immediately reject the
// request.
updateResource(0.9);
test_server_->waitForGaugeEq("overload.envoy.overload_actions.stop_accepting_connections.active",
0);
response->waitForEndStream();
EXPECT_TRUE(response->complete());
EXPECT_EQ("503", response->headers().Status()->value().getStringView());
EXPECT_EQ("envoy overloaded", response->body());
codec_client_->close();
}
} // namespace Envoy