Skip to content

Commit 2cb9dac

Browse files
committed
HDFS-8071. Redundant checkFileProgress() in PART II of getAdditionalBlock(). Contributed by Konstantin Shvachko.
1 parent 64cf079 commit 2cb9dac

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,9 @@ Release 2.7.0 - UNRELEASED
465465
HDFS-7811. Avoid recursive call getStoragePolicyID in
466466
INodeFile#computeQuotaUsage. (Xiaoyu Yao and jing9)
467467

468+
HDFS-8071. Redundant checkFileProgress() in PART II of getAdditionalBlock().
469+
(shv)
470+
468471
OPTIMIZATIONS
469472

470473
HDFS-7454. Reduce memory footprint for AclEntries in NameNode.

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3032,6 +3032,10 @@ LocatedBlock getAdditionalBlock(String src, long fileId, String clientName,
30323032
FileState fileState = analyzeFileState(
30333033
src, fileId, clientName, previous, onRetryBlock);
30343034
final INodeFile pendingFile = fileState.inode;
3035+
// Check if the penultimate block is minimally replicated
3036+
if (!checkFileProgress(src, pendingFile, false)) {
3037+
throw new NotReplicatedYetException("Not replicated yet: " + src);
3038+
}
30353039
src = fileState.path;
30363040

30373041
if (onRetryBlock[0] != null && onRetryBlock[0].getLocations().length > 0) {
@@ -3244,11 +3248,6 @@ FileState analyzeFileState(String src,
32443248
"last block in file " + lastBlockInFile);
32453249
}
32463250
}
3247-
3248-
// Check if the penultimate block is minimally replicated
3249-
if (!checkFileProgress(src, pendingFile, false)) {
3250-
throw new NotReplicatedYetException("Not replicated yet: " + src);
3251-
}
32523251
return new FileState(pendingFile, src, iip);
32533252
}
32543253

@@ -3551,28 +3550,23 @@ Block createNewBlock() throws IOException {
35513550
* replicated. If not, return false. If checkall is true, then check
35523551
* all blocks, otherwise check only penultimate block.
35533552
*/
3554-
private boolean checkFileProgress(String src, INodeFile v, boolean checkall) {
3555-
readLock();
3556-
try {
3557-
if (checkall) {
3558-
// check all blocks of the file.
3559-
for (BlockInfoContiguous block: v.getBlocks()) {
3560-
if (!isCompleteBlock(src, block, blockManager.minReplication)) {
3561-
return false;
3562-
}
3563-
}
3564-
} else {
3565-
// check the penultimate block of this file
3566-
BlockInfoContiguous b = v.getPenultimateBlock();
3567-
if (b != null
3568-
&& !isCompleteBlock(src, b, blockManager.minReplication)) {
3553+
boolean checkFileProgress(String src, INodeFile v, boolean checkall) {
3554+
if (checkall) {
3555+
// check all blocks of the file.
3556+
for (BlockInfoContiguous block: v.getBlocks()) {
3557+
if (!isCompleteBlock(src, block, blockManager.minReplication)) {
35693558
return false;
35703559
}
35713560
}
3572-
return true;
3573-
} finally {
3574-
readUnlock();
3561+
} else {
3562+
// check the penultimate block of this file
3563+
BlockInfoContiguous b = v.getPenultimateBlock();
3564+
if (b != null
3565+
&& !isCompleteBlock(src, b, blockManager.minReplication)) {
3566+
return false;
3567+
}
35753568
}
3569+
return true;
35763570
}
35773571

35783572
private static boolean isCompleteBlock(String src, BlockInfoContiguous b, int minRepl) {

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestAddBlockRetry.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.doAnswer;
2525
import static org.mockito.Mockito.spy;
2626

27+
import java.io.IOException;
2728
import java.lang.reflect.Field;
2829
import java.util.EnumSet;
2930
import java.util.HashSet;
@@ -90,7 +91,7 @@ public void tearDown() throws Exception {
9091
public void testRetryAddBlockWhileInChooseTarget() throws Exception {
9192
final String src = "/testRetryAddBlockWhileInChooseTarget";
9293

93-
FSNamesystem ns = cluster.getNamesystem();
94+
final FSNamesystem ns = cluster.getNamesystem();
9495
BlockManager spyBM = spy(ns.getBlockManager());
9596
final NamenodeProtocols nn = cluster.getNameNodeRpc();
9697

@@ -107,11 +108,15 @@ public DatanodeStorageInfo[] answer(InvocationOnMock invocation)
107108
LOG.info("chooseTarget for " + src);
108109
DatanodeStorageInfo[] ret =
109110
(DatanodeStorageInfo[]) invocation.callRealMethod();
111+
assertTrue("Penultimate block must be complete",
112+
checkFileProgress(src, false));
110113
count++;
111114
if(count == 1) { // run second addBlock()
112115
LOG.info("Starting second addBlock for " + src);
113116
nn.addBlock(src, "clientName", null, null,
114117
INodeId.GRANDFATHER_INODE_ID, null);
118+
assertTrue("Penultimate block must be complete",
119+
checkFileProgress(src, false));
115120
LocatedBlocks lbs = nn.getBlockLocations(src, 0, Long.MAX_VALUE);
116121
assertEquals("Must be one block", 1, lbs.getLocatedBlocks().size());
117122
lb2 = lbs.get(0);
@@ -142,6 +147,16 @@ public DatanodeStorageInfo[] answer(InvocationOnMock invocation)
142147
assertEquals("Blocks are not equal", lb1.getBlock(), lb2.getBlock());
143148
}
144149

150+
boolean checkFileProgress(String src, boolean checkall) throws IOException {
151+
final FSNamesystem ns = cluster.getNamesystem();
152+
ns.readLock();
153+
try {
154+
return ns.checkFileProgress(src, ns.dir.getINode(src).asFile(), checkall);
155+
} finally {
156+
ns.readUnlock();
157+
}
158+
}
159+
145160
/*
146161
* Since NameNode will not persist any locations of the block, addBlock()
147162
* retry call after restart NN should re-select the locations and return to

0 commit comments

Comments
 (0)