Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot.CURRENT_STATE_ID;
import static org.apache.hadoop.util.Time.now;
Expand Down Expand Up @@ -223,7 +224,7 @@ static LocatedBlock makeLocatedBlock(FSNamesystem fsn, BlockInfo blk,
*/
static LocatedBlock storeAllocatedBlock(FSNamesystem fsn, String src,
long fileId, String clientName, ExtendedBlock previous,
DatanodeStorageInfo[] targets) throws IOException {
DatanodeStorageInfo[] targets, AtomicReference<BlockInfo> newBlockRef) throws IOException {
long offset;
// Run the full analysis again, since things could have changed
// while chooseTarget() was executing.
Expand Down Expand Up @@ -256,7 +257,8 @@ static LocatedBlock storeAllocatedBlock(FSNamesystem fsn, String src,
// allocate new block, record block locations in INode.
Block newBlock = fsn.createNewBlock(blockType);
INodesInPath inodesInPath = INodesInPath.fromINode(pendingFile);
saveAllocatedBlock(fsn, src, inodesInPath, newBlock, targets, blockType);
BlockInfo newBlockInfo = saveAllocatedBlock(fsn, src, inodesInPath, newBlock, targets, blockType);
newBlockRef.set(newBlockInfo);

persistNewBlock(fsn, src, pendingFile);
offset = pendingFile.computeFileSize();
Expand Down Expand Up @@ -781,17 +783,17 @@ private static void persistNewBlock(
* @param targets target datanodes where replicas of the new block is placed
* @throws QuotaExceededException If addition of block exceeds space quota
*/
static void saveAllocatedBlock(FSNamesystem fsn, String src,
static BlockInfo saveAllocatedBlock(FSNamesystem fsn, String src,
INodesInPath inodesInPath, Block newBlock, DatanodeStorageInfo[] targets,
BlockType blockType) throws IOException {
assert fsn.hasWriteLock(RwLockMode.GLOBAL);
BlockInfo b = addBlock(fsn.dir, src, inodesInPath, newBlock, targets,
blockType);
logAllocatedBlock(src, b);
DatanodeStorageInfo.incrementBlocksScheduled(targets);
return b;
}

private static void logAllocatedBlock(String src, BlockInfo b) {
static void logAllocatedBlock(String src, BlockInfo b) {
if (!NameNode.stateChangeLog.isInfoEnabled()) {
return;
}
Expand All @@ -803,7 +805,7 @@ private static void logAllocatedBlock(String src, BlockInfo b) {
if (uc != null) {
uc.appendUCPartsConcise(sb);
}
sb.append(" for " + src);
sb.append(" for ").append(src);
NameNode.stateChangeLog.info(sb.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
Expand Down Expand Up @@ -3111,14 +3112,18 @@ LocatedBlock getAdditionalBlock(
checkOperation(OperationCategory.WRITE);
writeLock(RwLockMode.GLOBAL);
LocatedBlock lb;
AtomicReference<BlockInfo> newBlockRef = new AtomicReference<>();
try {
checkOperation(OperationCategory.WRITE);
lb = FSDirWriteFileOp.storeAllocatedBlock(
this, src, fileId, clientName, previous, targets);
this, src, fileId, clientName, previous, targets, newBlockRef);
} finally {
writeUnlock(RwLockMode.GLOBAL, operationName);
}
getEditLog().logSync();
if (newBlockRef.get() != null) {
FSDirWriteFileOp.logAllocatedBlock(src, newBlockRef.get());
}
return lb;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.util.EnumSet;
import java.util.concurrent.atomic.AtomicReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void testRetryAddBlockWhileInChooseTarget() throws Exception {
LocatedBlock newBlock;
try {
newBlock = FSDirWriteFileOp.storeAllocatedBlock(ns, src,
HdfsConstants.GRANDFATHER_INODE_ID, "clientName", null, targets);
HdfsConstants.GRANDFATHER_INODE_ID, "clientName", null, targets, new AtomicReference<>());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should use like a mockito-spy or verify to show that the logging happens as as side effect, and possibly when it doesn't happen, (eror etc)

} finally {
ns.writeUnlock(RwLockMode.GLOBAL, "testRetryAddBlockWhileInChooseTarget");
}
Expand Down
Loading