Search in sources :

Example 1 with FileAlreadyExistsException

use of alluxio.exception.FileAlreadyExistsException in project alluxio by Alluxio.

the class InodeTree method createPath.

/**
   * Creates a file or directory at path.
   *
   * @param inodePath the path
   * @param options method options
   * @return a {@link CreatePathResult} representing the modified inodes and created inodes during
   *         path creation
   * @throws FileAlreadyExistsException when there is already a file at path if we want to create a
   *         directory there
   * @throws BlockInfoException when blockSizeBytes is invalid
   * @throws InvalidPathException when path is invalid, for example, (1) when there is nonexistent
   *         necessary parent directories and recursive is false, (2) when one of the necessary
   *         parent directories is actually a file
   * @throws IOException if creating the path fails
   * @throws FileDoesNotExistException if the parent of the path does not exist and the recursive
   *         option is false
   */
public CreatePathResult createPath(LockedInodePath inodePath, CreatePathOptions<?> options) throws FileAlreadyExistsException, BlockInfoException, InvalidPathException, IOException, FileDoesNotExistException {
    AlluxioURI path = inodePath.getUri();
    if (path.isRoot()) {
        String errorMessage = ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(path);
        LOG.error(errorMessage);
        throw new FileAlreadyExistsException(errorMessage);
    }
    if (options instanceof CreateFileOptions) {
        CreateFileOptions fileOptions = (CreateFileOptions) options;
        if (fileOptions.getBlockSizeBytes() < 1) {
            throw new BlockInfoException("Invalid block size " + fileOptions.getBlockSizeBytes());
        }
    }
    if (!(inodePath instanceof MutableLockedInodePath)) {
        throw new InvalidPathException(ExceptionMessage.NOT_MUTABLE_INODE_PATH.getMessage(inodePath.getUri()));
    }
    LOG.debug("createPath {}", path);
    TraversalResult traversalResult = traverseToInode(inodePath, LockMode.WRITE_PARENT);
    InodeLockList lockList = traversalResult.getInodeLockList();
    MutableLockedInodePath extensibleInodePath = (MutableLockedInodePath) inodePath;
    String[] pathComponents = extensibleInodePath.getPathComponents();
    String name = path.getName();
    // pathIndex is the index into pathComponents where we start filling in the path from the inode.
    int pathIndex = extensibleInodePath.getInodes().size();
    if (pathIndex < pathComponents.length - 1) {
        // Otherwise we add the remaining path components to the list of components to create.
        if (!options.isRecursive()) {
            final String msg = new StringBuilder().append("File ").append(path).append(" creation failed. Component ").append(pathIndex).append("(").append(pathComponents[pathIndex]).append(") does not exist").toString();
            LOG.error("FileDoesNotExistException: {}", msg);
            throw new FileDoesNotExistException(msg);
        }
    }
    // The ancestor inode (parent or ancestor) of the target path.
    Inode<?> ancestorInode = extensibleInodePath.getAncestorInode();
    if (!ancestorInode.isDirectory()) {
        throw new InvalidPathException("Could not traverse to parent directory of path " + path + ". Component " + pathComponents[pathIndex - 1] + " is not a directory.");
    }
    InodeDirectory currentInodeDirectory = (InodeDirectory) ancestorInode;
    List<Inode<?>> createdInodes = new ArrayList<>();
    List<Inode<?>> modifiedInodes = new ArrayList<>();
    // These are inodes that already exist, that should be journaled as persisted.
    List<Inode<?>> existingNonPersisted = new ArrayList<>();
    // These are inodes to mark as persisted at the end of this method.
    List<Inode<?>> toPersistDirectories = new ArrayList<>();
    if (options.isPersisted()) {
        // Directory persistence will not happen until the end of this method.
        toPersistDirectories.addAll(traversalResult.getNonPersisted());
        existingNonPersisted.addAll(traversalResult.getNonPersisted());
    }
    if (pathIndex < (pathComponents.length - 1) || currentInodeDirectory.getChild(name) == null) {
        // (1) There are components in parent paths that need to be created. Or
        // (2) The last component of the path needs to be created.
        // In these two cases, the last traversed Inode will be modified.
        modifiedInodes.add(currentInodeDirectory);
    }
    // TODO(gpang): We may not have to lock the newly created inodes if the last inode is write
    // locked. This could improve performance. Further investigation is needed.
    // Fill in the ancestor directories that were missing.
    // NOTE, we set the mode of missing ancestor directories to be the default value, rather
    // than inheriting the option of the final file to create, because it may not have
    // "execute" permission.
    CreateDirectoryOptions missingDirOptions = CreateDirectoryOptions.defaults().setMountPoint(false).setPersisted(options.isPersisted()).setOwner(options.getOwner()).setGroup(options.getGroup());
    for (int k = pathIndex; k < (pathComponents.length - 1); k++) {
        InodeDirectory dir = InodeDirectory.create(mDirectoryIdGenerator.getNewDirectoryId(), currentInodeDirectory.getId(), pathComponents[k], missingDirOptions);
        // Lock the newly created inode before subsequent operations, and add it to the lock group.
        lockList.lockWriteAndCheckNameAndParent(dir, currentInodeDirectory, pathComponents[k]);
        dir.setPinned(currentInodeDirectory.isPinned());
        currentInodeDirectory.addChild(dir);
        currentInodeDirectory.setLastModificationTimeMs(options.getOperationTimeMs());
        if (options.isPersisted()) {
            toPersistDirectories.add(dir);
        }
        createdInodes.add(dir);
        mInodes.add(dir);
        currentInodeDirectory = dir;
    }
    // Create the final path component. First we need to make sure that there isn't already a file
    // here with that name. If there is an existing file that is a directory and we're creating a
    // directory, update persistence property of the directories if needed, otherwise, throw
    // FileAlreadyExistsException unless options.allowExists is true.
    Inode<?> lastInode = currentInodeDirectory.getChild(name);
    if (lastInode != null) {
        // Lock the last inode before subsequent operations, and add it to the lock group.
        lockList.lockWriteAndCheckNameAndParent(lastInode, currentInodeDirectory, name);
        if (lastInode.isDirectory() && options instanceof CreateDirectoryOptions && !lastInode.isPersisted() && options.isPersisted()) {
            // The final path component already exists and is not persisted, so it should be added
            // to the non-persisted Inodes of traversalResult.
            existingNonPersisted.add(lastInode);
            toPersistDirectories.add(lastInode);
        } else if (!lastInode.isDirectory() || !(options instanceof CreateDirectoryOptions && ((CreateDirectoryOptions) options).isAllowExists())) {
            String errorMessage = ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(path);
            LOG.error(errorMessage);
            throw new FileAlreadyExistsException(errorMessage);
        }
    } else {
        if (options instanceof CreateDirectoryOptions) {
            CreateDirectoryOptions directoryOptions = (CreateDirectoryOptions) options;
            lastInode = InodeDirectory.create(mDirectoryIdGenerator.getNewDirectoryId(), currentInodeDirectory.getId(), name, directoryOptions);
            // Lock the created inode before subsequent operations, and add it to the lock group.
            lockList.lockWriteAndCheckNameAndParent(lastInode, currentInodeDirectory, name);
            if (directoryOptions.isPersisted()) {
                toPersistDirectories.add(lastInode);
            }
            lastInode.setPinned(currentInodeDirectory.isPinned());
        } else if (options instanceof CreateFileOptions) {
            CreateFileOptions fileOptions = (CreateFileOptions) options;
            lastInode = InodeFile.create(mContainerIdGenerator.getNewContainerId(), currentInodeDirectory.getId(), name, System.currentTimeMillis(), fileOptions);
            // Lock the created inode before subsequent operations, and add it to the lock group.
            lockList.lockWriteAndCheckNameAndParent(lastInode, currentInodeDirectory, name);
            if (currentInodeDirectory.isPinned()) {
                // Update set of pinned file ids.
                mPinnedInodeFileIds.add(lastInode.getId());
            }
            lastInode.setPinned(currentInodeDirectory.isPinned());
        }
        createdInodes.add(lastInode);
        mInodes.add(lastInode);
        currentInodeDirectory.addChild(lastInode);
        currentInodeDirectory.setLastModificationTimeMs(options.getOperationTimeMs());
    }
    // we mark it as persisted.
    for (Inode<?> inode : toPersistDirectories) {
        MountTable.Resolution resolution = mMountTable.resolve(getPath(inode));
        String ufsUri = resolution.getUri().toString();
        UnderFileSystem ufs = resolution.getUfs();
        MkdirsOptions mkdirsOptions = MkdirsOptions.defaults().setCreateParent(false).setOwner(inode.getOwner()).setGroup(inode.getGroup()).setMode(new Mode(inode.getMode()));
        if (ufs.isDirectory(ufsUri) || ufs.mkdirs(ufsUri, mkdirsOptions)) {
            inode.setPersistenceState(PersistenceState.PERSISTED);
        }
    }
    // Extend the inodePath with the created inodes.
    extensibleInodePath.getInodes().addAll(createdInodes);
    LOG.debug("createFile: File Created: {} parent: {}", lastInode, currentInodeDirectory);
    return new CreatePathResult(modifiedInodes, createdInodes, existingNonPersisted);
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) MkdirsOptions(alluxio.underfs.options.MkdirsOptions) CreateDirectoryOptions(alluxio.master.file.options.CreateDirectoryOptions) ArrayList(java.util.ArrayList) InvalidPathException(alluxio.exception.InvalidPathException) CreateFileOptions(alluxio.master.file.options.CreateFileOptions) UnderFileSystem(alluxio.underfs.UnderFileSystem) Mode(alluxio.security.authorization.Mode) BlockInfoException(alluxio.exception.BlockInfoException) AlluxioURI(alluxio.AlluxioURI)

