forked from reactor/reactor-netty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PooledConnectionProviderDefaultMetricsTest.java
309 lines (278 loc) · 12.3 KB
/
PooledConnectionProviderDefaultMetricsTest.java
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
/*
* Copyright (c) 2019-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.resources;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.BaseHttpTest;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.netty.http.server.HttpServer;
import java.security.cert.CertificateException;
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import static org.assertj.core.api.Assertions.assertThat;
import static reactor.netty.Metrics.ACTIVE_CONNECTIONS;
import static reactor.netty.Metrics.ACTIVE_STREAMS;
import static reactor.netty.Metrics.MAX_CONNECTIONS;
import static reactor.netty.Metrics.CONNECTION_PROVIDER_PREFIX;
import static reactor.netty.Metrics.IDLE_CONNECTIONS;
import static reactor.netty.Metrics.PENDING_CONNECTIONS;
import static reactor.netty.Metrics.MAX_PENDING_CONNECTIONS;
import static reactor.netty.Metrics.NAME;
import static reactor.netty.Metrics.PENDING_STREAMS;
import static reactor.netty.Metrics.TOTAL_CONNECTIONS;
import static reactor.netty.http.client.HttpClientState.STREAM_CONFIGURED;
/**
* @author Violeta Georgieva
*/
class PooledConnectionProviderDefaultMetricsTest extends BaseHttpTest {
static SelfSignedCertificate ssc;
private MeterRegistry registry;
@BeforeAll
static void createSelfSignedCertificate() throws CertificateException {
ssc = new SelfSignedCertificate();
}
@BeforeEach
void setUp() {
registry = new SimpleMeterRegistry();
Metrics.addRegistry(registry);
}
@AfterEach
void tearDown() {
Metrics.removeRegistry(registry);
registry.clear();
registry.close();
}
@Test
void testConnectionProviderMetricsDisabledAndHttpClientMetricsEnabledHttp1() throws Exception {
// by default, when the max number of pending acquire is not specified, it will bet set to 2 * max-connection
// (see PoolFactory from PoolConnectionProvider.java)
ConnectionProvider provider = ConnectionProvider.create("test1", 1);
try {
doTest(createServer(), createClient(provider, () -> disposableServer.address()), "test1", true, 1, 2);
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}
@Test
void testConnectionProviderMetricsDisabledAndHttpClientMetricsEnabledHttp2() throws Exception {
// by default, when the max number of pending acquire is not specified, it will bet set to 2 * max-connection
// (see PoolFactory from PoolConnectionProvider.java)
Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
Http2SslContextSpec clientCtx =
Http2SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
ConnectionProvider provider = ConnectionProvider.create("test2", 1);
try {
doTest(createServer().protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(serverCtx)),
createClient(provider, () -> disposableServer.address()).protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(clientCtx)),
"test2", true, 1, 2);
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}
@Test
void testConnectionProviderMetricsEnableAndHttpClientMetricsDisabledHttp1() throws Exception {
ConnectionProvider provider =
ConnectionProvider.builder("test3")
.maxConnections(1)
.pendingAcquireMaxCount(10)
.metrics(true)
.lifo()
.build();
try {
doTest(createServer(), createClient(provider, () -> disposableServer.address()), "test3", false, 1, 10);
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}
@Test
void testConnectionProviderMetricsEnableAndHttpClientMetricsDisabledHttp2() throws Exception {
Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
Http2SslContextSpec clientCtx =
Http2SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
ConnectionProvider provider =
ConnectionProvider.builder("test4")
.maxConnections(1)
.pendingAcquireMaxCount(10)
.metrics(true)
.lifo()
.build();
try {
doTest(createServer().protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(serverCtx)),
createClient(provider, () -> disposableServer.address()).protocol(HttpProtocol.H2).secure(spec -> spec.sslContext(clientCtx)),
"test4", false, 1, 10);
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}
private void doTest(HttpServer server, HttpClient client, String poolName, boolean clientMetricsEnabled,
int expectedMaxConnection, int expectedMaxPendingAcquire) throws Exception {
disposableServer =
server.handle((req, res) -> res.header("Connection", "close")
.sendString(Mono.just("test")))
.bindNow();
AtomicBoolean metrics = new AtomicBoolean(false);
CountDownLatch latch = new CountDownLatch(1);
boolean isSecured = client.configuration().sslProvider() != null;
client.doOnResponse((res, conn) -> {
conn.channel()
.closeFuture()
.addListener(f -> latch.countDown());
double totalConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName);
double activeConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName);
double maxConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName);
double idleConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName);
double pendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName);
double maxPendingConnections = getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName);
if (totalConnections == 1 && activeConnections == 1 && idleConnections == 0 && pendingConnections == 0 &&
maxConnections == expectedMaxConnection && maxPendingConnections == expectedMaxPendingAcquire) {
metrics.set(true);
}
if (isSecured) {
double activeStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName);
double pendingStreams = getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName);
if (activeStreams == 1 && pendingStreams == 0) {
metrics.set(true);
}
}
})
.metrics(clientMetricsEnabled, Function.identity())
.get()
.uri("/")
.responseContent()
.aggregate()
.asString()
.block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
assertThat(metrics.get()).isTrue();
if (isSecured) {
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(1);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName)).isEqualTo(1);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, "http2." + poolName)).isEqualTo(0);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, "http2." + poolName)).isEqualTo(1);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_STREAMS, "http2." + poolName)).isEqualTo(0);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2." + poolName)).isEqualTo(0);
}
else {
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + TOTAL_CONNECTIONS, poolName)).isEqualTo(0);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + ACTIVE_CONNECTIONS, poolName)).isEqualTo(0);
}
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + IDLE_CONNECTIONS, poolName)).isEqualTo(0);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_CONNECTIONS, poolName)).isEqualTo(0);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_CONNECTIONS, poolName)).isEqualTo(expectedMaxConnection);
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + MAX_PENDING_CONNECTIONS, poolName)).isEqualTo(expectedMaxPendingAcquire);
}
@Test
void testConnectionPoolPendingAcquireSize() throws Exception {
Http2SslContextSpec serverCtx = Http2SslContextSpec.forServer(ssc.certificate(), ssc.privateKey());
Http2SslContextSpec clientCtx =
Http2SslContextSpec.forClient()
.configure(builder -> builder.trustManager(InsecureTrustManagerFactory.INSTANCE));
disposableServer =
HttpServer.create()
.port(0)
.protocol(HttpProtocol.H2)
.secure(spec -> spec.sslContext(serverCtx))
.handle((req, res) ->
res.sendString(Flux.range(0, 10)
.map(i -> "test")
.delayElements(Duration.ofMillis(4))))
.bindNow();
ConnectionProvider provider = ConnectionProvider
.builder("testConnectionPoolPendingAcquireSize")
.pendingAcquireMaxCount(1000)
.maxConnections(500)
.metrics(true)
.build();
try {
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger counter = new AtomicInteger();
HttpClient client =
HttpClient.create(provider)
.port(disposableServer.port())
.protocol(HttpProtocol.H2)
.secure(spec -> spec.sslContext(clientCtx))
.observe((conn, state) -> {
if (state == STREAM_CONFIGURED) {
counter.incrementAndGet();
conn.onTerminate()
.subscribe(null,
t -> conn.channel().eventLoop().execute(() -> {
if (counter.decrementAndGet() == 0) {
latch.countDown();
}
}),
() -> conn.channel().eventLoop().execute(() -> {
if (counter.decrementAndGet() == 0) {
latch.countDown();
}
}));
}
});
Flux.range(0, 1000)
.flatMap(i ->
client.get()
.uri("/")
.responseContent()
.aggregate()
.asString()
.timeout(Duration.ofMillis(ThreadLocalRandom.current().nextInt(1, 35)), Mono.just("timeout")))
.blockLast(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2.testConnectionPoolPendingAcquireSize")).isEqualTo(0);
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(30));
}
// deRegistered
assertThat(getGaugeValue(CONNECTION_PROVIDER_PREFIX + PENDING_STREAMS, "http2.testConnectionPoolPendingAcquireSize")).isEqualTo(-1);
}
private double getGaugeValue(String gaugeName, String poolName) {
Gauge gauge = registry.find(gaugeName).tag(NAME, poolName).gauge();
double result = -1;
if (gauge != null) {
result = gauge.value();
}
return result;
}
}