hadoop-hdfs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sur...@apache.org
Subject svn commit: r1097905 [3/14] - in /hadoop/hdfs/trunk: ./ bin/ src/c++/libhdfs/ src/contrib/hdfsproxy/ src/contrib/hdfsproxy/src/java/org/apache/hadoop/hdfsproxy/ src/java/ src/java/org/apache/hadoop/hdfs/ src/java/org/apache/hadoop/hdfs/protocol/ src/ja...
Date Fri, 29 Apr 2011 18:16:38 GMT
Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/Storage.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/Storage.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/Storage.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/Storage.java Fri Apr 29 18:16:32 2011
@@ -78,18 +78,21 @@ public abstract class Storage extends St
   // last layout version that did not support persistent rbw replicas
   public static final int PRE_RBW_LAYOUT_VERSION = -19;
   
+  // last layout version that is before federation
+  public static final int LAST_PRE_FEDERATION_LAYOUT_VERSION = -30;
+  
   /** Layout versions of 0.20.203 release */
   public static final int[] LAYOUT_VERSIONS_203 = {-19, -31};
 
   private   static final String STORAGE_FILE_LOCK     = "in_use.lock";
   protected static final String STORAGE_FILE_VERSION  = "VERSION";
   public    static final String STORAGE_DIR_CURRENT   = "current";
-  private   static final String STORAGE_DIR_PREVIOUS  = "previous";
-  private   static final String STORAGE_TMP_REMOVED   = "removed.tmp";
-  private   static final String STORAGE_TMP_PREVIOUS  = "previous.tmp";
-  private   static final String STORAGE_TMP_FINALIZED = "finalized.tmp";
-  private   static final String STORAGE_TMP_LAST_CKPT = "lastcheckpoint.tmp";
-  private   static final String STORAGE_PREVIOUS_CKPT = "previous.checkpoint";
+  public    static final String STORAGE_DIR_PREVIOUS  = "previous";
+  public    static final String STORAGE_TMP_REMOVED   = "removed.tmp";
+  public    static final String STORAGE_TMP_PREVIOUS  = "previous.tmp";
+  public    static final String STORAGE_TMP_FINALIZED = "finalized.tmp";
+  public    static final String STORAGE_TMP_LAST_CKPT = "lastcheckpoint.tmp";
+  public    static final String STORAGE_PREVIOUS_CKPT = "previous.checkpoint";
   
   public enum StorageState {
     NON_EXISTENT,
@@ -115,7 +118,7 @@ public abstract class Storage extends St
     public boolean isOfType(StorageDirType type);
   }
   
