IGNITE-4545 Added cache for router hostnames. - Fixes #1428. Signed-off-by: Andrey Novikov <anovi...@gridgain.com>
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/27ba69b5 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/27ba69b5 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/27ba69b5 Branch: refs/heads/ignite-gg-11810-1 Commit: 27ba69b55f645a022f2e84cc4db76309b02850e3 Parents: 79401b2 Author: Andrey Novikov <anovi...@gridgain.com> Authored: Mon Jan 16 11:22:34 2017 +0700 Committer: Andrey Novikov <anovi...@gridgain.com> Committed: Mon Jan 16 11:22:34 2017 +0700 ---------------------------------------------------------------------- .../GridClientConnectionManagerAdapter.java | 7 ++- .../impl/connection/GridClientTopology.java | 53 ++++++++++++++++---- 2 files changed, 49 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/27ba69b5/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java index 12baee0..f714e7a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java @@ -85,6 +85,9 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo /** Class logger. */ private final Logger log; + /** All local enabled MACs. */ + private final Collection<String> macs; + /** NIO server. */ private GridNioServer srv; @@ -166,6 +169,8 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo if (marshId == null && cfg.getMarshaller() == null) throw new GridClientException("Failed to start client (marshaller is not configured)."); + macs = U.allLocalMACs(); + if (cfg.getProtocol() == GridClientProtocol.TCP) { try { IgniteLogger gridLog = new JavaLogger(false); @@ -316,7 +321,7 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo } 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<>(); http://git-wip-us.apache.org/repos/asf/ignite/blob/27ba69b5/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientTopology.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientTopology.java b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientTopology.java index effd5b3..97aa586 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientTopology.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientTopology.java @@ -21,7 +21,6 @@ import java.net.InetSocketAddress; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.Set; @@ -61,12 +60,18 @@ public class GridClientTopology { /** Cached last error prevented topology from update. */ private GridClientException lastError; + /** Router addresses from configuration. */ + private final String routers; + /** * Set of router addresses to infer direct connectivity * when client is working in router connection mode. * {@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. */ private final GridClientProtocol prot; @@ -96,8 +101,38 @@ public class GridClientTopology { metricsCache = cfg.isEnableMetricsCache(); attrCache = cfg.isEnableAttributesCache(); 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(); } /** @@ -279,7 +314,7 @@ public class GridClientTopology { try { if (lastError != null) throw new GridClientDisconnectedException( - "Topology is failed [protocol=" + prot + ", routers=" + routerAddrs + ']', lastError); + "Topology is failed [protocol=" + prot + ", routers=" + routers + ']', lastError); else return nodes.get(id); } @@ -376,19 +411,17 @@ public class GridClientTopology { (metricsCache && attrCache) || (node.attributes().isEmpty() && node.metrics() == null); // Try to bypass object copying. - if (noAttrsAndMetrics && routerAddrs == null && node.connectable()) + if (noAttrsAndMetrics && routerAddrs.isEmpty() && node.connectable()) return node; // Return a new node instance based on the original one. GridClientNodeImpl.Builder nodeBuilder = GridClientNodeImpl.builder(node, !attrCache, !metricsCache); for (InetSocketAddress addr : node.availableAddresses(prot, true)) { - boolean router = routerAddrs == null || - routerAddrs.contains(addr.getHostName() + ":" + addr.getPort()) || - routerAddrs.contains(addr.getAddress().getHostAddress() + ":" + addr.getPort()); + boolean router = routerAddrs.isEmpty() || routerAddrs.contains(addr); 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) { nodeBuilder.connectable(true);