HDFS-7638. Small fix and few refinements for FSN#truncate. (yliu)

This commit is contained in:
yliu 2015-01-24 16:29:50 -08:00 committed by Konstantin V Shvachko
parent a116e1fe9e
commit 0fdd09a895
2 changed files with 13 additions and 9 deletions

View File

@ -489,6 +489,8 @@ Release 2.7.0 - UNRELEASED
HDFS-7606. Fix potential NPE in INodeFile.getBlocks(). (Byron Wong via shv) HDFS-7606. Fix potential NPE in INodeFile.getBlocks(). (Byron Wong via shv)
HDFS-7638: Small fix and few refinements for FSN#truncate. (yliu)
Release 2.6.1 - UNRELEASED Release 2.6.1 - UNRELEASED
INCOMPATIBLE CHANGES INCOMPATIBLE CHANGES

View File

@ -1971,18 +1971,22 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
boolean res; boolean res;
byte[][] pathComponents = FSDirectory.getPathComponentsForReservedPath(src); byte[][] pathComponents = FSDirectory.getPathComponentsForReservedPath(src);
writeLock(); writeLock();
BlocksMapUpdateInfo toRemoveBlocks = new BlocksMapUpdateInfo();
try { try {
checkOperation(OperationCategory.WRITE); checkOperation(OperationCategory.WRITE);
checkNameNodeSafeMode("Cannot truncate for " + src); checkNameNodeSafeMode("Cannot truncate for " + src);
src = dir.resolvePath(pc, src, pathComponents); src = dir.resolvePath(pc, src, pathComponents);
res = truncateInternal(src, newLength, clientName, res = truncateInternal(src, newLength, clientName,
clientMachine, mtime, pc); clientMachine, mtime, pc, toRemoveBlocks);
stat = FSDirStatAndListingOp.getFileInfo(dir, src, false, stat = dir.getAuditFileInfo(dir.getINodesInPath4Write(src, false));
FSDirectory.isReservedRawName(src), true);
} finally { } finally {
writeUnlock(); writeUnlock();
} }
getEditLog().logSync(); getEditLog().logSync();
if (!toRemoveBlocks.getToDeleteList().isEmpty()) {
removeBlocks(toRemoveBlocks);
toRemoveBlocks.clear();
}
logAuditEvent(true, "truncate", src, null, stat); logAuditEvent(true, "truncate", src, null, stat);
return res; return res;
} }
@ -1993,17 +1997,17 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
*/ */
boolean truncateInternal(String src, long newLength, boolean truncateInternal(String src, long newLength,
String clientName, String clientMachine, String clientName, String clientMachine,
long mtime, FSPermissionChecker pc) long mtime, FSPermissionChecker pc,
BlocksMapUpdateInfo toRemoveBlocks)
throws IOException, UnresolvedLinkException { throws IOException, UnresolvedLinkException {
assert hasWriteLock(); assert hasWriteLock();
INodesInPath iip = dir.getINodesInPath4Write(src, true); INodesInPath iip = dir.getINodesInPath4Write(src, true);
if (isPermissionEnabled) { if (isPermissionEnabled) {
dir.checkPathAccess(pc, iip, FsAction.WRITE); dir.checkPathAccess(pc, iip, FsAction.WRITE);
} }
INodeFile file = iip.getLastINode().asFile(); INodeFile file = INodeFile.valueOf(iip.getLastINode(), src);
// Opening an existing file for write. May need lease recovery. // Opening an existing file for write. May need lease recovery.
recoverLeaseInternal(iip, src, clientName, clientMachine, false); recoverLeaseInternal(iip, src, clientName, clientMachine, false);
file = INodeFile.valueOf(iip.getLastINode(), src);
// Truncate length check. // Truncate length check.
long oldLength = file.computeFileSize(); long oldLength = file.computeFileSize();
if(oldLength == newLength) { if(oldLength == newLength) {
@ -2015,9 +2019,8 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
", truncate size: " + newLength + "."); ", truncate size: " + newLength + ".");
} }
// Perform INodeFile truncation. // Perform INodeFile truncation.
BlocksMapUpdateInfo collectedBlocks = new BlocksMapUpdateInfo();
boolean onBlockBoundary = dir.truncate(iip, newLength, boolean onBlockBoundary = dir.truncate(iip, newLength,
collectedBlocks, mtime); toRemoveBlocks, mtime);
Block truncateBlock = null; Block truncateBlock = null;
if(! onBlockBoundary) { if(! onBlockBoundary) {
// Open file for write, but don't log into edits // Open file for write, but don't log into edits
@ -2028,7 +2031,6 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
} }
getEditLog().logTruncate(src, clientName, clientMachine, newLength, mtime, getEditLog().logTruncate(src, clientName, clientMachine, newLength, mtime,
truncateBlock); truncateBlock);
removeBlocks(collectedBlocks);
return onBlockBoundary; return onBlockBoundary;
} }