Author: szetszwo Date: Thu Jul 26 04:38:17 2012 New Revision: 1365882 URL: http://svn.apache.org/viewvc?rev=1365882&view=rev Log: HDFS-2533. Backport: Remove needless synchronization on some FSDataSet methods. Contributed by Brandon Li
Modified: hadoop/common/branches/branch-1/CHANGES.txt hadoop/common/branches/branch-1/src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java Modified: hadoop/common/branches/branch-1/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/CHANGES.txt?rev=1365882&r1=1365881&r2=1365882&view=diff ============================================================================== --- hadoop/common/branches/branch-1/CHANGES.txt (original) +++ hadoop/common/branches/branch-1/CHANGES.txt Thu Jul 26 04:38:17 2012 @@ -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 Modified: hadoop/common/branches/branch-1/src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-1/src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java?rev=1365882&r1=1365881&r2=1365882&view=diff ============================================================================== --- hadoop/common/branches/branch-1/src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java (original) +++ hadoop/common/branches/branch-1/src/hdfs/org/apache/hadoop/hdfs/server/datanode/FSDataset.java Thu Jul 26 04:38:17 2012 @@ -19,6 +19,7 @@ package org.apache.hadoop.hdfs.server.da 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 FSCons /** * 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 FSCons 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); }