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 2016/08/11 16:13:04 UTC

svn commit: r1756005 - in /zookeeper/branches/branch-3.4: ./ src/java/main/org/apache/zookeeper/server/quorum/ src/java/test/org/apache/zookeeper/ src/java/test/org/apache/zookeeper/server/quorum/ src/java/test/org/apache/zookeeper/test/

Author: cnauroth
Date: Thu Aug 11 16:13:04 2016
New Revision: 1756005

URL: http://svn.apache.org/viewvc?rev=1756005&view=rev
Log:
ZOOKEEPER-2452: Back-port ZOOKEEPER-1460 to 3.4 for IPv6 literal address support. (Abraham Fine via cnauroth)

Modified:
    zookeeper/branches/branch-3.4/CHANGES.txt
    zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZKTestCase.java
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientBase.java
    zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientTest.java

Modified: zookeeper/branches/branch-3.4/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/CHANGES.txt?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.4/CHANGES.txt Thu Aug 11 16:13:04 2016
@@ -47,6 +47,9 @@ BUGFIXES:
 
   Fix command handling in the C client shell (phunt via fpj)
 
+  ZOOKEEPER-2452: Back-port ZOOKEEPER-1460 to 3.4 for IPv6 literal
+  address support. (Abraham Fine via cnauroth)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-2240 Make the three-node minimum more explicit in 

Modified: zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java (original)
+++ zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java Thu Aug 11 16:13:04 2016
@@ -92,6 +92,27 @@ public class QuorumPeerConfig {
         }
     }
 
