Ver código fonte

ZOOKEEPER-3237: Allow IPv6 wildcard address in peer config

Author: Brian Nixon <nixon@fb.com>

Reviewers: eolivelli@apache.org, andor@apache.org

Closes #761 from enixon/wildcard and squashes the following commits:

c2f12462e [Brian Nixon] refactor QuorumServerTest for greater clarity
311cc1732 [Brian Nixon] move new ServerDuplicateTest tests to QuorumServerTest
1aa517a45 [Brian Nixon] replace use of ReconfigTest with a new class ServerDuplicateTest
5e00c03f1 [Brian Nixon] add a slight delay to remove flakiness
82c73bae9 [Brian Nixon] add ReconfigTest::testWildcards
c483c948f [Brian Nixon] ZOOKEEPER-3237: Allow IPv6 wildcard address in peer config
Brian Nixon 6 anos atrás
pai
commit
7ca06c2cbb

+ 2 - 3
zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java

@@ -390,8 +390,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
         }
 
         private List<InetSocketAddress> excludedSpecialAddresses(List<InetSocketAddress> addrs) {
-            List<InetSocketAddress> included = new ArrayList<InetSocketAddress>();
-            InetAddress wcAddr = new InetSocketAddress(0).getAddress();
+            List<InetSocketAddress> included = new ArrayList<>();
 
             for (InetSocketAddress addr : addrs) {
                 if (addr == null) {
@@ -400,7 +399,7 @@ public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider
                 InetAddress inetaddr = addr.getAddress();
 
                 if (inetaddr == null ||
-                    inetaddr.equals(wcAddr) || // wildCard address(0.0.0.0)
+                    inetaddr.isAnyLocalAddress() || // wildCard addresses (0.0.0.0 or [::])
                     inetaddr.isLoopbackAddress()) { // loopback address(localhost/127.0.0.1)
                     continue;
                 }

+ 66 - 20
zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java

@@ -18,45 +18,52 @@ package org.apache.zookeeper.server.quorum;
 
 import static org.junit.Assert.assertEquals;
 
+import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
 import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.net.InetSocketAddress;
+
 public class QuorumServerTest extends ZKTestCase {
+    private String ipv6n1 = "[2500:0:0:0:0:0:1:0]";
+    private String ipv6n2 = "[2600:0:0:0:0:0:1:0]";
+    private String ipv4config = "127.0.0.1:1234:1236";
+
     @Test
     public void testToString() throws ConfigException {
-        String config = "127.0.0.1:1234:1236:participant;0.0.0.0:1237";
-        String expected = "127.0.0.1:1234:1236:participant;0.0.0.0:1237";
-        QuorumServer qs = new QuorumServer(0, config);
+        String provided = ipv4config + ":participant;0.0.0.0:1237";
+        String expected = ipv4config + ":participant;0.0.0.0:1237";
+        QuorumServer qs = new QuorumServer(0, provided);
         Assert.assertEquals("Use IP address", expected, qs.toString());
 
-        config = "127.0.0.1:1234:1236;0.0.0.0:1237";
-        expected = "127.0.0.1:1234:1236:participant;0.0.0.0:1237";
-        qs = new QuorumServer(0, config);
+        provided = ipv4config + ";0.0.0.0:1237";
+        expected = ipv4config + ":participant;0.0.0.0:1237";
+        qs = new QuorumServer(0, provided);
         Assert.assertEquals("Type unspecified", expected, qs.toString());
 
-        config = "127.0.0.1:1234:1236:observer;0.0.0.0:1237";
-        expected = "127.0.0.1:1234:1236:observer;0.0.0.0:1237";
-        qs = new QuorumServer(0, config);
+        provided = ipv4config + ":observer;0.0.0.0:1237";
+        expected = ipv4config + ":observer;0.0.0.0:1237";
+        qs = new QuorumServer(0, provided);
         Assert.assertEquals("Observer type", expected, qs.toString());
 
-        config = "127.0.0.1:1234:1236:participant;1237";
-        expected = "127.0.0.1:1234:1236:participant;0.0.0.0:1237";
-        qs = new QuorumServer(0, config);
+        provided = ipv4config + ":participant;1237";
+        expected = ipv4config + ":participant;0.0.0.0:1237";
+        qs = new QuorumServer(0, provided);
         Assert.assertEquals("Client address unspecified",
                             expected, qs.toString());
 
-        config = "127.0.0.1:1234:1236:participant;1.2.3.4:1237";
-        expected = "127.0.0.1:1234:1236:participant;1.2.3.4:1237";
-        qs = new QuorumServer(0, config);
+        provided = ipv4config + ":participant;1.2.3.4:1237";
+        expected = ipv4config + ":participant;1.2.3.4:1237";
+        qs = new QuorumServer(0, provided);
         Assert.assertEquals("Client address specified",
                             expected, qs.toString());
 
-        config = "example.com:1234:1236:participant;1237";
+        provided = "example.com:1234:1236:participant;1237";
         expected = "example.com:1234:1236:participant;0.0.0.0:1237";
-        qs = new QuorumServer(0, config);
+        qs = new QuorumServer(0, provided);
         Assert.assertEquals("Use hostname", expected, qs.toString());
     }
 
@@ -69,9 +76,9 @@ public class QuorumServerTest extends ZKTestCase {
 
     @Test
     public void constructionUnderstandsIpv6LiteralsInClientConfig() throws ConfigException {
-        String config = "127.0.0.1:1234:1236:participant;[::1]:1237";
+        String config = ipv4config + ":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());
+        assertEquals(ipv4config + ":participant;[0:0:0:0:0:0:0:1]:1237", qs.toString());
     }
 
     @Test(expected = ConfigException.class)
@@ -81,6 +88,45 @@ public class QuorumServerTest extends ZKTestCase {
 
     @Test(expected = ConfigException.class)
     public void unbalancedIpv6LiteralsInClientConfigFailToBeParsed() throws ConfigException {
-        new QuorumServer(0, "127.0.0.1:1234:1236:participant;[::1:1237");
+        new QuorumServer(0, ipv4config + ":participant;[::1:1237");
+    }
+
+    @Test
+    public void testWildcard() throws KeeperException.BadArgumentsException {
+        String[] addrs = new String[]{"127.0.0.1", "[0:0:0:0:0:0:0:1]", "0.0.0.0", "[::]"};
+        for (int i = 0; i < addrs.length; i++) {
+            for (int j = i; j < addrs.length; j++) {
+                QuorumPeer.QuorumServer server1 =
+                        new QuorumPeer.QuorumServer(1,
+                                new InetSocketAddress(ipv6n1, 1234), // peer
+                                new InetSocketAddress(ipv6n1, 1236), // election
+                                new InetSocketAddress(addrs[i], 1237)  // client
+                        );
+                QuorumPeer.QuorumServer server2 =
+                        new QuorumPeer.QuorumServer(2,
+                                new InetSocketAddress(ipv6n2, 1234), // peer
+                                new InetSocketAddress(ipv6n2, 1236), // election
+                                new InetSocketAddress(addrs[j], 1237)  // client
+                        );
+                server1.checkAddressDuplicate(server2);
+            }
+        }
+    }
+
+    @Test(expected = KeeperException.BadArgumentsException.class)
+    public void testDuplicate() throws KeeperException.BadArgumentsException {
+        QuorumPeer.QuorumServer server1 =
+                new QuorumPeer.QuorumServer(1,
+                        new InetSocketAddress(ipv6n1, 1234), // peer
+                        new InetSocketAddress(ipv6n1, 1236), // election
+                        new InetSocketAddress(ipv6n1, 1237)  // client
+                );
+        QuorumPeer.QuorumServer server2 =
+                new QuorumPeer.QuorumServer(2,
+                        new InetSocketAddress(ipv6n2, 1234), // peer
+                        new InetSocketAddress(ipv6n2, 1236), // election
+                        new InetSocketAddress(ipv6n1, 1237)  // client
+                );
+        server1.checkAddressDuplicate(server2);
     }
 }