You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ha...@apache.org on 2017/06/25 22:07:11 UTC

zookeeper git commit: ZOOKEEPER-2815: 1. Using try clause to close resource; 2. Others code refactoring for PERSISTENCE module

Repository: zookeeper
Updated Branches:
  refs/heads/master fb267672d -> 8ec4c58eb


ZOOKEEPER-2815: 1. Using try clause to close resource; 2. Others code refactoring for PERSISTENCE module

MINOR:
* Using try clause to close resource;
* Others code refactoring for PERSISTENCE module.

Author: asdf2014 <15...@qq.com>

Reviewers: Michael Han <ha...@apache.org>

Closes #283 from asdf2014/code_refactoring


Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo
Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/8ec4c58e
Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/8ec4c58e
Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/8ec4c58e

Branch: refs/heads/master
Commit: 8ec4c58eb37880a812bb2e23e8b3ec2a5e3b7f32
Parents: fb26767
Author: asdf2014 <15...@qq.com>
Authored: Sun Jun 25 15:07:06 2017 -0700
Committer: Michael Han <ha...@apache.org>
Committed: Sun Jun 25 15:07:06 2017 -0700

----------------------------------------------------------------------
 .../zookeeper/server/persistence/FileSnap.java  | 62 +++++++++-----------
 .../server/persistence/FileTxnLog.java          | 16 +++--
 .../server/persistence/FileTxnSnapLog.java      |  9 ++-
 .../zookeeper/server/persistence/TxnLog.java    |  4 +-
 .../zookeeper/server/persistence/Util.java      | 21 +++----
 5 files changed, 49 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8ec4c58e/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
