use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.
the class FileSystemMaster method getFileInfoInternal.
/**
* @param inodePath the {@link LockedInodePath} to get the {@link FileInfo} for
* @return the {@link FileInfo} for the given inode
* @throws FileDoesNotExistException if the file does not exist
* @throws AccessControlException if permission denied
*/
private FileInfo getFileInfoInternal(LockedInodePath inodePath) throws FileDoesNotExistException, AccessControlException {
Inode<?> inode = inodePath.getInode();
AlluxioURI uri = inodePath.getUri();
FileInfo fileInfo = inode.generateClientFileInfo(uri.toString());
fileInfo.setInMemoryPercentage(getInMemoryPercentage(inode));
if (inode instanceof InodeFile) {
try {
fileInfo.setFileBlockInfos(getFileBlockInfoListInternal(inodePath));
} catch (InvalidPathException e) {
throw new FileDoesNotExistException(e.getMessage(), e);
}
}
MountTable.Resolution resolution;
try {
resolution = mMountTable.resolve(uri);
} catch (InvalidPathException e) {
throw new FileDoesNotExistException(e.getMessage(), e);
}
AlluxioURI resolvedUri = resolution.getUri();
// Only set the UFS path if the path is nested under a mount point.
if (!uri.equals(resolvedUri)) {
fileInfo.setUfsPath(resolvedUri.toString());
}
Metrics.FILE_INFOS_GOT.inc();
return fileInfo;
}
use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.
the class FileSystemMaster method reportLostFile.
/**
* Reports a file as lost.
*
* @param fileId the id of the file
* @throws FileDoesNotExistException if the file does not exist
*/
// Currently used by Lineage Master
// TODO(binfan): Add permission checking for internal APIs
public void reportLostFile(long fileId) throws FileDoesNotExistException {
try (LockedInodePath inodePath = mInodeTree.lockFullInodePath(fileId, InodeTree.LockMode.READ)) {
Inode<?> inode = inodePath.getInode();
if (inode.isDirectory()) {
LOG.warn("Reported file is a directory {}", inode);
return;
}
List<Long> blockIds = new ArrayList<>();
try {
for (FileBlockInfo fileBlockInfo : getFileBlockInfoListInternal(inodePath)) {
blockIds.add(fileBlockInfo.getBlockInfo().getBlockId());
}
} catch (InvalidPathException e) {
LOG.info("Failed to get file info {}", fileId, e);
}
mBlockMaster.reportLostBlocks(blockIds);
LOG.info("Reported file loss of blocks {}. Alluxio will recompute it: {}", blockIds, fileId);
}
}
use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.
the class FileSystemMaster method renameInternal.
/**
* Implements renaming.
*
* @param srcInodePath the path of the rename source
* @param dstInodePath the path to the rename destination
* @param replayed whether the operation is a result of replaying the journal
* @param options method options
* @throws FileDoesNotExistException if a non-existent file is encountered
* @throws InvalidPathException if an invalid path is encountered
* @throws IOException if an I/O error is encountered
*/
private void renameInternal(LockedInodePath srcInodePath, LockedInodePath dstInodePath, boolean replayed, RenameOptions options) throws FileDoesNotExistException, InvalidPathException, IOException {
// Rename logic:
// 1. Change the source inode name to the destination name.
// 2. Insert the source inode into the destination parent.
// 3. Do UFS operations if necessary.
// 4. Remove the source inode (reverting the name) from the source parent.
// 5. Set the last modification times for both source and destination parent inodes.
Inode<?> srcInode = srcInodePath.getInode();
AlluxioURI srcPath = srcInodePath.getUri();
AlluxioURI dstPath = dstInodePath.getUri();
InodeDirectory srcParentInode = srcInodePath.getParentInodeDirectory();
InodeDirectory dstParentInode = dstInodePath.getParentInodeDirectory();
String srcName = srcPath.getName();
String dstName = dstPath.getName();
LOG.debug("Renaming {} to {}", srcPath, dstPath);
// 1. Change the source inode name to the destination name.
srcInode.setName(dstName);
srcInode.setParentId(dstParentInode.getId());
// 2. Insert the source inode into the destination parent.
if (!dstParentInode.addChild(srcInode)) {
// On failure, revert changes and throw exception.
srcInode.setName(srcName);
srcInode.setParentId(srcParentInode.getId());
throw new InvalidPathException("Destination path: " + dstPath + " already exists.");
}
// If the source file is persisted, rename it in the UFS.
try {
if (!replayed && srcInode.isPersisted()) {
MountTable.Resolution resolution = mMountTable.resolve(srcPath);
String ufsSrcPath = resolution.getUri().toString();
UnderFileSystem ufs = resolution.getUfs();
String ufsDstUri = mMountTable.resolve(dstPath).getUri().toString();
// Create ancestor directories from top to the bottom. We cannot use recursive create
// parents here because the permission for the ancestors can be different.
List<Inode<?>> dstInodeList = dstInodePath.getInodeList();
Stack<Pair<String, MkdirsOptions>> ufsDirsToMakeWithOptions = new Stack<>();
AlluxioURI curUfsDirPath = new AlluxioURI(ufsDstUri).getParent();
// The dst inode does not exist yet, so the last inode in the list is the existing parent.
for (int i = dstInodeList.size() - 1; i >= 0; i--) {
if (ufs.isDirectory(curUfsDirPath.toString())) {
break;
}
Inode<?> curInode = dstInodeList.get(i);
MkdirsOptions mkdirsOptions = MkdirsOptions.defaults().setCreateParent(false).setOwner(curInode.getOwner()).setGroup(curInode.getGroup()).setMode(new Mode(curInode.getMode()));
ufsDirsToMakeWithOptions.push(new Pair<>(curUfsDirPath.toString(), mkdirsOptions));
curUfsDirPath = curUfsDirPath.getParent();
}
while (!ufsDirsToMakeWithOptions.empty()) {
Pair<String, MkdirsOptions> ufsDirAndPerm = ufsDirsToMakeWithOptions.pop();
if (!ufs.mkdirs(ufsDirAndPerm.getFirst(), ufsDirAndPerm.getSecond())) {
throw new IOException(ExceptionMessage.FAILED_UFS_CREATE.getMessage(ufsDirAndPerm.getFirst()));
}
}
boolean success;
if (srcInode.isFile()) {
success = ufs.renameFile(ufsSrcPath, ufsDstUri);
} else {
success = ufs.renameDirectory(ufsSrcPath, ufsDstUri);
}
if (!success) {
throw new IOException(ExceptionMessage.FAILED_UFS_RENAME.getMessage(ufsSrcPath, ufsDstUri));
}
}
} catch (Exception e) {
// On failure, revert changes and throw exception.
if (!dstParentInode.removeChild(dstName)) {
LOG.error("Failed to revert rename changes. Alluxio metadata may be inconsistent.");
}
srcInode.setName(srcName);
srcInode.setParentId(srcParentInode.getId());
throw e;
}
// TODO(jiri): A crash between now and the time the rename operation is journaled will result in
// an inconsistency between Alluxio and UFS.
// 4. Remove the source inode (reverting the name) from the source parent. The name must be
// reverted or removeChild will not be able to find the appropriate child entry since it is
// keyed on the original name.
srcInode.setName(srcName);
if (!srcParentInode.removeChild(srcInode)) {
// This should never happen.
LOG.error("Failed to rename {} to {} in Alluxio. Alluxio and under storage may be " + "inconsistent.", srcPath, dstPath);
srcInode.setName(dstName);
if (!dstParentInode.removeChild(dstName)) {
LOG.error("Failed to revert changes when renaming {} to {}. Alluxio metadata may be " + "inconsistent.", srcPath, dstPath);
}
srcInode.setName(srcName);
srcInode.setParentId(srcParentInode.getId());
throw new IOException("Failed to remove source path " + srcPath + " from parent");
}
srcInode.setName(dstName);
// 5. Set the last modification times for both source and destination parent inodes.
// Note this step relies on setLastModificationTimeMs being thread safe to guarantee the
// correct behavior when multiple files are being renamed within a directory.
dstParentInode.setLastModificationTimeMs(options.getOperationTimeMs());
srcParentInode.setLastModificationTimeMs(options.getOperationTimeMs());
Metrics.PATHS_RENAMED.inc();
}
use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.
the class FileSystemMaster method deleteInternal.
/**
* Implements file deletion.
*
* @param inodePath the file {@link LockedInodePath}
* @param replayed whether the operation is a result of replaying the journal
* @param opTimeMs the time of the operation
* @param deleteOptions the method optitions
* @throws FileDoesNotExistException if a non-existent file is encountered
* @throws IOException if an I/O error is encountered
* @throws InvalidPathException if the specified path is the root
* @throws DirectoryNotEmptyException if recursive is false and the file is a nonempty directory
*/
private void deleteInternal(LockedInodePath inodePath, boolean replayed, long opTimeMs, DeleteOptions deleteOptions) throws FileDoesNotExistException, IOException, DirectoryNotEmptyException, InvalidPathException {
// journaled will result in an inconsistency between Alluxio and UFS.
if (!inodePath.fullPathExists()) {
return;
}
Inode<?> inode = inodePath.getInode();
if (inode == null) {
return;
}
boolean recursive = deleteOptions.isRecursive();
boolean alluxioOnly = deleteOptions.isAlluxioOnly();
if (inode.isDirectory() && !recursive && ((InodeDirectory) inode).getNumberOfChildren() > 0) {
// true
throw new DirectoryNotEmptyException(ExceptionMessage.DELETE_NONEMPTY_DIRECTORY_NONRECURSIVE, inode.getName());
}
if (mInodeTree.isRootId(inode.getId())) {
// The root cannot be deleted.
throw new InvalidPathException(ExceptionMessage.DELETE_ROOT_DIRECTORY.getMessage());
}
List<Inode<?>> delInodes = new ArrayList<>();
delInodes.add(inode);
try (InodeLockList lockList = mInodeTree.lockDescendants(inodePath, InodeTree.LockMode.WRITE)) {
delInodes.addAll(lockList.getInodes());
TempInodePathForDescendant tempInodePath = new TempInodePathForDescendant(inodePath);
// file, we deal with the checkpoints and blocks as well.
for (int i = delInodes.size() - 1; i >= 0; i--) {
Inode<?> delInode = delInodes.get(i);
// the path to delInode for getPath should already be locked.
AlluxioURI alluxioUriToDel = mInodeTree.getPath(delInode);
tempInodePath.setDescendant(delInode, alluxioUriToDel);
// Currently, it will result in an inconsistency between Alluxio and UFS.
if (!replayed && delInode.isPersisted()) {
try {
// TODO(calvin): Add tests (ALLUXIO-1831)
if (mMountTable.isMountPoint(alluxioUriToDel)) {
unmountInternal(alluxioUriToDel);
} else {
// Delete the file in the under file system.
MountTable.Resolution resolution = mMountTable.resolve(alluxioUriToDel);
String ufsUri = resolution.getUri().toString();
UnderFileSystem ufs = resolution.getUfs();
boolean failedToDelete = false;
if (!alluxioOnly) {
if (delInode.isFile()) {
if (!ufs.deleteFile(ufsUri)) {
failedToDelete = ufs.isFile(ufsUri);
if (!failedToDelete) {
LOG.warn("The file to delete does not exist in ufs: {}", ufsUri);
}
}
} else {
if (!ufs.deleteDirectory(ufsUri, alluxio.underfs.options.DeleteOptions.defaults().setRecursive(true))) {
failedToDelete = ufs.isDirectory(ufsUri);
if (!failedToDelete) {
LOG.warn("The directory to delete does not exist in ufs: {}", ufsUri);
}
}
}
if (failedToDelete) {
LOG.error("Failed to delete {} from the under filesystem", ufsUri);
throw new IOException(ExceptionMessage.DELETE_FAILED_UFS.getMessage(ufsUri));
}
}
}
} catch (InvalidPathException e) {
LOG.warn(e.getMessage());
}
}
if (delInode.isFile()) {
// Remove corresponding blocks from workers and delete metadata in master.
mBlockMaster.removeBlocks(((InodeFile) delInode).getBlockIds(), true);
}
mInodeTree.deleteInode(tempInodePath, opTimeMs);
}
}
Metrics.PATHS_DELETED.inc(delInodes.size());
}
use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.
the class InodeTree method traverseToInode.
/**
* Traverses the tree to find the given path components. Hints for the lock mode at each path
* component can be specified.
*
* @param pathComponents the components of the path to traverse
* @param lockMode the {@link LockMode} for the path
* @param lockHints optional {@link List} to specify the lock type for each path component; this
* can be shorter than pathComponents
* @return the {@link TraversalResult} for this traversal
* @throws InvalidPathException if the path is invalid
*/
private TraversalResult traverseToInode(String[] pathComponents, LockMode lockMode, List<LockMode> lockHints) throws InvalidPathException {
List<Inode<?>> nonPersistedInodes = new ArrayList<>();
List<Inode<?>> inodes = new ArrayList<>();
InodeLockList lockList = new InodeLockList();
// This must be set to true before returning a valid value, otherwise all the inodes will be
// unlocked.
boolean valid = false;
try {
if (pathComponents == null) {
throw new InvalidPathException(ExceptionMessage.PATH_COMPONENTS_INVALID.getMessage("null"));
} else if (pathComponents.length == 0) {
throw new InvalidPathException(ExceptionMessage.PATH_COMPONENTS_INVALID.getMessage("empty"));
} else if (pathComponents.length == 1) {
if (pathComponents[0].equals("")) {
if (getLockModeForComponent(0, pathComponents.length, lockMode, lockHints) == LockMode.READ) {
lockList.lockRead(mRoot);
} else {
lockList.lockWrite(mRoot);
}
inodes.add(mRoot);
valid = true;
return TraversalResult.createFoundResult(nonPersistedInodes, inodes, lockList);
} else {
throw new InvalidPathException(ExceptionMessage.PATH_COMPONENTS_INVALID_START.getMessage(pathComponents[0]));
}
}
if (getLockModeForComponent(0, pathComponents.length, lockMode, lockHints) == LockMode.READ) {
lockList.lockRead(mRoot);
} else {
lockList.lockWrite(mRoot);
}
inodes.add(mRoot);
TraversalResult result = traverseToInodeInternal(pathComponents, inodes, nonPersistedInodes, lockList, lockMode, lockHints);
valid = true;
return result;
} finally {
if (!valid) {
lockList.close();
}
}
}
Aggregations