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 6ea7c227bddde..0928c9061cf43 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 macs; + /** NIO server. */ private GridNioServer srv; @@ -166,6 +169,8 @@ protected GridClientConnectionManagerAdapter(UUID clientId, 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); @@ -315,7 +320,7 @@ protected GridClientConnectionManagerAdapter(UUID clientId, } 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 srvs = new LinkedHashSet<>(); 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 effd5b3943148..97aa5861308db 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.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 routerAddrs; + private final Set routerAddrs; + + /** List of all known local MACs */ + private final Collection macsCache; /** Protocol. */ private final GridClientProtocol prot; @@ -96,8 +101,38 @@ public GridClientTopology(GridClientConfiguration cfg) { 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 GridClientNode node(UUID id) throws GridClientException { 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 @@ private GridClientNodeImpl prepareNode(final GridClientNodeImpl node) { (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.attribute(ATTR_MACS).split(", ")); if (router && reachable) { nodeBuilder.connectable(true);