+    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(":");
+        }
+    }
+
     /**
      * Parse a ZooKeeper configuration file
      * @param path the patch of the configuration file
@@ -179,7 +200,7 @@ public class QuorumPeerConfig {
             } else if (key.startsWith("server.")) {
                 int dot = key.indexOf('.');
                 long sid = Long.parseLong(key.substring(dot + 1));
-                String parts[] = value.split(":");
+                String parts[] = splitWithLeadingHostname(value);
                 if ((parts.length != 2) && (parts.length != 3) && (parts.length !=4)) {
                     LOG.error(value
                        + " does not have the form host:port or host:port:port " +

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZKTestCase.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZKTestCase.java?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZKTestCase.java (original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZKTestCase.java Thu Aug 11 16:13:04 2016
@@ -18,6 +18,7 @@
 
 package org.apache.zookeeper;
 
+import org.junit.Assume;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.junit.Rule;
@@ -26,6 +27,10 @@ import org.junit.rules.TestWatchman;
 import org.junit.runner.RunWith;
 import org.junit.runners.model.FrameworkMethod;
 
+import java.io.IOException;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+
 /**
  * Base class for a non-parameterized ZK test.
  *
@@ -67,4 +72,13 @@ public class ZKTestCase {
 
     };
 
+    protected void assumeIPv6Available() {
+        try {
+            InetAddress address = Inet6Address.getByName("0:0:0:0:0:0:0:1");
+            Assume.assumeTrue(address.isReachable(1000));
+        } catch (IOException exception) {
+            Assume.assumeTrue(false);
+        }
+    }
+
 }

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java (original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java Thu Aug 11 16:13:04 2016
@@ -458,6 +458,135 @@ public class QuorumPeerMainTest extends
         Assert.assertTrue("complains about host", found);
     }
 
+    @Test
+    public void testValidIpv6AddressInQuorum() throws Exception {
+        assumeIPv6Available();
+
+        ClientBase.setupTestEnv();
+
+        // setup the logger to capture all logs
+        Layout layout =
+                Logger.getRootLogger().getAppender("CONSOLE").getLayout();
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        WriterAppender appender = new WriterAppender(layout, os);
+        appender.setImmediateFlush(true);
+        appender.setThreshold(Level.INFO);
+        Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
+        qlogger.addAppender(appender);
+
+        try {
+            final int CLIENT_PORT_QP1 = PortAssignment.unique();
+            final int CLIENT_PORT_QP2 = PortAssignment.unique();
+
+            String quorumCfgSection =
+                    "server.1=127.0.0.1:" + PortAssignment.unique()
+                    + ":" + PortAssignment.unique()
+                    + "\nserver.2=[0:0:0:0:0:0:0:1]:" + PortAssignment.unique()
+                    + ":" + PortAssignment.unique();
+
+            MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
+            MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
+
+            q1.start();
+            q2.start();
+
+            Assert.assertTrue("waiting for server 1 being up",
+                    ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
+                            ClientBase.CONNECTION_TIMEOUT));
+
+            Assert.assertTrue("waiting for server 2 being up",
+                    ClientBase.waitForServerUp("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1,
+                            ClientBase.CONNECTION_TIMEOUT));
+
+            q1.shutdown();
+            q2.shutdown();
+
+            Assert.assertTrue("waiting for server 1 down",
+                    ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
+                            ClientBase.CONNECTION_TIMEOUT));
+
+            Assert.assertTrue("waiting for server 2 down",
+                    ClientBase.waitForServerDown("[0:0:0:0:0:0:0:1]:" + CLIENT_PORT_QP1,
+                            ClientBase.CONNECTION_TIMEOUT));
+
+        } finally {
+            qlogger.removeAppender(appender);
+        }
+
+        os.close();
+        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
+        String line;
+        boolean found = false;
+        Pattern p =
+                Pattern.compile(".*Resolved hostname: 0:0:0:0:0:0:0:1.*");
+        while ((line = r.readLine()) != null) {
+            found = p.matcher(line).matches();
+            if (found) {
+                break;
+            }
+        }
+        Assert.assertTrue("IPv6 address resolved", found);
+    }
+
+    @Test
+    public void testInvalidIpv6AddressInQuorum() throws Exception {
+        assumeIPv6Available();
+
+        ClientBase.setupTestEnv();
+
+        // setup the logger to capture all logs
+        Layout layout =
+                Logger.getRootLogger().getAppender("CONSOLE").getLayout();
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        WriterAppender appender = new WriterAppender(layout, os);
+        appender.setImmediateFlush(true);
+        appender.setThreshold(Level.INFO);
+        Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum");
+        qlogger.addAppender(appender);
+
+        try {
+            final int CLIENT_PORT_QP1 = PortAssignment.unique();
+
+            String quorumCfgSection =
+                    "server.1=127.0.0.1:" + PortAssignment.unique()
+                    + ":" + PortAssignment.unique()
+                    + "\nserver.2=[0:0:0:0:0:0:0:1:" + PortAssignment.unique()
+                    + ":" + PortAssignment.unique();
+
+            MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
+            q1.start();
+
+            boolean isup =
+                    ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1,
+                            30000);
+
+            Assert.assertFalse("Server never came up", isup);
+
+            q1.shutdown();
+
+            Assert.assertTrue("waiting for server 1 down",
+                    ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1,
+                            ClientBase.CONNECTION_TIMEOUT));
+
+        } finally {
+            qlogger.removeAppender(appender);
+        }
+
+        os.close();
+        LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
+        String line;
+        boolean found = false;
+        Pattern p =
+                Pattern.compile(".*QuorumPeerConfig\\$ConfigException.*");
+        while ((line = r.readLine()) != null) {
+            found = p.matcher(line).matches();
+            if (found) {
+                break;
+            }
+        }
+        Assert.assertTrue("complains about configuration", found);
+    }
+
     /**
      * Verify handling of inconsistent peer type
      */

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientBase.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientBase.java?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientBase.java (original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientBase.java Thu Aug 11 16:13:04 2016
@@ -71,7 +71,9 @@ public abstract class ClientBase extends
     static final File BASETEST =
         new File(System.getProperty("build.test.dir", "build"));
 
-    protected String hostPort = "127.0.0.1:" + PortAssignment.unique();
+    protected int port = PortAssignment.unique();
+    protected String hostPort = "127.0.0.1:" + port;
+    protected String ipv6HostPort = "[0:0:0:0:0:0:0:1]:" + port;
     protected int maxCnxns = 0;
     protected ServerCnxnFactory serverFactory = null;
     protected File tmpDir = null;

Modified: zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientTest.java
URL: http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientTest.java?rev=1756005&r1=1756004&r2=1756005&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientTest.java (original)
+++ zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/test/ClientTest.java Thu Aug 11 16:13:04 2016
@@ -93,14 +93,22 @@ public class ClientTest extends ClientBa
     public void testClientwithoutWatcherObj() throws IOException,
             InterruptedException, KeeperException
     {
-        performClientTest(false);
+        performClientTest(false, hostPort);
     }
 
     @Test
     public void testClientWithWatcherObj() throws IOException,
             InterruptedException, KeeperException
     {
-        performClientTest(true);
+        performClientTest(true, hostPort);
+    }
+
+    @Test
+    public void testClientWithIPv6Address() throws IOException,
+            InterruptedException, KeeperException
+    {
+        assumeIPv6Available();
+        performClientTest(true, ipv6HostPort);
     }
 
     /** Exercise the testable functions, verify tostring, etc... */
@@ -310,7 +318,7 @@ public class ClientTest extends ClientBa
         }
     }
 
-    private void performClientTest(boolean withWatcherObj)
+    private void performClientTest(boolean withWatcherObj, String hostPort)
         throws IOException, InterruptedException, KeeperException
     {
         ZooKeeper zk = null;