diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java index 7411a6a695..0e7a59b0c8 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManager.java @@ -1050,17 +1050,19 @@ public class BlockManager implements BlockStatsMXBean { final byte[] blockIndices = blk.isStriped() ? new byte[numMachines] : null; int j = 0, i = 0; if (numMachines > 0) { + final boolean noCorrupt = (numCorruptReplicas == 0); for(DatanodeStorageInfo storage : blocksMap.getStorages(blk)) { - final DatanodeDescriptor d = storage.getDatanodeDescriptor(); - final boolean replicaCorrupt = corruptReplicas.isReplicaCorrupt(blk, d); - if ((isCorrupt || (!replicaCorrupt)) && - storage.getState() != State.FAILED) { - machines[j++] = storage; - // TODO this can be more efficient - if (blockIndices != null) { - byte index = ((BlockInfoStriped) blk).getStorageBlockIndex(storage); - assert index >= 0; - blockIndices[i++] = index; + if (storage.getState() != State.FAILED) { + if (noCorrupt) { + machines[j++] = storage; + i = setBlockIndices(blk, blockIndices, i, storage); + } else { + final DatanodeDescriptor d = storage.getDatanodeDescriptor(); + final boolean replicaCorrupt = isReplicaCorrupt(blk, d); + if (isCorrupt || !replicaCorrupt) { + machines[j++] = storage; + i = setBlockIndices(blk, blockIndices, i, storage); + } } } } @@ -4586,4 +4588,19 @@ public class BlockManager implements BlockStatsMXBean { boolean isGenStampInFuture(Block block) { return blockIdManager.isGenStampInFuture(block); } + + boolean isReplicaCorrupt(BlockInfo blk, DatanodeDescriptor d) { + return corruptReplicas.isReplicaCorrupt(blk, d); + } + + private int setBlockIndices(BlockInfo blk, byte[] blockIndices, int i, + DatanodeStorageInfo storage) { + // TODO this can be more efficient + if (blockIndices != null) { + byte index = ((BlockInfoStriped)blk).getStorageBlockIndex(storage); + assert index >= 0; + blockIndices[i++] = index; + } + return i; + } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManager.java index bbaf91f338..535acd7349 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManager.java @@ -1265,4 +1265,23 @@ public class TestBlockManager { file.delete(); } } + + @Test + public void testIsReplicaCorruptCall() throws Exception { + BlockManager spyBM = spy(bm); + List origStorages = getStorages(0, 1, 3); + List origNodes = getNodes(origStorages); + BlockInfo blockInfo = addBlockOnNodes(0, origNodes); + spyBM.createLocatedBlocks(new BlockInfo[]{blockInfo}, 3L, false, 0L, 3L, + false, false, null, null); + verify(spyBM, Mockito.atLeast(0)). + isReplicaCorrupt(Mockito.any(BlockInfo.class), + Mockito.any(DatanodeDescriptor.class)); + addCorruptBlockOnNodes(0, origNodes); + spyBM.createLocatedBlocks(new BlockInfo[]{blockInfo}, 3L, false, 0L, 3L, + false, false, null, null); + verify(spyBM, Mockito.atLeast(1)). + isReplicaCorrupt(Mockito.any(BlockInfo.class), + Mockito.any(DatanodeDescriptor.class)); + } }