|
21 | 21 | import static org.apache.pulsar.common.util.Runnables.catchingAndLoggingThrowables;
|
22 | 22 | import com.fasterxml.jackson.core.JsonProcessingException;
|
23 | 23 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 24 | +import com.google.common.annotations.VisibleForTesting; |
24 | 25 | import com.google.common.base.Strings;
|
| 26 | +import io.netty.handler.codec.http.HttpRequest; |
| 27 | +import io.netty.handler.codec.http.HttpResponse; |
25 | 28 | import io.netty.util.concurrent.DefaultThreadFactory;
|
26 | 29 | import java.io.IOException;
|
| 30 | +import java.net.InetSocketAddress; |
| 31 | +import java.nio.charset.StandardCharsets; |
27 | 32 | import java.util.Map;
|
28 | 33 | import java.util.Objects;
|
| 34 | +import java.util.concurrent.ExecutionException; |
29 | 35 | import java.util.concurrent.Executors;
|
30 | 36 | import java.util.concurrent.ScheduledExecutorService;
|
31 | 37 | import java.util.concurrent.TimeUnit;
|
32 | 38 | import lombok.Data;
|
33 | 39 | import lombok.NonNull;
|
34 | 40 | import lombok.extern.slf4j.Slf4j;
|
35 |
| -import org.apache.http.HttpEntity; |
36 |
| -import org.apache.http.HttpHeaders; |
37 |
| -import org.apache.http.client.ResponseHandler; |
38 |
| -import org.apache.http.client.methods.HttpUriRequest; |
39 |
| -import org.apache.http.client.methods.RequestBuilder; |
40 |
| -import org.apache.http.impl.client.CloseableHttpClient; |
41 |
| -import org.apache.http.impl.client.HttpClients; |
42 |
| -import org.apache.http.util.EntityUtils; |
| 41 | +import org.apache.pulsar.PulsarVersion; |
43 | 42 | import org.apache.pulsar.client.api.Authentication;
|
44 | 43 | import org.apache.pulsar.client.api.AuthenticationFactory;
|
45 | 44 | import org.apache.pulsar.client.api.ControlledClusterFailoverBuilder;
|
46 | 45 | import org.apache.pulsar.client.api.PulsarClient;
|
47 | 46 | import org.apache.pulsar.client.api.ServiceUrlProvider;
|
48 | 47 | import org.apache.pulsar.common.util.ObjectMapperFactory;
|
| 48 | +import org.asynchttpclient.AsyncHttpClient; |
| 49 | +import org.asynchttpclient.AsyncHttpClientConfig; |
| 50 | +import org.asynchttpclient.BoundRequestBuilder; |
| 51 | +import org.asynchttpclient.DefaultAsyncHttpClient; |
| 52 | +import org.asynchttpclient.DefaultAsyncHttpClientConfig; |
| 53 | +import org.asynchttpclient.Request; |
| 54 | +import org.asynchttpclient.Response; |
| 55 | +import org.asynchttpclient.channel.DefaultKeepAliveStrategy; |
49 | 56 | import org.checkerframework.checker.nullness.qual.Nullable;
|
50 | 57 |
|
51 | 58 | @Slf4j
|
52 | 59 | public class ControlledClusterFailover implements ServiceUrlProvider {
|
| 60 | + private static final int DEFAULT_CONNECT_TIMEOUT_IN_SECONDS = 10; |
| 61 | + private static final int DEFAULT_READ_TIMEOUT_IN_SECONDS = 30; |
| 62 | + private static final int DEFAULT_MAX_REDIRECTS = 20; |
| 63 | + |
53 | 64 | private PulsarClientImpl pulsarClient;
|
54 | 65 | private volatile String currentPulsarServiceUrl;
|
55 | 66 | private volatile ControlledConfiguration currentControlledConfiguration;
|
56 | 67 | private final ScheduledExecutorService executor;
|
57 | 68 | private final long interval;
|
58 | 69 | private ObjectMapper objectMapper = null;
|
59 |
| - private final CloseableHttpClient httpClient; |
60 |
| - private final HttpUriRequest request; |
61 |
| - private final ResponseHandler<String> responseHandler; |
| 70 | + private final AsyncHttpClient httpClient; |
| 71 | + private final BoundRequestBuilder requestBuilder; |
62 | 72 |
|
63 | 73 | private ControlledClusterFailover(ControlledClusterFailoverBuilderImpl builder) throws IOException {
|
64 | 74 | this.currentPulsarServiceUrl = builder.defaultServiceUrl;
|
65 | 75 | this.interval = builder.interval;
|
66 | 76 | this.executor = Executors.newSingleThreadScheduledExecutor(
|
67 | 77 | new DefaultThreadFactory("pulsar-service-provider"));
|
68 |
| - this.httpClient = HttpClients.custom().build(); |
69 | 78 |
|
70 |
| - RequestBuilder requestBuilder = RequestBuilder.get() |
71 |
| - .setUri(builder.urlProvider) |
72 |
| - .setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); |
| 79 | + this.httpClient = buildHttpClient(); |
| 80 | + this.requestBuilder = httpClient.prepareGet(builder.urlProvider) |
| 81 | + .addHeader("Accept", "application/json"); |
73 | 82 |
|
74 | 83 | if (builder.header != null && !builder.header.isEmpty()) {
|
75 |
| - builder.header.forEach(requestBuilder::setHeader); |
| 84 | + builder.header.forEach(requestBuilder::addHeader); |
76 | 85 | }
|
77 |
| - this.request = requestBuilder.build(); |
78 |
| - responseHandler = httpResponse -> { |
79 |
| - int status = httpResponse.getStatusLine().getStatusCode(); |
80 |
| - if (status >= 200 && status < 300) { |
81 |
| - HttpEntity entity = httpResponse.getEntity(); |
82 |
| - return entity != null ? EntityUtils.toString(entity) : null; |
83 |
| - } else { |
84 |
| - log.warn("Unexpected response status: {}", status); |
85 |
| - return null; |
86 |
| - } |
87 |
| - }; |
88 | 86 | }
|
89 | 87 |
|
90 |
| - public HttpUriRequest getRequest() { |
91 |
| - return this.request; |
| 88 | + private AsyncHttpClient buildHttpClient() { |
| 89 | + DefaultAsyncHttpClientConfig.Builder confBuilder = new DefaultAsyncHttpClientConfig.Builder(); |
| 90 | + confBuilder.setFollowRedirect(true); |
| 91 | + confBuilder.setMaxRedirects(DEFAULT_MAX_REDIRECTS); |
| 92 | + confBuilder.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_IN_SECONDS * 1000); |
| 93 | + confBuilder.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_SECONDS * 1000); |
| 94 | + confBuilder.setUserAgent(String.format("Pulsar-Java-v%s", PulsarVersion.getVersion())); |
| 95 | + confBuilder.setKeepAliveStrategy(new DefaultKeepAliveStrategy() { |
| 96 | + @Override |
| 97 | + public boolean keepAlive(InetSocketAddress remoteAddress, Request ahcRequest, |
| 98 | + HttpRequest request, HttpResponse response) { |
| 99 | + // Close connection upon a server error or per HTTP spec |
| 100 | + return (response.status().code() / 100 != 5) |
| 101 | + && super.keepAlive(remoteAddress, ahcRequest, request, response); |
| 102 | + } |
| 103 | + }); |
| 104 | + AsyncHttpClientConfig config = confBuilder.build(); |
| 105 | + return new DefaultAsyncHttpClient(config); |
92 | 106 | }
|
93 | 107 |
|
94 | 108 | @Override
|
@@ -139,15 +153,26 @@ public String getCurrentPulsarServiceUrl() {
|
139 | 153 | return this.currentPulsarServiceUrl;
|
140 | 154 | }
|
141 | 155 |
|
| 156 | + @VisibleForTesting |
| 157 | + protected BoundRequestBuilder getRequestBuilder() { |
| 158 | + return this.requestBuilder; |
| 159 | + } |
| 160 | + |
142 | 161 | protected ControlledConfiguration fetchControlledConfiguration() throws IOException {
|
143 | 162 | // call the service to get service URL
|
144 | 163 | try {
|
145 |
| - String jsonStr = httpClient.execute(request, responseHandler); |
146 |
| - return getObjectMapper().readValue(jsonStr, ControlledConfiguration.class); |
147 |
| - } catch (IOException e) { |
148 |
| - log.warn("Failed to fetch controlled configuration. ", e); |
149 |
| - return null; |
| 164 | + Response response = requestBuilder.execute().get(); |
| 165 | + int statusCode = response.getStatusCode(); |
| 166 | + if (statusCode == 200) { |
| 167 | + String content = response.getResponseBody(StandardCharsets.UTF_8); |
| 168 | + return getObjectMapper().readValue(content, ControlledConfiguration.class); |
| 169 | + } |
| 170 | + log.warn("Failed to fetch controlled configuration, status code: {}", statusCode); |
| 171 | + } catch (InterruptedException | ExecutionException e) { |
| 172 | + log.error("Failed to fetch controlled configuration ", e); |
150 | 173 | }
|
| 174 | + |
| 175 | + return null; |
151 | 176 | }
|
152 | 177 |
|
153 | 178 | private ObjectMapper getObjectMapper() {
|
|
0 commit comments