Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 37C0D200C1A for ; Mon, 13 Feb 2017 21:46:46 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 366D9160B60; Mon, 13 Feb 2017 20:46:46 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 58075160B4A for ; Mon, 13 Feb 2017 21:46:45 +0100 (CET) Received: (qmail 6417 invoked by uid 500); 13 Feb 2017 20:46:44 -0000 Mailing-List: contact dev-help@zookeeper.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@zookeeper.apache.org Delivered-To: mailing list dev@zookeeper.apache.org Received: (qmail 6399 invoked by uid 99); 13 Feb 2017 20:46:44 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Feb 2017 20:46:44 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id F28D0C0408 for ; Mon, 13 Feb 2017 20:46:43 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -1.199 X-Spam-Level: X-Spam-Status: No, score=-1.199 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id e-3as5GSQLwD for ; Mon, 13 Feb 2017 20:46:43 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id C1CF35F29A for ; Mon, 13 Feb 2017 20:46:42 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 201B0E0626 for ; Mon, 13 Feb 2017 20:46:42 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id CC0AD21D65 for ; Mon, 13 Feb 2017 20:46:41 +0000 (UTC) Date: Mon, 13 Feb 2017 20:46:41 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: dev@zookeeper.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (ZOOKEEPER-2691) recreateSocketAddresses may recreate the unreachable IP address MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Mon, 13 Feb 2017 20:46:46 -0000 [ https://issues.apache.org/jira/browse/ZOOKEEPER-2691?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15864390#comment-15864390 ] ASF GitHub Bot commented on ZOOKEEPER-2691: ------------------------------------------- Github user hanm commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/173#discussion_r100893741 --- Diff: src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java --- @@ -159,7 +159,7 @@ public QuorumServer(long id, String hostname, public void recreateSocketAddresses() { InetAddress address = null; try { - address = InetAddress.getByName(this.hostname); + address = getReachableAddress(this.hostname, 2000); --- End diff -- Instead of hardcoding 2000 here, we can have a constant variable represent the timeout value at the beginning of QuorumPeer.java. [QuorumCnxManager](https://github.com/apache/zookeeper/blob/master/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java) is a good example on how it uses constants. > recreateSocketAddresses may recreate the unreachable IP address > --------------------------------------------------------------- > > Key: ZOOKEEPER-2691 > URL: https://issues.apache.org/jira/browse/ZOOKEEPER-2691 > Project: ZooKeeper > Issue Type: Bug > Affects Versions: 3.4.8 > Environment: Centos6.5 > Java8 > ZooKeeper3.4.8 > Reporter: JiangJiafu > Priority: Minor > > The QuorumPeer$QuorumServer.recreateSocketAddress() is used to resolved the hostname to a new IP address(InetAddress) when any exception happens to the socket. It will be very useful when a hostname can be resolved to more than one IP address. > But the problem is Java API InetAddress.getByName(String hostname) will always return the first IP address when the hostname can be resolved to more than one IP address, and the first IP address may be unreachable forever. For example, if a machine has two network interfaces: eth0, eth1, say eth0 has ip1, eth1 has ip2, the relationship between hostname and the IP addresses is set in /etc/hosts. When I "close" the eth0 by command "ifdown eth0", the InetAddress.getByName(String hostname) will still return ip1, which is unreachable forever. > So I think it will be better to check the IP address by InetAddress.isReachable(long) and choose the reachable IP address. > I have modified the ZooKeeper source code, and test the new code in my own environment, and it can work very well when I turn down some network interfaces using "ifdown" command. > The original code is: > {code:title=QuorumPeer.java|borderStyle=solid} > public void recreateSocketAddresses() { > InetAddress address = null; > try { > address = InetAddress.getByName(this.hostname); > LOG.info("Resolved hostname: {} to address: {}", this.hostname, address); > this.addr = new InetSocketAddress(address, this.port); > if (this.electionPort > 0){ > this.electionAddr = new InetSocketAddress(address, this.electionPort); > } > } catch (UnknownHostException ex) { > LOG.warn("Failed to resolve address: {}", this.hostname, ex); > // Have we succeeded in the past? > if (this.addr != null) { > // Yes, previously the lookup succeeded. Leave things as they are > return; > } > // The hostname has never resolved. Create our InetSocketAddress(es) as unresolved > this.addr = InetSocketAddress.createUnresolved(this.hostname, this.port); > if (this.electionPort > 0){ > this.electionAddr = InetSocketAddress.createUnresolved(this.hostname, > this.electionPort); > } > } > } > {code} > After my modification: > {code:title=QuorumPeer.java|borderStyle=solid} > public void recreateSocketAddresses() { > InetAddress address = null; > try { > address = getReachableAddress(this.hostname); > LOG.info("Resolved hostname: {} to address: {}", this.hostname, address); > this.addr = new InetSocketAddress(address, this.port); > if (this.electionPort > 0){ > this.electionAddr = new InetSocketAddress(address, this.electionPort); > } > } catch (UnknownHostException ex) { > LOG.warn("Failed to resolve address: {}", this.hostname, ex); > // Have we succeeded in the past? > if (this.addr != null) { > // Yes, previously the lookup succeeded. Leave things as they are > return; > } > // The hostname has never resolved. Create our InetSocketAddress(es) as unresolved > this.addr = InetSocketAddress.createUnresolved(this.hostname, this.port); > if (this.electionPort > 0){ > this.electionAddr = InetSocketAddress.createUnresolved(this.hostname, > this.electionPort); > } > } > } > public InetAddress getReachableAddress(String hostname) throws UnknownHostException { > InetAddress[] addresses = InetAddress.getAllByName(hostname); > for (InetAddress a : addresses) { > try { > if (a.isReachable(5000)) { > return a; > } > } catch (IOException e) { > LOG.warn("IP address {} is unreachable", a); > } > } > // All the IP address is unreachable, just return the first one. > return addresses[0]; > } > {code} -- This message was sent by Atlassian JIRA (v6.3.15#6346)