6868import org .apache .hadoop .hdfs .server .blockmanagement .PendingDataNodeMessages .ReportedBlockInfo ;
6969import org .apache .hadoop .hdfs .server .common .HdfsServerConstants .BlockUCState ;
7070import org .apache .hadoop .hdfs .server .common .HdfsServerConstants .ReplicaState ;
71+ import org .apache .hadoop .hdfs .server .namenode .INodesInPath ;
7172import org .apache .hadoop .hdfs .server .namenode .NameNode ;
7273import org .apache .hadoop .hdfs .server .namenode .NameNode .OperationCategory ;
7374import org .apache .hadoop .hdfs .server .namenode .Namesystem ;
@@ -622,12 +623,13 @@ private static boolean commitBlock(
622623 *
623624 * @param bc block collection
624625 * @param commitBlock - contains client reported block length and generation
626+ * @param iip - INodes in path to bc
625627 * @return true if the last block is changed to committed state.
626628 * @throws IOException if the block does not have at least a minimal number
627629 * of replicas reported from data-nodes.
628630 */
629631 public boolean commitOrCompleteLastBlock (BlockCollection bc ,
630- Block commitBlock ) throws IOException {
632+ Block commitBlock , INodesInPath iip ) throws IOException {
631633 if (commitBlock == null )
632634 return false ; // not committing, this is a block allocation retry
633635 BlockInfoContiguous lastBlock = bc .getLastBlock ();
@@ -639,19 +641,21 @@ public boolean commitOrCompleteLastBlock(BlockCollection bc,
639641 final boolean b = commitBlock (
640642 (BlockInfoContiguousUnderConstruction ) lastBlock , commitBlock );
641643 if (countNodes (lastBlock ).liveReplicas () >= minReplication )
642- completeBlock (bc , bc .numBlocks ()-1 , false );
644+ completeBlock (bc , bc .numBlocks ()-1 , iip , false );
643645 return b ;
644646 }
645647
646648 /**
647649 * Convert a specified block of the file to a complete block.
648650 * @param bc file
649651 * @param blkIndex block index in the file
652+ * @param iip - INodes in path to file containing curBlock; if null,
653+ * this will be resolved internally
650654 * @throws IOException if the block does not have at least a minimal number
651655 * of replicas reported from data-nodes.
652656 */
653657 private BlockInfoContiguous completeBlock (final BlockCollection bc ,
654- final int blkIndex , boolean force ) throws IOException {
658+ final int blkIndex , INodesInPath iip , boolean force ) throws IOException {
655659 if (blkIndex < 0 )
656660 return null ;
657661 BlockInfoContiguous curBlock = bc .getBlocks ()[blkIndex ];
@@ -666,7 +670,7 @@ private BlockInfoContiguous completeBlock(final BlockCollection bc,
666670 if (!force && ucBlock .getBlockUCState () != BlockUCState .COMMITTED )
667671 throw new IOException (
668672 "Cannot complete block: block has not been COMMITTED by the client" );
669- BlockInfoContiguous completeBlock = ucBlock . convertToCompleteBlock ();
673+ BlockInfoContiguous completeBlock = convertToCompleteBlock (ucBlock , iip );
670674 // replace penultimate block in file
671675 bc .setBlock (blkIndex , completeBlock );
672676
@@ -685,15 +689,34 @@ private BlockInfoContiguous completeBlock(final BlockCollection bc,
685689 }
686690
687691 private BlockInfoContiguous completeBlock (final BlockCollection bc ,
688- final BlockInfoContiguous block , boolean force ) throws IOException {
692+ final BlockInfoContiguous block , INodesInPath iip , boolean force )
693+ throws IOException {
689694 BlockInfoContiguous [] fileBlocks = bc .getBlocks ();
690695 for (int idx = 0 ; idx < fileBlocks .length ; idx ++)
691696 if (fileBlocks [idx ] == block ) {
692- return completeBlock (bc , idx , force );
697+ return completeBlock (bc , idx , iip , force );
693698 }
694699 return block ;
695700 }
696701
702+ /**
703+ * Convert a specified block of the file to a complete block.
704+ * Skips validity checking and safe mode block total updates; use
705+ * {@link BlockManager#completeBlock} to include these.
706+ * @param curBlock - block to be completed
707+ * @param iip - INodes in path to file containing curBlock; if null,
708+ * this will be resolved internally
709+ * @throws IOException if the block does not have at least a minimal number
710+ * of replicas reported from data-nodes.
711+ */
712+ private BlockInfoContiguous convertToCompleteBlock (
713+ BlockInfoContiguousUnderConstruction curBlock , INodesInPath iip )
714+ throws IOException {
715+ BlockInfoContiguous complete = curBlock .convertToCompleteBlock ();
716+ namesystem .getFSDirectory ().updateSpaceForCompleteBlock (curBlock , iip );
717+ return complete ;
718+ }
719+
697720 /**
698721 * Force the given block in the given file to be marked as complete,
699722 * regardless of whether enough replicas are present. This is necessary
@@ -702,7 +725,7 @@ private BlockInfoContiguous completeBlock(final BlockCollection bc,
702725 public BlockInfoContiguous forceCompleteBlock (final BlockCollection bc ,
703726 final BlockInfoContiguousUnderConstruction block ) throws IOException {
704727 block .commitBlock (block );
705- return completeBlock (bc , block , true );
728+ return completeBlock (bc , block , null , true );
706729 }
707730
708731
@@ -2525,7 +2548,7 @@ private void addStoredBlockImmediate(BlockInfoContiguous storedBlock,
25252548 int numCurrentReplica = countLiveNodes (storedBlock );
25262549 if (storedBlock .getBlockUCState () == BlockUCState .COMMITTED
25272550 && numCurrentReplica >= minReplication ) {
2528- completeBlock (storedBlock .getBlockCollection (), storedBlock , false );
2551+ completeBlock (storedBlock .getBlockCollection (), storedBlock , null , false );
25292552 } else if (storedBlock .isComplete () && result == AddBlockResult .ADDED ) {
25302553 // check whether safe replication is reached for the block
25312554 // only complete blocks are counted towards that.
@@ -2599,7 +2622,7 @@ private Block addStoredBlock(final BlockInfoContiguous block,
25992622
26002623 if (storedBlock .getBlockUCState () == BlockUCState .COMMITTED &&
26012624 numLiveReplicas >= minReplication ) {
2602- storedBlock = completeBlock (bc , storedBlock , false );
2625+ storedBlock = completeBlock (bc , storedBlock , null , false );
26032626 } else if (storedBlock .isComplete () && result == AddBlockResult .ADDED ) {
26042627 // check whether safe replication is reached for the block
26052628 // only complete blocks are counted towards that
0 commit comments