Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ private HttpClientConnectionManager poolConnectionManager(LayeredConnectionSocke
PoolingHttpClientConnectionManager phccm = connMgrBuilder.build();
poolControl = phccm;
if (metricsRegistry != null) {
connectionFactory.enableMetrics();
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
try {
String mGroupName = ClientConfigProperties.METRICS_GROUP_NAME.getOrDefault(configuration);
Class<?> micrometerLoader = getClass().getClassLoader().loadClass("com.clickhouse.client.api.metrics.MicrometerLoader");
Expand All @@ -307,6 +308,8 @@ private HttpClientConnectionManager poolConnectionManager(LayeredConnectionSocke
applyMethod.invoke(micrometerLoader, metricsRegistry, mGroupName, connectionFactory);
} catch (Exception e) {
LOG.error("Failed to register metrics", e);
// No gauge will drain the recorded times, so stop recording them.
connectionFactory.disableMetrics();
}
}
return phccm;
Expand Down Expand Up @@ -1380,9 +1383,24 @@ public MeteredManagedHttpClientConnectionFactory(Http1Config http1Config, CharCo

ConcurrentLinkedQueue<Long> times = new ConcurrentLinkedQueue<>();

// Connection times are drained only by the metrics gauge (see getTime()), so they are recorded
// only when a metrics registry is attached. Otherwise the queue would grow with every new connection.
private volatile boolean metricsEnabled = false;

public void enableMetrics() {
this.metricsEnabled = true;
}

public void disableMetrics() {
this.metricsEnabled = false;
this.times.clear();
}

@Override
public ManagedHttpClientConnection createConnection(Socket socket) throws IOException {
if (!metricsEnabled) {
return super.createConnection(socket);
}
long startT = System.currentTimeMillis();
try {
return super.createConnection(socket);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.clickhouse.client.api.internal;

import org.apache.hc.core5.http.config.CharCodingConfig;
import org.apache.hc.core5.http.config.Http1Config;
import org.apache.hc.core5.http.impl.io.DefaultHttpResponseParserFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.net.Socket;

public class MeteredConnectionFactoryTest {

private static HttpAPIClientHelper.MeteredManagedHttpClientConnectionFactory newFactory() {
return new HttpAPIClientHelper.MeteredManagedHttpClientConnectionFactory(
Http1Config.DEFAULT, CharCodingConfig.DEFAULT, DefaultHttpResponseParserFactory.INSTANCE);
}

@Test(groups = {"unit"})
public void testConnectionTimesNotRecordedWithoutMetrics() throws Exception {
HttpAPIClientHelper.MeteredManagedHttpClientConnectionFactory factory = newFactory();
for (int i = 0; i < 10; i++) {
factory.createConnection(new Socket());
}
Assert.assertEquals(factory.times.size(), 0, "Connection times must not accumulate when metrics are disabled");
}

@Test(groups = {"unit"})
public void testConnectionTimesRecordedAndDrainedWithMetrics() throws Exception {
HttpAPIClientHelper.MeteredManagedHttpClientConnectionFactory factory = newFactory();
factory.enableMetrics();
for (int i = 0; i < 10; i++) {
factory.createConnection(new Socket());
}
Assert.assertEquals(factory.times.size(), 10);
factory.getTime();
Assert.assertEquals(factory.times.size(), 0);
}

@Test(groups = {"unit"})
public void testConnectionTimesNotRecordedAfterMetricsDisabled() throws Exception {
HttpAPIClientHelper.MeteredManagedHttpClientConnectionFactory factory = newFactory();
factory.enableMetrics();
factory.createConnection(new Socket());
factory.disableMetrics();
Assert.assertEquals(factory.times.size(), 0);
factory.createConnection(new Socket());
Assert.assertEquals(factory.times.size(), 0, "Connection times must not accumulate once metrics are disabled");
}
}
4 changes: 4 additions & 0 deletions history/latest/3253.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- **[client-v2]** Fixed a memory leak where every new pooled HTTP connection added one entry to an internal queue of
connection times that was only drained by the metrics gauge. Without a metrics registry the queue was never emptied
and grew for the lifetime of the client. Connection times are now recorded only when metrics are enabled. `jdbc-v2`
is affected as well because it uses `client-v2`. (https://github.com/ClickHouse/clickhouse-java/issues/3253)
Loading