Uploaded image for project: 'Hadoop HDFS'
  1. Hadoop HDFS
  2. HDFS-9236

Missing sanity check for block size during block recovery



    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.7.1
    • 2.8.0, 3.0.0-alpha1
    • datanode
    • None
    • Reviewed


      Ran into an issue while running test against faulty data-node code.

      Currently in DataNode.java:

        /** Block synchronization */
        void syncBlock(RecoveringBlock rBlock,
                               List<BlockRecord> syncList) throws IOException {
          // Calculate the best available replica state.
          ReplicaState bestState = ReplicaState.RWR;
          // Calculate list of nodes that will participate in the recovery
          // and the new block size
          List<BlockRecord> participatingList = new ArrayList<BlockRecord>();
          final ExtendedBlock newBlock = new ExtendedBlock(bpid, blockId,
              -1, recoveryId);
          switch(bestState) {
          case RBW:
          case RWR:
            long minLength = Long.MAX_VALUE;
            for(BlockRecord r : syncList) {
              ReplicaState rState = r.rInfo.getOriginalReplicaState();
              if(rState == bestState) {
                minLength = Math.min(minLength, r.rInfo.getNumBytes());
              newBlock.getGenerationStamp(), newBlock.getNumBytes(), true, false,
              datanodes, storages);

      This code is called by the DN coordinating the block recovery. In the above case, it is possible for none of the rState (reported by DNs with copies of the replica being recovered) to match the bestState. This can either be caused by faulty DN code or stale/modified/corrupted files on DN. When this happens the DN will end up reporting the minLengh of Long.MAX_VALUE.

      Unfortunately there is no check on the NN for replica length. See FSNamesystem.java:

        void commitBlockSynchronization(ExtendedBlock oldBlock,
            long newgenerationstamp, long newlength,
            boolean closeFile, boolean deleteblock, DatanodeID[] newtargets,
            String[] newtargetstorages) throws IOException {
            if (deleteblock) {
              Block blockToDel = ExtendedBlock.getLocalBlock(oldBlock);
              boolean remove = iFile.removeLastBlock(blockToDel) != null;
              if (remove) {
            } else {
              // update last block
              if(!copyTruncate) {
                //>>>> XXX block length is updated without any check <<<<//
          if (closeFile) {
            LOG.info("commitBlockSynchronization(oldBlock=" + oldBlock
                + ", file=" + src
                + (copyTruncate ? ", newBlock=" + truncatedBlock
                    : ", newgenerationstamp=" + newgenerationstamp)
                + ", newlength=" + newlength
                + ", newtargets=" + Arrays.asList(newtargets) + ") successful");
          } else {
            LOG.info("commitBlockSynchronization(" + oldBlock + ") successful");

      After this point the block length becomes Long.MAX_VALUE. Any subsequent block report (even with correct length) will cause the block to be marked as corrupted. Since this is block could be the last block of the file. If this happens and the client goes away, NN won’t be able to recover the lease and close the file because the last block is under-replicated.

      I believe we need to have a sanity check for block size on both DN and NN to prevent such case from happening.


        1. HDFS-9236.007.patch
          8 kB
          Tony Wu
        2. HDFS-9236.006.patch
          7 kB
          Tony Wu
        3. HDFS-9236.005.patch
          5 kB
          Tony Wu
        4. HDFS-9236.004.patch
          5 kB
          Tony Wu
        5. HDFS-9236.003.patch
          5 kB
          Tony Wu
        6. HDFS-9236.002.patch
          5 kB
          Tony Wu
        7. HDFS-9236.001.patch
          8 kB
          Tony Wu



            twu Tony Wu
            twu Tony Wu
            0 Vote for this issue
            14 Start watching this issue