Repository: ignite Updated Branches: refs/heads/ignite-zk 45bd0a287 -> 74526d19e
zk Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/74526d19 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/74526d19 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/74526d19 Branch: refs/heads/ignite-zk Commit: 74526d19ef4015bc26c24474cb9f45877d8b01cb Parents: 45bd0a2 Author: sboikov <sboi...@gridgain.com> Authored: Mon Dec 4 17:11:14 2017 +0300 Committer: sboikov <sboi...@gridgain.com> Committed: Mon Dec 4 17:13:06 2017 +0300 ---------------------------------------------------------------------- .../spi/discovery/zk/ZookeeperDiscoverySpi.java | 77 ++++++++++++++------ .../ZookeeperDiscoverySpiBasicTest.java | 58 ++++++++++++++- 2 files changed, 110 insertions(+), 25 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/74526d19/modules/core/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java b/modules/core/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java index 8a9693b..2d2ff01 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/discovery/zk/ZookeeperDiscoverySpi.java @@ -20,7 +20,10 @@ package org.apache.ignite.spi.discovery.zk; import java.io.IOException; import java.io.Serializable; import java.net.InetAddress; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.UUID; import org.apache.ignite.IgniteLogger; @@ -39,6 +42,7 @@ import org.apache.ignite.spi.IgniteSpiConfiguration; import org.apache.ignite.spi.IgniteSpiContext; import org.apache.ignite.spi.IgniteSpiException; import org.apache.ignite.spi.IgniteSpiMultipleInstancesSupport; +import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi; import org.apache.ignite.spi.discovery.DiscoveryMetricsProvider; import org.apache.ignite.spi.discovery.DiscoverySpi; import org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage; @@ -51,6 +55,9 @@ import org.apache.ignite.spi.discovery.zk.internal.ZookeeperClusterNode; import org.apache.ignite.spi.discovery.zk.internal.ZookeeperDiscoveryImpl; import org.jetbrains.annotations.Nullable; +import static org.apache.ignite.IgniteSystemProperties.IGNITE_CONSISTENT_ID_BY_HOST_WITHOUT_PORT; +import static org.apache.ignite.IgniteSystemProperties.getBoolean; + /** * */ @@ -105,6 +112,9 @@ public class ZookeeperDiscoverySpi extends IgniteSpiAdapter implements Discovery @GridToStringExclude private Serializable consistentId; + /** Local node addresses. */ + private IgniteBiTuple<Collection<String>, Collection<String>> addrs; + /** */ @LoggerResource @GridToStringExclude @@ -182,14 +192,54 @@ public class ZookeeperDiscoverySpi extends IgniteSpiAdapter implements Discovery if (consistentId == null) { consistentId = ignite.configuration().getConsistentId(); - // TODO ZK - if (consistentId == null) - consistentId = ignite.configuration().getNodeId(); + if (consistentId == null) { + final List<String> sortedAddrs = new ArrayList<>(addrs.get1()); + + Collections.sort(sortedAddrs); + + if (getBoolean(IGNITE_CONSISTENT_ID_BY_HOST_WITHOUT_PORT)) + consistentId = U.consistentId(sortedAddrs); + else { + Integer commPort = (Integer)locNodeAttrs.get( + TcpCommunicationSpi.class.getSimpleName() + "." + TcpCommunicationSpi.ATTR_PORT); + + if (commPort == null) + throw new IgniteSpiException("Can not initialized consistent ID."); + + consistentId = U.consistentId(sortedAddrs, (Integer)commPort); + } + } } return consistentId; } + /** + * + */ + private void initAddresses() { + if (addrs == null) { + String locHost = ignite != null ? ignite.configuration().getLocalHost() : null; + + InetAddress locAddr; + + try { + locAddr = U.resolveLocalHost(locHost); + } + catch (IOException e) { + throw new IgniteSpiException("Unknown local address: " + locHost, e); + } + + try { + addrs = U.resolveLocalAddresses(locAddr); + } + catch (Exception e) { + throw new IgniteSpiException("Failed to resolve local host to set of external addresses: " + locHost, + e); + } + } + } + /** {@inheritDoc} */ @Override public Collection<ClusterNode> getRemoteNodes() { return impl.remoteNodes(); @@ -358,26 +408,7 @@ public class ZookeeperDiscoverySpi extends IgniteSpiAdapter implements Discovery private ZookeeperClusterNode initLocalNode() { assert ignite != null; - String locHost = ignite.configuration().getLocalHost(); - - InetAddress locAddr; - - try { - locAddr = U.resolveLocalHost(locHost); - } - catch (IOException e) { - throw new IgniteSpiException("Unknown local address: " + locHost, e); - } - - IgniteBiTuple<Collection<String>, Collection<String>> addrs; - - try { - addrs = U.resolveLocalAddresses(locAddr); - } - catch (Exception e) { - throw new IgniteSpiException("Failed to resolve local host to set of external addresses: " + locHost, e); - } - + initAddresses(); consistentId = consistentId(); http://git-wip-us.apache.org/repos/asf/ignite/blob/74526d19/modules/core/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoverySpiBasicTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoverySpiBasicTest.java b/modules/core/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoverySpiBasicTest.java index c95bdf7..875d264 100644 --- a/modules/core/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoverySpiBasicTest.java +++ b/modules/core/src/test/java/org/apache/ignite/spi/discovery/zk/internal/ZookeeperDiscoverySpiBasicTest.java @@ -50,6 +50,7 @@ import org.apache.ignite.events.EventType; import org.apache.ignite.internal.DiscoverySpiTestListener; import org.apache.ignite.internal.IgniteInternalFuture; import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.IgniteNodeAttributes; import org.apache.ignite.internal.IgnitionEx; import org.apache.ignite.internal.managers.discovery.DiscoveryLocalJoinData; import org.apache.ignite.internal.managers.discovery.IgniteDiscoverySpi; @@ -116,6 +117,9 @@ public class ZookeeperDiscoverySpiBasicTest extends GridCommonAbstractTest { /** */ private Map<String, Object> userAttrs; + /** */ + private boolean dfltConsistenId; + /** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { if (testSockNio) @@ -123,7 +127,8 @@ public class ZookeeperDiscoverySpiBasicTest extends GridCommonAbstractTest { IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); - cfg.setConsistentId(igniteInstanceName); + if (!dfltConsistenId) + cfg.setConsistentId(igniteInstanceName); ZookeeperDiscoverySpi zkSpi = new ZookeeperDiscoverySpi(); @@ -325,7 +330,7 @@ public class ZookeeperDiscoverySpiBasicTest extends GridCommonAbstractTest { /** * @throws Exception If failed. */ - public void testAddresses() throws Exception { + public void testNodeAddresses() throws Exception { startGridsMultiThreaded(3); client = true; @@ -350,6 +355,55 @@ public class ZookeeperDiscoverySpiBasicTest extends GridCommonAbstractTest { /** * @throws Exception If failed. */ + public void testSetConsistentId() throws Exception { + startGridsMultiThreaded(3); + + client = true; + + startGridsMultiThreaded(3, 3); + + waitForTopology(6); + + for (Ignite node : G.allGrids()) { + ClusterNode locNode0 = node.cluster().localNode(); + + assertEquals(locNode0.attribute(IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME), + locNode0.consistentId()); + + for (ClusterNode node0 : node.cluster().nodes()) { + assertEquals(node0.attribute(IgniteNodeAttributes.ATTR_IGNITE_INSTANCE_NAME), + node0.consistentId()); + } + } + } + + /** + * @throws Exception If failed. + */ + public void testDefaultConsistentId() throws Exception { + dfltConsistenId = true; + + startGridsMultiThreaded(3); + + client = true; + + startGridsMultiThreaded(3, 3); + + waitForTopology(6); + + for (Ignite node : G.allGrids()) { + ClusterNode locNode0 = node.cluster().localNode(); + + assertNotNull(locNode0.consistentId()); + + for (ClusterNode node0 : node.cluster().nodes()) + assertNotNull(node0.consistentId()); + } + } + + /** + * @throws Exception If failed. + */ public void testClientNodesStatus() throws Exception { startGrid(0);