Browse Source

HDFS-2533. Backport: Remove needless synchronization on some FSDataSet methods. Contributed by Brandon Li

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1@1365882 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze 13 years ago
parent
commit
f3e5b4a5be
2 changed files with 38 additions and 6 deletions
  1. 5 0
      CHANGES.txt
  2. 33 6
      src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java

+ 5 - 0
CHANGES.txt

@@ -51,6 +51,11 @@ Release 1.2.0 - unreleased
 
     HDFS-496. Backport: Use PureJavaCrc32 in HDFS.  (Brandon Li via szetszwo)
 
+  OPTIMIZATIONS
+
+    HDFS-2533. Backport: Remove needless synchronization on some FSDataSet
+    methods.  (Brandon Li via szetszwo)
+
   BUG FIXES
 
     HADOOP-8460. Document proper setting of HADOOP_PID_DIR and

+ 33 - 6
src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java

@@ -19,6 +19,7 @@ package org.apache.hadoop.hdfs.server.datanode;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
@@ -1062,7 +1063,7 @@ public class FSDataset implements FSConstants, FSDatasetInterface {
   /**
    * Get File name for a given block.
    */
-  public synchronized File getBlockFile(Block b) throws IOException {
+  public File getBlockFile(Block b) throws IOException {
     File f = validateBlockFile(b);
     if(f == null) {
       if (InterDatanodeProtocol.LOG.isDebugEnabled()) {
@@ -1083,14 +1084,40 @@ public class FSDataset implements FSConstants, FSDatasetInterface {
     return info;
   }
   
-  public synchronized InputStream getBlockInputStream(Block b) throws IOException {
-    return new FileInputStream(getBlockFile(b));
+  public InputStream getBlockInputStream(Block b) throws IOException {
+    File f = getBlockFileNoExistsCheck(b);
+    try {
+      return new FileInputStream(f);
+    } catch (FileNotFoundException fnfe) {
+      throw new IOException("Block " + b + " is not valid. "
+          + "Expected block file at " + f + " does not exist.");
+    }
+  }
+
+  /**
+   * Return the File associated with a block, without first checking that it
+   * exists. This should be used when the next operation is going to open the
+   * file for read anyway, and thus the exists check is redundant.
+   */
+  private File getBlockFileNoExistsCheck(Block b) throws IOException {
+    File f = getFile(b);
+    if (f == null) {
+      throw new IOException("Block " + b + " is not valid");
+    }
+    return f;
   }
 
-  public synchronized InputStream getBlockInputStream(Block b, long seekOffset) throws IOException {
+  public InputStream getBlockInputStream(Block b, long seekOffset)
+      throws IOException {
+    File blockFile = getBlockFileNoExistsCheck(b);
+    RandomAccessFile blockInFile;
+    try {
+      blockInFile = new RandomAccessFile(blockFile, "r");
+    } catch (FileNotFoundException fnfe) {
+      throw new IOException("Block " + b + " is not valid. "
+          + "Expected block file at " + blockFile + " does not exist.");
+    }
 
-    File blockFile = getBlockFile(b);
-    RandomAccessFile blockInFile = new RandomAccessFile(blockFile, "r");
     if (seekOffset > 0) {
       blockInFile.seek(seekOffset);
     }