sijie commented on a change in pull request #4018: Support multi-host for
pulsar-admin
URL: https://github.com/apache/pulsar/pull/4018#discussion_r277501935
##########
File path:
pulsar-client-admin/src/main/java/org/apache/pulsar/client/admin/internal/http/AsyncHttpConnector.java
##########
@@ -76,63 +86,102 @@ public boolean keepAlive(Request ahcRequest, HttpRequest
request, HttpResponse r
}
});
- if (conf != null && StringUtils.isNotBlank(conf.getServiceUrl())
- && conf.getServiceUrl().startsWith("https://")) {
-
- SslContext sslCtx = null;
-
- // Set client key and certificate if available
- AuthenticationDataProvider authData =
conf.getAuthentication().getAuthData();
- if (authData.hasDataForTls()) {
- sslCtx = SecurityUtility.createNettySslContextForClient(
- conf.isTlsAllowInsecureConnection() ||
!conf.isTlsHostnameVerificationEnable(),
- conf.getTlsTrustCertsFilePath(),
- authData.getTlsCertificates(),
authData.getTlsPrivateKey());
- } else {
- sslCtx = SecurityUtility.createNettySslContextForClient(
- conf.isTlsAllowInsecureConnection() ||
!conf.isTlsHostnameVerificationEnable(),
- conf.getTlsTrustCertsFilePath());
- }
+ serviceNameResolver = new PulsarServiceNameResolver();
+ if (conf != null && StringUtils.isNotBlank(conf.getServiceUrl())) {
+ serviceNameResolver.updateServiceUrl(conf.getServiceUrl());
+ if (conf.getServiceUrl().startsWith("https://")) {
+
+ SslContext sslCtx = null;
+
+ // Set client key and certificate if available
+ AuthenticationDataProvider authData =
conf.getAuthentication().getAuthData();
+ if (authData.hasDataForTls()) {
+ sslCtx =
SecurityUtility.createNettySslContextForClient(conf.isTlsAllowInsecureConnection()
|| !conf.isTlsHostnameVerificationEnable(),
+
conf.getTlsTrustCertsFilePath(), authData.getTlsCertificates(),
authData.getTlsPrivateKey());
+ } else {
+ sslCtx =
SecurityUtility.createNettySslContextForClient(conf.isTlsAllowInsecureConnection()
|| !conf.isTlsHostnameVerificationEnable(),
+
conf.getTlsTrustCertsFilePath());
+ }
- confBuilder.setSslContext(sslCtx);
+ confBuilder.setSslContext(sslCtx);
+ }
}
httpClient = new DefaultAsyncHttpClient(confBuilder.build());
}
- @Override
public ClientResponse apply(ClientRequest jerseyRequest) {
- CompletableFuture<ClientResponse> future = new CompletableFuture<>();
- try {
- Future<?> resultFuture = apply(jerseyRequest, new
AsyncConnectorCallback() {
- @Override
- public void response(ClientResponse response) {
- future.complete(response);
+ List<InetSocketAddress> addresses =
serviceNameResolver.getAddressList();
+ CompletableFuture<ClientResponse> future = null;
+ int lastTry = addresses.size() - 1;
+ for (int i = 0; i < addresses.size() * 3; i++) {
+ InetSocketAddress address = addresses.get(i % 3);
+ URI requestUri = replaceWithNew(address, jerseyRequest.getUri());
+ jerseyRequest.setUri(requestUri);
+ CompletableFuture<ClientResponse> tempFuture = new
CompletableFuture<>();
+ try {
+ resolveRequest(tempFuture, jerseyRequest);
+ } catch (InterruptedException e) {
+ throw new ProcessingException(e.getMessage(), e);
+ } catch (ExecutionException ex) {
+ if (i != lastTry && ex.getCause() instanceof ConnectException)
{
+ log.error("Connection refused.", ex);
+ continue;
}
-
- @Override
- public void failure(Throwable failure) {
- future.completeExceptionally(failure);
+ Throwable e = ex.getCause() == null ? ex : ex.getCause();
+ throw new ProcessingException(e.getMessage(), e);
+ } catch (TimeoutException e) {
+ if (i == lastTry) {
+ throw new ProcessingException("Request timeout.", e);
}
- });
+ log.error("Request timeout. Next trying...", e);
Review comment:
I think you also need to consider checking timeout and adding backoff logic.
1) if the request has been taking more than timeout, we should stop retrying.
2) add a backoff for next retry. so the retry logic won't overwhelm the
whole cluster.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services