# ignite-917: review (ignite-1033 fixed).
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/532a98f1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/532a98f1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/532a98f1 Branch: refs/heads/ignite-788-dev-review Commit: 532a98f13933ce34168b64854a735f4f9ee4b8bd Parents: 2ad4fe1 Author: ashutak <ashu...@gridgain.com> Authored: Wed Jun 24 16:19:27 2015 +0300 Committer: ashutak <ashu...@gridgain.com> Committed: Wed Jun 24 16:19:27 2015 +0300 ---------------------------------------------------------------------- .../ClientAbstractConnectivitySelfTest.java | 4 +- .../org/apache/ignite/cluster/ClusterNode.java | 2 + .../internal/cluster/ClusterGroupAdapter.java | 30 +++-- .../cluster/IgniteClusterAsyncImpl.java | 2 +- .../ignite/internal/util/IgniteUtils.java | 6 +- .../internal/ClusterForHostsSelfTest.java | 113 +++++++++++++++++++ .../ignite/internal/GridProjectionSelfTest.java | 19 ---- .../internal/util/IgniteUtilsSelfTest.java | 22 ++++ .../ignite/testsuites/IgniteBasicTestSuite.java | 1 + 9 files changed, 157 insertions(+), 42 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/clients/src/test/java/org/apache/ignite/internal/client/integration/ClientAbstractConnectivitySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/clients/src/test/java/org/apache/ignite/internal/client/integration/ClientAbstractConnectivitySelfTest.java b/modules/clients/src/test/java/org/apache/ignite/internal/client/integration/ClientAbstractConnectivitySelfTest.java index 5aec7e5..a2e7c38 100644 --- a/modules/clients/src/test/java/org/apache/ignite/internal/client/integration/ClientAbstractConnectivitySelfTest.java +++ b/modules/clients/src/test/java/org/apache/ignite/internal/client/integration/ClientAbstractConnectivitySelfTest.java @@ -20,9 +20,9 @@ package org.apache.ignite.internal.client.integration; import org.apache.ignite.*; import org.apache.ignite.internal.client.*; import org.apache.ignite.internal.util.*; -import org.apache.ignite.lang.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; import org.apache.ignite.testframework.*; import org.apache.ignite.testframework.junits.common.*; import org.jetbrains.annotations.*; @@ -146,7 +146,7 @@ public abstract class ClientAbstractConnectivitySelfTest extends GridCommonAbstr startRestNode("grid1", LOOPBACK_IP, defaultRestPort()); checkConnectivityByIp(LOOPBACK_IP, F.t((Collection<String>)Collections.singleton(LOOPBACK_IP), - (Collection<String>)Collections.singleton(""))); + (Collection<String>)Collections.<String>emptySet())); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/main/java/org/apache/ignite/cluster/ClusterNode.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/cluster/ClusterNode.java b/modules/core/src/main/java/org/apache/ignite/cluster/ClusterNode.java index 85fd08a..bb792d5 100644 --- a/modules/core/src/main/java/org/apache/ignite/cluster/ClusterNode.java +++ b/modules/core/src/main/java/org/apache/ignite/cluster/ClusterNode.java @@ -188,6 +188,8 @@ public interface ClusterNode { * <p> * If {@link IgniteConfiguration#getLocalHost()} value is {@code null} then local wildcard address will be used, * and this method returns host names of all addresses of that node. + * <p/ + * Note: the loopback address will skipped from the results. * * @return Collection of host names. */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java index 1831321..b940017 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/cluster/ClusterGroupAdapter.java @@ -578,7 +578,9 @@ public class ClusterGroupAdapter implements ClusterGroupEx, Externalizable { /** {@inheritDoc} */ @Override public final ClusterGroup forHost(String host, String... hosts) { - return forPredicate(new HostsFilter(host, hosts)); + A.notNull(host, "host"); + + return forPredicate(new HostsFilter(host, hosts)); } /** {@inheritDoc} */ @@ -772,30 +774,24 @@ public class ClusterGroupAdapter implements ClusterGroupEx, Externalizable { private static final long serialVersionUID = 0L; /** Hosts Names. */ - private final HashSet hashInputHostNames; + private final Collection<String> validHostNames = new HashSet<>(); /** - * @param name First host name. - * @param names Host names + * @param name First host name. + * @param names Host names */ - private HostsFilter(String name, String[] names) { - hashInputHostNames = new HashSet(); - - if (name != null) - hashInputHostNames.add(name); + private HostsFilter(String name, String... names) { + validHostNames.add(name); - if (names != null && (names.length > 0)) { - for (String currentInputHostName : names) - hashInputHostNames.add(currentInputHostName); - } + if (names != null && (names.length > 0)) + Collections.addAll(validHostNames, names); } /** {@inheritDoc} */ @Override public boolean apply(ClusterNode n) { - for (String currentHostName : n.hostNames()) { - if (hashInputHostNames.contains(currentHostName)) { - return true; - } + for (String hostName : n.hostNames()) { + if (validHostNames.contains(hostName)) + return true; } return false; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java index ca6cf32..6e68527 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/cluster/IgniteClusterAsyncImpl.java @@ -233,7 +233,7 @@ public class IgniteClusterAsyncImpl extends AsyncSupportAdapter<IgniteCluster> /** {@inheritDoc} */ @Override public ClusterGroup forHost(String host, String... hosts) { - return cluster.forHost(host, hosts); + return cluster.forHost(host, hosts); } /** {@inheritDoc} */ http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java index 6623e85..f457d6c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java @@ -1658,10 +1658,10 @@ public abstract class IgniteUtils { String ipAddr = addr.getHostAddress(); - hostName = F.isEmpty(hostName) || hostName.equals(ipAddr) || addr.isLoopbackAddress() ? "" : hostName; - addrs.add(ipAddr); - hostNames.add(hostName); + + if (!F.isEmpty(hostName) && !addr.isLoopbackAddress()) + hostNames.add(hostName); } /** http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java new file mode 100644 index 0000000..59c3db9 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/ClusterForHostsSelfTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal; + +import org.apache.ignite.*; +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.testframework.junits.common.*; + +import java.lang.reflect.*; +import java.util.*; + +/** + * Test for {@link ClusterGroup#forHost(String, String...)}. + * + * @see GridProjectionSelfTest + */ +@GridCommonTest(group = "Kernal Self") +public class ClusterForHostsSelfTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + Collection<String> hostNames = null; + + if ("forHostTest".equals(gridName)) + hostNames = Arrays.asList("h_1", "h_2", "h_3"); + + IgniteConfiguration cfg = super.getConfiguration(gridName); + + if (hostNames != null) { + TcpDiscoverySpi disco = (TcpDiscoverySpi)cfg.getDiscoverySpi(); + + cfg.setDiscoverySpi(new CustomHostsTcpDiscoverySpi(hostNames).setIpFinder(disco.getIpFinder())); + } + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testForHosts() throws Exception { + Ignite ignite = startGrid("forHostTest"); + + assertEquals(1, ignite.cluster().forHost("h_1").nodes().size()); + assertEquals(1, ignite.cluster().forHost("h_1", "h_3").nodes().size()); + assertEquals(1, ignite.cluster().forHost("unknown_host", "h_2").nodes().size()); + assertEquals(1, ignite.cluster().forHost("h_1", "h_3", "unknown_host", "h_2").nodes().size()); + + assertEquals(0, ignite.cluster().forHost("unknown_host").nodes().size()); + + boolean gotNpe = false; + + try { + assertEquals(0, ignite.cluster().forHost(null, null, null).nodes().size()); + } + catch (NullPointerException e) { + gotNpe = true; + } + + assertTrue(gotNpe); + } + + /** + * Tcp discovery spi that allow to customise hostNames of created local node. + */ + private static class CustomHostsTcpDiscoverySpi extends TcpDiscoverySpi { + /** Hosts. */ + private final Collection<String> hosts; + + /** + * @param hosts Host names which will be retuned by {@link ClusterNode#hostNames()} of created local node. + */ + CustomHostsTcpDiscoverySpi(Collection<String> hosts) { + this.hosts = hosts; + } + + /** + * @param srvPort Server port. + */ + @Override protected void initLocalNode(int srvPort, boolean addExtAddrAttr) { + super.initLocalNode(srvPort, addExtAddrAttr); + + try { + Field hostNamesField = locNode.getClass().getDeclaredField("hostNames"); + + hostNamesField.setAccessible(true); + + hostNamesField.set(locNode, hosts); + } + catch (IllegalAccessException | NoSuchFieldException e) { + U.error(log, "Looks like implementation of " + locNode.getClass() + + " class was changed. Need to update test.", e); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/test/java/org/apache/ignite/internal/GridProjectionSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/GridProjectionSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/GridProjectionSelfTest.java index bd625d9..9fbad80 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/GridProjectionSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/GridProjectionSelfTest.java @@ -248,23 +248,4 @@ public class GridProjectionSelfTest extends GridProjectionAbstractTest { return even ? cnt - 1 : cnt - 2; } - - /** - * @throws Exception If failed. - */ - public void testForHost() throws Exception { - Collection<ClusterNode> allNodes = ignite.cluster().nodes(); - ClusterNode localNode = ignite.cluster().localNode(); - ArrayList<String> inputHostNames = new ArrayList<String> (); - - for (ClusterNode currentNode : allNodes) - Collections.addAll(inputHostNames, currentNode.hostNames().toArray(new String[0])); - - String[] inputHostNamesArray = inputHostNames.toArray(new String[] {}); - ClusterGroup resultGroup = ignite.cluster().forHost(inputHostNamesArray[0], inputHostNamesArray); - ClusterGroup nullTestGroup = ignite.cluster().forHost(null, null); - - assert((resultGroup.node(localNode.id())) != null); - assert((nullTestGroup.node(localNode.id())) == null); - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java index 6e3e0e2..4762001 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java @@ -23,6 +23,7 @@ import org.apache.ignite.compute.*; import org.apache.ignite.internal.util.lang.*; import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.internal.*; +import org.apache.ignite.lang.*; import org.apache.ignite.testframework.*; import org.apache.ignite.testframework.http.*; import org.apache.ignite.testframework.junits.*; @@ -698,6 +699,27 @@ public class IgniteUtilsSelfTest extends GridCommonAbstractTest { } /** + * @throws Exception If failed. + */ + public void testResolveLocalAddresses() throws Exception { + InetAddress inetAddress = InetAddress.getByName("0.0.0.0"); + + IgniteBiTuple<Collection<String>, Collection<String>> addrs = U.resolveLocalAddresses(inetAddress); + + Collection<String> hostNames = addrs.get2(); + + assertFalse(hostNames.contains(null)); + assertFalse(hostNames.contains("")); + assertFalse(hostNames.contains("127.0.0.1")); + + assertFalse(F.exist(hostNames, new IgnitePredicate<String>() { + @Override public boolean apply(String hostName) { + return hostName.contains("localhost") || hostName.contains("0:0:0:0:0:0:0:1"); + } + })); + } + + /** * Test enum. */ private enum TestEnum { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/532a98f1/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java index 2d14728..8be9f31 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java @@ -66,6 +66,7 @@ public class IgniteBasicTestSuite extends TestSuite { suite.addTest(new TestSuite(GridSelfTest.class)); GridTestUtils.addTestIfNeeded(suite, GridProjectionSelfTest.class, ignoredTests); + suite.addTest(new TestSuite(ClusterForHostsSelfTest.class)); GridTestUtils.addTestIfNeeded(suite, GridMessagingSelfTest.class, ignoredTests); suite.addTest(new TestSuite(IgniteMessagingWithClientTest.class)); GridTestUtils.addTestIfNeeded(suite, GridMessagingNoPeerClassLoadingSelfTest.class, ignoredTests);