Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8777817677 for ; Sun, 1 Mar 2015 20:02:07 +0000 (UTC) Received: (qmail 88714 invoked by uid 500); 1 Mar 2015 20:02:07 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 88678 invoked by uid 500); 1 Mar 2015 20:02:07 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 88669 invoked by uid 99); 1 Mar 2015 20:02:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Mar 2015 20:02:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 01 Mar 2015 20:02:05 +0000 Received: (qmail 87822 invoked by uid 99); 1 Mar 2015 20:01:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 01 Mar 2015 20:01:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 46418E0D43; Sun, 1 Mar 2015 20:01:45 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sevdokimov@apache.org To: commits@ignite.incubator.apache.org Date: Sun, 01 Mar 2015 20:01:47 -0000 Message-Id: <429b3556fb1e4d0ea6c28114e84479f1@git.apache.org> In-Reply-To: <0c0df158b03549a29dfdd08c32b2d879@git.apache.org> References: <0c0df158b03549a29dfdd08c32b2d879@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [03/10] incubator-ignite git commit: # IGNITE-11 Optimization obtaining localhosts. X-Virus-Checked: Checked by ClamAV on apache.org # IGNITE-11 Optimization obtaining localhosts. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/e89a70cc Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/e89a70cc Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/e89a70cc Branch: refs/heads/sprint-2 Commit: e89a70cc911648b081b56c4c19e6f59d137b3a8e Parents: e7171b0 Author: sevdokimov Authored: Sun Feb 22 13:11:28 2015 +0300 Committer: sevdokimov Committed: Sun Feb 22 13:11:28 2015 +0300 ---------------------------------------------------------------------- .../ignite/internal/util/IgniteUtils.java | 87 +++++++++++++++++--- 1 file changed, 76 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/e89a70cc/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 5db0da4..4cf5718 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 @@ -300,6 +300,8 @@ public abstract class IgniteUtils { private static final Map, C1> exceptionConverters; + private volatile static IgniteBiTuple, Collection> cachedLocalAddr; + /** * Initializes enterprise check. */ @@ -1489,6 +1491,55 @@ public abstract class IgniteUtils { return locHost0 != null && !resetLocalHost().equals(locHost0); } + public static List filterReachable(List addrs) { + final int reachTimeout = 2000; + + if (addrs.isEmpty()) + return Collections.emptyList(); + + if (addrs.size() == 1) { + if (reachable(addrs.get(1), reachTimeout)) + return Collections.singletonList(addrs.get(1)); + + return Collections.emptyList(); + } + + final List res = new ArrayList<>(addrs.size()); + + Collection> futs = new ArrayList<>(addrs.size()); + + ExecutorService executor = Executors.newFixedThreadPool(Math.min(10, addrs.size())); + + for (final InetAddress addr : addrs) { + futs.add(executor.submit(new Runnable() { + @Override + public void run() { + if (reachable(addr, reachTimeout)) { + synchronized (res) { + res.add(addr); + } + } + } + })); + } + + for (Future fut : futs) { + try { + fut.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + + throw new IgniteException("Thread has been interrupted.", e); + } catch (ExecutionException e) { + throw new IgniteException(e); + } + } + + executor.shutdown(); + + return res; + } + /** * Returns host names consistent with {@link #resolveLocalHost(String)}. So when it returns * a common address this method returns single host name, and when a wildcard address passed @@ -1501,28 +1552,42 @@ public abstract class IgniteUtils { */ public static IgniteBiTuple, Collection> resolveLocalAddresses(InetAddress locAddr) throws IOException, IgniteCheckedException { + assert locAddr != null; Collection addrs = new ArrayList<>(); Collection hostNames = new ArrayList<>(); if (locAddr.isAnyLocalAddress()) { - // It should not take longer than 2 seconds to reach - // local address on any network. - int reachTimeout = 2000; + IgniteBiTuple, Collection> res = cachedLocalAddr; - for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) { - for (InetAddress addr : asIterable(itf.getInetAddresses())) { - if (!addr.isLinkLocalAddress() && reachable(itf, addr, reachTimeout)) - addresses(addr, addrs, hostNames); + if (res == null) { + List localAddrs = new ArrayList<>(); + + for (NetworkInterface itf : asIterable(NetworkInterface.getNetworkInterfaces())) { + for (InetAddress addr : asIterable(itf.getInetAddresses())) { + if (!addr.isLinkLocalAddress()) + localAddrs.add(addr); + } } + + localAddrs = filterReachable(localAddrs); + + for (InetAddress addr : localAddrs) + addresses(addr, addrs, hostNames); + + if (F.isEmpty(addrs)) + throw new IgniteCheckedException("No network addresses found (is networking enabled?)."); + + res = F.t(addrs, hostNames); + + cachedLocalAddr = res; } - if (F.isEmpty(addrs)) - throw new IgniteCheckedException("No network addresses found (is networking enabled?)."); + return res; } - else - addresses(locAddr, addrs, hostNames); + + addresses(locAddr, addrs, hostNames); return F.t(addrs, hostNames); }