Example 2 with FileAlreadyExistsException

use of alluxio.exception.FileAlreadyExistsException in project alluxio by Alluxio.

the class FileSystemMasterTest method rename.

/**
   * Tests the {@link FileSystemMaster#rename(AlluxioURI, AlluxioURI, RenameOptions)} method.
   */
@Test
public void rename() throws Exception {
    mFileSystemMaster.createFile(NESTED_FILE_URI, mNestedFileOptions);
    // try to rename a file to root
    try {
        mFileSystemMaster.rename(NESTED_FILE_URI, ROOT_URI, RenameOptions.defaults());
        Assert.fail("Renaming to root should fail.");
    } catch (InvalidPathException e) {
        Assert.assertEquals(ExceptionMessage.RENAME_CANNOT_BE_TO_ROOT.getMessage(), e.getMessage());
    }
    // move root to another path
    try {
        mFileSystemMaster.rename(ROOT_URI, TEST_URI, RenameOptions.defaults());
        Assert.fail("Should not be able to rename root");
    } catch (InvalidPathException e) {
        Assert.assertEquals(ExceptionMessage.ROOT_CANNOT_BE_RENAMED.getMessage(), e.getMessage());
    }
    // move to existing path
    try {
        mFileSystemMaster.rename(NESTED_FILE_URI, NESTED_URI, RenameOptions.defaults());
        Assert.fail("Should not be able to overwrite existing file.");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(NESTED_URI.getPath()), e.getMessage());
    }
    // move a nested file to a root file
    mFileSystemMaster.rename(NESTED_FILE_URI, TEST_URI, RenameOptions.defaults());
    Assert.assertEquals(mFileSystemMaster.getFileInfo(TEST_URI).getPath(), TEST_URI.getPath());
    // move a file where the dst is lexicographically earlier than the source
    AlluxioURI newDst = new AlluxioURI("/abc_test");
    mFileSystemMaster.rename(TEST_URI, newDst, RenameOptions.defaults());
    Assert.assertEquals(mFileSystemMaster.getFileInfo(newDst).getPath(), newDst.getPath());
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 3 with FileAlreadyExistsException

