Skip to content

Commit

Permalink
IGNITE-4545 Added cache for router hostnames. - Fixes #1428.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Novikov <anovikov@gridgain.com>

(cherry picked from commit 27ba69b)
  • Loading branch information
nva committed Jan 17, 2017
1 parent 393eb94 commit b0ac987
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Expand Up @@ -85,6 +85,9 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo
/** Class logger. */ /** Class logger. */
private final Logger log; private final Logger log;


/** All local enabled MACs. */
private final Collection<String> macs;

/** NIO server. */ /** NIO server. */
private GridNioServer srv; private GridNioServer srv;


Expand Down Expand Up @@ -166,6 +169,8 @@ protected GridClientConnectionManagerAdapter(UUID clientId,
if (marshId == null && cfg.getMarshaller() == null) if (marshId == null && cfg.getMarshaller() == null)
throw new GridClientException("Failed to start client (marshaller is not configured)."); throw new GridClientException("Failed to start client (marshaller is not configured).");


macs = U.allLocalMACs();

if (cfg.getProtocol() == GridClientProtocol.TCP) { if (cfg.getProtocol() == GridClientProtocol.TCP) {
try { try {
IgniteLogger gridLog = new JavaLogger(false); IgniteLogger gridLog = new JavaLogger(false);
Expand Down Expand Up @@ -316,7 +321,7 @@ protected GridClientConnectionManagerAdapter(UUID clientId,
} }


boolean sameHost = node.attributes().isEmpty() || boolean sameHost = node.attributes().isEmpty() ||
F.containsAny(U.allLocalMACs(), node.attribute(ATTR_MACS).toString().split(", ")); F.containsAny(macs, node.attribute(ATTR_MACS).toString().split(", "));


Collection<InetSocketAddress> srvs = new LinkedHashSet<>(); Collection<InetSocketAddress> srvs = new LinkedHashSet<>();


Expand Down
Expand Up @@ -21,7 +21,6 @@
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -61,12 +60,18 @@ public class GridClientTopology {
/** Cached last error prevented topology from update. */ /** Cached last error prevented topology from update. */
private GridClientException lastError; private GridClientException lastError;


/** Router addresses from configuration. */
private final String routers;

/** /**
* Set of router addresses to infer direct connectivity * Set of router addresses to infer direct connectivity
* when client is working in router connection mode. * when client is working in router connection mode.
* {@code null} when client is working in direct connection node. * {@code null} when client is working in direct connection node.
*/ */
private final Set<String> routerAddrs; private final Set<InetSocketAddress> routerAddrs;

/** List of all known local MACs */
private final Collection<String> macsCache;


/** Protocol. */ /** Protocol. */
private final GridClientProtocol prot; private final GridClientProtocol prot;
Expand Down Expand Up @@ -96,8 +101,38 @@ public GridClientTopology(GridClientConfiguration cfg) {
metricsCache = cfg.isEnableMetricsCache(); metricsCache = cfg.isEnableMetricsCache();
attrCache = cfg.isEnableAttributesCache(); attrCache = cfg.isEnableAttributesCache();
prot = cfg.getProtocol(); prot = cfg.getProtocol();
routerAddrs = (!cfg.getRouters().isEmpty() && cfg.getServers().isEmpty()) ?
new HashSet<>(cfg.getRouters()) : null; if (!cfg.getRouters().isEmpty() && cfg.getServers().isEmpty()) {
routers = cfg.getRouters().toString();

routerAddrs = U.newHashSet(cfg.getRouters().size());

for (String router : cfg.getRouters()) {
int portIdx = router.lastIndexOf(":");

if (portIdx > 0) {
String hostName = router.substring(0, portIdx);

try {
int port = Integer.parseInt(router.substring(portIdx + 1));

InetSocketAddress inetSockAddr = new InetSocketAddress(hostName, port);

routerAddrs.add(inetSockAddr);
}
catch (Exception ignore) {
// No-op.
}
}
}
}
else {
routers = null;

routerAddrs = Collections.emptySet();
}

macsCache = U.allLocalMACs();
} }


/** /**
Expand Down Expand Up @@ -279,7 +314,7 @@ public GridClientNode node(UUID id) throws GridClientException {
try { try {
if (lastError != null) if (lastError != null)
throw new GridClientDisconnectedException( throw new GridClientDisconnectedException(
"Topology is failed [protocol=" + prot + ", routers=" + routerAddrs + ']', lastError); "Topology is failed [protocol=" + prot + ", routers=" + routers + ']', lastError);
else else
return nodes.get(id); return nodes.get(id);
} }
Expand Down Expand Up @@ -376,19 +411,17 @@ private GridClientNodeImpl prepareNode(final GridClientNodeImpl node) {
(metricsCache && attrCache) || (node.attributes().isEmpty() && node.metrics() == null); (metricsCache && attrCache) || (node.attributes().isEmpty() && node.metrics() == null);


// Try to bypass object copying. // Try to bypass object copying.
if (noAttrsAndMetrics && routerAddrs == null && node.connectable()) if (noAttrsAndMetrics && routerAddrs.isEmpty() && node.connectable())
return node; return node;


// Return a new node instance based on the original one. // Return a new node instance based on the original one.
GridClientNodeImpl.Builder nodeBuilder = GridClientNodeImpl.builder(node, !attrCache, !metricsCache); GridClientNodeImpl.Builder nodeBuilder = GridClientNodeImpl.builder(node, !attrCache, !metricsCache);


for (InetSocketAddress addr : node.availableAddresses(prot, true)) { for (InetSocketAddress addr : node.availableAddresses(prot, true)) {
boolean router = routerAddrs == null || boolean router = routerAddrs.isEmpty() || routerAddrs.contains(addr);
routerAddrs.contains(addr.getHostName() + ":" + addr.getPort()) ||
routerAddrs.contains(addr.getAddress().getHostAddress() + ":" + addr.getPort());


boolean reachable = noAttrsAndMetrics || !addr.getAddress().isLoopbackAddress() || boolean reachable = noAttrsAndMetrics || !addr.getAddress().isLoopbackAddress() ||
F.containsAny(U.allLocalMACs(), node.attribute(ATTR_MACS).toString().split(", ")); F.containsAny(macsCache, node.<String>attribute(ATTR_MACS).split(", "));


if (router && reachable) { if (router && reachable) {
nodeBuilder.connectable(true); nodeBuilder.connectable(true);
Expand Down

0 comments on commit b0ac987

Please sign in to comment.