Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade httpcomponents #449

Closed
wants to merge 7 commits into from
Closed
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
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,9 @@
<version>1.16.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.16</version>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
5 changes: 3 additions & 2 deletions signalfx-connection/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<!-- test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

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.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.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -23,6 +22,7 @@

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 {

Expand Down Expand Up @@ -51,18 +51,18 @@ protected AbstractHttpReceiverConnection(SignalFxReceiverEndpoint endpoint, int

protected AbstractHttpReceiverConnection(SignalFxReceiverEndpoint endpoint, int timeoutMs, int maxRetries,
HttpClientConnectionManager httpClientConnectionManager, List<Class<? extends IOException>> 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();
}
Expand Down Expand Up @@ -95,12 +95,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);
Expand Down Expand Up @@ -154,10 +154,12 @@ 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
return null;
}


}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
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.HttpRequest;
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<Integer> 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);
Expand All @@ -25,6 +33,13 @@ public RetryHandler() {
}

public RetryHandler(final int maxRetries, List<Class<? extends IOException>> clazzes) {
super(maxRetries, true, 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
}
}

This file was deleted.

This file was deleted.

10 changes: 6 additions & 4 deletions signalfx-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>5.2.4</version>
</dependency>

<!-- test -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void addDataPoints(String auth, List<SignalFxProtocolBuffers.DataPoint> 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);
Expand Down Expand Up @@ -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);
Expand Down
Loading