zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cnaur...@apache.org
Subject svn commit: r1709292 - in /zookeeper/branches/branch-3.5: CHANGES.txt src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java
Date Sun, 18 Oct 2015 17:00:36 GMT
Author: cnauroth
Date: Sun Oct 18 17:00:36 2015
New Revision: 1709292

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

Modified:
    zookeeper/branches/branch-3.5/CHANGES.txt
    zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
    zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java

Modified: zookeeper/branches/branch-3.5/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/CHANGES.txt?rev=1709292&r1=1709291&r2=1709292&view=diff
==============================================================================
--- zookeeper/branches/branch-3.5/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.5/CHANGES.txt Sun Oct 18 17:00:36 2015
@@ -30,6 +30,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-2270: Allow MBeanRegistry to be overridden for better unit tests

Modified: zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java?rev=1709292&r1=1709291&r2=1709292&view=diff
==============================================================================
--- zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
(original)
+++ zookeeper/branches/branch-3.5/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
Sun Oct 18 17:00:36 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/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java?rev=1709292&r1=1709291&r2=1709292&view=diff
==============================================================================
--- zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java
(original)
+++ zookeeper/branches/branch-3.5/src/java/test/org/apache/zookeeper/server/quorum/QuorumServerTest.java
Sun Oct 18 17:00:36 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");
+    }
 }



Mime
View raw message