From 951ab11f402ee1d2239cda60df5f9ad07460955a Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 15:48:18 -0700 Subject: [PATCH 1/7] upgrade httpcomponents --- pom.xml | 21 ++-- signalfx-connection/pom.xml | 9 +- .../AbstractHttpReceiverConnection.java | 47 ++++---- .../com/signalfx/connection/RetryHandler.java | 15 ++- .../signalfx/connection/RetryStrategy.java | 25 ----- .../connection/RetryStrategyTest.java | 100 ------------------ signalfx-metrics/pom.xml | 19 +++- signalfx-signalflow/pom.xml | 19 +++- .../signalflow/ServerSentEventsTransport.java | 56 ++++------ .../signalflow/WebSocketTransport.java | 2 +- 10 files changed, 110 insertions(+), 203 deletions(-) delete mode 100644 signalfx-connection/src/main/java/com/signalfx/connection/RetryStrategy.java delete mode 100644 signalfx-connection/src/test/java/com/signalfx/connection/RetryStrategyTest.java diff --git a/pom.xml b/pom.xml index b2f42b1b..4c292d7f 100644 --- a/pom.xml +++ b/pom.xml @@ -165,16 +165,21 @@ commons-codec 1.16.1 + + + + + - org.apache.httpcomponents - httpclient - 4.5.14 - - - org.apache.httpcomponents - httpcore - 4.4.16 + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + + + + + org.apache.commons commons-lang3 diff --git a/signalfx-connection/pom.xml b/signalfx-connection/pom.xml index 11bc4670..5ef597d0 100644 --- a/signalfx-connection/pom.xml +++ b/signalfx-connection/pom.xml @@ -51,9 +51,14 @@ signalfx-endpoint ${project.version} + + + + - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 diff --git a/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java b/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java index 5434ca4b..d76ea8a1 100644 --- a/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java +++ b/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java @@ -2,27 +2,30 @@ import com.signalfx.endpoint.SignalFxReceiverEndpoint; import java.nio.charset.StandardCharsets; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpStatus; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.entity.GzipCompressingEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; + +import org.apache.hc.client5.http.HttpRequestRetryStrategy; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.entity.GzipCompressingEntity; +import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.core5.http.*; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.util.TimeValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import static com.signalfx.connection.RetryDefaults.DEFAULT_MAX_RETRIES; import static com.signalfx.connection.RetryDefaults.DEFAULT_NON_RETRYABLE_EXCEPTIONS; +import static java.util.concurrent.TimeUnit.MILLISECONDS; public abstract class AbstractHttpReceiverConnection { @@ -51,18 +54,18 @@ protected AbstractHttpReceiverConnection(SignalFxReceiverEndpoint endpoint, int protected AbstractHttpReceiverConnection(SignalFxReceiverEndpoint endpoint, int timeoutMs, int maxRetries, HttpClientConnectionManager httpClientConnectionManager, List> nonRetryableExceptions) { + HttpRequestRetryStrategy retryStrategy = new RetryHandler(maxRetries, nonRetryableExceptions); this.client = HttpClientBuilder.create() .setConnectionManager(httpClientConnectionManager) - .setRetryHandler(new RetryHandler(maxRetries, nonRetryableExceptions)) - .setServiceUnavailableRetryStrategy(new RetryStrategy(maxRetries)) + .setRetryStrategy(retryStrategy) .build(); - this.host = new HttpHost(endpoint.getHostname(), endpoint.getPort(), endpoint.getScheme()); + this.host = new HttpHost(endpoint.getScheme(), endpoint.getHostname(), endpoint.getPort()); HttpHost proxy = createHttpProxyFromSystemProperties(endpoint.getHostname()); this.requestConfig = RequestConfig.custom() - .setSocketTimeout(timeoutMs) - .setConnectionRequestTimeout(timeoutMs) - .setConnectTimeout(timeoutMs) + .setResponseTimeout(timeoutMs, MILLISECONDS) + .setConnectionRequestTimeout(timeoutMs, MILLISECONDS) + .setConnectTimeout(timeoutMs, MILLISECONDS) .setProxy(proxy) .build(); } @@ -95,12 +98,12 @@ protected void checkHttpResponse(CloseableHttpResponse resp) { final String body; try { body = EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8); - } catch (IOException e) { + } catch (IOException | ParseException e) { throw new RuntimeException("Unable to get response content", e); } - if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { + if (resp.getCode() != HttpStatus.SC_OK) { throw new RuntimeException("Invalid status code " - + resp.getStatusLine().getStatusCode() + ": " + body); + + resp.getCode() + ": " + body); } if (!"\"OK\"".equals(body)) { throw new RuntimeException("Invalid response body: " + body); @@ -154,7 +157,7 @@ protected HttpHost createHttpProxyFromSystemProperties(String endpointHostname) } // return http proxy host - return new HttpHost(proxyHost.trim(), Integer.parseInt(proxyPort.trim()), "http"); + return new HttpHost("http", proxyHost.trim(), Integer.parseInt(proxyPort.trim())); } // http proxy is not configured diff --git a/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java b/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java index 9d2ed088..5ffff98d 100644 --- a/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java +++ b/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java @@ -1,20 +1,27 @@ package com.signalfx.connection; import java.io.IOException; +import java.util.Arrays; import java.util.List; -import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; +import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.util.TimeValue; import static com.signalfx.connection.RetryDefaults.DEFAULT_MAX_RETRIES; import static com.signalfx.connection.RetryDefaults.DEFAULT_NON_RETRYABLE_EXCEPTIONS; /** - * Compared to the {@link DefaultHttpRequestRetryHandler} we allow retry on {@link + * Compared to the {@link DefaultHttpRequestRetryStrategy} we allow retry on {@link * javax.net.ssl.SSLException}, because it gets thrown when we try to send data points over a * connection that our server has already closed. It is still unknown how exactly our server closes * "stale" connections in such a way that http client is unable to detect this. */ -class RetryHandler extends DefaultHttpRequestRetryHandler { +class RetryHandler extends DefaultHttpRequestRetryStrategy { + + // NOTE: The default is Arrays.asList(429, 503) but we keep our own special list here for historical reasons + private static final List RETRYABLE_CODES = Arrays.asList(HttpStatus.SC_REQUEST_TIMEOUT, HttpStatus.SC_GATEWAY_TIMEOUT, + 598, -1); public RetryHandler(final int maxRetries) { this(maxRetries, DEFAULT_NON_RETRYABLE_EXCEPTIONS); @@ -25,6 +32,6 @@ public RetryHandler() { } public RetryHandler(final int maxRetries, List> clazzes) { - super(maxRetries, true, clazzes); + super(maxRetries, TimeValue.ofSeconds(1), clazzes, RETRYABLE_CODES); } } diff --git a/signalfx-connection/src/main/java/com/signalfx/connection/RetryStrategy.java b/signalfx-connection/src/main/java/com/signalfx/connection/RetryStrategy.java deleted file mode 100644 index 93c20708..00000000 --- a/signalfx-connection/src/main/java/com/signalfx/connection/RetryStrategy.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.signalfx.connection; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.ServiceUnavailableRetryStrategy; -import org.apache.http.protocol.HttpContext; - -public class RetryStrategy implements ServiceUnavailableRetryStrategy { - private final int maxRetries; - - public RetryStrategy(final int maxRetries) { - this.maxRetries = maxRetries; - } - - @Override - public boolean retryRequest(final HttpResponse httpResponse, final int executionCount, final HttpContext httpContext) { - final int statusCode = httpResponse.getStatusLine().getStatusCode(); - return executionCount <= maxRetries && (statusCode == HttpStatus.SC_REQUEST_TIMEOUT || statusCode == HttpStatus.SC_GATEWAY_TIMEOUT || statusCode == 598 || statusCode == -1); - } - - @Override - public long getRetryInterval() { - return 0; - } -} diff --git a/signalfx-connection/src/test/java/com/signalfx/connection/RetryStrategyTest.java b/signalfx-connection/src/test/java/com/signalfx/connection/RetryStrategyTest.java deleted file mode 100644 index 112b30bd..00000000 --- a/signalfx-connection/src/test/java/com/signalfx/connection/RetryStrategyTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.signalfx.connection; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.ProtocolVersion; -import org.apache.http.StatusLine; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.impl.DefaultHttpResponseFactory; -import org.apache.http.protocol.HttpContext; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class RetryStrategyTest { - @Test - public void shouldSetRetryOnRequestTimeout() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(HttpStatus.SC_REQUEST_TIMEOUT); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertTrue(retryStrategy.retryRequest(mockResp, 1, mockHttpContext)); - } - - @Test - public void shouldSetRetryOnGatewayTimeout() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(HttpStatus.SC_GATEWAY_TIMEOUT); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertTrue(retryStrategy.retryRequest(mockResp, 1, mockHttpContext)); - } - - @Test - public void shouldSetRetryOnNegativeStatus() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(-1); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertTrue(retryStrategy.retryRequest(mockResp, 1, mockHttpContext)); - } - - @Test - public void shouldSetRetryOnInvalidStatusCode() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(598); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertTrue(retryStrategy.retryRequest(mockResp, 1, mockHttpContext)); - } - - @Test - public void shouldNotRetryOnOtherStatusCode() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(HttpStatus.SC_BAD_GATEWAY); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertFalse(retryStrategy.retryRequest(mockResp, 1, mockHttpContext)); - } - - @Test - public void shouldNotRetryIfRetriesExceeded() { - final RetryStrategy retryStrategy = new RetryStrategy(3); - - final StatusLine mockStatusLine = generateStatusLineByCode(HttpStatus.SC_GATEWAY_TIMEOUT); - final HttpContext mockHttpContext = new HttpClientContext(); - final HttpResponse mockResp = DefaultHttpResponseFactory.INSTANCE.newHttpResponse(mockStatusLine, mockHttpContext); - - assertFalse(retryStrategy.retryRequest(mockResp, 4, mockHttpContext)); - } - - private StatusLine generateStatusLineByCode(final int statusCode) { - return new StatusLine() { - @Override - public ProtocolVersion getProtocolVersion() { - return null; - } - - @Override - public int getStatusCode() { - return statusCode; - } - - @Override - public String getReasonPhrase() { - return null; - } - }; - } -} \ No newline at end of file diff --git a/signalfx-metrics/pom.xml b/signalfx-metrics/pom.xml index 31a6f5fa..3993efe9 100644 --- a/signalfx-metrics/pom.xml +++ b/signalfx-metrics/pom.xml @@ -85,13 +85,24 @@ com.fasterxml.jackson.core jackson-core + + + + - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + + + + + - org.apache.httpcomponents - httpcore + org.apache.httpcomponents.core5 + httpcore5 + 5.2.4 diff --git a/signalfx-signalflow/pom.xml b/signalfx-signalflow/pom.xml index 4d7a9b86..904ed9a7 100644 --- a/signalfx-signalflow/pom.xml +++ b/signalfx-signalflow/pom.xml @@ -79,13 +79,24 @@ com.google.guava guava + + + + - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 + 5.3.1 + + + + + - org.apache.httpcomponents - httpcore + org.apache.httpcomponents.core5 + httpcore5 + 5.2.4 commons-io diff --git a/signalfx-signalflow/src/main/java/com/signalfx/signalflow/ServerSentEventsTransport.java b/signalfx-signalflow/src/main/java/com/signalfx/signalflow/ServerSentEventsTransport.java index da357d06..2040395b 100644 --- a/signalfx-signalflow/src/main/java/com/signalfx/signalflow/ServerSentEventsTransport.java +++ b/signalfx-signalflow/src/main/java/com/signalfx/signalflow/ServerSentEventsTransport.java @@ -3,35 +3,25 @@ */ package com.signalfx.signalflow; -import java.io.BufferedReader; -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UnsupportedEncodingException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.regex.Pattern; - -import org.apache.http.HttpEntity; -import org.apache.http.NameValuePair; -import org.apache.http.StatusLine; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.conn.BasicHttpClientConnectionManager; -import org.apache.http.message.BasicNameValuePair; +import com.signalfx.connection.AbstractHttpReceiverConnection; +import com.signalfx.endpoint.SignalFxEndpoint; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.http.io.entity.StringEntity; +import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.apache.hc.core5.net.URIBuilder; +import org.apache.hc.core5.util.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.signalfx.connection.AbstractHttpReceiverConnection; -import com.signalfx.endpoint.SignalFxEndpoint; +import java.io.*; +import java.net.URISyntaxException; +import java.util.*; +import java.util.regex.Pattern; /** * Server-Sent Events transport. @@ -284,8 +274,9 @@ public TransportConnection(SignalFxEndpoint endpoint) { public TransportConnection(SignalFxEndpoint endpoint, int timeoutMs, int maxRetries) { super(endpoint, timeoutMs, maxRetries, new BasicHttpClientConnectionManager()); - this.transportRequestConfig = RequestConfig.custom().setSocketTimeout(0) + this.transportRequestConfig = RequestConfig.custom() .setConnectionRequestTimeout(this.requestConfig.getConnectionRequestTimeout()) + .setResponseTimeout(Timeout.DISABLED) .setConnectTimeout(this.requestConfig.getConnectTimeout()) .setProxy(this.requestConfig.getProxy()).build(); @@ -297,7 +288,7 @@ public CloseableHttpResponse post(String token, String path, throws SignalFlowException { HttpPost httpPost = null; try { - List params = new ArrayList(); + List params = new ArrayList<>(); if (parameters != null) { for (Map.Entry entry : parameters.entrySet()) { params.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); @@ -323,8 +314,7 @@ public CloseableHttpResponse post(String token, String path, CloseableHttpResponse response = client.execute(httpPost); - StatusLine statusLine = response.getStatusLine(); - int statuscode = statusLine.getStatusCode(); + int statuscode = response.getCode(); if ((statuscode < 200) || (statuscode >= 300)) { try { @@ -333,9 +323,9 @@ public CloseableHttpResponse post(String token, String path, log.error("failed to close response", ex); } - String errorMessage = statusLine.getStatusCode() + ": failed post [ " + httpPost - + " ] reason: " + statusLine.getReasonPhrase(); - throw new SignalFlowException(statusLine.getStatusCode(), errorMessage); + String errorMessage = response.getCode() + ": failed post [ " + httpPost + + " ] reason: " + response.getReasonPhrase(); + throw new SignalFlowException(response.getCode(), errorMessage); } return response; diff --git a/signalfx-signalflow/src/main/java/com/signalfx/signalflow/WebSocketTransport.java b/signalfx-signalflow/src/main/java/com/signalfx/signalflow/WebSocketTransport.java index c80be652..e924e073 100644 --- a/signalfx-signalflow/src/main/java/com/signalfx/signalflow/WebSocketTransport.java +++ b/signalfx-signalflow/src/main/java/com/signalfx/signalflow/WebSocketTransport.java @@ -25,7 +25,7 @@ import java.util.zip.GZIPInputStream; import org.apache.commons.io.IOUtils; -import org.apache.http.client.utils.URIBuilder; +import org.apache.hc.core5.net.URIBuilder; import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; From 574d051611f93870ac939a849cf112982915adb7 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 15:49:29 -0700 Subject: [PATCH 2/7] cleanup --- pom.xml | 10 ---------- signalfx-connection/pom.xml | 4 ---- signalfx-metrics/pom.xml | 9 --------- signalfx-signalflow/pom.xml | 9 --------- 4 files changed, 32 deletions(-) diff --git a/pom.xml b/pom.xml index 4c292d7f..e9e7845a 100644 --- a/pom.xml +++ b/pom.xml @@ -165,21 +165,11 @@ commons-codec 1.16.1 - - - - - org.apache.httpcomponents.client5 httpclient5 5.3.1 - - - - - org.apache.commons commons-lang3 diff --git a/signalfx-connection/pom.xml b/signalfx-connection/pom.xml index 5ef597d0..db9ae786 100644 --- a/signalfx-connection/pom.xml +++ b/signalfx-connection/pom.xml @@ -51,10 +51,6 @@ signalfx-endpoint ${project.version} - - - - org.apache.httpcomponents.client5 httpclient5 diff --git a/signalfx-metrics/pom.xml b/signalfx-metrics/pom.xml index 3993efe9..db807576 100644 --- a/signalfx-metrics/pom.xml +++ b/signalfx-metrics/pom.xml @@ -85,20 +85,11 @@ com.fasterxml.jackson.core jackson-core - - - - org.apache.httpcomponents.client5 httpclient5 5.3.1 - - - - - org.apache.httpcomponents.core5 httpcore5 diff --git a/signalfx-signalflow/pom.xml b/signalfx-signalflow/pom.xml index 904ed9a7..f2fce18a 100644 --- a/signalfx-signalflow/pom.xml +++ b/signalfx-signalflow/pom.xml @@ -79,20 +79,11 @@ com.google.guava guava - - - - org.apache.httpcomponents.client5 httpclient5 5.3.1 - - - - - org.apache.httpcomponents.core5 httpcore5 From c121af0edf895dfc643504e544af2eff72237b3b Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 16:08:48 -0700 Subject: [PATCH 3/7] fix build --- .../signalfx/metrics/aws/AWSInstanceInfo.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/aws/AWSInstanceInfo.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/aws/AWSInstanceInfo.java index b7f97801..b2f01ced 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/aws/AWSInstanceInfo.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/aws/AWSInstanceInfo.java @@ -8,11 +8,13 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.Map; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.HttpClientBuilder; +import java.util.concurrent.TimeUnit; + +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.core5.http.ClassicHttpResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,13 +37,13 @@ public class AWSInstanceInfo { * @return null if the value was not obtained for any reason */ public static String get(int timeoutInMs) { - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeoutInMs).build(); + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeoutInMs, TimeUnit.MILLISECONDS).build(); HttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig) .build(); HttpGet request = new HttpGet(URL); try { - HttpResponse response = client.execute(request); + ClassicHttpResponse response = client.executeOpen(null, request, null); try (InputStream inputStream = response.getEntity().getContent()) { return parse(inputStream); } From bd70d4810fba41e754de3de1998b377bb86bb227 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 16:24:59 -0700 Subject: [PATCH 4/7] build fixes --- ...tpDataPointProtobufReceiverConnection.java | 23 +++++++++--------- ...ctHttpEventProtobufReceiverConnection.java | 9 ++++--- .../HttpClientConnectionManagerFactory.java | 24 ++++++++++--------- ...DataPointProtobufReceiverConnectionV2.java | 4 +--- .../HttpDataPointProtobufReceiverFactory.java | 3 +-- ...HttpEventProtobufReceiverConnectionV2.java | 6 ++--- .../HttpEventProtobufReceiverFactory.java | 2 +- ...taPointProtobufReceiverConnectionTest.java | 11 +++++---- 8 files changed, 41 insertions(+), 41 deletions(-) diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpDataPointProtobufReceiverConnection.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpDataPointProtobufReceiverConnection.java index 34833105..6ceccf0b 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpDataPointProtobufReceiverConnection.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpDataPointProtobufReceiverConnection.java @@ -5,16 +5,17 @@ import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.SignalFxMetricsException; import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.InputStreamEntity; +import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.apache.hc.core5.net.URLEncodedUtils; + import java.util.ArrayList; -import org.apache.http.HttpEntity; -import org.apache.http.NameValuePair; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.InputStreamEntity; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.util.EntityUtils; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -66,7 +67,7 @@ public void addDataPoints(String auth, List d getEndpointForAddDatapoints(), compress); - int code = resp.getStatusLine().getStatusCode(); + int code = resp.getCode(); // SignalFx may respond with various 2xx return codes for success. if (code < 200 || code > 299) { throw new SignalFxMetricsException("Invalid status code " + code); @@ -119,7 +120,7 @@ public void backfillDataPoints(String auth, String metric, String metricType, St "/v1/backfill?" + URLEncodedUtils.format(params, StandardCharsets.UTF_8), false); - int code = resp.getStatusLine().getStatusCode(); + int code = resp.getCode(); // SignalFx may respond with various 2xx return codes for success. if (code < 200 || code > 299) { throw new SignalFxMetricsException("Invalid status code " + code); diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpEventProtobufReceiverConnection.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpEventProtobufReceiverConnection.java index de8e1923..12f54dca 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpEventProtobufReceiverConnection.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/AbstractHttpEventProtobufReceiverConnection.java @@ -3,15 +3,14 @@ import java.io.IOException; import java.util.List; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ContentType; - import com.signalfx.connection.AbstractHttpReceiverConnection; import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.SignalFxMetricsException; import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; public abstract class AbstractHttpEventProtobufReceiverConnection extends AbstractHttpReceiverConnection implements EventReceiver { diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpClientConnectionManagerFactory.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpClientConnectionManagerFactory.java index f9ca5e05..f9251aa7 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpClientConnectionManagerFactory.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpClientConnectionManagerFactory.java @@ -1,18 +1,20 @@ package com.signalfx.metrics.connection; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.client5.http.socket.ConnectionSocketFactory; +import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory; +import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier; +import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; +import org.apache.hc.core5.http.config.RegistryBuilder; +import org.apache.hc.core5.http.io.SocketConfig; +import org.apache.hc.core5.ssl.SSLContexts; + import java.io.IOException; +import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLSocket; -import org.apache.http.config.RegistryBuilder; -import org.apache.http.config.SocketConfig; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.conn.socket.PlainConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.ssl.SSLContexts; - public class HttpClientConnectionManagerFactory { private HttpClientConnectionManagerFactory() { @@ -27,7 +29,7 @@ public static HttpClientConnectionManager withTimeoutMs(int timeoutMs) { .build()); httpClientConnectionManager.setDefaultSocketConfig( - SocketConfig.custom().setSoTimeout(timeoutMs).build()); + SocketConfig.custom().setSoTimeout(timeoutMs, TimeUnit.MILLISECONDS).build()); return httpClientConnectionManager; } @@ -39,7 +41,7 @@ private static class SSLConnectionSocketFactoryWithTimeout extends SSLConnection private final int timeoutMs; public SSLConnectionSocketFactoryWithTimeout(int timeoutMs) { - super(SSLContexts.createDefault(), SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER); + super(SSLContexts.createDefault(), new DefaultHostnameVerifier()); this.timeoutMs = timeoutMs; } diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java index 01d2ab0d..f8821088 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java @@ -5,13 +5,11 @@ import java.util.List; import java.util.Map; -import org.apache.http.HttpEntity; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ByteArrayEntity; import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.SignalFxMetricsException; import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; public class HttpDataPointProtobufReceiverConnectionV2 extends AbstractHttpDataPointProtobufReceiverConnection { diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverFactory.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverFactory.java index db80c863..0b899dbb 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverFactory.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverFactory.java @@ -1,7 +1,5 @@ package com.signalfx.metrics.connection; -import org.apache.http.conn.HttpClientConnectionManager; - import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.SignalFxMetricsException; @@ -9,6 +7,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import static com.signalfx.connection.RetryDefaults.DEFAULT_MAX_RETRIES; import static com.signalfx.connection.RetryDefaults.DEFAULT_NON_RETRYABLE_EXCEPTIONS; diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverConnectionV2.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverConnectionV2.java index 0a7ddf90..07217917 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverConnectionV2.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverConnectionV2.java @@ -2,12 +2,12 @@ import java.util.List; -import org.apache.http.HttpEntity; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ByteArrayEntity; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; public class HttpEventProtobufReceiverConnectionV2 extends AbstractHttpEventProtobufReceiverConnection { diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverFactory.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverFactory.java index 2ee986fb..2f9510d3 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverFactory.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpEventProtobufReceiverFactory.java @@ -1,6 +1,6 @@ package com.signalfx.metrics.connection; -import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import com.signalfx.endpoint.SignalFxReceiverEndpoint; import com.signalfx.metrics.SignalFxMetricsException; diff --git a/signalfx-metrics/src/test/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionTest.java b/signalfx-metrics/src/test/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionTest.java index 2c3bda8f..5f1069de 100644 --- a/signalfx-metrics/src/test/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionTest.java +++ b/signalfx-metrics/src/test/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionTest.java @@ -18,10 +18,11 @@ import java.util.zip.GZIPInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.http.HttpStatus; -import org.apache.http.NameValuePair; -import org.apache.http.client.utils.URLEncodedUtils; -import org.apache.http.message.BasicNameValuePair; + +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.http.NameValuePair; +import org.apache.hc.core5.http.message.BasicNameValuePair; +import org.apache.hc.core5.net.URLEncodedUtils; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; @@ -173,7 +174,7 @@ public void testBackfill() throws Exception { new SignalFxEndpoint(uri.getScheme(), uri.getHost(), uri.getPort())) .createDataPointReceiver(); - ArrayList values = new ArrayList(Arrays.asList( + List values = new ArrayList(Arrays.asList( SignalFxProtocolBuffers.PointValue.newBuilder().setTimestamp(System.currentTimeMillis()) .setValue(SignalFxProtocolBuffers.Datum.newBuilder().setDoubleValue(123.0)).build() )); From f5ad4284332f511bc82ae6ccc3e6f352db15f981 Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 16:26:45 -0700 Subject: [PATCH 5/7] build fixes --- .../connection/HttpDataPointProtobufReceiverConnectionV2.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java index f8821088..f279efc4 100644 --- a/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java +++ b/signalfx-metrics/src/main/java/com/signalfx/metrics/connection/HttpDataPointProtobufReceiverConnectionV2.java @@ -10,6 +10,8 @@ import com.signalfx.metrics.SignalFxMetricsException; import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers; import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; public class HttpDataPointProtobufReceiverConnectionV2 extends AbstractHttpDataPointProtobufReceiverConnection { From 0f720c95c732fcd0bffc0da6d43ebcfd02aea51e Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 17:11:52 -0700 Subject: [PATCH 6/7] fix protoc on mac m1 --- signalfx-protoc/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/signalfx-protoc/pom.xml b/signalfx-protoc/pom.xml index c37fe871..489fa216 100644 --- a/signalfx-protoc/pom.xml +++ b/signalfx-protoc/pom.xml @@ -72,6 +72,7 @@ ${protobuf.version} + com.google.protobuf:protoc:${protobuf.version} java From fe6c928d976012887e7c8710aa515df9b31f782c Mon Sep 17 00:00:00 2001 From: Jason Plumb Date: Thu, 18 Apr 2024 17:12:45 -0700 Subject: [PATCH 7/7] fix test --- .../connection/AbstractHttpReceiverConnection.java | 5 ++--- .../main/java/com/signalfx/connection/RetryHandler.java | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java b/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java index d76ea8a1..1e3adca3 100644 --- a/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java +++ b/signalfx-connection/src/main/java/com/signalfx/connection/AbstractHttpReceiverConnection.java @@ -7,20 +7,17 @@ import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.entity.GzipCompressingEntity; -import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.core5.http.*; import org.apache.hc.core5.http.io.entity.EntityUtils; -import org.apache.hc.core5.util.TimeValue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import static com.signalfx.connection.RetryDefaults.DEFAULT_MAX_RETRIES; @@ -163,4 +160,6 @@ protected HttpHost createHttpProxyFromSystemProperties(String endpointHostname) // http proxy is not configured return null; } + + } diff --git a/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java b/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java index 5ffff98d..e2c719ce 100644 --- a/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java +++ b/signalfx-connection/src/main/java/com/signalfx/connection/RetryHandler.java @@ -5,6 +5,7 @@ import java.util.List; import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy; +import org.apache.hc.core5.http.HttpRequest; import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.util.TimeValue; @@ -34,4 +35,11 @@ public RetryHandler() { public RetryHandler(final int maxRetries, List> clazzes) { super(maxRetries, TimeValue.ofSeconds(1), clazzes, RETRYABLE_CODES); } + + @Override + protected boolean handleAsIdempotent(HttpRequest request) { + // Previous implementation did not consider idempotency, so we have to assume we want to + // retry, regardless of the request method. + return true; // OOF yes + } }