use of alluxio.exception.FileAlreadyExistsException in project alluxio by Alluxio.

the class FileSystemMasterTest method testLoadMetadata.

/**
   * Tests load metadata logic.
   */
@Test
public void testLoadMetadata() throws Exception {
    FileUtils.createDir(Paths.get(mUnderFS).resolve("a").toString());
    mFileSystemMaster.loadMetadata(new AlluxioURI("alluxio:/a"), LoadMetadataOptions.defaults().setCreateAncestors(true));
    mFileSystemMaster.loadMetadata(new AlluxioURI("alluxio:/a"), LoadMetadataOptions.defaults().setCreateAncestors(true));
    // TODO(peis): Avoid this hack by adding an option in getFileInfo to skip loading metadata.
    try {
        mFileSystemMaster.createDirectory(new AlluxioURI("alluxio:/a"), CreateDirectoryOptions.defaults());
        Assert.fail("createDirectory was expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(new AlluxioURI("alluxio:/a")), e.getMessage());
    }
    FileUtils.createFile(Paths.get(mUnderFS).resolve("a/f1").toString());
    FileUtils.createFile(Paths.get(mUnderFS).resolve("a/f2").toString());
    mFileSystemMaster.loadMetadata(new AlluxioURI("alluxio:/a/f1"), LoadMetadataOptions.defaults().setCreateAncestors(true));
    // This should not throw file exists exception those a/f1 is loaded.
    mFileSystemMaster.loadMetadata(new AlluxioURI("alluxio:/a"), LoadMetadataOptions.defaults().setCreateAncestors(true).setLoadDirectChildren(true));
    // TODO(peis): Avoid this hack by adding an option in getFileInfo to skip loading metadata.
    try {
        mFileSystemMaster.createFile(new AlluxioURI("alluxio:/a/f2"), CreateFileOptions.defaults());
        Assert.fail("createDirectory was expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(new AlluxioURI("alluxio:/a/f2")), e.getMessage());
    }
    mFileSystemMaster.loadMetadata(new AlluxioURI("alluxio:/a"), LoadMetadataOptions.defaults().setCreateAncestors(true).setLoadDirectChildren(true));
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 4 with FileAlreadyExistsException

use of alluxio.exception.FileAlreadyExistsException in project alluxio by Alluxio.

the class MountTableTest method path.

/**
   * Tests the different methods of the {@link MountTable} class with a path.
   */
@Test
public void path() throws Exception {
    // Test add()
    mMountTable.add(new AlluxioURI("/mnt/foo"), new AlluxioURI("/foo"), mDefaultOptions);
    mMountTable.add(new AlluxioURI("/mnt/bar"), new AlluxioURI("/bar"), mDefaultOptions);
    try {
        mMountTable.add(new AlluxioURI("/mnt/foo"), new AlluxioURI("/foo2"), mDefaultOptions);
        Assert.fail("Should not be able to add a mount to an existing mount.");
    } catch (FileAlreadyExistsException e) {
        // Exception expected
        Assert.assertEquals(ExceptionMessage.MOUNT_POINT_ALREADY_EXISTS.getMessage("/mnt/foo"), e.getMessage());
    }
    try {
        mMountTable.add(new AlluxioURI("/mnt/bar/baz"), new AlluxioURI("/baz"), mDefaultOptions);
    } catch (InvalidPathException e) {
        // Exception expected
        Assert.assertEquals(ExceptionMessage.MOUNT_POINT_PREFIX_OF_ANOTHER.getMessage("/mnt/bar", "/mnt/bar/baz"), e.getMessage());
    }
    // Test resolve()
    Assert.assertEquals(new AlluxioURI("/foo"), mMountTable.resolve(new AlluxioURI("/mnt/foo")).getUri());
    Assert.assertEquals(new AlluxioURI("/foo/x"), mMountTable.resolve(new AlluxioURI("/mnt/foo/x")).getUri());
    Assert.assertEquals(new AlluxioURI("/bar"), mMountTable.resolve(new AlluxioURI("/mnt/bar")).getUri());
    Assert.assertEquals(new AlluxioURI("/bar/y"), mMountTable.resolve(new AlluxioURI("/mnt/bar/y")).getUri());
    Assert.assertEquals(new AlluxioURI("/bar/baz"), mMountTable.resolve(new AlluxioURI("/mnt/bar/baz")).getUri());
    Assert.assertEquals(new AlluxioURI("/foobar"), mMountTable.resolve(new AlluxioURI("/foobar")).getUri());
    Assert.assertEquals(new AlluxioURI("/"), mMountTable.resolve(new AlluxioURI("/")).getUri());
    // Test getMountPoint()
    Assert.assertEquals("/mnt/foo", mMountTable.getMountPoint(new AlluxioURI("/mnt/foo")));
    Assert.assertEquals("/mnt/foo", mMountTable.getMountPoint(new AlluxioURI("/mnt/foo/x")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("/mnt/bar")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("/mnt/bar/y")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("/mnt/bar/baz")));
    Assert.assertNull(mMountTable.getMountPoint(new AlluxioURI("/mnt")));
    Assert.assertNull(mMountTable.getMountPoint(new AlluxioURI("/")));
    // Test isMountPoint()
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("/")));
    Assert.assertTrue(mMountTable.isMountPoint(new AlluxioURI("/mnt/foo")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("/mnt/foo/bar")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("/mnt")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("/mnt/foo3")));
    Assert.assertTrue(mMountTable.isMountPoint(new AlluxioURI("/mnt/bar")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("/mnt/bar/baz")));
    // Test delete()
    Assert.assertTrue(mMountTable.delete(new AlluxioURI("/mnt/bar")));
    Assert.assertTrue(mMountTable.delete(new AlluxioURI("/mnt/foo")));
    Assert.assertFalse(mMountTable.delete(new AlluxioURI("/mnt/foo")));
    Assert.assertFalse(mMountTable.delete(new AlluxioURI("/")));
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 5 with FileAlreadyExistsException

use of alluxio.exception.FileAlreadyExistsException in project alluxio by Alluxio.

the class UnderStorageSystemInterfaceIntegrationTest method loadMetadata.

/**
   * Tests load metadata on list.
   */
@Test
public void loadMetadata() throws Exception {
    String dirName = "loadMetaDataRoot";
    String rootDir = PathUtils.concatPath(mUnderfsAddress, dirName);
    mUfs.mkdirs(rootDir);
    String rootFile1 = PathUtils.concatPath(rootDir, "file1");
    createEmptyFile(rootFile1);
    String rootFile2 = PathUtils.concatPath(rootDir, "file2");
    createEmptyFile(rootFile2);
    AlluxioURI rootAlluxioURI = new AlluxioURI("/" + dirName);
    FileSystem client = mLocalAlluxioClusterResource.get().getClient();
    client.listStatus(rootAlluxioURI, ListStatusOptions.defaults().setLoadMetadataType(LoadMetadataType.Always));
    try {
        client.createDirectory(rootAlluxioURI, CreateDirectoryOptions.defaults());
        Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(rootAlluxioURI), e.getMessage());
    }
    AlluxioURI file1URI = rootAlluxioURI.join("file1");
    try {
        client.createFile(file1URI, CreateFileOptions.defaults()).close();
        Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(file1URI), e.getMessage());
    }
    AlluxioURI file2URI = rootAlluxioURI.join("file2");
    try {
        client.createFile(file2URI, CreateFileOptions.defaults()).close();
        Assert.fail("create is expected to fail with FileAlreadyExistsException");
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(file2URI), e.getMessage());
    }
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) FileSystem(alluxio.client.file.FileSystem) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)18 AlluxioURI (alluxio.AlluxioURI)14 InvalidPathException (alluxio.exception.InvalidPathException)9 Test (org.junit.Test)8 AlluxioException (alluxio.exception.AlluxioException)4 IOException (java.io.IOException)4 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)3 CreateDirectoryOptions (alluxio.master.file.options.CreateDirectoryOptions)3 CreateFileOptions (alluxio.master.file.options.CreateFileOptions)3 Mode (alluxio.security.authorization.Mode)3 UnderFileSystem (alluxio.underfs.UnderFileSystem)3 FileSystem (alluxio.client.file.FileSystem)1 CreateDirectoryOptions (alluxio.client.file.options.CreateDirectoryOptions)1 AccessControlException (alluxio.exception.AccessControlException)1 BlockInfoException (alluxio.exception.BlockInfoException)1 FileAlreadyCompletedException (alluxio.exception.FileAlreadyCompletedException)1 InvalidFileSizeException (alluxio.exception.InvalidFileSizeException)1 UnexpectedAlluxioException (alluxio.exception.UnexpectedAlluxioException)1 HeartbeatThread (alluxio.heartbeat.HeartbeatThread)1 Inode (alluxio.master.file.meta.Inode)1