-  private NodeType storageType;    // Type of the node using this storage 
+  protected NodeType storageType;    // Type of the node using this storage 
   protected List<StorageDirectory> storageDirs = new ArrayList<StorageDirectory>();
   
   private class DirIterator implements Iterator<StorageDirectory> {
@@ -198,19 +201,32 @@ public abstract class Storage extends St
    */
   @InterfaceAudience.Private
   public class StorageDirectory {
-    File              root; // root directory
-    FileLock          lock; // storage lock
-    StorageDirType dirType; // storage dir type
+    final File root;              // root directory
+    final boolean useLock;        // flag to enable storage lock
+    final StorageDirType dirType; // storage dir type
+    FileLock lock;                // storage lock
     
     public StorageDirectory(File dir) {
       // default dirType is null
-      this(dir, null);
+      this(dir, null, true);
     }
     
     public StorageDirectory(File dir, StorageDirType dirType) {
+      this(dir, dirType, true);
+    }
+    
+    /**
+     * Constructor
+     * @param dir directory corresponding to the storage
+     * @param dirType storage directory type
+     * @param useLock true - enables locking on the storage directory and false
+     *          disables locking
+     */
+    public StorageDirectory(File dir, StorageDirType dirType, boolean useLock) {
       this.root = dir;
       this.lock = null;
       this.dirType = dirType;
+      this.useLock = useLock;
     }
     
     /**
@@ -235,22 +251,26 @@ public abstract class Storage extends St
     public void read() throws IOException {
       read(getVersionFile());
     }
-    
     public void read(File from) throws IOException {
+      Properties props = readFrom(from);
+      getFields(props, this);
+    }
+    
+    public Properties readFrom(File from) throws IOException {
       RandomAccessFile file = new RandomAccessFile(from, "rws");
       FileInputStream in = null;
+      Properties props = new Properties();
       try {
         in = new FileInputStream(file.getFD());
         file.seek(0);
-        Properties props = new Properties();
         props.load(in);
-        getFields(props, this);
       } finally {
         if (in != null) {
           in.close();
         }
         file.close();
       }
+      return props;
     }
 
     /**
@@ -610,6 +630,10 @@ public abstract class Storage extends St
      * @throws IOException if locking fails
      */
     public void lock() throws IOException {
+      if (!useLock) {
+        LOG.info("Locking is disabled");
+        return;
+      }
       this.lock = tryLock();
       if (lock == null) {
         String msg = "Cannot lock storage " + this.root 
@@ -666,11 +690,6 @@ public abstract class Storage extends St
     this.storageType = type;
   }
   
-  protected Storage(NodeType type, int nsID, long cT) {
-    super(FSConstants.LAYOUT_VERSION, nsID, cT);
-    this.storageType = type;
-  }
-  
   protected Storage(NodeType type, StorageInfo storageInfo) {
     super(storageInfo);
     this.storageType = type;
@@ -716,8 +735,9 @@ public abstract class Storage extends St
   public static void checkVersionUpgradable(int oldVersion) 
                                      throws IOException {
     if (oldVersion > LAST_UPGRADABLE_LAYOUT_VERSION) {
-      String msg = "*********** Upgrade is not supported from this older" +
-                   " version of storage to the current version." + 
+      String msg = "*********** Upgrade is not supported from this " +
+                   " older version " + oldVersion + 
+                   " of storage to the current version." + 
                    " Please upgrade to " + LAST_UPGRADABLE_HADOOP_VERSION +
                    " or a later version and then upgrade to current" +
                    " version. Old layout version is " + 
@@ -741,29 +761,11 @@ public abstract class Storage extends St
   protected void getFields(Properties props, 
                            StorageDirectory sd 
                            ) throws IOException {
-    String sv, st, sid, sct;
-    sv = props.getProperty("layoutVersion");
-    st = props.getProperty("storageType");
-    sid = props.getProperty("namespaceID");
-    sct = props.getProperty("cTime");
-    if (sv == null || st == null || sid == null || sct == null)
-      throw new InconsistentFSStateException(sd.root,
-                                             "file " + STORAGE_FILE_VERSION + " is invalid.");
-    int rv = Integer.parseInt(sv);
-    NodeType rt = NodeType.valueOf(st);
-    int rid = Integer.parseInt(sid);
-    long rct = Long.parseLong(sct);
-    if (!storageType.equals(rt) ||
-        !((namespaceID == 0) || (rid == 0) || namespaceID == rid))
-      throw new InconsistentFSStateException(sd.root,
-                                             "is incompatible with others.");
-    if (rv < FSConstants.LAYOUT_VERSION) // future version
-      throw new IncorrectVersionException(rv, "storage directory " 
-                                          + sd.root.getCanonicalPath());
-    layoutVersion = rv;
-    storageType = rt;
-    namespaceID = rid;
-    cTime = rct;
+    setLayoutVersion(props, sd);
+    setNamespaceID(props, sd);
+    setStorageType(props, sd);
+    setcTime(props, sd);
+    setClusterId(props, layoutVersion, sd);
   }
   
   /**
@@ -779,6 +781,10 @@ public abstract class Storage extends St
     props.setProperty("layoutVersion", String.valueOf(layoutVersion));
     props.setProperty("storageType", storageType.toString());
     props.setProperty("namespaceID", String.valueOf(namespaceID));
+    // Set clusterID in version LAST_PRE_FEDERATION_LAYOUT_VERSION or before
+    if (layoutVersion < LAST_PRE_FEDERATION_LAYOUT_VERSION) {
+      props.setProperty("clusterID", clusterID);
+    }
     props.setProperty("cTime", String.valueOf(cTime));
   }
 
@@ -861,10 +867,74 @@ public abstract class Storage extends St
 
   public static String getRegistrationID(StorageInfo storage) {
     return "NS-" + Integer.toString(storage.getNamespaceID())
+      + "-" + storage.getClusterID()
       + "-" + Integer.toString(storage.getLayoutVersion())
       + "-" + Long.toString(storage.getCTime());
   }
   
+  String getProperty(Properties props, StorageDirectory sd,
+      String name) throws InconsistentFSStateException {
+    String property = props.getProperty(name);
+    if (property == null) {
+      throw new InconsistentFSStateException(sd.root, "file "
+          + STORAGE_FILE_VERSION + " has " + name + " mising.");
+    }
+    return property;
+  }
+  
+  /** Validate and set storage type from {@link Properties}*/
+  protected void setStorageType(Properties props, StorageDirectory sd)
+      throws InconsistentFSStateException {
+    NodeType type = NodeType.valueOf(getProperty(props, sd, "storageType"));
+    if (!storageType.equals(type)) {
+      throw new InconsistentFSStateException(sd.root,
+          "node type is incompatible with others.");
+    }
+    storageType = type;
+  }
+  
+  /** Validate and set ctime from {@link Properties}*/
+  protected void setcTime(Properties props, StorageDirectory sd)
+      throws InconsistentFSStateException {
+    cTime = Long.parseLong(getProperty(props, sd, "cTime"));
+  }
+
+  /** Validate and set clusterId from {@link Properties}*/
+  protected void setClusterId(Properties props, int layoutVersion,
+      StorageDirectory sd) throws InconsistentFSStateException {
+    // No Cluster ID in version LAST_PRE_FEDERATION_LAYOUT_VERSION or before
+    if (layoutVersion < Storage.LAST_PRE_FEDERATION_LAYOUT_VERSION) {
+      String cid = getProperty(props, sd, "clusterID");
+      if (!(clusterID.equals("") || cid.equals("") || clusterID.equals(cid))) {
+        throw new InconsistentFSStateException(sd.getRoot(),
+            "cluster Id is incompatible with others.");
+      }
+      clusterID = cid;
+    }
+  }
+  
+  /** Validate and set layout version from {@link Properties}*/
+  protected void setLayoutVersion(Properties props, StorageDirectory sd)
+      throws IncorrectVersionException, InconsistentFSStateException {
+    int lv = Integer.parseInt(getProperty(props, sd, "layoutVersion"));
+    if (lv < FSConstants.LAYOUT_VERSION) { // future version
+      throw new IncorrectVersionException(lv, "storage directory "
+          + sd.root.getAbsolutePath());
+    }
+    layoutVersion = lv;
+  }
+  
+  /** Validate and set namespaceID version from {@link Properties}*/
+  protected void setNamespaceID(Properties props, StorageDirectory sd)
+      throws InconsistentFSStateException {
+    int nsId = Integer.parseInt(getProperty(props, sd, "namespaceID"));
+    if (namespaceID != 0 && nsId != 0 && namespaceID != nsId) {
+      throw new InconsistentFSStateException(sd.root,
+          "namespaceID is incompatible with others.");
+    }
+    namespaceID = nsId;
+  }
+  
   public static boolean is203LayoutVersion(int layoutVersion) {
     for (int lv203 : LAYOUT_VERSIONS_203) {
       if (lv203 == layoutVersion) {

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/StorageInfo.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/StorageInfo.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/StorageInfo.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/common/StorageInfo.java Fri Apr 29 18:16:32 2011
@@ -23,7 +23,7 @@ import java.io.IOException;
 
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.io.Writable;
-
+import org.apache.hadoop.io.WritableUtils;
 
 /**
  * Common class for storage information.
@@ -34,14 +34,16 @@ import org.apache.hadoop.io.Writable;
 public class StorageInfo implements Writable {
   public int   layoutVersion;   // layout version of the storage data
   public int   namespaceID;     // id of the file system
+  public String clusterID;      // id of the cluster
   public long  cTime;           // creation time of the file system state
   
   public StorageInfo () {
-    this(0, 0, 0L);
+    this(0, 0, "", 0L);
   }
   
-  public StorageInfo(int layoutV, int nsID, long cT) {
+  public StorageInfo(int layoutV, int nsID, String cid, long cT) {
     layoutVersion = layoutV;
+    clusterID = cid;
     namespaceID = nsID;
     cTime = cT;
   }
@@ -63,13 +65,19 @@ public class StorageInfo implements Writ
   public int    getNamespaceID()  { return namespaceID; }
 
   /**
+   * cluster id of the file system.<p>
+   */
+  public String    getClusterID()  { return clusterID; }
+  
+  /**
    * Creation time of the file system state.<p>
    * Modified during upgrades.
    */
   public long   getCTime()        { return cTime; }
-
+  
   public void   setStorageInfo(StorageInfo from) {
     layoutVersion = from.layoutVersion;
+    clusterID = from.clusterID;
     namespaceID = from.namespaceID;
     cTime = from.cTime;
   }
@@ -80,12 +88,21 @@ public class StorageInfo implements Writ
   public void write(DataOutput out) throws IOException {
     out.writeInt(getLayoutVersion());
     out.writeInt(getNamespaceID());
+    WritableUtils.writeString(out, clusterID);
     out.writeLong(getCTime());
   }
 
   public void readFields(DataInput in) throws IOException {
     layoutVersion = in.readInt();
     namespaceID = in.readInt();
+    clusterID = WritableUtils.readString(in);
     cTime = in.readLong();
   }
+  
+  public String toString() {
+    StringBuilder sb = new StringBuilder();
+    sb.append("lv=").append(layoutVersion).append(";cid=").append(clusterID)
+    .append(";nsid=").append(namespaceID).append(";c=").append(cTime);
+    return sb.toString();
+  }
 }

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockReceiver.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockReceiver.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockReceiver.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockReceiver.java Fri Apr 29 18:16:32 2011
@@ -36,14 +36,16 @@ import java.util.zip.Checksum;
 
 import org.apache.commons.logging.Log;
 import org.apache.hadoop.fs.FSOutputSummer;
-import org.apache.hadoop.hdfs.protocol.Block;
 import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.BlockConstructionStage;
 import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.PacketHeader;
 import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.PipelineAck;
 import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.Status;
 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
+import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
 import org.apache.hadoop.hdfs.protocol.FSConstants;
 import org.apache.hadoop.hdfs.protocol.LocatedBlock;
+import org.apache.hadoop.hdfs.server.protocol.DatanodeProtocol;
+import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
 import org.apache.hadoop.hdfs.util.DataTransferThrottler;
 import org.apache.hadoop.io.IOUtils;
 import org.apache.hadoop.util.Daemon;
@@ -87,14 +89,14 @@ class BlockReceiver implements Closeable
   private final boolean isDatanode;
 
   /** the block to receive */
-  private final Block block; 
+  private final ExtendedBlock block; 
   /** the replica to write */
   private final ReplicaInPipelineInterface replicaInfo;
   /** pipeline stage */
   private final BlockConstructionStage stage;
   private final boolean isTransfer;
 
-  BlockReceiver(final Block block, final DataInputStream in,
+  BlockReceiver(final ExtendedBlock block, final DataInputStream in,
       final String inAddr, final String myAddr,
       final BlockConstructionStage stage, 
       final long newGs, final long minBytesRcvd, final long maxBytesRcvd, 
@@ -145,14 +147,16 @@ class BlockReceiver implements Closeable
         case PIPELINE_SETUP_APPEND:
           replicaInfo = datanode.data.append(block, newGs, minBytesRcvd);
           if (datanode.blockScanner != null) { // remove from block scanner
-            datanode.blockScanner.deleteBlock(block);
+            datanode.blockScanner.deleteBlock(block.getBlockPoolId(),
+                block.getLocalBlock());
           }
           block.setGenerationStamp(newGs);
           break;
         case PIPELINE_SETUP_APPEND_RECOVERY:
           replicaInfo = datanode.data.recoverAppend(block, newGs, minBytesRcvd);
           if (datanode.blockScanner != null) { // remove from block scanner
-            datanode.blockScanner.deleteBlock(block);
+            datanode.blockScanner.deleteBlock(block.getBlockPoolId(),
+                block.getLocalBlock());
           }
           block.setGenerationStamp(newGs);
           break;
@@ -267,7 +271,8 @@ class BlockReceiver implements Closeable
    * affect this datanode unless it is caused by interruption.
    */
   private void handleMirrorOutError(IOException ioe) throws IOException {
-    LOG.info(datanode.dnRegistration + ":Exception writing block " +
+    String bpid = block.getBlockPoolId();
+    LOG.info(datanode.getDNRegistrationForBP(bpid) + ":Exception writing block " +
              block + " to mirror " + mirrorAddr + "\n" +
              StringUtils.stringifyException(ioe));
     if (Thread.interrupted()) { // shut down if the thread is interrupted
@@ -286,6 +291,7 @@ class BlockReceiver implements Closeable
   private void verifyChunks( byte[] dataBuf, int dataOff, int len, 
                              byte[] checksumBuf, int checksumOff ) 
                              throws IOException {
+    DatanodeProtocol nn = datanode.getBPNamenode(block.getBlockPoolId());
     while (len > 0) {
       int chunkLen = Math.min(len, bytesPerChecksum);
       
@@ -298,7 +304,7 @@ class BlockReceiver implements Closeable
                       srcDataNode + " to namenode");
             LocatedBlock lb = new LocatedBlock(block, 
                                             new DatanodeInfo[] {srcDataNode});
-            datanode.namenode.reportBadBlocks(new LocatedBlock[] {lb});
+            nn.reportBadBlocks(new LocatedBlock[] {lb});
           } catch (IOException e) {
             LOG.warn("Failed to report bad block " + block + 
                       " from datanode " + srcDataNode + " to namenode");
@@ -974,10 +980,12 @@ class BlockReceiver implements Closeable
               datanode.closeBlock(block, DataNode.EMPTY_DEL_HINT);
               if (ClientTraceLog.isInfoEnabled() && isClient) {
                 long offset = 0;
+                DatanodeRegistration dnR = 
+                  datanode.getDNRegistrationForBP(block.getBlockPoolId());
                 ClientTraceLog.info(String.format(DN_CLIENTTRACE_FORMAT,
                       inAddr, myAddr, block.getNumBytes(),
                       "HDFS_WRITE", clientname, offset,
-                      datanode.dnRegistration.getStorageID(), block, endTime-startTime));
+                      dnR.getStorageID(), block, endTime-startTime));
               } else {
                 LOG.info("Received block " + block + " of size "
                     + block.getNumBytes() + " from " + inAddr);

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockSender.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockSender.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockSender.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockSender.java Fri Apr 29 18:16:32 2011
@@ -31,7 +31,7 @@ import java.util.Arrays;
 
 import org.apache.commons.logging.Log;
 import org.apache.hadoop.fs.ChecksumException;
-import org.apache.hadoop.hdfs.protocol.Block;
+import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
 import org.apache.hadoop.hdfs.protocol.FSConstants;
 import org.apache.hadoop.hdfs.protocol.DataTransferProtocol.PacketHeader;
 import org.apache.hadoop.hdfs.util.DataTransferThrottler;
@@ -47,7 +47,7 @@ class BlockSender implements java.io.Clo
   public static final Log LOG = DataNode.LOG;
   static final Log ClientTraceLog = DataNode.ClientTraceLog;
   
-  private Block block; // the block to read from
+  private ExtendedBlock block; // the block to read from
 
   /** the replica to read from */
   private final Replica replica;
@@ -83,21 +83,22 @@ class BlockSender implements java.io.Clo
   private volatile ChunkChecksum lastChunkChecksum = null;
 
   
-  BlockSender(Block block, long startOffset, long length,
+  BlockSender(ExtendedBlock block, long startOffset, long length,
               boolean corruptChecksumOk, boolean chunkOffsetOK,
               boolean verifyChecksum, DataNode datanode) throws IOException {
     this(block, startOffset, length, corruptChecksumOk, chunkOffsetOK,
          verifyChecksum, datanode, null);
   }
 
-  BlockSender(Block block, long startOffset, long length,
+  BlockSender(ExtendedBlock block, long startOffset, long length,
               boolean corruptChecksumOk, boolean chunkOffsetOK,
               boolean verifyChecksum, DataNode datanode, String clientTraceFmt)
       throws IOException {
     try {
       this.block = block;
       synchronized(datanode.data) { 
-        this.replica = datanode.data.getReplica(block.getBlockId());
+        this.replica = datanode.data.getReplica(block.getBlockPoolId(), 
+            block.getBlockId());
         if (replica == null) {
           throw new ReplicaNotFoundException(block);
         }
@@ -153,9 +154,8 @@ class BlockSender implements java.io.Clo
       this.clientTraceFmt = clientTraceFmt;
 
       if ( !corruptChecksumOk || datanode.data.metaFileExists(block) ) {
-        checksumIn = new DataInputStream(
-                new BufferedInputStream(datanode.data.getMetaDataInputStream(block),
-                                        BUFFER_SIZE));
+        checksumIn = new DataInputStream(new BufferedInputStream(datanode.data
+            .getMetaDataInputStream(block), BUFFER_SIZE));
 
         // read and handle the common header here. For now just a version
        BlockMetadataHeader header = BlockMetadataHeader.readHeader(checksumIn);
@@ -201,7 +201,8 @@ class BlockSender implements java.io.Clo
           || (length + startOffset) > endOffset) {
         String msg = " Offset " + startOffset + " and length " + length
         + " don't match block " + block + " ( blockLen " + endOffset + " )";
-        LOG.warn(datanode.dnRegistration + ":sendBlock() : " + msg);
+        LOG.warn(datanode.getDNRegistrationForBP(block.getBlockPoolId()) +
+            ":sendBlock() : " + msg);
         throw new IOException(msg);
       }
       

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockVolumeChoosingPolicy.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockVolumeChoosingPolicy.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockVolumeChoosingPolicy.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/BlockVolumeChoosingPolicy.java Fri Apr 29 18:16:32 2011
@@ -18,6 +18,8 @@
 package org.apache.hadoop.hdfs.server.datanode;
 
 import java.io.IOException;
+import java.util.List;
+
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
 import org.apache.hadoop.hdfs.server.datanode.FSDataset.FSVolume;
@@ -44,7 +46,7 @@ public interface BlockVolumeChoosingPoli
    * @return the chosen volume to store the block.
    * @throws IOException when disks are unavailable or are full.
    */
-  public FSVolume chooseVolume(FSVolume[] volumes, long blockSize)
+  public FSVolume chooseVolume(List<FSVolume> volumes, long blockSize)
     throws IOException;
 
 }

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/DataBlockScanner.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/DataBlockScanner.java?rev=1097905&r1=1097904&r2=1097905&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/DataBlockScanner.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/server/datanode/DataBlockScanner.java Fri Apr 29 18:16:32 2011
@@ -18,947 +18,279 @@
 
 package org.apache.hadoop.hdfs.server.datanode;
 
-import java.io.BufferedReader;
-import java.io.Closeable;
-import java.io.DataOutputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.FileReader;
 import java.io.IOException;
-import java.io.PrintStream;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
 import java.util.Iterator;
-import java.util.List;
-import java.util.Random;
-import java.util.TreeSet;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.TreeMap;
 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hdfs.DFSConfigKeys;
 import org.apache.hadoop.hdfs.protocol.Block;
-import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
-import org.apache.hadoop.hdfs.protocol.LocatedBlock;
-import org.apache.hadoop.hdfs.server.common.GenerationStamp;
-import org.apache.hadoop.hdfs.util.DataTransferThrottler;
-import org.apache.hadoop.io.IOUtils;
-import org.apache.hadoop.util.StringUtils;
+import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
+import org.apache.hadoop.hdfs.server.datanode.DataNode.BPOfferService;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 /**
- * Performs two types of scanning:
- * <li> Gets block files from the data directories and reconciles the
- * difference between the blocks on the disk and in memory in
- * {@link FSDataset}</li>
- * <li> Scans the data directories for block files and verifies that
- * the files are not corrupt</li>
- * This keeps track of blocks and their last verification times.
- * Currently it does not modify the metadata for block.
+ * DataBlockScanner manages block scanning for all the block pools. For each
+ * block pool a {@link BlockPoolSliceScanner} is created which runs in a separate
+ * thread to scan the blocks for that block pool. When a {@link BPOfferService}
+ * becomes alive or dies, blockPoolScannerMap in this class is updated.
  */
-
-class DataBlockScanner implements Runnable {
-  
+@InterfaceAudience.Private
+public class DataBlockScanner implements Runnable {
   public static final Log LOG = LogFactory.getLog(DataBlockScanner.class);
+  private final DataNode datanode;
+  private final FSDataset dataset;
+  private final Configuration conf;
   
-  private static final int MAX_SCAN_RATE = 8 * 1024 * 1024; // 8MB per sec
-  private static final int MIN_SCAN_RATE = 1 * 1024 * 1024; // 1MB per sec
-  
-  static final long DEFAULT_SCAN_PERIOD_HOURS = 21*24L; // three weeks
-  private static final long ONE_DAY = 24*3600*1000L;
-  
-  static final DateFormat dateFormat = 
-                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
-  
-  static final String verificationLogFile = "dncp_block_verification.log";
-  static final int verficationLogLimit = 5; // * numBlocks.
-
-  private long scanPeriod = DEFAULT_SCAN_PERIOD_HOURS * 3600 * 1000;
-  DataNode datanode;
-  FSDataset dataset;
-  
-  // sorted set
-  TreeSet<BlockScanInfo> blockInfoSet;
-  HashMap<Block, BlockScanInfo> blockMap;
-  
-  long totalScans = 0;
-  long totalVerifications = 0; // includes remote verification by clients.
-  long totalScanErrors = 0;
-  long totalTransientErrors = 0;
-  
-  long currentPeriodStart = System.currentTimeMillis();
-  long bytesLeft = 0; // Bytes to scan in this period
-  long totalBytesToScan = 0;
-  
-  private LogFileHandler verificationLog;
-  
-  Random random = new Random();
-  
-  DataTransferThrottler throttler = null;
-  
-  // Reconciles blocks on disk to blocks in memory
-  DirectoryScanner dirScanner;
-
-  private static enum ScanType {
-    REMOTE_READ,           // Verified when a block read by a client etc
-    VERIFICATION_SCAN,     // scanned as part of periodic verfication
-    NONE,
-  }
-  
-  static class BlockScanInfo implements Comparable<BlockScanInfo> {
-    Block block;
-    long lastScanTime = 0;
-    long lastLogTime = 0;
-    ScanType lastScanType = ScanType.NONE; 
-    boolean lastScanOk = true;
-    
-    BlockScanInfo(Block block) {
-      this.block = block;
-    }
-    
-    public int hashCode() {
-      return block.hashCode();
-    }
-    
-    public boolean equals(Object other) {
-      return other instanceof BlockScanInfo &&
-             compareTo((BlockScanInfo)other) == 0;
-    }
-    
-    long getLastScanTime() {
-      return ( lastScanType == ScanType.NONE) ? 0 : lastScanTime;
-    }
-    
-    public int compareTo(BlockScanInfo other) {
-      long t1 = lastScanTime;
-      long t2 = other.lastScanTime;
-      return ( t1 < t2 ) ? -1 : 
-                          (( t1 > t2 ) ? 1 : block.compareTo(other.block)); 
-    }
-  }
+  /**
+   * Map to find the BlockPoolScanner for a given block pool id. This is updated
+   * when a BPOfferService becomes alive or dies.
+   */
+  private final TreeMap<String, BlockPoolSliceScanner> blockPoolScannerMap = 
+    new TreeMap<String, BlockPoolSliceScanner>();
+  Thread blockScannerThread = null;
   
   DataBlockScanner(DataNode datanode, FSDataset dataset, Configuration conf) {
     this.datanode = datanode;
     this.dataset = dataset;
-    scanPeriod = conf.getInt(DFSConfigKeys.DFS_DATANODE_SCAN_PERIOD_HOURS_KEY, 
-                             DFSConfigKeys.DFS_DATANODE_SCAN_PERIOD_HOURS_DEFAULT);
-    if ( scanPeriod <= 0 ) {
-      scanPeriod = DEFAULT_SCAN_PERIOD_HOURS;
-    }
-    scanPeriod *= 3600 * 1000;
-    // initialized when the scanner thread is started.
-
-    dirScanner = new DirectoryScanner(dataset, conf);
+    this.conf = conf;
   }
   
-  private synchronized boolean isInitialized() {
-    return throttler != null;
-  }
-  
-  private void updateBytesToScan(long len, long lastScanTime) {
-    // len could be negative when a block is deleted.
-    totalBytesToScan += len;
-    if ( lastScanTime < currentPeriodStart ) {
-      bytesLeft += len;
-    }
-    // Should we change throttler bandwidth every time bytesLeft changes?
-    // not really required.
-  }
-  
-  private synchronized void addBlockInfo(BlockScanInfo info) {
-    boolean added = blockInfoSet.add(info);
-    blockMap.put(info.block, info);
-    
-    if ( added ) {
-      LogFileHandler log = verificationLog;
-      if (log != null) {
-        log.setMaxNumLines(blockMap.size() * verficationLogLimit);
+  public void run() {
+    String currentBpId = "";
+    boolean firstRun = true;
+    while (datanode.shouldRun && !Thread.interrupted()) {
+      //Sleep everytime except in the first interation.
+      if (!firstRun) {
+        try {
+          Thread.sleep(5000);
+        } catch (InterruptedException ex) {
+          // Interrupt itself again to set the interrupt status
+          blockScannerThread.interrupt();
+          continue;
+        }
+      } else {
+        firstRun = false;
       }
-      updateBytesToScan(info.block.getNumBytes(), info.lastScanTime);
-    }
-  }
-  
-  private synchronized void delBlockInfo(BlockScanInfo info) {
-    boolean exists = blockInfoSet.remove(info);
-    blockMap.remove(info.block);
-    if ( exists ) {
-      LogFileHandler log = verificationLog;
-      if (log != null) {
-        log.setMaxNumLines(blockMap.size() * verficationLogLimit);
+      
+      BlockPoolSliceScanner bpScanner = getNextBPScanner(currentBpId);
+      if (bpScanner == null) {
+        // Possible if thread is interrupted
+        continue;
       }
-      updateBytesToScan(-info.block.getNumBytes(), info.lastScanTime);
-    }
-  }
-  
-  /** Update blockMap by the given LogEntry */
-  private synchronized void updateBlockInfo(LogEntry e) {
-    BlockScanInfo info = blockMap.get(new Block(e.blockId, 0, e.genStamp));
-    
-    if(info != null && e.verificationTime > 0 && 
-        info.lastScanTime < e.verificationTime) {
-      delBlockInfo(info);
-      info.lastScanTime = e.verificationTime;
-      info.lastScanType = ScanType.VERIFICATION_SCAN;
-      addBlockInfo(info);
-    }
-  }
-
-  private void init() {
-    
-    // get the list of blocks and arrange them in random order
-    List<Block> arr = dataset.getFinalizedBlocks();
-    Collections.shuffle(arr);
-    
-    blockInfoSet = new TreeSet<BlockScanInfo>();
-    blockMap = new HashMap<Block, BlockScanInfo>();
-    
-    long scanTime = -1;
-    for (Block block : arr) {
-      BlockScanInfo info = new BlockScanInfo( block );
-      info.lastScanTime = scanTime--; 
-      //still keep 'info.lastScanType' to NONE.
-      addBlockInfo(info);
-    }
-
-    /* Pick the first directory that has any existing scanner log.
-     * otherwise, pick the first directory.
-     */
-    File dir = null;
-    FSDataset.FSVolume[] volumes = dataset.volumes.volumes;
-    for(FSDataset.FSVolume vol : volumes) {
-      if (LogFileHandler.isFilePresent(vol.getDir(), verificationLogFile)) {
-        dir = vol.getDir();
-        break;
+      currentBpId = bpScanner.getBlockPoolId();
+      // If BPOfferService for this pool is not alive, don't process it
+      if (!datanode.isBPServiceAlive(currentBpId)) {
+        LOG.warn("Block Pool " + currentBpId + " is not alive");
+        // Remove in case BP service died abruptly without proper shutdown
+        removeBlockPool(currentBpId);
+        continue;
       }
-    }
-    if (dir == null) {
-      dir = volumes[0].getDir();
-    }
-    
-    try {
-      // max lines will be updated later during initialization.
-      verificationLog = new LogFileHandler(dir, verificationLogFile, 100);
-    } catch (IOException e) {
-      LOG.warn("Could not open verfication log. " +
-               "Verification times are not stored.");
-    }
-    
-    synchronized (this) {
-      throttler = new DataTransferThrottler(200, MAX_SCAN_RATE);
+      bpScanner.scanBlockPoolSlice();
     }
   }
 
-  private synchronized long getNewBlockScanTime() {
-    /* If there are a lot of blocks, this returns a random time with in 
-     * the scan period. Otherwise something sooner.
-     */
-    long period = Math.min(scanPeriod, 
-                           Math.max(blockMap.size(),1) * 600 * 1000L);
-    return System.currentTimeMillis() - scanPeriod + 
-           random.nextInt((int)period);    
-  }
-
-  /** Adds block to list of blocks */
-  synchronized void addBlock(Block block) {
-    if (!isInitialized()) {
-      return;
-    }
+  // Wait for at least one block pool to be up
+  private void waitForInit(String bpid) {
+    UpgradeManagerDatanode um = null;
+    if(bpid != null && !bpid.equals(""))
+      um = DataNode.getUpgradeManagerDatanode(bpid);
     
-    BlockScanInfo info = blockMap.get(block);
-    if ( info != null ) {
-      LOG.warn("Adding an already existing block " + block);
-      delBlockInfo(info);
+    while ((um != null && ! um.isUpgradeCompleted())
+        || (getBlockPoolSetSize() < datanode.getAllBpOs().length)
+        || (getBlockPoolSetSize() < 1)) {
+      try {
+        Thread.sleep(5000);
+      } catch (InterruptedException e) {
+        blockScannerThread.interrupt();
+        return;
+      }
     }
-    
-    info = new BlockScanInfo(block);    
-    info.lastScanTime = getNewBlockScanTime();
-    
-    addBlockInfo(info);
-    adjustThrottler();
   }
   
-  /** Deletes the block from internal structures */
-  synchronized void deleteBlock(Block block) {
-    if (!isInitialized()) {
-      return;
-    }
-    BlockScanInfo info = blockMap.get(block);
-    if ( info != null ) {
-      delBlockInfo(info);
+  /**
+   * Find next block pool id to scan. There should be only one current
+   * verification log file. Find which block pool contains the current
+   * verification log file and that is used as the starting block pool id. If no
+   * current files are found start with first block-pool in the blockPoolSet.
+   * However, if more than one current files are found, the one with latest 
+   * modification time is used to find the next block pool id.
+   */
+  private BlockPoolSliceScanner getNextBPScanner(String currentBpId) {
+    
+    String nextBpId = null;
+    while ((nextBpId == null) && datanode.shouldRun
+        && !blockScannerThread.isInterrupted()) {
+      waitForInit(currentBpId);
+      synchronized (this) {
+        if (getBlockPoolSetSize() > 0) {          
+          // Find nextBpId by finding the last modified current log file, if any
+          long lastScanTime = -1;
+          Iterator<String> bpidIterator = blockPoolScannerMap.keySet()
+              .iterator();
+          while (bpidIterator.hasNext()) {
+            String bpid = bpidIterator.next();
+            for (FSDataset.FSVolume vol : dataset.volumes.getVolumes()) {
+              try {
+                File currFile = BlockPoolSliceScanner.getCurrentFile(vol, bpid);
+                if (currFile.exists()) {
+                  long lastModified = currFile.lastModified();
+                  if (lastScanTime < lastModified) {
+                    lastScanTime = lastModified;
+                    nextBpId = bpid;
+                  }
+                }
+              } catch (IOException e) {
+                LOG.warn("Received exception: ", e);
+              }
+            }
+          }
+          
+          // nextBpId can still be null if no current log is found,
+          // find nextBpId sequentially.
+          if (nextBpId == null) {
+            if ("".equals(currentBpId)) {
+              nextBpId = blockPoolScannerMap.firstKey();
+            } else {
+              nextBpId = blockPoolScannerMap.higherKey(currentBpId);
+              if (nextBpId == null) {
+                nextBpId = blockPoolScannerMap.firstKey();
+              }
+            }
+          }
+          if (nextBpId != null) {
+            return getBPScanner(nextBpId);
+          }
+        }
+      }
+      LOG.warn("No block pool is up, going to wait");
+      try {
+        Thread.sleep(5000);
+      } catch (InterruptedException ex) {
+        LOG.warn("Received exception: " + ex);
+        blockScannerThread.interrupt();
+        return null;
+      }
     }
+    return null;
   }
 
-  /** @return the last scan time */
-  synchronized long getLastScanTime(Block block) {
-    if (!isInitialized()) {
-      return 0;
-    }
-    BlockScanInfo info = blockMap.get(block);
-    return info == null? 0: info.lastScanTime;
+  private synchronized int getBlockPoolSetSize() {
+    return blockPoolScannerMap.size();
   }
-
-  /** Deletes blocks from internal structures */
-  void deleteBlocks(Block[] blocks) {
-    for ( Block b : blocks ) {
-      deleteBlock(b);
-    }
+  
+  private synchronized BlockPoolSliceScanner getBPScanner(String bpid) {
+    return blockPoolScannerMap.get(bpid);
   }
   
-  void verifiedByClient(Block block) {
-    updateScanStatus(block, ScanType.REMOTE_READ, true);
+  private synchronized String[] getBpIdList() {
+    return blockPoolScannerMap.keySet().toArray(
+        new String[blockPoolScannerMap.keySet().size()]);
   }
   
-  private synchronized void updateScanStatus(Block block, 
-                                             ScanType type,
-                                             boolean scanOk) {
-    if (!isInitialized()) {
-      return;
-    }
-    BlockScanInfo info = blockMap.get(block);
-    
-    if ( info != null ) {
-      delBlockInfo(info);
+  public void addBlock(ExtendedBlock block) {
+    BlockPoolSliceScanner bpScanner = getBPScanner(block.getBlockPoolId());
+    if (bpScanner != null) {
+      bpScanner.addBlock(block);
     } else {
-      // It might already be removed. Thats ok, it will be caught next time.
-      info = new BlockScanInfo(block);
-    }
-    
-    long now = System.currentTimeMillis();
-    info.lastScanType = type;
-    info.lastScanTime = now;
-    info.lastScanOk = scanOk;
-    addBlockInfo(info);
-    
-    if (type == ScanType.REMOTE_READ) {
-      totalVerifications++;
-    }
-        
-    // Don't update meta data too often in case of REMOTE_READ
-    // of if the verification failed.
-    long diff = now - info.lastLogTime;
-    if (!scanOk || (type == ScanType.REMOTE_READ &&
-                    diff < scanPeriod/3 && diff < ONE_DAY)) {
-      return;
-    }
-    
-    info.lastLogTime = now;
-    LogFileHandler log = verificationLog;
-    if (log != null) {
-      log.appendLine("date=\"" + dateFormat.format(new Date(now)) + "\"\t " +
-          "time=\"" + now + "\"\t " +
-          "genstamp=\"" + block.getGenerationStamp() + "\"\t " +
-          "id=\"" + block.getBlockId() +"\"");
+      LOG.warn("No block pool scanner found for block pool id: "
+          + block.getBlockPoolId());
     }
   }
   
-  private void handleScanFailure(Block block) {
-    
-    LOG.info("Reporting bad block " + block + " to namenode.");
-    
-    try {
-      DatanodeInfo[] dnArr = { new DatanodeInfo(datanode.dnRegistration) };
-      LocatedBlock[] blocks = { new LocatedBlock(block, dnArr) }; 
-      datanode.namenode.reportBadBlocks(blocks);
-    } catch (IOException e){
-      /* One common reason is that NameNode could be in safe mode.
-       * Should we keep on retrying in that case?
-       */
-      LOG.warn("Failed to report bad block " + block + " to namenode : " +
-               " Exception : " + StringUtils.stringifyException(e));
+  public synchronized boolean isInitialized(String bpid) {
+    BlockPoolSliceScanner bpScanner = getBPScanner(bpid);
+    if (bpScanner != null) {
+      return bpScanner.isInitialized();
     }
+    return false;
   }
-  
-  static private class LogEntry {
-    long blockId = -1;
-    long verificationTime = -1;
-    long genStamp = GenerationStamp.GRANDFATHER_GENERATION_STAMP;
-    
-    /**
-     * The format consists of single line with multiple entries. each 
-     * entry is in the form : name="value".
-     * This simple text and easily extendable and easily parseable with a
-     * regex.
-     */
-    private static Pattern entryPattern = 
-      Pattern.compile("\\G\\s*([^=\\p{Space}]+)=\"(.*?)\"\\s*");
-    
-    static LogEntry parseEntry(String line) {
-      LogEntry entry = new LogEntry();
-      
-      Matcher matcher = entryPattern.matcher(line);
-      while (matcher.find()) {
-        String name = matcher.group(1);
-        String value = matcher.group(2);
-        
-        try {
-          if (name.equals("id")) {
-            entry.blockId = Long.valueOf(value);
-          } else if (name.equals("time")) {
-            entry.verificationTime = Long.valueOf(value);
-          } else if (name.equals("genstamp")) {
-            entry.genStamp = Long.valueOf(value);
-          }
-        } catch(NumberFormatException nfe) {
-          LOG.warn("Cannot parse line: " + line, nfe);
-          return null;
-        }
-      }
-      
-      return entry;
+
+  public synchronized void printBlockReport(StringBuilder buffer,
+      boolean summary) {
+    String[] bpIdList = getBpIdList();
+    if (bpIdList == null || bpIdList.length == 0) {
+      buffer.append("Periodic block scanner is not yet initialized. "
+          + "Please check back again after some time.");
+      return;
+    }
+    for (String bpid : bpIdList) {
+      BlockPoolSliceScanner bpScanner = getBPScanner(bpid);
+      buffer.append("\n\nBlock report for block pool: "+bpid + "\n");
+      bpScanner.printBlockReport(buffer, summary);
+      buffer.append("\n");
     }
   }
   
-  private synchronized void adjustThrottler() {
-    long timeLeft = currentPeriodStart+scanPeriod - System.currentTimeMillis();
-    long bw = Math.max(bytesLeft*1000/timeLeft, MIN_SCAN_RATE);
-    throttler.setBandwidth(Math.min(bw, MAX_SCAN_RATE));
+  public void deleteBlock(String poolId, Block toDelete) {
+    BlockPoolSliceScanner bpScanner = getBPScanner(poolId);
+    if (bpScanner != null) {
+      bpScanner.deleteBlock(toDelete);
+    } else {
+      LOG.warn("No block pool scanner found for block pool id: "
+          + poolId);
+    }
   }
-  
-  private void verifyBlock(Block block) {
-    
-    BlockSender blockSender = null;
-
-    /* In case of failure, attempt to read second time to reduce
-     * transient errors. How do we flush block data from kernel 
-     * buffers before the second read? 
-     */
-    for (int i=0; i<2; i++) {
-      boolean second = (i > 0);
-      
-      try {
-        adjustThrottler();
-        
-        blockSender = new BlockSender(block, 0, -1, false, 
-                                               false, true, datanode);
-
-        DataOutputStream out = 
-                new DataOutputStream(new IOUtils.NullOutputStream());
-        
-        blockSender.sendBlock(out, null, throttler);
-
-        LOG.info((second ? "Second " : "") +
-                 "Verification succeeded for " + block);
-        
-        if ( second ) {
-          totalTransientErrors++;
-        }
-        
-        updateScanStatus(block, ScanType.VERIFICATION_SCAN, true);
 
-        return;
-      } catch (IOException e) {
-
-        updateScanStatus(block, ScanType.VERIFICATION_SCAN, false);
-
-        // If the block does not exists anymore, then its not an error
-        if ( dataset.getFile(block) == null ) {
-          LOG.info("Verification failed for " + block + ". Its ok since " +
-          "it not in datanode dataset anymore.");
-          deleteBlock(block);
-          return;
-        }
-
-        LOG.warn((second ? "Second " : "First ") + 
-                 "Verification failed for " + block + ". Exception : " +
-                 StringUtils.stringifyException(e));
-        
-        if (second) {
-          totalScanErrors++;
-          datanode.getMetrics().blockVerificationFailures.inc(); 
-          handleScanFailure(block);
-          return;
-        } 
-      } finally {
-        IOUtils.closeStream(blockSender);
-        datanode.getMetrics().blocksVerified.inc();
-        totalScans++;
-        totalVerifications++;
-      }
+  public void deleteBlocks(String poolId, Block[] toDelete) {
+    BlockPoolSliceScanner bpScanner = getBPScanner(poolId);
+    if (bpScanner != null) {
+      bpScanner.deleteBlocks(toDelete);
+    } else {
+      LOG.warn("No block pool scanner found for block pool id: "
+          + poolId);
     }
   }
   
-  private synchronized long getEarliestScanTime() {
-    if ( blockInfoSet.size() > 0 ) {
-      return blockInfoSet.first().lastScanTime;
-    }
-    return Long.MAX_VALUE; 
-  }
-  
-  // Picks one block and verifies it
-  private void verifyFirstBlock() {
-    Block block = null;
-    synchronized (this) {
-      if ( blockInfoSet.size() > 0 ) {
-        block = blockInfoSet.first().block;
-      }
-    }
-    
-    if ( block != null ) {
-      verifyBlock(block);
+  public synchronized void shutdown() {
+    if (blockScannerThread != null) {
+      blockScannerThread.interrupt();
     }
   }
-  
-  /** returns false if the process was interrupted
-   * because the thread is marked to exit.
-   */
-  private boolean assignInitialVerificationTimes() {
-    int numBlocks = 1;
-    LogFileHandler log = null;
-    synchronized (this) {
-      log = verificationLog;
-      numBlocks = Math.max(blockMap.size(), 1);
-    }
-    
-    //First udpates the last verification times from the log file.
-    LogFileHandler.Reader logReader = null;
-    try {
-      if (log != null) {
-        logReader = log.new Reader(false);
-      }
-    } catch (IOException e) {
-      LOG.warn("Could not read previous verification times : " +
-               StringUtils.stringifyException(e));
-    }
-    
-    if (log != null) {
-      log.updateCurNumLines();
-    }
-    
-    try {
-    // update verification times from the verificationLog.
-    while (logReader != null && logReader.hasNext()) {
-      if (!datanode.shouldRun || Thread.interrupted()) {
-        return false;
-      }
-      LogEntry entry = LogEntry.parseEntry(logReader.next());
-      if (entry != null) {
-        updateBlockInfo(entry);
-      }
-    }
-    } finally {
-      IOUtils.closeStream(logReader);
-    }
-    
-    /* Initially spread the block reads over half of 
-     * MIN_SCAN_PERIOD so that we don't keep scanning the 
-     * blocks too quickly when restarted.
-     */
-    long verifyInterval = (long) (Math.min( scanPeriod/2.0/numBlocks,
-                                            10*60*1000 ));
-    long lastScanTime = System.currentTimeMillis() - scanPeriod;
-    
-    /* Before this loop, entries in blockInfoSet that are not
-     * updated above have lastScanTime of <= 0 . Loop until first entry has
-     * lastModificationTime > 0.
-     */    
-    synchronized (this) {
-      if (blockInfoSet.size() > 0 ) {
-        BlockScanInfo info;
-        while ((info =  blockInfoSet.first()).lastScanTime < 0) {
-          delBlockInfo(info);        
-          info.lastScanTime = lastScanTime;
-          lastScanTime += verifyInterval;
-          addBlockInfo(info);
-        }
-      }
+
+  public synchronized void addBlockPool(String blockPoolId) {
+    if (blockPoolScannerMap.get(blockPoolId) != null) {
+      return;
     }
-    
-    return true;
-  }
-  
-  private synchronized void startNewPeriod() {
-    LOG.info("Starting a new period : work left in prev period : " +
-             String.format("%.2f%%", (bytesLeft * 100.0)/totalBytesToScan));
-    // reset the byte counts :
-    bytesLeft = totalBytesToScan;
-    currentPeriodStart = System.currentTimeMillis();
-  }
-  
-  public void run() {
+    BlockPoolSliceScanner bpScanner = new BlockPoolSliceScanner(datanode, dataset,
+        conf, blockPoolId);
     try {
-      
-      init();
-      
-      //Read last verification times
-      if (!assignInitialVerificationTimes()) {
-        return;
-      }
-      
-      adjustThrottler();
-      
-      while (datanode.shouldRun && !Thread.interrupted()) {
-        long now = System.currentTimeMillis();
-        synchronized (this) {
-          if ( now >= (currentPeriodStart + scanPeriod)) {
-            startNewPeriod();
-          }
-        }
-        if (dirScanner.newScanPeriod(now)) {
-          dirScanner.reconcile();
-          now = System.currentTimeMillis();
-        }
-        if ( (now - getEarliestScanTime()) >= scanPeriod ) {
-          verifyFirstBlock();
-        } else {
-          try {
-            Thread.sleep(1000);
-          } catch (InterruptedException ignored) {}
-        }
-      }
-    } catch (RuntimeException e) {
-      LOG.warn("RuntimeException during DataBlockScanner.run() : " +
-               StringUtils.stringifyException(e));
-      throw e;
-    } finally {
-      shutdown();
-      LOG.info("Exiting DataBlockScanner thread.");
+      bpScanner.init();
+    } catch (IOException ex) {
+      LOG.warn("Failed to initialized block scanner for pool id="+blockPoolId);
+      return;
     }
+    blockPoolScannerMap.put(blockPoolId, bpScanner);
+    LOG.info("Added bpid=" + blockPoolId + " to blockPoolScannerMap, new size="
+        + blockPoolScannerMap.size());
   }
   
-  synchronized void shutdown() {
-    LogFileHandler log = verificationLog;
-    verificationLog = null;
-    dirScanner.shutdown();
-    if (log != null) {
-      log.close();
-    }
+  public synchronized void removeBlockPool(String blockPoolId) {
+    blockPoolScannerMap.remove(blockPoolId);
+    LOG.info("Removed bpid="+blockPoolId+" from blockPoolScannerMap");
   }
   
-  synchronized void printBlockReport(StringBuilder buffer, 
-                                     boolean summaryOnly) {
-    long oneHour = 3600*1000;
-    long oneDay = 24*oneHour;
-    long oneWeek = 7*oneDay;
-    long fourWeeks = 4*oneWeek;
-    
-    int inOneHour = 0;
-    int inOneDay = 0;
-    int inOneWeek = 0;
-    int inFourWeeks = 0;
-    int inScanPeriod = 0;
-    int neverScanned = 0;
-    
-    int total = blockInfoSet.size();
-    
-    long now = System.currentTimeMillis();
-    
-    Date date = new Date();
-    
-    for(Iterator<BlockScanInfo> it = blockInfoSet.iterator(); it.hasNext();) {
-      BlockScanInfo info = it.next();
-      
-      long scanTime = info.getLastScanTime();
-      long diff = now - scanTime;
-      
-      if (diff <= oneHour) inOneHour++;
-      if (diff <= oneDay) inOneDay++;
-      if (diff <= oneWeek) inOneWeek++;
-      if (diff <= fourWeeks) inFourWeeks++;
-      if (diff <= scanPeriod) inScanPeriod++;      
-      if (scanTime <= 0) neverScanned++;
-      
-      if (!summaryOnly) {
-        date.setTime(scanTime);
-        String scanType = 
-          (info.lastScanType == ScanType.REMOTE_READ) ? "remote" : 
-            ((info.lastScanType == ScanType.VERIFICATION_SCAN) ? "local" :
-              "none");
-        buffer.append(String.format("%-26s : status : %-6s type : %-6s" +
-                                    " scan time : " +
-                                    "%-15d %s\n", info.block, 
-                                    (info.lastScanOk ? "ok" : "failed"),
-                                    scanType, scanTime,
-                                    (scanTime <= 0) ? "not yet verified" : 
-                                      dateFormat.format(date)));
-      }
+  // This method is used for testing
+  long getBlocksScannedInLastRun(String bpid) throws IOException {
+    BlockPoolSliceScanner bpScanner = getBPScanner(bpid);
+    if (bpScanner == null) {
+      throw new IOException("Block Pool: "+bpid+" is not running");
+    } else {
+      return bpScanner.getBlocksScannedInLastRun();
     }
-    
-    double pctPeriodLeft = (scanPeriod + currentPeriodStart - now)
-                           *100.0/scanPeriod;
-    double pctProgress = (totalBytesToScan == 0) ? 100 :
-                         (totalBytesToScan-bytesLeft)*10000.0/totalBytesToScan/
-                         (100-pctPeriodLeft+1e-10);
-    
-    buffer.append(String.format("\nTotal Blocks                 : %6d" +
-                                "\nVerified in last hour        : %6d" +
-                                "\nVerified in last day         : %6d" +
-                                "\nVerified in last week        : %6d" +
-                                "\nVerified in last four weeks  : %6d" +
-                                "\nVerified in SCAN_PERIOD      : %6d" +
-                                "\nNot yet verified             : %6d" +
-                                "\nVerified since restart       : %6d" +
-                                "\nScans since restart          : %6d" +
-                                "\nScan errors since restart    : %6d" +
-                                "\nTransient scan errors        : %6d" +
-                                "\nCurrent scan rate limit KBps : %6d" +
-                                "\nProgress this period         : %6.0f%%" +
-                                "\nTime left in cur period      : %6.2f%%" +
-                                "\n", 
-                                total, inOneHour, inOneDay, inOneWeek,
-                                inFourWeeks, inScanPeriod, neverScanned,
-                                totalVerifications, totalScans, 
-                                totalScanErrors, totalTransientErrors, 
-                                Math.round(throttler.getBandwidth()/1024.0),
-                                pctProgress, pctPeriodLeft));
   }
-  
-  /**
-   * This class takes care of log file used to store the last verification
-   * times of the blocks. It rolls the current file when it is too big etc.
-   * If there is an error while writing, it stops updating with an error
-   * message.
-   */
-  private static class LogFileHandler {
-    
-    private static final String curFileSuffix = ".curr";
-    private static final String prevFileSuffix = ".prev";
-    
-    // Don't roll files more often than this
-    private static final long minRollingPeriod = 6 * 3600 * 1000L; // 6 hours
-    private static final long minWarnPeriod = minRollingPeriod;
-    private static final int minLineLimit = 1000;
-    
-    
-    static boolean isFilePresent(File dir, String filePrefix) {
-      return new File(dir, filePrefix + curFileSuffix).exists() ||
-             new File(dir, filePrefix + prevFileSuffix).exists();
-    }
-    private File curFile;
-    private File prevFile;
-    
-    private int maxNumLines = -1; // not very hard limit on number of lines.
-    private int curNumLines = -1;
-    
-    long lastWarningTime = 0;
-    
-    private PrintStream out;
-    
-    int numReaders = 0;
-        
-    /**
-     * Opens the log file for appending.
-     * Note that rolling will happen only after "updateLineCount()" is 
-     * called. This is so that line count could be updated in a separate
-     * thread without delaying start up.
-     * 
-     * @param dir where the logs files are located.
-     * @param filePrefix prefix of the file.
-     * @param maxNumLines max lines in a file (its a soft limit).
-     * @throws IOException
-     */
-    LogFileHandler(File dir, String filePrefix, int maxNumLines) 
-                                                throws IOException {
-      curFile = new File(dir, filePrefix + curFileSuffix);
-      prevFile = new File(dir, filePrefix + prevFileSuffix);
-      openCurFile();
-      curNumLines = -1;
-      setMaxNumLines(maxNumLines);
-    }
-    
-    // setting takes affect when next entry is added.
-    synchronized void setMaxNumLines(int maxNumLines) {
-      this.maxNumLines = Math.max(maxNumLines, minLineLimit);
-    }
-    
-    /**
-     * Append "\n" + line.
-     * If the log file need to be rolled, it will done after 
-     * appending the text.
-     * This does not throw IOException when there is an error while 
-     * appending. Currently does not throw an error even if rolling 
-     * fails (may be it should?).
-     * return true if append was successful.
-     */
-    synchronized boolean appendLine(String line) {
-      out.println();
-      out.print(line);
-      curNumLines += (curNumLines < 0) ? -1 : 1;
-      try {
-        rollIfRequired();
-      } catch (IOException e) {
-        warn("Rolling failed for " + curFile + " : " + e.getMessage());
-        return false;
-      }
-      return true;
-    }
-    
-    //warns only once in a while
-    synchronized private void warn(String msg) {
-      long now = System.currentTimeMillis();
-      if ((now - lastWarningTime) >= minWarnPeriod) {
-        lastWarningTime = now;
-        LOG.warn(msg);
-      }
-    }
-    
-    private synchronized void openCurFile() throws FileNotFoundException {
-      close();
-      out = new PrintStream(new FileOutputStream(curFile, true));
-    }
-    
-    //This reads the current file and updates the count.
-    void updateCurNumLines() {
-      int count = 0;
-      Reader it = null;
-      try {
-        for(it = new Reader(true); it.hasNext(); count++) {
-          it.next();
-        }
-      } catch (IOException e) {
-        
-      } finally {
-        synchronized (this) {
-          curNumLines = count;
-        }
-        IOUtils.closeStream(it);
-      }
-    }
-    
-    private void rollIfRequired() throws IOException {
-      if (curNumLines < maxNumLines || numReaders > 0) {
-        return;
-      }
-      
-      long now = System.currentTimeMillis();
-      if (now < minRollingPeriod) {
-        return;
-      }
-      
-      if (!prevFile.delete() && prevFile.exists()) {
-        throw new IOException("Could not delete " + prevFile);
-      }
-      
-      close();
-
-      if (!curFile.renameTo(prevFile)) {
-        openCurFile();
-        throw new IOException("Could not rename " + curFile + 
-                              " to " + prevFile);
-      }
-      
-      openCurFile();
-      updateCurNumLines();
-    }
-    
-    synchronized void close() {
-      if (out != null) {
-        out.close();
-        out = null;
-      }
-    }
-    
-    /**
-     * This is used to read the lines in order.
-     * If the data is not read completely (i.e, untill hasNext() returns
-     * false), it needs to be explicitly 
-     */
-    private class Reader implements Iterator<String>, Closeable {
-      
-      BufferedReader reader;
-      File file;
-      String line;
-      boolean closed = false;
-      
-      private Reader(boolean skipPrevFile) throws IOException {
-        synchronized (LogFileHandler.this) {
-          numReaders++; 
-        }
-        reader = null;
-        file = (skipPrevFile) ? curFile : prevFile;
-        readNext();        
-      }
-      
-      private boolean openFile() throws IOException {
-
-        for(int i=0; i<2; i++) {
-          if (reader != null || i > 0) {
-            // move to next file
-            file = (file == prevFile) ? curFile : null;
-          }
-          if (file == null) {
-            return false;
-          }
-          if (file.exists()) {
-            break;
-          }
-        }
-        
-        if (reader != null ) {
-          reader.close();
-          reader = null;
-        }
-        
-        reader = new BufferedReader(new FileReader(file));
-        return true;
-      }
-      
-      // read next line if possible.
-      private void readNext() throws IOException {
-        line = null;
-        try {
-          if (reader != null && (line = reader.readLine()) != null) {
-            return;
-          }
-          if (line == null) {
-            // move to the next file.
-            if (openFile()) {
-              readNext();
-            }
-          }
-        } finally {
-          if (!hasNext()) {
-            close();
-          }
-        }
-      }
-      
-      public boolean hasNext() {
-        return line != null;
-      }
 
-      public String next() {
-        String curLine = line;
-        try {
-          readNext();
-        } catch (IOException e) {
-          LOG.info("Could not reade next line in LogHandler : " +
-                   StringUtils.stringifyException(e));
-        }
-        return curLine;
-      }
-
-      public void remove() {
-        throw new RuntimeException("remove() is not supported.");
-      }
-
-      public void close() throws IOException {
-        if (!closed) {
-          try {
-            if (reader != null) {
-              reader.close();
-            }
-          } finally {
-            file = null;
-            reader = null;
-            closed = true;
-            synchronized (LogFileHandler.this) {
-              numReaders--;
-              assert(numReaders >= 0);
-            }
-          }
-        }
-      }
-    }    
+  public void start() {
+    blockScannerThread = new Thread(this);
+    blockScannerThread.setDaemon(true);
+    blockScannerThread.start();
   }
   
   @InterfaceAudience.Private
@@ -967,25 +299,23 @@ class DataBlockScanner implements Runnab
 
     public void doGet(HttpServletRequest request, 
                       HttpServletResponse response) throws IOException {
-      
       response.setContentType("text/plain");
       
-      DataBlockScanner blockScanner = (DataBlockScanner)  
-          getServletContext().getAttribute("datanode.blockScanner");
+      DataNode datanode = (DataNode) getServletContext().getAttribute("datanode");
+      DataBlockScanner blockScanner = datanode.blockScanner;
       
       boolean summary = (request.getParameter("listblocks") == null);
       
       StringBuilder buffer = new StringBuilder(8*1024);
       if (blockScanner == null) {
+        LOG.warn("Periodic block scanner is not running");
         buffer.append("Periodic block scanner is not running. " +
                       "Please check the datanode log if this is unexpected.");
-      } else if (blockScanner.isInitialized()) {
-        blockScanner.printBlockReport(buffer, summary);
       } else {
-        buffer.append("Periodic block scanner is not yet initialized. " +
-                      "Please check back again after some time.");
+        blockScanner.printBlockReport(buffer, summary);
       }
       response.getWriter().write(buffer.toString()); // extra copy!
     }
   }
+
 }



Mime
View raw message