forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx_limit_integration_test.cc
279 lines (227 loc) · 10.7 KB
/
cx_limit_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
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/network/filter.h"
#include "envoy/registry/registry.h"
#include "source/common/network/utility.h"
#include "test/config/utility.h"
#include "test/integration/integration.h"
#include "test/test_common/logging.h"
#include "test/test_common/simulated_time_system.h"
#include "gtest/gtest.h"
namespace Envoy {
namespace {
class ConnectionLimitIntegrationTest : public testing::TestWithParam<Network::Address::IpVersion>,
public Event::TestUsingSimulatedTime,
public BaseIntegrationTest {
public:
ConnectionLimitIntegrationTest()
: BaseIntegrationTest(GetParam(), ConfigHelper::tcpProxyConfig()) {}
void setEmptyListenerLimit() {
config_helper_.addRuntimeOverride("envoy.resource_limits.listener.listener_0.connection_limit",
"");
}
void setListenerLimit(const uint32_t num_conns) {
config_helper_.addRuntimeOverride("envoy.resource_limits.listener.listener_0.connection_limit",
std::to_string(num_conns));
}
void setGlobalLimit(const uint32_t num_conns) {
config_helper_.addRuntimeOverride("overload.global_downstream_max_connections",
std::to_string(num_conns));
}
void setGlobalLimitOptOut() {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* listener = bootstrap.mutable_static_resources()->mutable_listeners(0);
listener->set_ignore_global_conn_limit(true);
});
}
void setAdminGlobalLimitOptOut() {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
auto* admin = bootstrap.mutable_admin();
admin->set_ignore_global_conn_limit(true);
});
}
void initialize() override { BaseIntegrationTest::initialize(); }
AssertionResult waitForConnections(uint32_t envoy_downstream_connections) {
// The multiplier of 2 is because both Envoy's downstream connections and
// the test server's downstream connections are counted by the global
// counter.
uint32_t expected_connections = envoy_downstream_connections * 2;
for (int i = 0; i < 10; ++i) {
if (Network::AcceptedSocketImpl::acceptedSocketCount() == expected_connections) {
return AssertionSuccess();
}
// TODO(mattklein123): Do not use a real sleep here. Switch to events with waitFor().
timeSystem().realSleepDoNotUseWithoutScrutiny(std::chrono::milliseconds(500));
}
if (Network::AcceptedSocketImpl::acceptedSocketCount() == expected_connections) {
return AssertionSuccess();
}
return AssertionFailure();
}
// Assumes a limit of 2 connections.
void doTest(std::function<void()> init_func, std::string&& check_stat) {
init_func();
std::vector<IntegrationTcpClientPtr> tcp_clients;
std::vector<FakeRawConnectionPtr> raw_conns;
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_FALSE(
fake_upstreams_[0]->waitForRawConnection(raw_conns.back(), std::chrono::milliseconds(500)));
tcp_clients.back()->waitForDisconnect();
// Get rid of the client that failed to connect.
tcp_clients.back()->close();
tcp_clients.pop_back();
// Close the first connection that was successful so that we can open a new successful
// connection.
tcp_clients.front()->close();
ASSERT_TRUE(raw_conns.front()->waitForDisconnect());
// Make sure to not try to connect again until the acceptedSocketCount is updated.
ASSERT_TRUE(waitForConnections(1));
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
const bool isV4 = (version_ == Network::Address::IpVersion::v4);
const std::string counter_prefix = (isV4 ? "listener.127.0.0.1_0." : "listener.[__1]_0.");
test_server_->waitForCounterEq(counter_prefix + check_stat, 1);
for (auto& tcp_client : tcp_clients) {
tcp_client->close();
}
tcp_clients.clear();
raw_conns.clear();
}
};
INSTANTIATE_TEST_SUITE_P(IpVersions, ConnectionLimitIntegrationTest,
testing::ValuesIn(TestEnvironment::getIpVersionsForTest()),
TestUtility::ipTestParamsToString);
TEST_P(ConnectionLimitIntegrationTest, TestListenerLimit) {
std::function<void()> init_func = [this]() {
setListenerLimit(2);
initialize();
};
doTest(init_func, "downstream_cx_overflow");
}
TEST_P(ConnectionLimitIntegrationTest, TestEmptyGlobalCxRuntimeLimit) {
const std::string log_line = "no configured limit to the number of allowed active connections.";
EXPECT_LOG_CONTAINS("warn", log_line, { initialize(); });
}
TEST_P(ConnectionLimitIntegrationTest, TestEmptyListenerRuntimeLimit) {
const std::string log_line =
"Listener connection limit runtime key "
"envoy.resource_limits.listener.listener_0.connection_limit is empty. There are currently "
"no limitations on the number of accepted connections for listener listener_0.";
EXPECT_LOG_CONTAINS("warn", log_line, {
setEmptyListenerLimit();
initialize();
});
}
TEST_P(ConnectionLimitIntegrationTest, TestGlobalLimit) {
std::function<void()> init_func = [this]() {
// Includes twice the number of connections expected because the tracking is performed via a
// static variable and the fake upstream has a listener. This causes upstream connections to the
// fake upstream to also be tracked as part of the global downstream connection tracking.
setGlobalLimit(4);
initialize();
};
doTest(init_func, "downstream_global_cx_overflow");
}
TEST_P(ConnectionLimitIntegrationTest, TestBothLimits) {
std::function<void()> init_func = [this]() {
// Includes twice the number of connections expected because the tracking is performed via a
// static variable and the fake upstream has a listener. This causes upstream connections to the
// fake upstream to also be tracked as part of the global downstream connection tracking.
setGlobalLimit(4);
// Setting the listener limit to a much higher value and making sure the right stat gets
// incremented when both limits are set.
setListenerLimit(100);
initialize();
};
doTest(init_func, "downstream_global_cx_overflow");
}
TEST_P(ConnectionLimitIntegrationTest, TestGlobalLimitOptOut) {
// Includes 4 connections because the tracking is performed regardless of whether a specific
// listener has opted out. Since the fake upstream has a listener, we need to keep value at 4 so
// it can accept connections. (2 downstream listener conns + 2 upstream listener conns)
setGlobalLimit(4);
setGlobalLimitOptOut();
setAdminGlobalLimitOptOut();
setListenerLimit(100);
initialize();
std::vector<IntegrationTcpClientPtr> tcp_clients;
std::vector<FakeRawConnectionPtr> raw_conns;
for (int i = 0; i < 6; ++i) {
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
}
// Get rid of the client that failed to connect.
tcp_clients.back()->close();
tcp_clients.pop_back();
// admin connections should succeed
tcp_clients.emplace_back(makeTcpConnection(lookupPort("admin")));
raw_conns.emplace_back();
ASSERT_TRUE(tcp_clients.back()->connected());
const bool isV4 = (version_ == Network::Address::IpVersion::v4);
const std::string counter_prefix = (isV4 ? "listener.127.0.0.1_0." : "listener.[__1]_0.");
// listener_0 does not hit any connection limits
test_server_->waitForCounterEq(counter_prefix + "downstream_global_cx_overflow", 0);
test_server_->waitForCounterEq(counter_prefix + "downstream_cx_overflow", 0);
test_server_->waitForCounterEq("listener.admin.downstream_global_cx_overflow", 0);
test_server_->waitForCounterEq("listener.admin.downstream_cx_overflow", 0);
for (auto& tcp_client : tcp_clients) {
tcp_client->close();
}
tcp_clients.clear();
raw_conns.clear();
}
TEST_P(ConnectionLimitIntegrationTest, TestListenerLimitWithGlobalOptOut) {
// Includes 4 connections because the tracking is performed regardless of whether a specific
// listener has opted out. Since the fake upstream has a listener, we need to keep value at 4 so
// it can accept connections. (2 downstream listener conns + 2 upstream listener conns)
setGlobalLimit(4);
setGlobalLimitOptOut();
// Only allow 2 connections for the listener even though it has opted out of the global connection
// limit
setListenerLimit(2);
initialize();
std::vector<IntegrationTcpClientPtr> tcp_clients;
std::vector<FakeRawConnectionPtr> raw_conns;
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_TRUE(fake_upstreams_[0]->waitForRawConnection(raw_conns.back()));
ASSERT_TRUE(tcp_clients.back()->connected());
// 3rd connection should fail because we've hit the listener connection limit, not because
// we've hit a global limit
tcp_clients.emplace_back(makeTcpConnection(lookupPort("listener_0")));
raw_conns.emplace_back();
ASSERT_FALSE(
fake_upstreams_[0]->waitForRawConnection(raw_conns.back(), std::chrono::milliseconds(500)));
tcp_clients.back()->waitForDisconnect();
// Get rid of the client that failed to connect.
tcp_clients.back()->close();
tcp_clients.pop_back();
const bool isV4 = (version_ == Network::Address::IpVersion::v4);
const std::string counter_prefix = (isV4 ? "listener.127.0.0.1_0." : "listener.[__1]_0.");
// listener_0 does hits the listener connection limit
test_server_->waitForCounterEq(counter_prefix + "downstream_global_cx_overflow", 0);
test_server_->waitForCounterEq(counter_prefix + "downstream_cx_overflow", 1);
for (auto& tcp_client : tcp_clients) {
tcp_client->close();
}
tcp_clients.clear();
raw_conns.clear();
}
} // namespace
} // namespace Envoy