1313import com .clickhouse .client .api .data_formats .internal .SerializerUtils ;
1414import com .clickhouse .client .api .enums .ProxyType ;
1515import com .clickhouse .client .api .http .ClickHouseHttpProto ;
16+ import org .apache .hc .client5 .http .AuthenticationStrategy ;
1617import org .apache .hc .client5 .http .ConnectTimeoutException ;
18+ import org .apache .hc .client5 .http .classic .ExecChain ;
19+ import org .apache .hc .client5 .http .classic .ExecChainHandler ;
1720import org .apache .hc .client5 .http .classic .methods .HttpPost ;
1821import org .apache .hc .client5 .http .config .ConnectionConfig ;
1922import org .apache .hc .client5 .http .config .RequestConfig ;
23+ import org .apache .hc .client5 .http .impl .ChainElement ;
24+ import org .apache .hc .client5 .http .impl .DefaultAuthenticationStrategy ;
25+ import org .apache .hc .client5 .http .impl .DefaultClientConnectionReuseStrategy ;
26+ import org .apache .hc .client5 .http .impl .DefaultSchemePortResolver ;
2027import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
28+ import org .apache .hc .client5 .http .impl .classic .ConnectExec ;
2129import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
2230import org .apache .hc .client5 .http .impl .io .BasicHttpClientConnectionManager ;
2331import org .apache .hc .client5 .http .impl .io .ManagedHttpClientConnectionFactory ;
2432import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
2533import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
2634import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
35+ import org .apache .hc .client5 .http .io .ManagedHttpClientConnection ;
2736import org .apache .hc .client5 .http .protocol .HttpClientContext ;
2837import org .apache .hc .client5 .http .socket .ConnectionSocketFactory ;
2938import org .apache .hc .client5 .http .socket .LayeredConnectionSocketFactory ;
3039import org .apache .hc .client5 .http .socket .PlainConnectionSocketFactory ;
3140import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactory ;
41+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
3242import org .apache .hc .core5 .http .ClassicHttpResponse ;
3343import org .apache .hc .core5 .http .ConnectionRequestTimeoutException ;
3444import org .apache .hc .core5 .http .ContentType ;
3545import org .apache .hc .core5 .http .Header ;
3646import org .apache .hc .core5 .http .HttpEntity ;
47+ import org .apache .hc .core5 .http .HttpException ;
3748import org .apache .hc .core5 .http .HttpHeaders ;
3849import org .apache .hc .core5 .http .HttpHost ;
3950import org .apache .hc .core5 .http .HttpRequest ;
4556import org .apache .hc .core5 .http .impl .io .DefaultHttpResponseParserFactory ;
4657import org .apache .hc .core5 .http .io .SocketConfig ;
4758import org .apache .hc .core5 .http .io .entity .EntityTemplate ;
59+ import org .apache .hc .core5 .http .protocol .DefaultHttpProcessor ;
4860import org .apache .hc .core5 .http .protocol .HttpContext ;
61+ import org .apache .hc .core5 .http .protocol .RequestTargetHost ;
62+ import org .apache .hc .core5 .http .protocol .RequestUserAgent ;
4963import org .apache .hc .core5 .io .CloseMode ;
5064import org .apache .hc .core5 .io .IOCallback ;
5165import org .apache .hc .core5 .net .URIBuilder ;
@@ -220,12 +234,23 @@ private HttpClientConnectionManager poolConnectionManager(LayeredConnectionSocke
220234 .setBufferSize (networkBufferSize )
221235 .build (),
222236 CharCodingConfig .DEFAULT ,
223- DefaultHttpResponseParserFactory .INSTANCE );
237+ DefaultHttpResponseParserFactory .INSTANCE ) {
238+ @ Override
239+ public ManagedHttpClientConnection createConnection (Socket socket ) throws IOException {
240+ long startT = System .nanoTime ();
241+ try {
242+ return super .createConnection (socket );
243+ } finally {
244+ LOG .info ("connection created in " + (System .nanoTime () - startT ) + "ns" );
245+ }
246+ }
247+ };
224248
225249 connMgrBuilder .setConnectionFactory (connectionFactory );
226250 connMgrBuilder .setSSLSocketFactory (sslConnectionSocketFactory );
227251 connMgrBuilder .setDefaultSocketConfig (socketConfig );
228252 PoolingHttpClientConnectionManager phccm = connMgrBuilder .build ();
253+
229254 if (metricsRegistry != null ) {
230255 try {
231256 String mGroupName = chConfiguration .getOrDefault (ClientConfigProperties .METRICS_GROUP_NAME .getKey (),
@@ -393,6 +418,9 @@ public ClassicHttpResponse executeRequest(ClickHouseNode server, Map<String, Obj
393418 HttpClientContext context = HttpClientContext .create ();
394419
395420 try {
421+ {
422+ // increment request count
423+ }
396424 ClassicHttpResponse httpResponse = httpClient .executeOpen (null , req , context );
397425 boolean serverCompression = MapUtils .getFlag (requestConfig , chConfiguration , ClientConfigProperties .COMPRESS_SERVER_RESPONSE .getKey ());
398426 httpResponse .setEntity (wrapResponseEntity (httpResponse .getEntity (), httpResponse .getCode (), serverCompression , useHttpCompression ));
@@ -418,6 +446,10 @@ public ClassicHttpResponse executeRequest(ClickHouseNode server, Map<String, Obj
418446 LOG .warn ("Failed to connect to '{}': {}" , server .getHost (), e .getMessage ());
419447 throw new ClientException ("Failed to connect" , e );
420448 } catch (ConnectionRequestTimeoutException | ServerException | NoHttpResponseException | ClientException | SocketTimeoutException e ) {
449+ if (e instanceof ConnectionRequestTimeoutException ) {
450+ // add failed request because of connection request timeout
451+ req .getConfig ().getConnectionRequestTimeout (); // record timeout value
452+ }
421453 throw e ;
422454 } catch (Exception e ) {
423455 throw new ClientException ("Failed to execute request" , e );
0 commit comments