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

[EJBCLIENT-309] Add expiration for failed connections in discovery provider #359

Merged
merged 2 commits into from
Feb 20, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -80,13 +82,21 @@ final class RemotingEJBDiscoveryProvider implements DiscoveryProvider, Discovere

private final ConcurrentHashMap<String, NodeInformation> nodes = new ConcurrentHashMap<>();

private final Set<URI> failedDestinations = Collections.newSetFromMap(new ConcurrentHashMap<URI, Boolean>());
private final Map<URI, Long> failedDestinations = new ConcurrentHashMap<URI, Long>();

private final ConcurrentHashMap<String, Set<String>> clusterNodes = new ConcurrentHashMap<>();

private final ConcurrentHashMap<String, URI> effectiveAuthURIs = new ConcurrentHashMap<>();



private static final long DESTINATION_RECHECK_INTERVAL =
AccessController.doPrivileged((PrivilegedAction<Long>) () -> {
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
Expand Down Expand Up @@ -114,6 +124,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.nanoTime() - failureTimestamp;
return delta < DESTINATION_RECHECK_INTERVAL;
}
}

public DiscoveryRequest discover(final ServiceType serviceType, final FilterSpec filterSpec, final DiscoveryResult result) {
if (! serviceType.implies(ServiceType.of("ejb", "jboss"))) {
Expand Down Expand Up @@ -142,7 +162,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;
}
Expand Down Expand Up @@ -180,7 +200,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);
Expand Down Expand Up @@ -346,7 +368,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.nanoTime());
countDown();
}

Expand All @@ -363,7 +385,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.nanoTime());
countDown();
}

Expand Down Expand Up @@ -465,7 +487,7 @@ void countDown() {
phase2 = true;
outstandingCount.incrementAndGet();
for (URI uri : everything) {
if(!failedDestinations.contains(uri)) {
if(!failedDestinations.containsKey(uri)) {
connectAndDiscover(uri, effectiveAuthMappings.get(uri));
}
}
Expand Down