瀏覽代碼

reverting ZOOKEEPER-408 patch since it breaks the build

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@781266 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 16 年之前
父節點
當前提交
016f8c2ab7
共有 24 個文件被更改,包括 341 次插入461 次删除
  1. 2 2
      src/java/main/org/apache/zookeeper/ClientCnxn.java
  2. 2 5
      src/java/main/org/apache/zookeeper/ZooKeeperMain.java
  3. 5 7
      src/java/main/org/apache/zookeeper/server/DataNode.java
  4. 6 42
      src/java/main/org/apache/zookeeper/server/DataTree.java
  5. 2 10
      src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java
  6. 3 10
      src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java
  7. 1 1
      src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.java
  8. 83 89
      src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
  9. 3 9
      src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
  10. 2 17
      src/java/main/org/apache/zookeeper/server/persistence/Util.java
  11. 1 5
      src/java/main/org/apache/zookeeper/server/quorum/FollowerHandler.java
  12. 1 2
      src/java/main/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java
  13. 1 7
      src/java/main/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java
  14. 2 7
      src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java
  15. 1 5
      src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java
  16. 0 39
      src/java/test/config/findbugsExcludeFile.xml
  17. 15 15
      src/java/test/org/apache/zookeeper/test/ACLTest.java
  18. 9 9
      src/java/test/org/apache/zookeeper/test/AsyncOpsTest.java
  19. 23 22
      src/java/test/org/apache/zookeeper/test/ClientBase.java
  20. 63 62
      src/java/test/org/apache/zookeeper/test/FLENewEpochTest.java
  21. 58 55
      src/java/test/org/apache/zookeeper/test/FLETest.java
  22. 46 32
      src/java/test/org/apache/zookeeper/test/HierarchicalQuorumTest.java
  23. 4 4
      src/java/test/org/apache/zookeeper/test/LETest.java
  24. 8 5
      src/java/test/org/apache/zookeeper/test/QuorumBase.java

+ 2 - 2
src/java/main/org/apache/zookeeper/ClientCnxn.java

