You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by cn...@apache.org on 2015/10/18 19:00:41 UTC

svn commit: r1709293 - in /zookeeper/trunk: CHANGES.txt src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java

Author: cnauroth
Date: Sun Oct 18 17:00:41 2015
New Revision: 1709293

URL: http://svn.apache.org/viewvc?rev=1709293&view=rev
Log:
ZOOKEEPER-1460: IPv6 literal address not supported for quorum members (Joseph Walton via cnauroth)

Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
    zookeeper/trunk/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1709293&r1=1709292&r2=1709293&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Sun Oct 18 17:00:41 2015
@@ -187,6 +187,9 @@ BUGFIXES:
   ZOOKEEPER-2281: ZK Server startup fails if there are spaces in the JAVA_HOME
   path (Neha Bathra via cnauroth)
 
+  ZOOKEEPER-1460: IPv6 literal address not supported for quorum members
+  (Joseph Walton via cnauroth)
+
 IMPROVEMENTS:
   ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed Wanderman-Milne via shralex)  
 

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java?rev=1709293&r1=1709292&r2=1709293&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java Sun Oct 18 17:00:41 2015
@@ -173,17 +173,38 @@ public class QuorumPeer extends ZooKeepe
                type = LearnerType.PARTICIPANT;
             } else {
                throw new ConfigException("Unrecognised peertype: " + s);
-            }   
-        }       
+            }
+        }
+
+        private static String[] splitWithLeadingHostname(String s)
+                throws ConfigException
+        {
+            /* Does it start with an IPv6 literal? */
+            if (s.startsWith("[")) {
+                int i = s.indexOf("]:");
+                if (i < 0) {
+                    throw new ConfigException(s + " starts with '[' but has no matching ']:'");
+                }
+
+                String[] sa = s.substring(i + 2).split(":");
+                String[] nsa = new String[sa.length + 1];
+                nsa[0] = s.substring(1, i);
+                System.arraycopy(sa, 0, nsa, 1, sa.length);
+
+                return nsa;
+            } else {
+                return s.split(":");
+            }
+        }
 
-        private static final String wrongFormat = " does not have the form server_cofig or server_config;client_config"+
+        private static final String wrongFormat = " does not have the form server_config or server_config;client_config"+
         " where server_config is host:port:port or host:port:port:type and client_config is port or host:port";
-        
+
         public QuorumServer(long sid, String addressStr) throws ConfigException {
             // LOG.warn("sid = " + sid + " addressStr = " + addressStr);
             this.id = sid;
             String serverClientParts[] = addressStr.split(";");
-            String serverParts[] = serverClientParts[0].split(":");
+            String serverParts[] = splitWithLeadingHostname(serverClientParts[0]);
             if ((serverClientParts.length > 2) || (serverParts.length < 3)
                     || (serverParts.length > 4)) {
                 throw new ConfigException(addressStr + wrongFormat);
@@ -191,7 +212,7 @@ public class QuorumPeer extends ZooKeepe
 
             if (serverClientParts.length == 2) {
                 //LOG.warn("ClientParts: " + serverClientParts[1]);
-                String clientParts[] = serverClientParts[1].split(":");
+                String clientParts[] = splitWithLeadingHostname(serverClientParts[1]);
                 if (clientParts.length > 2) {
                     throw new ConfigException(addressStr + wrongFormat);
                 }
@@ -252,11 +273,21 @@ public class QuorumPeer extends ZooKeepe
             this.myAddrs = excludedSpecialAddresses(this.myAddrs);
         }
 
+        private static String delimitedHostString(InetSocketAddress addr)
+        {
+            String host = addr.getHostString();
+            if (host.contains(":")) {
+                return "[" + host + "]";
+            } else {
+                return host;
+            }
+        }
+
         public String toString(){
-            StringWriter sw = new StringWriter();            
+            StringWriter sw = new StringWriter();
             //addr should never be null, but just to make sure
-            if (addr !=null) { 
-                sw.append(addr.getHostString());
+            if (addr !=null) {
+                sw.append(delimitedHostString(addr));
                 sw.append(":");
                 sw.append(String.valueOf(addr.getPort()));
             }
@@ -268,7 +299,7 @@ public class QuorumPeer extends ZooKeepe
             else if (type == LearnerType.PARTICIPANT) sw.append(":participant");            
             if (clientAddr!=null){
                 sw.append(";");
-                sw.append(clientAddr.getHostString());
+                sw.append(delimitedHostString(clientAddr));
                 sw.append(":");
                 sw.append(String.valueOf(clientAddr.getPort()));
             }

Modified: zookeeper/trunk/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java?rev=1709293&r1=1709292&r2=1709293&view=diff
==============================================================================
--- zookeeper/trunk/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java (original)
+++ zookeeper/trunk/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java Sun Oct 18 17:00:41 2015
@@ -16,6 +16,8 @@
  */
 package org.apache.zookeeper.server.quorum;
 
+import static org.junit.Assert.assertEquals;
+
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
@@ -57,4 +59,28 @@ public class QuorumServerTest extends ZK
         qs = new QuorumServer(0, config);
         Assert.assertEquals("Use hostname", expected, qs.toString());
     }
+
+    @Test
+    public void constructionUnderstandsIpv6LiteralsInServerConfig() throws ConfigException {
+        String config = "[::1]:1234:1236:participant";
+        QuorumServer qs = new QuorumServer(0, config);
+        assertEquals("[0:0:0:0:0:0:0:1]:1234:1236:participant", qs.toString());
+    }
+
+    @Test
+    public void constructionUnderstandsIpv6LiteralsInClientConfig() throws ConfigException {
+        String config = "127.0.0.1:1234:1236:participant;[::1]:1237";
+        QuorumServer qs = new QuorumServer(0, config);
+        assertEquals("127.0.0.1:1234:1236:participant;[0:0:0:0:0:0:0:1]:1237", qs.toString());
+    }
+
+    @Test(expected = ConfigException.class)
+    public void unbalancedIpv6LiteralsInServerConfigFailToBeParsed() throws ConfigException {
+        new QuorumServer(0, "[::1:1234:1236:participant");
+    }
+
+    @Test(expected = ConfigException.class)
+    public void unbalancedIpv6LiteralsInClientConfigFailToBeParsed() throws ConfigException {
+        new QuorumServer(0, "127.0.0.1:1234:1236:participant;[::1:1237");
+    }
 }