use of alluxio.exception.DirectoryNotEmptyException in project alluxio by Alluxio.
the class FileSystemMasterIntegrationTest method deleteDirectoryWithFilesTest2.
@Test
public void deleteDirectoryWithFilesTest2() throws Exception {
mFsMaster.createDirectory(new AlluxioURI("/testFolder"), CreateDirectoryContext.defaults());
long fileId = mFsMaster.createFile(new AlluxioURI("/testFolder/testFile"), CreateFileContext.defaults()).getFileId();
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
try {
mFsMaster.delete(new AlluxioURI("/testFolder"), DeleteContext.defaults());
Assert.fail("Deleting a nonempty directory nonrecursively should fail");
} catch (DirectoryNotEmptyException e) {
Assert.assertEquals(ExceptionMessage.DELETE_NONEMPTY_DIRECTORY_NONRECURSIVE.getMessage("testFolder"), e.getMessage());
}
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
}
use of alluxio.exception.DirectoryNotEmptyException 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.DirectoryNotEmptyException in project alluxio by Alluxio.
the class FileSystemMasterIntegrationTest method deleteDirectoryWithFilesTest2.
@Test
public void deleteDirectoryWithFilesTest2() throws Exception {
mFsMaster.createDirectory(new AlluxioURI("/testFolder"), CreateDirectoryOptions.defaults());
long fileId = mFsMaster.createFile(new AlluxioURI("/testFolder/testFile"), CreateFileOptions.defaults());
Assert.assertEquals(1, mFsMaster.getFileId(new AlluxioURI("/testFolder")));
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
try {
mFsMaster.delete(new AlluxioURI("/testFolder"), DeleteOptions.defaults().setRecursive(false));
Assert.fail("Deleting a nonempty directory nonrecursively should fail");
} catch (DirectoryNotEmptyException e) {
Assert.assertEquals(ExceptionMessage.DELETE_NONEMPTY_DIRECTORY_NONRECURSIVE.getMessage("testFolder"), e.getMessage());
}
Assert.assertEquals(1, mFsMaster.getFileId(new AlluxioURI("/testFolder")));
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
}
use of alluxio.exception.DirectoryNotEmptyException in project alluxio by Alluxio.
the class FileSystemMasterIntegrationTest method deleteDirectoryWithDirectoriesTest2.
@Test
public void deleteDirectoryWithDirectoriesTest2() throws Exception {
mFsMaster.createDirectory(new AlluxioURI("/testFolder"), CreateDirectoryOptions.defaults());
mFsMaster.createDirectory(new AlluxioURI("/testFolder/testFolder2"), CreateDirectoryOptions.defaults());
long fileId = mFsMaster.createFile(new AlluxioURI("/testFolder/testFile"), CreateFileOptions.defaults());
long fileId2 = mFsMaster.createFile(new AlluxioURI("/testFolder/testFolder2/testFile2"), CreateFileOptions.defaults());
Assert.assertEquals(1, mFsMaster.getFileId(new AlluxioURI("/testFolder")));
Assert.assertEquals(2, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFolder2")));
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
Assert.assertEquals(fileId2, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFolder2/testFile2")));
try {
mFsMaster.delete(new AlluxioURI("/testFolder/testFolder2"), DeleteOptions.defaults().setRecursive(false));
Assert.fail("Deleting a nonempty directory nonrecursively should fail");
} catch (DirectoryNotEmptyException e) {
Assert.assertEquals(ExceptionMessage.DELETE_NONEMPTY_DIRECTORY_NONRECURSIVE.getMessage("testFolder2"), e.getMessage());
}
Assert.assertEquals(1, mFsMaster.getFileId(new AlluxioURI("/testFolder")));
Assert.assertEquals(2, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFolder2")));
Assert.assertEquals(fileId, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFile")));
Assert.assertEquals(fileId2, mFsMaster.getFileId(new AlluxioURI("/testFolder/testFolder2/testFile2")));
}
use of alluxio.exception.DirectoryNotEmptyException in project alluxio by Alluxio.
the class FileSystemMasterTest method deleteNonemptyDirectory.
/**
* Tests the {@link FileSystemMaster#delete(AlluxioURI, DeleteContext)} method with a
* non-empty directory.
*/
@Test
public void deleteNonemptyDirectory() throws Exception {
createFileWithSingleBlock(NESTED_FILE_URI);
String dirName = mFileSystemMaster.getFileInfo(NESTED_URI, GET_STATUS_CONTEXT).getName();
try {
mFileSystemMaster.delete(NESTED_URI, DeleteContext.defaults());
fail("Deleting a non-empty directory without setting recursive should fail");
} catch (DirectoryNotEmptyException e) {
String expectedMessage = ExceptionMessage.DELETE_NONEMPTY_DIRECTORY_NONRECURSIVE.getMessage(dirName);
assertEquals(expectedMessage, e.getMessage());
}
// Now delete with recursive set to true.
mFileSystemMaster.delete(NESTED_URI, DeleteContext.mergeFrom(DeletePOptions.newBuilder().setRecursive(true)));
}
Aggregations