From fe62ff78da4515d3da662279c6a81feed2aa2362 Mon Sep 17 00:00:00 2001 From: Igor Vagulin Date: Thu, 4 Oct 2018 10:16:27 +0300 Subject: [PATCH 1/2] EJBCLIENT-309 - add expiration for failed connections in discovery provider --- .../remote/RemotingEJBDiscoveryProvider.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java index f7b66f3d8..9dc896375 100644 --- a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java +++ b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java @@ -80,14 +80,13 @@ final class RemotingEJBDiscoveryProvider implements DiscoveryProvider, Discovere private final ConcurrentHashMap nodes = new ConcurrentHashMap<>(); - private final Set failedDestinations = Collections.newSetFromMap(new ConcurrentHashMap()); + private final Map failedDestinations = new ConcurrentHashMap(); private final ConcurrentHashMap> clusterNodes = new ConcurrentHashMap<>(); private final ConcurrentHashMap effectiveAuthURIs = new ConcurrentHashMap<>(); - public RemotingEJBDiscoveryProvider() { Endpoint.getCurrent(); //this will blow up if remoting is not present, preventing this from being registered } @@ -114,6 +113,16 @@ public void removeCluster(final String clusterName) { if (removed != null) removed.clear(); effectiveAuthURIs.remove(clusterName); } + + private boolean haveNotExpiredFailedDestination(URI uri) { + if(!failedDestinations.containsKey(uri)) + return false; + else { + long failureTimestamp = failedDestinations.get(uri); + long delta = System.currentTimeMillis() - failureTimestamp; + return delta < 5000; + } + } public DiscoveryRequest discover(final ServiceType serviceType, final FilterSpec filterSpec, final DiscoveryResult result) { if (! serviceType.implies(ServiceType.of("ejb", "jboss"))) { @@ -142,7 +151,7 @@ public DiscoveryRequest discover(final ServiceType serviceType, final FilterSpec } discoveryConnections = true; final URI uri = connection.getDestination(); - if (failedDestinations.contains(uri)) { + if (haveNotExpiredFailedDestination(uri)) { Logs.INVOCATION.tracef("EJB discovery provider: attempting to connect to configured connection %s, skipping because marked as failed", uri); continue; } @@ -180,7 +189,9 @@ public DiscoveryRequest discover(final ServiceType serviceType, final FilterSpec } } final URI uri = new URI(protocol, null, hostName, destination.getPort(), null, null, null); - if (! failedDestinations.contains(uri)) { + if (haveNotExpiredFailedDestination(uri)) { + Logs.INVOCATION.tracef("EJB discovery provider: attempting to connect to cluster connection %s, skipping because marked as failed", uri); + } else { maxConnections--; Logs.INVOCATION.tracef("EJB discovery provider: attempting to connect to cluster %s connection %s", clusterName, uri); discoveryAttempt.connectAndDiscover(uri, clusterName); @@ -346,7 +357,7 @@ public void handleCancelled(final URI destination) { public void handleFailed(final IOException exception, final URI destination) { DiscoveryAttempt.this.discoveryResult.reportProblem(exception); - failedDestinations.add(destination); + failedDestinations.put(destination, System.currentTimeMillis()); countDown(); } @@ -363,7 +374,7 @@ public void handleCancelled(final URI destination) { public void handleFailed(final IOException exception, final URI destination) { DiscoveryAttempt.this.discoveryResult.reportProblem(exception); - failedDestinations.add(destination); + failedDestinations.put(destination, System.currentTimeMillis()); countDown(); } @@ -465,7 +476,7 @@ void countDown() { phase2 = true; outstandingCount.incrementAndGet(); for (URI uri : everything) { - if(!failedDestinations.contains(uri)) { + if(!failedDestinations.containsKey(uri)) { connectAndDiscover(uri, effectiveAuthMappings.get(uri)); } } From 00092311049d619b41b17316b6c5bd1bd45824be Mon Sep 17 00:00:00 2001 From: Igor Vagulin Date: Tue, 22 Jan 2019 00:15:05 +0300 Subject: [PATCH 2/2] EJBCLIENT-309 - make recheck interval configurable - use nanoTime --- .../remote/RemotingEJBDiscoveryProvider.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java index 9dc896375..a1b5b8565 100644 --- a/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java +++ b/src/main/java/org/jboss/ejb/protocol/remote/RemotingEJBDiscoveryProvider.java @@ -30,6 +30,7 @@ import java.net.InetSocketAddress; import java.net.URI; import java.net.URISyntaxException; +import java.security.AccessController; import java.security.GeneralSecurityException; import java.security.PrivilegedAction; import java.util.ArrayList; @@ -40,6 +41,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import javax.net.ssl.SSLContext; @@ -85,7 +87,16 @@ final class RemotingEJBDiscoveryProvider implements DiscoveryProvider, Discovere private final ConcurrentHashMap> clusterNodes = new ConcurrentHashMap<>(); private final ConcurrentHashMap effectiveAuthURIs = new ConcurrentHashMap<>(); - + + private static final long DESTINATION_RECHECK_INTERVAL = + AccessController.doPrivileged((PrivilegedAction) () -> { + String val = System.getProperty("org.jboss.ejb.client.destination-recheck-interval"); + try { + return TimeUnit.MILLISECONDS.toNanos(Long.valueOf(val)); + } catch (NumberFormatException e) { + return TimeUnit.MILLISECONDS.toNanos(5000L); + } + }); public RemotingEJBDiscoveryProvider() { Endpoint.getCurrent(); //this will blow up if remoting is not present, preventing this from being registered @@ -119,8 +130,8 @@ private boolean haveNotExpiredFailedDestination(URI uri) { return false; else { long failureTimestamp = failedDestinations.get(uri); - long delta = System.currentTimeMillis() - failureTimestamp; - return delta < 5000; + long delta = System.nanoTime() - failureTimestamp; + return delta < DESTINATION_RECHECK_INTERVAL; } } @@ -357,7 +368,7 @@ public void handleCancelled(final URI destination) { public void handleFailed(final IOException exception, final URI destination) { DiscoveryAttempt.this.discoveryResult.reportProblem(exception); - failedDestinations.put(destination, System.currentTimeMillis()); + failedDestinations.put(destination, System.nanoTime()); countDown(); } @@ -374,7 +385,7 @@ public void handleCancelled(final URI destination) { public void handleFailed(final IOException exception, final URI destination) { DiscoveryAttempt.this.discoveryResult.reportProblem(exception); - failedDestinations.put(destination, System.currentTimeMillis()); + failedDestinations.put(destination, System.nanoTime()); countDown(); }