You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by br...@apache.org on 2009/11/18 02:03:32 UTC
svn commit: r881641 - in /hadoop/zookeeper/branches/branch-3.2: ./
src/java/main/org/apache/zookeeper/server/quorum/
src/java/test/org/apache/zookeeper/
src/java/test/org/apache/zookeeper/server/quorum/
Author: breed
Date: Wed Nov 18 01:03:32 2009
New Revision: 881641
URL: http://svn.apache.org/viewvc?rev=881641&view=rev
Log:
ZOOKEEPER-547. Sanity check in QuorumCnxn Manager and quorum communication port.
Added:
hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/PortAssignment.java
Modified:
hadoop/zookeeper/branches/branch-3.2/CHANGES.txt
hadoop/zookeeper/branches/branch-3.2/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java
hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java
Modified: hadoop/zookeeper/branches/branch-3.2/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/zookeeper/branches/branch-3.2/CHANGES.txt?rev=881641&r1=881640&r2=881641&view=diff
==============================================================================
--- hadoop/zookeeper/branches/branch-3.2/CHANGES.txt (original)
+++ hadoop/zookeeper/branches/branch-3.2/CHANGES.txt Wed Nov 18 01:03:32 2009
@@ -70,6 +70,9 @@
ZOOKEEPER-562. c client can flood server with pings if tcp send queue
filled (ben reed via mahadev)
+ ZOOKEEPER-547. Sanity check in QuorumCnxn Manager and quorum communication port.
+ (mahadev via breed)
+
IMPROVEMENTS:
NEW FEATURES:
Modified: hadoop/zookeeper/branches/branch-3.2/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java
URL: http://svn.apache.org/viewvc/hadoop/zookeeper/branches/branch-3.2/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java?rev=881641&r1=881640&r2=881641&view=diff
==============================================================================
--- hadoop/zookeeper/branches/branch-3.2/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java (original)
+++ hadoop/zookeeper/branches/branch-3.2/src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java Wed Nov 18 01:03:32 2009
@@ -59,7 +59,7 @@
*/
static final int CAPACITY = 100;
-
+ static final int PACKETMAXSIZE = 1024 * 1024;
/*
* Maximum number of attempts to connect to a peer
*/
@@ -597,11 +597,13 @@
}
msgLength.position(0);
int length = msgLength.getInt();
-
/**
* Allocates a new ByteBuffer to receive the message
*/
if (length > 0) {
+ if (length > PACKETMAXSIZE) {
+ throw new IOException("Invalid packet of length " + length);
+ }
byte[] msgArray = new byte[length];
ByteBuffer message = ByteBuffer.wrap(msgArray);
int numbytes = 0;
Added: hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/PortAssignment.java
URL: http://svn.apache.org/viewvc/hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/PortAssignment.java?rev=881641&view=auto
==============================================================================
--- hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/PortAssignment.java (added)
+++ hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/PortAssignment.java Wed Nov 18 01:03:32 2009
@@ -0,0 +1,34 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper;
+
+import org.apache.log4j.Logger;
+
+/** Assign ports to tests */
+public class PortAssignment {
+ private static final Logger LOG = Logger.getLogger(PortAssignment.class);
+
+ private static int nextPort = 11221;
+
+ /** Assign a new, unique port to the test */
+ public synchronized static int unique() {
+ LOG.info("assigning port " + nextPort);
+ return nextPort++;
+ }
+}
Modified: hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java
URL: http://svn.apache.org/viewvc/hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java?rev=881641&r1=881640&r2=881641&view=diff
==============================================================================
--- hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java (original)
+++ hadoop/zookeeper/branches/branch-3.2/src/java/test/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java Wed Nov 18 01:03:32 2009
@@ -22,10 +22,16 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.StringReader;
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
import java.util.regex.Pattern;
import junit.framework.TestCase;
@@ -35,11 +41,13 @@
import org.apache.log4j.Logger;
import org.apache.log4j.WriterAppender;
import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.PortAssignment;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.test.ClientBase;
+import org.apache.zookeeper.test.ClientTest;
import org.junit.Test;
@@ -227,6 +235,60 @@
assertTrue("complains about host", found);
}
+ /**
+ * verify if bad packets are being handled properly
+ * at the quorum port
+ * @throws Exception
+ */
+ public void testBadPackets() throws Exception {
+ LOG.info("STARTING " + getName());
+ ClientBase.setupTestEnv();
+ final int CLIENT_PORT_QP1 = PortAssignment.unique();
+ final int CLIENT_PORT_QP2 = PortAssignment.unique();
+ int electionPort1 = PortAssignment.unique();
+ int electionPort2 = PortAssignment.unique();
+ String quorumCfgSection =
+ "server.1=localhost:" + PortAssignment.unique()
+ + ":" + electionPort1
+ + "\nserver.2=localhost:" + PortAssignment.unique()
+ + ":" + electionPort2;
+
+ MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
+ MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
+ q1.start();
+ q2.start();
+
+ assertTrue("waiting for server 1 being up",
+ ClientBase.waitForServerUp("localhost:" + CLIENT_PORT_QP1,
+ CONNECTION_TIMEOUT));
+ assertTrue("waiting for server 2 being up",
+ ClientBase.waitForServerUp("localhost:" + CLIENT_PORT_QP2,
+ CONNECTION_TIMEOUT));
+
+ byte[] b = new byte[4];
+ int length = 1024*1024*1024;
+ ByteBuffer buff = ByteBuffer.wrap(b);
+ buff.putInt(length);
+ buff.position(0);
+ SocketChannel s = SocketChannel.open(new InetSocketAddress("localhost", electionPort1));
+ s.write(buff);
+ s.close();
+ buff.position(0);
+ s = SocketChannel.open(new InetSocketAddress("localhost", electionPort2));
+ s.write(buff);
+ s.close();
+
+ ZooKeeper zk = new ZooKeeper("localhost:" + CLIENT_PORT_QP1,
+ ClientBase.CONNECTION_TIMEOUT, this);
+
+ zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE,
+ CreateMode.PERSISTENT);
+ assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1");
+ zk.close();
+ q1.shutdown();
+ q2.shutdown();
+ }
+
/**
* Verify handling of quorum defaults
@@ -242,7 +304,8 @@
Logger.getRootLogger().getAppender("CONSOLE").getLayout();
ByteArrayOutputStream os = new ByteArrayOutputStream();
WriterAppender appender = new WriterAppender(layout, os);
- appender.setThreshold(Level.WARN);
+ appender.setImmediateFlush(true);
+ appender.setThreshold(Level.INFO);
Logger zlogger = Logger.getLogger("org.apache.zookeeper");
zlogger.addAppender(appender);
@@ -281,7 +344,7 @@
} finally {
zlogger.removeAppender(appender);
}
-
+ os.close();
LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
String line;
boolean found = false;