@@ -80,9 +80,9 @@ public class ClientCnxn {
      * Clients automatically reset watches during session reconnect, this
      * option allows the client to turn off this behavior by setting
      * the environment variable "zookeeper.disableAutoWatchReset" to "true" */
-    public static final boolean disableAutoWatchReset;
+    public static boolean disableAutoWatchReset;
    
-    public static final int packetLen;
+    public static int packetLen;
     static {
         // this var should not be public, but otw there is no easy way 
         // to test

+ 2 - 5
src/java/main/org/apache/zookeeper/ZooKeeperMain.java

@@ -46,7 +46,7 @@ import org.apache.zookeeper.data.Stat;
  */
 public class ZooKeeperMain {
     private static final Logger LOG = Logger.getLogger(ZooKeeperMain.class);
-    protected static final Map<String,String> commandMap = new HashMap<String,String>( );
+    protected static Map<String,String> commandMap = new HashMap<String,String>( );
 
     protected MyCommandOptions cl = new MyCommandOptions();
     protected HashMap<Integer,String> history = new HashMap<Integer,String>( );
@@ -522,11 +522,8 @@ public class ZooKeeperMain {
         // now create the direct children
         // and the stat and quota nodes
         String[] splits = path.split("/");
-        StringBuffer sb = new StringBuffer();
-        sb.append(quotaPath);
         for (int i=1; i<splits.length; i++) {
-            sb.append("/" + splits[i]);
-            quotaPath = sb.toString();
+            quotaPath = quotaPath + "/" + splits[i];
             try {
                 zk.create(quotaPath, null, Ids.OPEN_ACL_UNSAFE ,
                         CreateMode.PERSISTENT);

+ 5 - 7
src/java/main/org/apache/zookeeper/server/DataNode.java

@@ -53,9 +53,7 @@ public class DataNode implements Record {
     /** the list of children for this node. note 
      * that the list of children string does not 
      * contain the parent path -- just the last
-     * part of the path. This should be 
-     * synchronized on except deserializing 
-     * (for speed up issues).
+     * part of the path.
      */
     Set<String> children = new HashSet<String>();
 
@@ -85,7 +83,7 @@ public class DataNode implements Record {
      * fully
      * @param children
      */
-    public synchronized void setChildren(HashSet<String> children) {
+    public void setChildren(HashSet<String> children) {
         this.children = children;
     }
     
@@ -93,12 +91,12 @@ public class DataNode implements Record {
      * convenience methods to get the children
      * @return the children of this datanode
      */
-    public synchronized Set<String> getChildren() {
+    public Set<String> getChildren() {
         return this.children;
     }
     
    
-    synchronized public void copyStat(Stat to) {
+    public void copyStat(Stat to) {
         to.setAversion(stat.getAversion());
         to.setCtime(stat.getCtime());
         to.setCversion(stat.getCversion());
@@ -112,7 +110,7 @@ public class DataNode implements Record {
         to.setNumChildren(children.size());
     }
 
-    synchronized public void deserialize(InputArchive archive, String tag)
+    public void deserialize(InputArchive archive, String tag)
             throws IOException {
         archive.startRecord("node");
         data = archive.readBuffer("data");

+ 6 - 42
src/java/main/org/apache/zookeeper/server/DataTree.java

@@ -244,10 +244,8 @@ public class DataTree {
     public long approximateDataSize() {
         long result = 0;
         for (Map.Entry<String, DataNode> entry : nodes.entrySet()) {
-            synchronized(entry.getValue()) {
-                result += entry.getKey().length();
-                result += (entry.getValue().data == null? 0 : entry.getValue().data.length);
-            }
+            result += entry.getKey().length();
+            result += entry.getValue().data.length;
         }
         return result;
     }
@@ -337,11 +335,6 @@ public class DataTree {
         String statNode = Quotas.statPath(lastPrefix);
         DataNode node = nodes.get(statNode);
         StatsTrack updatedStat = null;
-        if (node == null) {
-            //should not happen
-            LOG.error("Missing count node for stat " + statNode);
-            return;
-        }
         synchronized(node) {
             updatedStat = new StatsTrack(new String(node.data));
             updatedStat.setCount(updatedStat.getCount() + diff);
@@ -351,11 +344,6 @@ public class DataTree {
         String quotaNode = Quotas.quotaPath(lastPrefix);
         node = nodes.get(quotaNode);
         StatsTrack thisStats = null;
-        if (node == null) {
-            //should not happen
-            LOG.error("Missing count node for quota " + quotaNode);
-            return;
-        }
         synchronized(node) {
             thisStats = new StatsTrack(new String(node.data));
         }
@@ -370,17 +358,10 @@ public class DataTree {
      * update the count of bytes of this stat datanode
      * @param lastPrefix the path of the node that is quotaed
      * @param diff the diff to added to number of bytes
-     * @throws IOException if path is not found
      */
-    public void updateBytes(String lastPrefix, long diff)  {
+    public void updateBytes(String lastPrefix, long diff) {
         String statNode = Quotas.statPath(lastPrefix);
         DataNode node = nodes.get(statNode);
-        if (node == null) {
-            //should never be null but just to make 
-            // findbugs happy
-            LOG.error("Missing stat node for bytes " + statNode);
-            return;
-        }
         StatsTrack updatedStat = null;
         synchronized(node) {
             updatedStat = new StatsTrack(new String(node.data));
@@ -390,12 +371,6 @@ public class DataTree {
         // now check if the bytes match the quota
         String quotaNode = Quotas.quotaPath(lastPrefix);
         node = nodes.get(quotaNode);
-        if (node == null) {
-            //should never be null but just to make
-            // findbugs happy
-            LOG.error("Missing quota node for bytes " + quotaNode);
-            return;
-        }
         StatsTrack thisStats = null;
         synchronized(node) {
             thisStats = new StatsTrack(new String(node.data));
@@ -535,11 +510,7 @@ public class DataTree {
         if (!rootZookeeper.equals(lastPrefix) && !("".equals(lastPrefix))) {
             // ok we have some match and need to update 
             updateCount(lastPrefix, -1);
-            int bytes = 0;
-            synchronized (node) {
-                bytes = (node.data == null? 0:-(node.data.length));
-            }
-            updateBytes(lastPrefix, bytes);
+            updateBytes(lastPrefix, node.data == null? 0:-(node.data.length));
         }
         ZooTrace.logTraceMessage(LOG,
                                  ZooTrace.EVENT_DELIVERY_TRACE_MASK,
@@ -560,9 +531,8 @@ public class DataTree {
         if (n == null) {
             throw new KeeperException.NoNodeException();
         }
-        byte lastdata[] = null;
+        byte lastdata[] = n.data;
         synchronized (n) {
-            lastdata = n.data;
             n.data = data;
             n.stat.setMtime(time);
             n.stat.setMzxid(zxid);
@@ -805,15 +775,13 @@ public class DataTree {
             return;
         }
         String[] children = null;
-        int len = 0;
         synchronized (node) {
             children = node.children.toArray(new
                     String[node.children.size()]);
-            len = (node.data == null? 0: node.data.length);
         }
         // add itself
         counts.count += 1;
-        counts.bytes += len;
+        counts.bytes += (long)node.data.length;
         if (children.length == 0) {
             return;
         }
@@ -991,10 +959,6 @@ public class DataTree {
             } else {
                 String parentPath = path.substring(0, lastSlash);
                 node.parent = nodes.get(parentPath);
-                if (node.parent == null) {
-                    throw new IOException("Invalid Datatree, unable to find " +
-                    		"parent " + parentPath + " of path " + path);
-                }
                 node.parent.children.add(path.substring(lastSlash + 1));
                 long eowner = node.stat.getEphemeralOwner();
                 if (eowner != 0) {

+ 2 - 10
src/java/main/org/apache/zookeeper/server/FinalRequestProcessor.java

@@ -192,11 +192,7 @@ public class FinalRequestProcessor implements RequestProcessor {
                 if (n == null) {
                     throw new KeeperException.NoNodeException();
                 }
-                Long aclL;
-                synchronized(n) {
-                    aclL = n.acl;
-                }
-                PrepRequestProcessor.checkACL(zks, zks.dataTree.convertLong(aclL),
+                PrepRequestProcessor.checkACL(zks, zks.dataTree.convertLong(n.acl),
                         ZooDefs.Perms.READ,
                         request.authInfo);
                 stat = new Stat();
@@ -233,11 +229,7 @@ public class FinalRequestProcessor implements RequestProcessor {
                 if (n == null) {
                     throw new KeeperException.NoNodeException();
                 }
-                Long aclG;
-                synchronized(n) {
-                    aclG = n.acl;
-                }
-                PrepRequestProcessor.checkACL(zks, zks.dataTree.convertLong(aclG), 
+                PrepRequestProcessor.checkACL(zks, zks.dataTree.convertLong(n.acl), 
                         ZooDefs.Perms.READ,
                         request.authInfo);
                 List<String> children = zks.dataTree.getChildren(

+ 3 - 10
src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java

@@ -23,7 +23,6 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.jute.Record;
@@ -74,7 +73,7 @@ public class PrepRequestProcessor extends Thread implements RequestProcessor {
      * this is only for testing purposes.
      * should never be useed otherwise
      */
-    public static final boolean failCreate = false;
+    public static boolean failCreate = false;
     
     LinkedBlockingQueue<Request> submittedRequests = new LinkedBlockingQueue<Request>();
 
@@ -125,14 +124,8 @@ public class PrepRequestProcessor extends Thread implements RequestProcessor {
             if (lastChange == null) {
                 DataNode n = zks.dataTree.getNode(path);
                 if (n != null) {
-                    Long acl;
-                    Set<String> children;
-                    synchronized(n) {
-                        acl = n.acl;
-                        children = n.children;
-                    }
-                    lastChange = new ChangeRecord(-1, path, n.stat, children
-                            .size(), zks.dataTree.convertLong(acl));
+                    lastChange = new ChangeRecord(-1, path, n.stat, n.children
+                            .size(), zks.dataTree.convertLong(n.acl));
                 }
             }
         }

+ 1 - 1
src/java/main/org/apache/zookeeper/server/SyncRequestProcessor.java

@@ -51,7 +51,7 @@ public class SyncRequestProcessor extends Thread implements RequestProcessor {
     /**
      * The number of log entries to log before starting a snapshot
      */
-    public static final int snapCount = ZooKeeperServer.getSnapCount();
+    public static int snapCount = ZooKeeperServer.getSnapCount();
 
     private Request requestOfDeath = Request.requestOfDeath;
 

+ 83 - 89
src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java

@@ -49,44 +49,25 @@ import org.apache.zookeeper.txn.TxnHeader;
  *
  */
 public class FileTxnLog implements TxnLog {
-    private static final Logger LOG;
-
-    static long preAllocSize =  65536 * 1024;
-
-    public final static int TXNLOG_MAGIC =
-        ByteBuffer.wrap("ZKLG".getBytes()).getInt();
-
-    public final static int VERSION = 2;
-
-    static {
-        LOG = Logger.getLogger(FileTxnLog.class);
-
-        forceSync =
-            !System.getProperty("zookeeper.forceSync", "yes").equals("no");
-
-        String size = System.getProperty("zookeeper.preAllocSize");
-        if (size != null) {
-            try {
-                preAllocSize = Long.parseLong(size) * 1024;
-            } catch (NumberFormatException e) {
-                LOG.warn(size + " is not a valid value for preAllocSize");
-            }
-        }
-    }
-
     long lastZxidSeen;
     volatile BufferedOutputStream logStream = null;
     volatile OutputArchive oa;
     volatile FileOutputStream fos = null;
-
+    
+    
     File logDir;
-    private static boolean forceSync = true;
+    public final static int TXNLOG_MAGIC =
+        ByteBuffer.wrap("ZKLG".getBytes()).getInt();
+    public final static int VERSION = 2;
+    private boolean forceSync = true;
     long dbId;
-    private LinkedList<FileOutputStream> streamsToFlush =
-        new LinkedList<FileOutputStream>();
+    private LinkedList<FileOutputStream> streamsToFlush = new LinkedList<FileOutputStream>();
+    static long preAllocSize =  65536 * 1024; 
     long currentSize;
     File logFileWrite = null;
-
+    
+    private static final Logger LOG = Logger.getLogger(FileTxnLog.class);
+  
     /**
      * constructor for FileTxnLog. Take the directory
      * where the txnlogs are stored
@@ -94,17 +75,27 @@ public class FileTxnLog implements TxnLog {
      */
     public FileTxnLog(File logDir) {
         this.logDir = logDir;
+        forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals(
+            "no");
+        String size = System.getProperty("zookeeper.preAllocSize");
+        if (size != null) {
+            try {
+                preAllocSize = Long.parseLong(size) * 1024;
+            } catch (NumberFormatException e) {
+                LOG.warn(size + " is not a valid value for preAllocSize");
+            }
+        }
     }
-
+    
     /**
      * method to allow setting preallocate size
      * of log file to pad the file.
-     * @param size the size to set to in bytes
+     * @param size the size to set to
      */
     public static void setPreallocSize(long size) {
         preAllocSize = size;
     }
-
+    
     /**
      * creates a checksum alogrithm to be used
      * @return the checksum used for this txnlog
@@ -116,7 +107,7 @@ public class FileTxnLog implements TxnLog {
 
     /**
      * rollover the current log file to a new one.
-     * @throws IOException
+     * @throws IOException 
      */
     public void rollLog() throws IOException {
         if (logStream != null) {
@@ -131,7 +122,7 @@ public class FileTxnLog implements TxnLog {
      * @param hdr the header of the transaction
      * @param txn the transaction part of the entry
      */
-    public synchronized void append(TxnHeader hdr, Record txn)
+    public synchronized void append(TxnHeader hdr, Record txn) 
         throws IOException {
         if (hdr != null) {
             if (hdr.getZxid() <= lastZxidSeen) {
@@ -140,7 +131,7 @@ public class FileTxnLog implements TxnLog {
                         + hdr.getType());
             }
             if (logStream==null) {
-               logFileWrite = new File(logDir, ("log." +
+               logFileWrite = new File(logDir, ("log." + 
                        Long.toHexString(hdr.getZxid())));
                fos = new FileOutputStream(logFileWrite);
                logStream=new BufferedOutputStream(fos);
@@ -154,15 +145,15 @@ public class FileTxnLog implements TxnLog {
             byte[] buf = Util.marshallTxnEntry(hdr, txn);
             if (buf == null || buf.length == 0) {
                 throw new IOException("Faulty serialization for header " +
-                        "and txn");
+                		"and txn");
             }
             Checksum crc = makeChecksumAlgorithm();
             crc.update(buf, 0, buf.length);
             oa.writeLong(crc.getValue(), "txnEntryCRC");
             Util.writeTxnBytes(oa, buf);
-        }
+        }    
     }
-
+    
     /**
      * pad the current file to increase its size
      * @param out the outputstream to be padded
@@ -171,7 +162,7 @@ public class FileTxnLog implements TxnLog {
     private void padFile(FileOutputStream out) throws IOException {
         currentSize = Util.padLogFile(out, currentSize, preAllocSize);
     }
-
+    
     /**
      * Find the log file that starts at, or just before, the snapshot. Return
      * this and all subsequent logs. Results are ordered by zxid of file,
@@ -190,7 +181,7 @@ public class FileTxnLog implements TxnLog {
             if (fzxid > snapshotZxid) {
                 continue;
             }
-            // the files
+            // the files 
             // are sorted with zxid's
             if (fzxid > logZxid) {
                 logZxid = fzxid;
@@ -205,9 +196,9 @@ public class FileTxnLog implements TxnLog {
             v.add(f);
         }
         return v.toArray(new File[0]);
-
+    
     }
-
+    
     /**
      * get the last zxid that was logged in the transaction logs
      * @return the last zxid logged in the transaction logs
@@ -216,10 +207,11 @@ public class FileTxnLog implements TxnLog {
         File[] files = getLogFiles(logDir.listFiles(), 0);
         long maxLog=files.length>0?
                 Util.getZxidFromName(files[files.length-1].getName(),"log"):-1;
-
-        // if a log file is more recent we must scan it to find
+        
+        // if a log file is more recent we must scan it to find 
         // the highest zxid
         long zxid = maxLog;
+        FileOutputStream logStream = null;
         try {
             FileTxnLog txn = new FileTxnLog(logDir);
             TxnIterator itr = txn.read(maxLog);
@@ -231,12 +223,17 @@ public class FileTxnLog implements TxnLog {
             }
         } catch (IOException e) {
             LOG.warn("Unexpected exception", e);
+        } finally {
+            if (logStream != null)
+                try {
+                    logStream.close();
+                } catch(IOException io){}
         }
         return zxid;
     }
-
+    
     /**
-     * commit the logs. make sure that evertyhing hits the
+     * commit the logs. make sure that evertyhing hits the 
      * disk
      */
     public synchronized void commit() throws IOException {
@@ -253,7 +250,7 @@ public class FileTxnLog implements TxnLog {
             streamsToFlush.removeFirst().close();
         }
     }
-
+    
     /**
      * start reading all the transactions from the given zxid
      * @param zxid the zxid to start reading transactions from
@@ -262,8 +259,8 @@ public class FileTxnLog implements TxnLog {
      */
     public TxnIterator read(long zxid) throws IOException {
         return new FileTxnIterator(logDir, zxid);
-    }
-
+    }   
+    
     /**
      * truncate the current transaction logs
      * @param zxid the zxid to truncate the logs to
@@ -278,13 +275,11 @@ public class FileTxnLog implements TxnLog {
         raf.setLength(pos);
         raf.close();
         while(itr.goToNextLog()) {
-            if (!itr.logFile.delete()) {
-                LOG.warn("Unable to truncate " + itr.logFile);
-            }
+            itr.logFile.delete();
         }
         return true;
     }
-
+    
     /**
      * read the header of the transaction file
      * @param file the transaction file to read
@@ -293,21 +288,20 @@ public class FileTxnLog implements TxnLog {
      */
     private static FileHeader readHeader(File file) throws IOException {
         InputStream is =null;
-        try {
+        try{
             is = new BufferedInputStream(new FileInputStream(file));
             InputArchive ia=BinaryInputArchive.getArchive(is);
             FileHeader hdr = new FileHeader();
             hdr.deserialize(ia, "fileheader");
             return hdr;
-         } finally {
-             try {
-                 if (is != null) is.close();
-             } catch (IOException e) {
-                 LOG.warn("Ignoring exception during close", e);
+         }finally{
+             try{
+                 if(is != null) is.close();
+             }catch(IOException e){
              }
-         }
+         }        
     }
-
+    
     /**
      * the dbid of this transaction database
      * @return the dbid of this database
@@ -320,10 +314,10 @@ public class FileTxnLog implements TxnLog {
             throw new IOException("Unsupported Format.");
         return fh.getDbid();
     }
-
+    
     /**
-     * this class implements the txnlog iterator interface
-     * which is used for reading the transaction logs
+     * this class implements the txnlog iterator interface 
+     * which is used for reading the transaction logs 
      */
     public static class FileTxnIterator implements TxnLog.TxnIterator {
         File logDir;
@@ -334,10 +328,10 @@ public class FileTxnLog implements TxnLog {
         InputArchive ia;
         static final String CRC_ERROR="CRC check failed";
         FileInputStream inputStream=null;
-        //stored files is the list of files greater than
+        //stored files is the list of files greater than 
         //the zxid we are looking for.
         private ArrayList<File> storedFiles;
-
+        
         /**
          * create an iterator over a transaction database directory
          * @param logDir the transaction database directory
@@ -349,10 +343,10 @@ public class FileTxnLog implements TxnLog {
           this.zxid = zxid;
           init();
         }
-
+        
         /**
          * initialize to the zxid specified
-         * this is inclusive of the zxid
+         * this is inclusive of the zxid 
          * @throws IOException
          */
         void init() throws IOException {
@@ -376,10 +370,10 @@ public class FileTxnLog implements TxnLog {
                     return;
             }
         }
-
+        
         /**
-         * go to the next logfile
-         * @return true if there is one and false if there is no
+         * go to the next logfile 
+         * @return true if there is one and false if there is no 
          * new file to be read
          * @throws IOException
          */
@@ -391,23 +385,23 @@ public class FileTxnLog implements TxnLog {
             }
             return false;
         }
-
+        
         /**
          * read the header fomr the inputarchive
          * @param ia the inputarchive to be read from
-         * @param is the inputstream
+         * @param is the inputstream 
          * @throws IOException
          */
-        protected void inStreamCreated(InputArchive ia, FileInputStream is)
+        protected void inStreamCreated(InputArchive ia, FileInputStream is) 
             throws IOException{
             FileHeader header= new FileHeader();
             header.deserialize(ia, "fileheader");
             if (header.getMagic() != FileTxnLog.TXNLOG_MAGIC) {
-                throw new IOException("Invalid magic number " + header.getMagic()
+                throw new IOException("Invalid magic number " + header.getMagic() 
                         + " != " + FileTxnLog.TXNLOG_MAGIC);
-            }
+            }  
         }
-
+        
         /**
          * Invoked to indicate that the input stream has been created.
          * @param ia input archive
@@ -424,15 +418,15 @@ public class FileTxnLog implements TxnLog {
             }
             return ia;
         }
-
+        
         /**
-         * create a checksum algorithm
+         * create a checksum algorithm 
          * @return the checksum algorithm
          */
         protected Checksum makeChecksumAlgorithm(){
             return new Adler32();
         }
-
+        
         /**
          * the iterator that moves to the next transaction
          * @return true if there is more transactions to be read
@@ -447,12 +441,12 @@ public class FileTxnLog implements TxnLog {
                 byte[] bytes = Util.readTxnBytes(ia);
                 // Since we preallocate, we define EOF to be an
                 if (bytes == null || bytes.length==0)
-                   throw new EOFException("Failed to read");
+                   throw new EOFException("Failed to read"); 
                 // EOF or corrupted record
                 // validate CRC
                 Checksum crc = makeChecksumAlgorithm();
                 crc.update(bytes, 0, bytes.length);
-                if (crcValue != crc.getValue())
+                if (crcValue != crc.getValue()) 
                     throw new IOException(CRC_ERROR);
                 if (bytes == null || bytes.length == 0)
                     return false;
@@ -465,7 +459,7 @@ public class FileTxnLog implements TxnLog {
                 inputStream.close();
                 inputStream = null;
                 ia = null;
-                // thsi means that the file has ended
+                // thsi means that the file has ended 
                 // we shoud go to the next file
                 if (!goToNextLog()) {
                     return false;
@@ -473,10 +467,10 @@ public class FileTxnLog implements TxnLog {
             }
             return true;
         }
-
+        
         /**
-         * reutrn the current header
-         * @return the current header that
+         * reutrn the current header 
+         * @return the current header that 
          * is read
          */
         public TxnHeader getHeader() {
@@ -491,9 +485,9 @@ public class FileTxnLog implements TxnLog {
         public Record getTxn() {
             return record;
         }
-
+        
         /**
-         * close the iterator
+         * close the iterator 
          * and release the resources.
          */
         public void close() throws IOException {

+ 3 - 9
src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java

@@ -71,20 +71,14 @@ public class FileTxnSnapLog {
      * @param dataDir the trasaction directory
      * @param snapDir the snapshot directory
      */
-    public FileTxnSnapLog(File dataDir, File snapDir) throws IOException {
+    public FileTxnSnapLog(File dataDir, File snapDir) {
         this.dataDir = new File(dataDir, version + VERSION);
         this.snapDir = new File(snapDir, version + VERSION);
         if (!this.dataDir.exists()) {
-            if (!this.dataDir.mkdirs()) {
-                throw new IOException("Unable to create data directory "
-                        + this.dataDir);
-            }
+            this.dataDir.mkdirs();
         }
         if (!this.snapDir.exists()) {
-            if (!this.snapDir.mkdirs()) {
-                throw new IOException("Unable to create snap directory "
-                        + this.snapDir);
-            }
+            this.snapDir.mkdirs();
         }
         txnLog = new FileTxnLog(this.dataDir);
         snapLog = new FileSnap(this.snapDir);

+ 2 - 17
src/java/main/org/apache/zookeeper/server/persistence/Util.java

@@ -24,7 +24,6 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.io.Serializable;
 import java.net.URI;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
@@ -171,17 +170,7 @@ public class Util {
         try {
             raf.seek(raf.length() - 5);
             byte bytes[] = new byte[5];
-            int readlen = 0;
-            int l;
-            while(readlen < 5 &&
-                  (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
-                readlen += l;
-            }
-            if (readlen != bytes.length) {
-                LOG.info("Invalid snapshot " + f
-                        + " too short, len = " + readlen);
-                return false;
-            }
+            raf.read(bytes);
             ByteBuffer bb = ByteBuffer.wrap(bytes);
             int len = bb.getInt();
             byte b = bb.get();
@@ -282,11 +271,7 @@ public class Util {
      * Compare file file names of form "prefix.version". Sort order result
      * returned in order of version.
      */
-    private static class DataDirFileComparator
-        implements Comparator<File>, Serializable
-    {
-        private static final long serialVersionUID = -2648639884525140318L;
-
+    private static class DataDirFileComparator implements Comparator<File> {
         private String prefix;
         private boolean ascending;
         public DataDirFileComparator(String prefix, boolean ascending) {

+ 1 - 5
src/java/main/org/apache/zookeeper/server/quorum/FollowerHandler.java

@@ -412,11 +412,7 @@ public class FollowerHandler extends Thread {
      * ping calls from the leader to the followers
      */
     public void ping() {
-        long id;
-        synchronized(leader) {
-            id = leader.lastProposed;
-        }
-        QuorumPacket ping = new QuorumPacket(Leader.PING, id,
+        QuorumPacket ping = new QuorumPacket(Leader.PING, leader.lastProposed,
                 null, null);
         queuePacket(ping);
     }

+ 1 - 2
src/java/main/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java

@@ -64,9 +64,8 @@ public class LeaderZooKeeperServer extends ZooKeeperServer {
         commitProcessor = new CommitProcessor(toBeAppliedProcessor,
                 Long.toString(getServerId()), false);
         commitProcessor.start();
-        ProposalRequestProcessor proposalProcessor = new ProposalRequestProcessor(this,
+        RequestProcessor proposalProcessor = new ProposalRequestProcessor(this,
                 commitProcessor);
-        proposalProcessor.initialize();
         firstProcessor = new PrepRequestProcessor(this, proposalProcessor);
         ((PrepRequestProcessor)firstProcessor).start();
     }

+ 1 - 7
src/java/main/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java

@@ -39,15 +39,9 @@ public class ProposalRequestProcessor implements RequestProcessor {
         this.nextProcessor = nextProcessor;
         AckRequestProcessor ackProcessor = new AckRequestProcessor(zks.getLeader());
         syncProcessor = new SyncRequestProcessor(zks, ackProcessor);
-    }
-    
-    /**
-     * initialize this processor
-     */
-    public void initialize() {
         syncProcessor.start();
     }
-    
+
     public void processRequest(Request request) {
         // LOG.warn("Ack>>> cxid = " + request.cxid + " type = " +
         // request.type + " id = " + request.sessionId);

+ 2 - 7
src/java/main/org/apache/zookeeper/server/quorum/QuorumCnxManager.java

@@ -491,13 +491,8 @@ public class QuorumCnxManager {
                         senderWorkerMap.remove(sid);
                         ArrayBlockingQueue<ByteBuffer> bq = queueSendMap.get(sid);
                         if(bq != null){
-                            if (bq.size() == 0) {
-                                boolean ret = bq.offer(b);
-                                if (!ret) {
-                                    // to appease findbugs
-                                    LOG.error("Not able to add to a quue of size 0");
-                                }
-                            }
+                            if (bq.size() == 0)
+                                bq.offer(b);
                         } else LOG.error("No queue for server " + sid);
                     }
                 }

+ 1 - 5
src/java/main/org/apache/zookeeper/server/quorum/QuorumPeer.java

@@ -207,12 +207,8 @@ public class QuorumPeer extends Thread implements QuorumStats.Provider {
                             break;
                         case LEADING:
                             responseBuffer.putLong(myid);
-                            long proposed;
                             try {
-                                synchronized(leader) {
-                                    proposed = leader.lastProposed;
-                                }
-                                responseBuffer.putLong(proposed);
+                                responseBuffer.putLong(leader.lastProposed);
                             } catch (NullPointerException npe) {
                                 // This can happen in state transitions,
                                 // just ignore the request

+ 0 - 39
src/java/test/config/findbugsExcludeFile.xml

@@ -65,43 +65,4 @@
     <Bug code="EI, EI2" />
   </Match>
 
-  <Match>
-    <Class name="org.apache.zookeeper.server.DataNode" />
-      <Bug code="EI2"/>
-  </Match>
-
-  <Match>
-    <Class name="org.apache.zookeeper.server.quorum.QuorumPacket" />
-       <Bug code="EI2, EI" />
-  </Match>
-
-  <Match>
-    <Class name="org.apache.zookeeper.ClientCnxn"/>
-      <Bug code="EI, EI2" />
-  </Match>
-
-  <Match>
-    <Class name="org.apache.zookeeper.server.DataNode"/>
-      <Field name="children"/> 
-      <Bug code="IS"/>
-  </Match>
-
-  <Match>
-     <Class name="org.apache.zookeeper.server.quorum.FollowerSessionTracker"/>
-       <Bug code="UrF"/>
-  </Match>
-
-  <!-- these are old classes just for upgrading and should go away -->
-  <Match>
-    <Class name="org.apache.zookeeper.server.upgrade.DataNodeV1"/>
-  </Match> 
-
-  <Match>
-    <Class name="org.apache.zookeeper.server.upgrade.DataTreeV1"/>
-  </Match>
-  
-  <Match>
-     <Class name="org.apache.zookeeper.server.quorum.AuthFastLeaderElection"/>
-  	<Bug code="SIC"/>
-  </Match>
 </FindBugsFilter>

+ 15 - 15
src/java/test/org/apache/zookeeper/test/ACLTest.java

@@ -45,7 +45,7 @@ public class ACLTest extends TestCase implements Watcher {
     private static String HOSTPORT = "127.0.0.1:2355";
     ZooKeeperServer zks;
     private CountDownLatch startSignal;
-
+    
     @Override
     protected void setUp() throws Exception {
         LOG.info("STARTING " + getName());
@@ -56,8 +56,8 @@ public class ACLTest extends TestCase implements Watcher {
     }
 
     /**
-     * Verify that acl optimization of storing just
-     * a few acls and there references in the data
+     * Verify that acl optimization of storing just 
+     * a few acls and there references in the data 
      * node is actually working.
      */
     public void testAcls() throws Exception {
@@ -69,7 +69,7 @@ public class ACLTest extends TestCase implements Watcher {
         NIOServerCnxn.Factory f = new NIOServerCnxn.Factory(PORT);
         f.startup(zks);
         LOG.info("starting up the zookeeper server .. waiting");
-        assertTrue("waiting for server being up",
+        assertTrue("waiting for server being up", 
                 ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT));
         ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
         String path;
@@ -102,17 +102,17 @@ public class ACLTest extends TestCase implements Watcher {
 
         zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
         f = new NIOServerCnxn.Factory(PORT);
-
+        
         f.startup(zks);
 
         assertTrue("waiting for server up",
                    ClientBase.waitForServerUp(HOSTPORT,
                                        CONNECTION_TIMEOUT));
-
+        
         startSignal.await(CONNECTION_TIMEOUT,
                 TimeUnit.MILLISECONDS);
         assertTrue("count == 0", startSignal.getCount() == 0);
-
+        
         assertTrue("acl map ", (101 == zks.dataTree.longKeyMap.size()));
         for (int j =200; j < 205; j++) {
             path = "/" + j;
@@ -133,20 +133,20 @@ public class ACLTest extends TestCase implements Watcher {
         assertTrue("waiting for server down",
                    ClientBase.waitForServerDown(HOSTPORT,
                            ClientBase.CONNECTION_TIMEOUT));
-
+        
     }
-
-    /*
-     * (non-Javadoc)
-     *
+    
+    /*                  
+     * (non-Javadoc)    
+     *                          
      * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatcherEvent)
-     */
+     */         
     public void process(WatchedEvent event) {
         LOG.info("Event:" + event.getState() + " " + event.getType() + " " + event.getPath());
         if (event.getState() == KeeperState.SyncConnected
                 && startSignal != null && startSignal.getCount() > 0)
-        {
-            startSignal.countDown();
+        {              
+            startSignal.countDown();      
         }
     }
 }

+ 9 - 9
src/java/test/org/apache/zookeeper/test/AsyncOpsTest.java

@@ -35,19 +35,19 @@ import org.junit.Test;
 /**
  * Functional testing of asynchronous operations, both positive and negative
  * testing.
- *
+ * 
  * This just scratches the surface, but exercises the basic async functionality.
  */
 public class AsyncOpsTest extends ClientBase {
     private static final Logger LOG = Logger.getLogger(AsyncOpsTest.class);
 
     private ZooKeeper zk;
-
+    
     @Before
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
+        
         LOG.info("STARTING " + getName());
 
         zk = createClient();
@@ -58,9 +58,9 @@ public class AsyncOpsTest extends ClientBase {
     @Override
     protected void tearDown() throws Exception {
         zk.close();
-
+        
         super.tearDown();
-
+        
         LOG.info("Test clients shutting down");
 
         LOG.info("FINISHED " + getName());
@@ -74,23 +74,23 @@ public class AsyncOpsTest extends ClientBase {
     @Test
     public void testAsyncCreateThree() {
         CountDownLatch latch = new CountDownLatch(3);
-
+        
         StringCB op1 = new StringCB(zk, latch);
         op1.setPath("/op1");
         StringCB op2 = new StringCB(zk, latch);
         op2.setPath("/op2");
         StringCB op3 = new StringCB(zk, latch);
         op3.setPath("/op3");
-
+        
         op1.create();
         op2.create();
         op3.create();
-
+        
         op1.verify();
         op2.verify();
         op3.verify();
     }
-
+    
     @Test
     public void testAsyncCreateFailure_NodeExists() {
         new StringCB(zk).verifyCreateFailure_NodeExists();

+ 23 - 22
src/java/test/org/apache/zookeeper/test/ClientBase.java

@@ -76,7 +76,7 @@ public abstract class ClientBase extends TestCase {
         // XXX this doesn't need to be volatile! (Should probably be final)
         volatile CountDownLatch clientConnected;
         volatile boolean connected;
-
+        
         public CountdownWatcher() {
             reset();
         }
@@ -106,7 +106,7 @@ public abstract class ClientBase extends TestCase {
             }
             if (!connected) {
                 throw new TimeoutException("Did not connect");
-
+         
             }
         }
         synchronized void waitForDisconnected(long timeout) throws InterruptedException, TimeoutException {
@@ -118,11 +118,11 @@ public abstract class ClientBase extends TestCase {
             }
             if (connected) {
                 throw new TimeoutException("Did not disconnect");
-
+         
             }
         }
     }
-
+    
     protected ZooKeeper createClient()
         throws IOException, InterruptedException
     {
@@ -225,7 +225,7 @@ public abstract class ClientBase extends TestCase {
         }
         return false;
     }
-
+    
     static void verifyThreadTerminated(Thread thread, long millis)
         throws InterruptedException
     {
@@ -233,7 +233,7 @@ public abstract class ClientBase extends TestCase {
         if (thread.isAlive()) {
             LOG.error("Thread " + thread.getName() + " : "
                     + Arrays.toString(thread.getStackTrace()));
-            assertFalse("thread " + thread.getName()
+            assertFalse("thread " + thread.getName() 
                     + " still alive after join", true);
         }
     }
@@ -246,17 +246,17 @@ public abstract class ClientBase extends TestCase {
         File tmpFile = File.createTempFile("test", ".junit", parentDir);
         // don't delete tmpFile - this ensures we don't attempt to create
         // a tmpDir with a duplicate name
-
+        
         File tmpDir = new File(tmpFile + ".dir");
         assertFalse(tmpDir.exists()); // never true if tmpfile does it's job
         assertTrue(tmpDir.mkdirs());
-
+        
         return tmpDir;
     }
-
+    
     static NIOServerCnxn.Factory createNewServerInstance(File dataDir,
             NIOServerCnxn.Factory factory, String hostPort)
-        throws IOException, InterruptedException
+        throws IOException, InterruptedException 
     {
         ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000);
         final int PORT = Integer.parseInt(hostPort.split(":")[1]);
@@ -271,7 +271,7 @@ public abstract class ClientBase extends TestCase {
 
         return factory;
     }
-
+    
     static void shutdownServerInstance(NIOServerCnxn.Factory factory,
             String hostPort)
     {
@@ -284,7 +284,7 @@ public abstract class ClientBase extends TestCase {
                                                     CONNECTION_TIMEOUT));
         }
     }
-
+    
     /**
      * Test specific setup
      */
@@ -294,20 +294,21 @@ public abstract class ClientBase extends TestCase {
         // resulting in test failure (client timeout on first session).
         // set env and directly in order to handle static init/gc issues
         System.setProperty("zookeeper.preAllocSize", "100");
-        FileTxnLog.setPreallocSize(100 * 1024);
+        FileTxnLog.setPreallocSize(100);
     }
-
+    
     @Override
     protected void setUp() throws Exception {
         LOG.info("STARTING " + getName());
-        setupTestEnv();
 
         JMXEnv.setUp();
-
+        
         tmpDir = createTmpDir(BASETEST);
+        
+        setupTestEnv();
 
         startServer();
-
+        
         LOG.info("Client test setup finished");
     }
 
@@ -317,7 +318,7 @@ public abstract class ClientBase extends TestCase {
         // ensure that only server and data bean are registered
         JMXEnv.ensureOnly("InMemoryDataTree", "StandaloneServer_port");
     }
-
+    
     protected void stopServer() throws Exception {
         LOG.info("STOPPING server");
         shutdownServerInstance(serverFactory, hostPort);
@@ -325,19 +326,19 @@ public abstract class ClientBase extends TestCase {
         // ensure no beans are leftover
         JMXEnv.ensureOnly();
     }
-
+    
     @Override
     protected void tearDown() throws Exception {
         LOG.info("tearDown starting");
 
         stopServer();
-
+        
         if (tmpDir != null) {
             //assertTrue("delete " + tmpDir.toString(), recursiveDelete(tmpDir));
             // FIXME see ZOOKEEPER-121 replace following line with previous
             recursiveDelete(tmpDir);
         }
-
+        
         JMXEnv.tearDown();
 
         LOG.info("FINISHED " + getName());
@@ -346,7 +347,7 @@ public abstract class ClientBase extends TestCase {
     public static MBeanServerConnection jmxConn() throws IOException {
         return JMXEnv.conn();
     }
-
+    
     private static boolean recursiveDelete(File d) {
         if (d.isDirectory()) {
             File children[] = d.listFiles();

+ 63 - 62
src/java/test/org/apache/zookeeper/test/FLENewEpochTest.java

@@ -45,10 +45,10 @@ public class FLENewEpochTest extends TestCase {
     File tmpdir[];
     int port[];
     volatile int [] round;
-
+    
     Semaphore start0;
     Semaphore finish3, finish0;
-
+    
     @Override
     public void setUp() throws Exception {
         count = 3;
@@ -64,11 +64,11 @@ public class FLENewEpochTest extends TestCase {
         round[0] = 0;
         round[1] = 0;
         round[2] = 0;
-
+        
         start0 = new Semaphore(0);
         finish0 = new Semaphore(0);
         finish3 = new Semaphore(0);
-
+        
         LOG.info("SetUp " + getName());
     }
 
@@ -82,6 +82,7 @@ public class FLENewEpochTest extends TestCase {
 
 
     class LEThread extends Thread {
+        FastLeaderElection le;
         int i;
         QuorumPeer peer;
 
@@ -89,67 +90,67 @@ public class FLENewEpochTest extends TestCase {
             this.i = i;
             this.peer = peer;
             LOG.info("Constructor: " + getName());
-
+            
         }
 
         public void run(){
-            boolean flag = true;
+        	boolean flag = true;
             try{
-                while(flag){
-                    Vote v = null;
-                    peer.setPeerState(ServerState.LOOKING);
-                    LOG.info("Going to call leader election again: " + i);
-                    v = peer.getElectionAlg().lookForLeader();
-
-                    if(v == null){
-                        fail("Thread " + i + " got a null vote");
-                    }
-
-                    /*
-                     * A real zookeeper would take care of setting the current vote. Here
-                     * we do it manually.
-                     */
-                    peer.setCurrentVote(v);
-
-                    LOG.info("Finished election: " + i + ", " + v.id);
-                    //votes[i] = v;
-
-                    switch(i){
-                    case 0:
-                        LOG.info("First peer, do nothing, just join");
-                        if(finish0.tryAcquire(1000, java.util.concurrent.TimeUnit.MILLISECONDS)){
-                        //if(threads.get(0).peer.getPeerState() == ServerState.LEADING ){
-                            LOG.info("Setting flag to false");
-                            flag = false;
-                        }
-                        break;
-                    case 1:
-                        LOG.info("Second entering case");
-                        if(round[1] != 0){
-                            finish0.release();
-                            flag = false;
-                        }
-                        else{
-                            finish3.acquire();
-                            start0.release();
-                        }
-                        LOG.info("Second is going to start second round");
-                        round[1]++;
-                        break;
-                    case 2:
-                        LOG.info("Third peer, shutting it down");
-                        ((FastLeaderElection) peer.getElectionAlg()).shutdown();
-                        peer.shutdown();
-                        flag = false;
-                        round[2] = 1;
-                        finish3.release();
-                        LOG.info("Third leaving");
-                        break;
-                    }
-                }
+            	while(flag){
+            		Vote v = null;
+            		peer.setPeerState(ServerState.LOOKING);
+            		LOG.info("Going to call leader election again: " + i);
+            		v = peer.getElectionAlg().lookForLeader();
+
+            		if(v == null){
+            			fail("Thread " + i + " got a null vote");
+            		}
+
+            		/*
+            		 * A real zookeeper would take care of setting the current vote. Here
+            		 * we do it manually.
+            		 */
+            		peer.setCurrentVote(v);
+
+            		LOG.info("Finished election: " + i + ", " + v.id);
+            		//votes[i] = v;
+
+            		switch(i){
+            		case 0:
+            			LOG.info("First peer, do nothing, just join");
+            			if(finish0.tryAcquire(1000, java.util.concurrent.TimeUnit.MILLISECONDS)){
+            			//if(threads.get(0).peer.getPeerState() == ServerState.LEADING ){
+            			    LOG.info("Setting flag to false");
+            			    flag = false;
+            			}
+            			break;
+            		case 1:
+            			LOG.info("Second entering case");
+            			if(round[1] != 0){
+            			    finish0.release();
+            			    flag = false;
+            			}
+            			else{
+            				finish3.acquire();
+            				start0.release();
+            			}
+            			LOG.info("Second is going to start second round");
+            			round[1]++;
+            			break;
+            		case 2:
+            			LOG.info("Third peer, shutting it down");
+            			((FastLeaderElection) peer.getElectionAlg()).shutdown();
+            			peer.shutdown();
+            			flag = false;
+            			round[2] = 1;
+            			finish3.release();
+            			LOG.info("Third leaving");
+            			break;
+            		}
+            	}
             } catch (Exception e){
-                e.printStackTrace();
-            }
+            	e.printStackTrace();
+            }    
         }
     }
 
@@ -163,7 +164,7 @@ public class FLENewEpochTest extends TestCase {
           for(int i = 0; i < count; i++) {
               peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(baseport+100+i),
                       new InetSocketAddress(baseLEport+100+i)));
-              tmpdir[i] = ClientBase.createTmpDir();
+              tmpdir[i] = File.createTempFile("letest", "test");
               port[i] = baseport+i;
           }
 
@@ -182,7 +183,7 @@ public class FLENewEpochTest extends TestCase {
           LEThread thread = new LEThread(peer, 0);
           thread.start();
           threads.add(thread);
-
+          
           LOG.info("Started threads " + getName());
 
           for(int i = 0; i < threads.size(); i++) {

+ 58 - 55
src/java/test/org/apache/zookeeper/test/FLETest.java

@@ -38,46 +38,48 @@ import org.junit.Test;
 public class FLETest extends TestCase {
     protected static final Logger LOG = Logger.getLogger(FLETest.class);
 
-    static class TestVote {
-        TestVote(int id, long leader) {
-            this.leader = leader;
-        }
-
-        long leader;
+    class TestVote{
+	TestVote(int id, long leader){
+		this.leader = leader;
+		this.id = id;
+	}
+
+	long leader;
+	int id;
     }
-
-    int countVotes(HashSet<TestVote> hs, long id) {
-        int counter = 0;
-        for(TestVote v : hs){
-            if(v.leader == id) counter++;
+ 
+    int countVotes(HashSet<TestVote> hs, long id){
+	int counter = 0;
+	for(TestVote v : hs){
+	   if(v.leader == id) counter++;
         }
 
-        return counter;
+	return counter;
     }
 
     int count;
     int baseport;
     int baseLEport;
-    HashMap<Long,QuorumServer> peers;
+    HashMap<Long,QuorumServer> peers; 
     ArrayList<LEThread> threads;
     HashMap<Integer, HashSet<TestVote> > voteMap;
     File tmpdir[];
     int port[];
     int successCount;
     Object finalObj;
-
+    
     volatile Vote votes[];
     volatile boolean leaderDies;
     volatile long leader = -1;
-    //volatile int round = 1;
+    //volatile int round = 1; 
     Random rand = new Random();
-
+    
     @Override
     public void setUp() throws Exception {
         count = 7;
         baseport= 33003;
         baseLEport = 43003;
-
+        
         peers = new HashMap<Long,QuorumServer>(count);
         threads = new ArrayList<LEThread>(count);
         voteMap = new HashMap<Integer, HashSet<TestVote> >();
@@ -86,7 +88,7 @@ public class FLETest extends TestCase {
         port = new int[count];
         successCount = 0;
         finalObj = new Object();
-
+        
         LOG.info("SetUp " + getName());
     }
 
@@ -97,8 +99,9 @@ public class FLETest extends TestCase {
         }
         LOG.info("FINISHED " + getName());
     }
-
+    
     class LEThread extends Thread {
+        FastLeaderElection le;
         int i;
         QuorumPeer peer;
         //int peerRound = 1;
@@ -115,28 +118,28 @@ public class FLETest extends TestCase {
                     peer.setPeerState(ServerState.LOOKING);
                     LOG.info("Going to call leader election again.");
                     v = peer.getElectionAlg().lookForLeader();
-                    if(v == null){
+                    if(v == null){ 
                         LOG.info("Thread " + i + " got a null vote");
                         break;
                     }
-
+                    
                     /*
                      * A real zookeeper would take care of setting the current vote. Here
                      * we do it manually.
                      */
                     peer.setCurrentVote(v);
-
+            
                     LOG.info("Finished election: " + i + ", " + v.id);
                     votes[i] = v;
-
+                    
                     /*
                      * Get the current value of the logical clock for this peer.
                      */
                     int lc = (int) ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock();
-
+                    
                     if (v.id == ((long) i)) {
                         /*
-                         * A leader executes this part of the code. If it is the first leader to be
+                         * A leader executes this part of the code. If it is the first leader to be 
                          * elected, then it fails right after. Otherwise, it waits until it has enough
                          * followers supporting it.
                          */
@@ -148,22 +151,22 @@ public class FLETest extends TestCase {
                                 ((FastLeaderElection) peer.getElectionAlg()).shutdown();
                                 leader = -1;
                                 LOG.info("Leader " + i + " dead");
-
-                                //round++;
+                                
+                                //round++; 
                                 FLETest.this.notifyAll();
-
+                                
                                 break;
-
+                                
                             } else {
                                 synchronized(voteMap){
                                     if(voteMap.get(lc) == null)
                                         voteMap.put(lc, new HashSet<TestVote>());
                                     HashSet<TestVote> hs = voteMap.get(lc);
                                     hs.add(new TestVote(i, v.id));
-
+                                    
                                     if(countVotes(hs, v.id) > (count/2)){
                                         leader = i;
-                                        LOG.info("Got majority: " + i);
+                                        LOG.info("Got majority: " + i);   
                                     } else {
                                         voteMap.wait(3000);
                                         LOG.info("Notified or expired: " + i);
@@ -172,7 +175,7 @@ public class FLETest extends TestCase {
                                             leader = i;
                                             LOG.info("Got majority: " + i);
                                         } else {
-                                            //round++;
+                                            //round++; 
                                         }
                                     }
                                 }
@@ -183,45 +186,45 @@ public class FLETest extends TestCase {
                                         successCount++;
                                         if(successCount > (count/2)) finalObj.notify();
                                     }
-
+                                    
                                     break;
                                 }
                             }
                         }
                     } else {
                         /*
-                         * Followers execute this part. They first add their vote to voteMap, and then
+                         * Followers execute this part. They first add their vote to voteMap, and then 
                          * they wait for bounded amount of time. A leader notifies followers through the
                          * FLETest.this object.
-                         *
+                         * 
                          * Note that I can get FLETest.this, and then voteMap before adding the vote of
                          * a follower, otherwise a follower would be blocked out until the leader notifies
                          * or leaves the synchronized block on FLEtest.this.
                          */
-
-
+                        
+                        
                         LOG.info("Logical clock " + ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock());
                         synchronized(voteMap){
                             LOG.info("Voting on " + votes[i].id + ", round " + ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock());
                             if(voteMap.get(lc) == null)
                                 voteMap.put(lc, new HashSet<TestVote>());
-                            HashSet<TestVote> hs = voteMap.get(lc);
-                            hs.add(new TestVote(i, votes[i].id));
+                            HashSet<TestVote> hs = voteMap.get(lc);    
+                            hs.add(new TestVote(i, votes[i].id)); 
                             if(countVotes(hs, votes[i].id) > (count/2)){
                                 LOG.info("Logical clock: " + lc + ", " + votes[i].id);
                                 voteMap.notify();
-                            }
+                            }    
                         }
-
+                        
                         /*
                          * In this part a follower waits until the leader notifies it, and remove its
                          * vote if the leader takes too long to respond.
                          */
                         synchronized(FLETest.this){
                             if (leader != votes[i].id) FLETest.this.wait(3000);
-
+                        
                             LOG.info("The leader: " + leader + " and my vote " + votes[i].id);
-                            synchronized(voteMap){
+                            synchronized(voteMap){ 
                                 if (leader == votes[i].id) {
                                     synchronized(finalObj){
                                         successCount++;
@@ -254,22 +257,22 @@ public class FLETest extends TestCase {
             }
         }
     }
-
+    
     @Test
     public void testLE() throws Exception {
-
+       
         FastLeaderElection le[] = new FastLeaderElection[count];
         leaderDies = true;
         boolean allowOneBadLeader = leaderDies;
-
+       
         LOG.info("TestLE: " + getName()+ ", " + count);
         for(int i = 0; i < count; i++) {
-            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(baseport+100+i),
+            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(baseport+100+i), 
                     new InetSocketAddress(baseLEport+100+i)));
-            tmpdir[i] = ClientBase.createTmpDir();
-            port[i] = baseport+i;
+            tmpdir[i] = File.createTempFile("letest", "test");
+            port[i] = baseport+i;    
         }
-
+        
         for(int i = 0; i < le.length; i++) {
             QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 2, 2, 2);
             peer.startLeaderElection();
@@ -278,8 +281,8 @@ public class FLETest extends TestCase {
             threads.add(thread);
         }
         LOG.info("Started threads " + getName());
-
-
+        
+        
         int waitCounter = 0;
         synchronized(finalObj){
             while((successCount <= count/2) && (waitCounter < 50)){
@@ -287,7 +290,7 @@ public class FLETest extends TestCase {
                 waitCounter++;
             }
         }
-
+        
        /*
         * Lists what threads haven-t joined. A thread doesn't join if it hasn't decided
         * upon a leader yet. It can happen that a peer is slow or disconnected, and it can
@@ -298,14 +301,14 @@ public class FLETest extends TestCase {
                 LOG.info("Threads didn't join: " + i);
             }
         }
-
+       
        /*
         * If we have a majority, then we are good to go.
         */
        if(successCount <= count/2){
            fail("Fewer than a a majority has joined");
        }
-
+       
        if(threads.get((int) leader).isAlive()){
            fail("Leader hasn't joined: " + leader);
        }

+ 46 - 32
src/java/test/org/apache/zookeeper/test/HierarchicalQuorumTest.java

@@ -17,64 +17,77 @@
  */
 
 package org.apache.zookeeper.test;
-import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Properties;
+
 import java.io.File;
+import java.io.ByteArrayInputStream;
 import java.net.InetSocketAddress;
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Properties;
+import java.util.HashSet;
 import java.util.Random;
 
-import junit.framework.TestCase;
-
-import org.apache.log4j.Logger;
+import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.server.quorum.FastLeaderElection;
 import org.apache.zookeeper.server.quorum.QuorumPeer;
 import org.apache.zookeeper.server.quorum.Vote;
 import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
 import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState;
 import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical;
+
+import org.apache.log4j.Logger;
+import junit.framework.TestCase;
 import org.junit.Before;
 import org.junit.Test;
 
 public class HierarchicalQuorumTest extends TestCase {
     private static final Logger LOG = Logger.getLogger(HierarchicalQuorumTest.class);
-
+    
+    class TestVote{
+        TestVote(int id, long leader){
+            this.leader = leader;
+            this.id = id;
+        }
+        long leader;
+        int id;
+    }
+    
     Properties qp;
-
+    
     int count;
     int baseport;
     int baseLEport;
-    HashMap<Long,QuorumServer> peers;
+    HashMap<Long,QuorumServer> peers; 
     ArrayList<LEThread> threads;
     File tmpdir[];
     int port[];
     Object finalObj;
-
+    
     volatile Vote votes[];
     volatile boolean leaderDies;
     volatile long leader = -1;
     Random rand = new Random();
-
-
+    
+    
     @Before
     @Override
     protected void setUp() throws Exception {
         count = 9;
         baseport= 33003;
         baseLEport = 43003;
-
+        
         peers = new HashMap<Long,QuorumServer>(count);
         threads = new ArrayList<LEThread>(count);
         votes = new Vote[count];
         tmpdir = new File[count];
         port = new int[count];
         finalObj = new Object();
-
+        
         String config = "group.1=0:1:2\n" +
         "group.2=3:4:5\n" +
-        "group.3=6:7:8\n\n" +
-        "weight.0=1\n" +
+        "group.3=6:7:8\n\n" + 
+        "weight.0=1\n" + 
         "weight.1=1\n" +
         "weight.2=1\n" +
         "weight.3=1\n" +
@@ -83,22 +96,23 @@ public class HierarchicalQuorumTest extends TestCase {
         "weight.6=1\n" +
         "weight.7=1\n" +
         "weight.8=1";
-
+        
         ByteArrayInputStream is = new ByteArrayInputStream(config.getBytes());
-        this.qp = new Properties();
+        this.qp = new Properties(); 
         qp.load(is);
-
+        
         LOG.info("SetUp " + getName());
     }
-
+    
     protected void tearDown() throws Exception {
         for(int i = 0; i < threads.size(); i++) {
             ((FastLeaderElection) threads.get(i).peer.getElectionAlg()).shutdown();
         }
         LOG.info("FINISHED " + getName());
     }
-
+    
     class LEThread extends Thread {
+        FastLeaderElection le;
         int i;
         QuorumPeer peer;
         //int peerRound = 1;
@@ -113,25 +127,25 @@ public class HierarchicalQuorumTest extends TestCase {
             try {
                 Vote v = null;
                 while(true){
-
+                    
                     //while(true) {
                     peer.setPeerState(ServerState.LOOKING);
                     LOG.info("Going to call leader election.");
                     v = peer.getElectionAlg().lookForLeader();
-                    if(v == null){
+                    if(v == null){ 
                         LOG.info("Thread " + i + " got a null vote");
                         return;
                     }
-
+                    
                     /*
                      * A real zookeeper would take care of setting the current vote. Here
                      * we do it manually.
                      */
                     peer.setCurrentVote(v);
-
+            
                     LOG.info("Finished election: " + i + ", " + v.id);
                     votes[i] = v;
-
+                
                     if((peer.getPeerState() == ServerState.FOLLOWING) ||
                             (peer.getPeerState() == ServerState.LEADING)) break;
                 }
@@ -141,19 +155,19 @@ public class HierarchicalQuorumTest extends TestCase {
             }
         }
     }
-
+    
     @Test
     public void testHierarchicalQuorum() throws Exception {
         FastLeaderElection le[] = new FastLeaderElection[count];
-
+       
         LOG.info("TestHierarchicalQuorum: " + getName()+ ", " + count);
         for(int i = 0; i < count; i++) {
-            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(baseport+100+i),
+            peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(baseport+100+i), 
                     new InetSocketAddress(baseLEport+100+i)));
-            tmpdir[i] = ClientBase.createTmpDir();
-            port[i] = baseport+i;
+            tmpdir[i] = File.createTempFile("letest", "test");
+            port[i] = baseport+i;    
         }
-
+        
         for(int i = 0; i < le.length; i++) {
             QuorumHierarchical hq = new QuorumHierarchical(qp);
             QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 2, 2, 2, hq);
@@ -163,7 +177,7 @@ public class HierarchicalQuorumTest extends TestCase {
             threads.add(thread);
         }
         LOG.info("Started threads " + getName());
-
+        
         for(int i = 0; i < threads.size(); i++) {
             threads.get(i).join(15000);
             if (threads.get(i).isAlive()) {

+ 4 - 4
src/java/test/org/apache/zookeeper/test/LETest.java

@@ -34,7 +34,7 @@ import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer;
 public class LETest extends TestCase {
     volatile Vote votes[];
     volatile boolean leaderDies;
-    volatile long leader = -1;
+    volatile long leader = -1; 
     Random rand = new Random();
     class LEThread extends Thread {
         LeaderElection le;
@@ -59,7 +59,7 @@ public class LETest extends TestCase {
                                 System.out.println("Leader " + i + " dying");
                                 leader = -2;
                             } else {
-                                leader = i;
+                                leader = i; 
                             }
                             LETest.this.notifyAll();
                         }
@@ -92,8 +92,8 @@ public class LETest extends TestCase {
         votes = new Vote[count];
         for(int i = 0; i < count; i++) {
             peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", baseport+100+i)));
-            tmpdir[i] = ClientBase.createTmpDir();
-            port[i] = baseport+i;
+            tmpdir[i] = File.createTempFile("letest", "test");
+            port[i] = baseport+i;    
         }
         LeaderElection le[] = new LeaderElection[count];
         leaderDies = true;

+ 8 - 5
src/java/test/org/apache/zookeeper/test/QuorumBase.java

@@ -34,15 +34,18 @@ import org.junit.After;
 public class QuorumBase extends ClientBase {
     private static final Logger LOG = Logger.getLogger(QuorumBase.class);
 
+
+
     File s1dir, s2dir, s3dir, s4dir, s5dir;
     QuorumPeer s1, s2, s3, s4, s5;
 
     protected void setUp() throws Exception {
         LOG.info("STARTING " + getName());
-        setupTestEnv();
 
         JMXEnv.setUp();
 
+        setupTestEnv();
+
         hostPort = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184,127.0.0.1:2185";
 
         s1dir = ClientBase.createTmpDir();
@@ -105,11 +108,11 @@ public class QuorumBase extends ClientBase {
         // make sure we have these 5 servers listed
         Set<String> ensureNames = new LinkedHashSet<String>();
         for (int i = 1; i <= 5; i++) {
-            ensureNames.add("InMemoryDataTree");
+            ensureNames.add("InMemoryDataTree"); 
         }
         for (int i = 1; i <= 5; i++) {
             ensureNames.add("name0=ReplicatedServer_id" + i
-                 + ",name1=replica." + i + ",name2=");
+                 + ",name1=replica." + i + ",name2="); 
         }
         for (int i = 1; i <= 5; i++) {
             for (int j = 1; j <= 5; j++) {
@@ -118,7 +121,7 @@ public class QuorumBase extends ClientBase {
             }
         }
         for (int i = 1; i <= 5; i++) {
-            ensureNames.add("name0=ReplicatedServer_id" + i);
+            ensureNames.add("name0=ReplicatedServer_id" + i); 
         }
         JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()]));
     }
@@ -141,7 +144,7 @@ public class QuorumBase extends ClientBase {
         }
 
         JMXEnv.tearDown();
-
+        
         LOG.info("FINISHED " + getName());
     }