From 33b8cac24d78795eea875e1adfd05dd9a1a3dcbd Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Sun, 27 May 2018 22:00:27 +0200 Subject: [PATCH 1/2] Adding retry to getHttpClient when "No System TLS" pops up #491 --- .../zalenium/registry/ZaleniumRegistry.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java b/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java index 0d5247d846..91a94394be 100644 --- a/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java +++ b/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java @@ -17,6 +17,7 @@ import org.openqa.grid.web.Hub; import org.openqa.grid.web.servlet.handler.RequestHandler; import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.http.HttpClient; import org.openqa.selenium.remote.server.log.LoggingManager; import de.zalando.ep.zalenium.proxy.AutoStartProxySet; @@ -24,6 +25,7 @@ import de.zalando.ep.zalenium.proxy.DockeredSeleniumStarter; import de.zalando.ep.zalenium.util.ZaleniumConfiguration; +import java.net.URL; import java.time.Clock; import java.util.List; import java.util.Map; @@ -79,7 +81,6 @@ public ZaleniumRegistry(Hub hub) { * * @param hub the {@link Hub} to associate this registry with * @param proxySet the {@link ProxySet} to manage proxies with - * @return the registry */ public ZaleniumRegistry(Hub hub, ProxySet proxySet) { super(hub); @@ -449,6 +450,31 @@ public void uncaughtException(Thread t, Throwable e) { } } + @Override + public HttpClient getHttpClient(URL url) { + // https://github.com/zalando/zalenium/issues/491 + int maxTries = 3; + for (int i = 0; i < maxTries; i++) { + try { + HttpClient client = httpClientFactory.createClient(url); + if (i > 0) { + String message = String.format("Successfully created HttpClient for url %s, after attempt #%s", url, (i+1)); + LOG.warn(message); + } + return client; + } catch (Exception | AssertionError e) { + String message = String.format("Error while getting the HttpClient for url %s, attempt #%s", url, (i+1)); + LOG.debug(message, e); + if (i == 2) { + throw e; + } + } + } + LOG.warn(String.format("Last attempt to get the HttpClient for url %s", url)); + return httpClientFactory.createClient(url); + } + + /** * iterates the queue of incoming new session request and assign them to proxy after they've been * sorted by priority, with priority defined by the prioritizer. From 00cb88dff8d98bfee6b936e370031b8203dce637 Mon Sep 17 00:00:00 2001 From: Diego Molina Date: Wed, 30 May 2018 19:33:49 +0200 Subject: [PATCH 2/2] Polishing retry mechanism #491 --- .../zalenium/registry/ZaleniumRegistry.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java b/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java index 91a94394be..0885f58df0 100644 --- a/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java +++ b/src/main/java/de/zalando/ep/zalenium/registry/ZaleniumRegistry.java @@ -30,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Random; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; @@ -454,27 +455,30 @@ public void uncaughtException(Thread t, Throwable e) { public HttpClient getHttpClient(URL url) { // https://github.com/zalando/zalenium/issues/491 int maxTries = 3; - for (int i = 0; i < maxTries; i++) { + for (int i = 1; i <= maxTries; i++) { try { HttpClient client = httpClientFactory.createClient(url); - if (i > 0) { - String message = String.format("Successfully created HttpClient for url %s, after attempt #%s", url, (i+1)); + if (i > 1) { + String message = String.format("Successfully created HttpClient for url %s, after attempt #%s", url, i); LOG.warn(message); } return client; } catch (Exception | AssertionError e) { - String message = String.format("Error while getting the HttpClient for url %s, attempt #%s", url, (i+1)); + String message = String.format("Error while getting the HttpClient for url %s, attempt #%s", url, i); LOG.debug(message, e); - if (i == 2) { + if (i == maxTries) { throw e; } + try { + Thread.sleep((new Random().nextInt(5) + 1) * 1000); + } catch (InterruptedException exception) { + LOG.error("Something went wrong while delaying the HttpClient creation after a failed atttempt", exception); + } } } - LOG.warn(String.format("Last attempt to get the HttpClient for url %s", url)); - return httpClientFactory.createClient(url); + throw new IllegalStateException(String.format("Something went wrong while creating a HttpClient for url %s", url)); } - /** * iterates the queue of incoming new session request and assign them to proxy after they've been * sorted by priority, with priority defined by the prioritizer.