index cf464a1..30ce7c7 100644
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
+++ b/src/java/main/org/apache/zookeeper/server/persistence/FileSnap.java
@@ -51,11 +51,12 @@ import org.apache.zookeeper.server.util.SerializeUtils;
 public class FileSnap implements SnapShot {
     File snapDir;
     private volatile boolean close = false;
-    private static final int VERSION=2;
-    private static final long dbId=-1;
+    private static final int VERSION = 2;
+    private static final long dbId = -1;
     private static final Logger LOG = LoggerFactory.getLogger(FileSnap.class);
     public final static int SNAP_MAGIC
-        = ByteBuffer.wrap("ZKSN".getBytes()).getInt();
+            = ByteBuffer.wrap("ZKSN".getBytes()).getInt();
+
     public FileSnap(File snapDir) {
         this.snapDir = snapDir;
     }
@@ -63,7 +64,7 @@ public class FileSnap implements SnapShot {
     /**
      * deserialize a data tree from the most recent snapshot
      * @return the zxid of the snapshot
-     */ 
+     */
     public long deserialize(DataTree dt, Map<Long, Integer> sessions)
             throws IOException {
         // we run through 100 snapshots (not all of them)
@@ -75,16 +76,13 @@ public class FileSnap implements SnapShot {
         }
         File snap = null;
         boolean foundValid = false;
-        for (int i = 0; i < snapList.size(); i++) {
+        for (int i = 0, snapListSize = snapList.size(); i < snapListSize; i++) {
             snap = snapList.get(i);
-            InputStream snapIS = null;
-            CheckedInputStream crcIn = null;
-            try {
-                LOG.info("Reading snapshot " + snap);
-                snapIS = new BufferedInputStream(new FileInputStream(snap));
-                crcIn = new CheckedInputStream(snapIS, new Adler32());
+            LOG.info("Reading snapshot " + snap);
+            try (InputStream snapIS = new BufferedInputStream(new FileInputStream(snap));
+                 CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32())) {
                 InputArchive ia = BinaryInputArchive.getArchive(crcIn);
-                deserialize(dt,sessions, ia);
+                deserialize(dt, sessions, ia);
                 long checkSum = crcIn.getChecksum().getValue();
                 long val = ia.readLong("val");
                 if (val != checkSum) {
@@ -92,14 +90,9 @@ public class FileSnap implements SnapShot {
                 }
                 foundValid = true;
                 break;
-            } catch(IOException e) {
+            } catch (IOException e) {
                 LOG.warn("problem reading snap file " + snap, e);
-            } finally {
-                if (snapIS != null) 
-                    snapIS.close();
-                if (crcIn != null) 
-                    crcIn.close();
-            } 
+            }
         }
         if (!foundValid) {
             throw new IOException("Not able to find valid snapshots in " + snapDir);
@@ -121,7 +114,7 @@ public class FileSnap implements SnapShot {
         header.deserialize(ia, "fileheader");
         if (header.getMagic() != SNAP_MAGIC) {
             throw new IOException("mismatching magic headers "
-                    + header.getMagic() + 
+                    + header.getMagic() +
                     " !=  " + FileSnap.SNAP_MAGIC);
         }
         SerializeUtils.deserializeSnapshot(dt,ia,sessions);
@@ -138,7 +131,7 @@ public class FileSnap implements SnapShot {
         }
         return files.get(0);
     }
-    
+
     /**
      * find the last (maybe) valid n snapshots. this does some 
      * minor checks on the validity of the snapshots. It just
@@ -177,7 +170,7 @@ public class FileSnap implements SnapShot {
     /**
      * find the last n snapshots. this does not have
      * any checks if the snapshot might be valid or not
-     * @param the number of most recent snapshots
+     * @param n the number of most recent snapshots
      * @return the last n snapshots
      * @throws IOException
      */
@@ -224,18 +217,17 @@ public class FileSnap implements SnapShot {
     public synchronized void serialize(DataTree dt, Map<Long, Integer> sessions, File snapShot)
             throws IOException {
         if (!close) {
-            OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
-            CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32());
-            //CheckedOutputStream cout = new CheckedOutputStream()
-            OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
-            FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
-            serialize(dt,sessions,oa, header);
-            long val = crcOut.getChecksum().getValue();
-            oa.writeLong(val, "val");
-            oa.writeString("/", "path");
-            sessOS.flush();
-            crcOut.close();
-            sessOS.close();
+            try (OutputStream sessOS = new BufferedOutputStream(new FileOutputStream(snapShot));
+                 CheckedOutputStream crcOut = new CheckedOutputStream(sessOS, new Adler32())) {
+                //CheckedOutputStream cout = new CheckedOutputStream()
+                OutputArchive oa = BinaryOutputArchive.getArchive(crcOut);
+                FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId);
+                serialize(dt, sessions, oa, header);
+                long val = crcOut.getChecksum().getValue();
+                oa.writeLong(val, "val");
+                oa.writeString("/", "path");
+                sessOS.flush();
+            }
         }
     }
 
@@ -249,4 +241,4 @@ public class FileSnap implements SnapShot {
         close = true;
     }
 
- }
+}

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8ec4c58e/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
index ee62bcc..9f55ab4 100644
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
+++ b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java
@@ -40,7 +40,6 @@ import org.apache.jute.BinaryOutputArchive;
 import org.apache.jute.InputArchive;
 import org.apache.jute.OutputArchive;
 import org.apache.jute.Record;
-import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator;
 import org.apache.zookeeper.server.util.SerializeUtils;
 import org.apache.zookeeper.txn.TxnHeader;
 import org.slf4j.Logger;
@@ -125,7 +124,7 @@ public class FileTxnLog implements TxnLog {
     volatile FileOutputStream fos = null;
 
     File logDir;
-    private final boolean forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals("no");;
+    private final boolean forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals("no");
     long dbId;
     private LinkedList<FileOutputStream> streamsToFlush =
         new LinkedList<FileOutputStream>();
@@ -153,7 +152,7 @@ public class FileTxnLog implements TxnLog {
     }
 
     /**
-     * creates a checksum alogrithm to be used
+     * creates a checksum algorithm to be used
      * @return the checksum used for this txnlog
      */
     protected Checksum makeChecksumAlgorithm(){
@@ -352,7 +351,7 @@ public class FileTxnLog implements TxnLog {
 
     /**
      *
-     * @return elapsed sync time of transaction log in miliseconds
+     * @return elapsed sync time of transaction log in milliseconds
      */
     public long getTxnLogSyncElapsedTime() {
         return syncElapsedMS;
@@ -415,7 +414,7 @@ public class FileTxnLog implements TxnLog {
     /**
      * read the header of the transaction file
      * @param file the transaction file to read
-     * @return header that was read fomr the file
+     * @return header that was read from the file
      * @throws IOException
      */
     private static FileHeader readHeader(File file) throws IOException {
@@ -595,8 +594,7 @@ public class FileTxnLog implements TxnLog {
                 }
             }
             goToNextLog();
-            if (!next())
-                return;
+            next();
         }
         
         /**
@@ -692,7 +690,7 @@ public class FileTxnLog implements TxnLog {
                 hdr = new TxnHeader();
                 record = SerializeUtils.deserializeTxn(bytes, hdr);
             } catch (EOFException e) {
-                LOG.debug("EOF excepton " + e);
+                LOG.debug("EOF exception " + e);
                 inputStream.close();
                 inputStream = null;
                 ia = null;
@@ -712,7 +710,7 @@ public class FileTxnLog implements TxnLog {
         }
 
         /**
-         * reutrn the current header
+         * return the current header
          * @return the current header that
          * is read
          */

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8ec4c58e/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
index 40a5cd6..8c2919f 100644
--- a/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
+++ b/src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java
@@ -83,7 +83,7 @@ public class FileTxnSnapLog {
     /**
      * the constructor which takes the datadir and
      * snapdir.
-     * @param dataDir the trasaction directory
+     * @param dataDir the transaction directory
      * @param snapDir the snapshot directory
      */
     public FileTxnSnapLog(File dataDir, File snapDir) throws IOException {
@@ -220,9 +220,8 @@ public class FileTxnSnapLog {
                     return dt.lastProcessedZxid;
                 }
                 if (hdr.getZxid() < highestZxid && highestZxid != 0) {
-                    LOG.error("{}(higestZxid) > {}(next log) for type {}",
-                            new Object[] { highestZxid, hdr.getZxid(),
-                                    hdr.getType() });
+                    LOG.error("{}(highestZxid) > {}(next log) for type {}",
+                            highestZxid, hdr.getZxid(), hdr.getType());
                 } else {
                     highestZxid = hdr.getZxid();
                 }
@@ -334,7 +333,7 @@ public class FileTxnSnapLog {
     /**
      * save the datatree and the sessions into a snapshot
      * @param dataTree the datatree to be serialized onto disk
-     * @param sessionsWithTimeouts the sesssion timeouts to be
+     * @param sessionsWithTimeouts the session timeouts to be
      * serialized onto disk
      * @throws IOException
      */

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8ec4c58e/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
index b829e8d..90e0f6f 100644
--- a/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
+++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLog.java
@@ -77,7 +77,7 @@ public interface TxnLog {
     long getDbId() throws IOException;
     
     /**
-     * commmit the transaction and make sure
+     * commit the transaction and make sure
      * they are persisted
      * @throws IOException
      */
@@ -85,7 +85,7 @@ public interface TxnLog {
 
     /**
      *
-     * @return transaction log's elapsed sync time in miliseconds
+     * @return transaction log's elapsed sync time in milliseconds
      */
     long getTxnLogSyncElapsedTime();
    

http://git-wip-us.apache.org/repos/asf/zookeeper/blob/8ec4c58e/src/java/main/org/apache/zookeeper/server/persistence/Util.java
----------------------------------------------------------------------
diff --git a/src/java/main/org/apache/zookeeper/server/persistence/Util.java b/src/java/main/org/apache/zookeeper/server/persistence/Util.java
index 7ef7f9c..08ad08a 100644
--- a/src/java/main/org/apache/zookeeper/server/persistence/Util.java
+++ b/src/java/main/org/apache/zookeeper/server/persistence/Util.java
@@ -81,7 +81,7 @@ public class Util {
     /**
      * Creates a valid transaction log file name. 
      * 
-     * @param zxid used as a file name suffix (extention)
+     * @param zxid used as a file name suffix (extension)
      * @return file name
      */
     public static String makeLogName(long zxid) {
@@ -130,7 +130,7 @@ public class Util {
    
     /**
      * Extracts zxid from the file name. The file name should have been created
-     * using one of the {@link makeLogName} or {@link makeSnapshotName}.
+     * using one of the {@link #makeLogName(long)} or {@link #makeSnapshotName(long)}.
      * 
      * @param name the file name to parse
      * @param prefix the file name prefix (snapshot or log)
@@ -163,10 +163,9 @@ public class Util {
             return false;
 
         // Check for a valid snapshot
-        RandomAccessFile raf = new RandomAccessFile(f, "r");
-        try {
+        try (RandomAccessFile raf = new RandomAccessFile(f, "r")) {
             // including the header and the last / bytes
-            // the snapshot should be atleast 10 bytes
+            // the snapshot should be at least 10 bytes
             if (raf.length() < 10) {
                 return false;
             }
@@ -174,8 +173,8 @@ public class Util {
             byte bytes[] = new byte[5];
             int readlen = 0;
             int l;
-            while(readlen < 5 &&
-                  (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
+            while (readlen < 5 &&
+                    (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) {
                 readlen += l;
             }
             if (readlen != bytes.length) {
@@ -191,20 +190,18 @@ public class Util {
                         + " byte = " + (b & 0xff));
                 return false;
             }
-        } finally {
-            raf.close();
         }
 
         return true;
     }
 
     /**
-     * Grows the file to the specified number of bytes. This only happenes if 
+     * Grows the file to the specified number of bytes. This only happenes if
      * the current file position is sufficiently close (less than 4K) to end of 
      * file. 
      * 
      * @param f output stream to pad
-     * @param currentSize application keeps track of the cuurent file size
+     * @param currentSize application keeps track of the current file size
      * @param preAllocSize how many bytes to pad
      * @return the new file size. It can be the same as currentSize if no
      * padding was done.
@@ -269,7 +266,7 @@ public class Util {
      * Write the serialized transaction record to the output archive.
      *  
      * @param oa output archive
-     * @param bytes serialized trasnaction record
+     * @param bytes serialized transaction record
      * @throws IOException
      */
     public static void writeTxnBytes(OutputArchive oa, byte[] bytes)