Search in sources :

Example 6 with FileAlreadyExistsException

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

the class FileSystemMaster method renameAndJournal.

/**
   * Renames a file to a destination.
   * <p>
   * Writes to the journal.
   *
   * @param srcInodePath the source path to rename
   * @param dstInodePath the destination path to rename the file to
   * @param options method options
   * @param journalContext the journalContext
   * @throws InvalidPathException if an invalid path is encountered
   * @throws FileDoesNotExistException if a non-existent file is encountered
   * @throws FileAlreadyExistsException if the file already exists
   * @throws IOException if an I/O error occurs
   */
private void renameAndJournal(LockedInodePath srcInodePath, LockedInodePath dstInodePath, RenameOptions options, JournalContext journalContext) throws InvalidPathException, FileDoesNotExistException, FileAlreadyExistsException, IOException {
    if (!srcInodePath.fullPathExists()) {
        throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(srcInodePath.getUri()));
    }
    Inode<?> srcInode = srcInodePath.getInode();
    // Renaming path to itself is a no-op.
    if (srcInodePath.getUri().equals(dstInodePath.getUri())) {
        return;
    }
    // Renaming the root is not allowed.
    if (srcInodePath.getUri().isRoot()) {
        throw new InvalidPathException(ExceptionMessage.ROOT_CANNOT_BE_RENAMED.getMessage());
    }
    if (dstInodePath.getUri().isRoot()) {
        throw new InvalidPathException(ExceptionMessage.RENAME_CANNOT_BE_TO_ROOT.getMessage());
    }
    // Renaming across mount points is not allowed.
    String srcMount = mMountTable.getMountPoint(srcInodePath.getUri());
    String dstMount = mMountTable.getMountPoint(dstInodePath.getUri());
    if ((srcMount == null && dstMount != null) || (srcMount != null && dstMount == null) || (srcMount != null && dstMount != null && !srcMount.equals(dstMount))) {
        throw new InvalidPathException(ExceptionMessage.RENAME_CANNOT_BE_ACROSS_MOUNTS.getMessage(srcInodePath.getUri(), dstInodePath.getUri()));
    }
    // Renaming onto a mount point is not allowed.
    if (mMountTable.isMountPoint(dstInodePath.getUri())) {
        throw new InvalidPathException(ExceptionMessage.RENAME_CANNOT_BE_ONTO_MOUNT_POINT.getMessage(dstInodePath.getUri()));
    }
    // srcComponents isn't a prefix of dstComponents.
    if (PathUtils.hasPrefix(dstInodePath.getUri().getPath(), srcInodePath.getUri().getPath())) {
        throw new InvalidPathException(ExceptionMessage.RENAME_CANNOT_BE_TO_SUBDIRECTORY.getMessage(srcInodePath.getUri(), dstInodePath.getUri()));
    }
    // Get the inodes of the src and dst parents.
    Inode<?> srcParentInode = srcInodePath.getParentInodeDirectory();
    if (!srcParentInode.isDirectory()) {
        throw new InvalidPathException(ExceptionMessage.PATH_MUST_HAVE_VALID_PARENT.getMessage(srcInodePath.getUri()));
    }
    Inode<?> dstParentInode = dstInodePath.getParentInodeDirectory();
    if (!dstParentInode.isDirectory()) {
        throw new InvalidPathException(ExceptionMessage.PATH_MUST_HAVE_VALID_PARENT.getMessage(dstInodePath.getUri()));
    }
    // Make sure destination path does not exist
    if (dstInodePath.fullPathExists()) {
        throw new FileAlreadyExistsException(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(dstInodePath.getUri()));
    }
    // Now we remove srcInode from its parent and insert it into dstPath's parent
    renameInternal(srcInodePath, dstInodePath, false, options);
    List<Inode<?>> persistedInodes = propagatePersistedInternal(srcInodePath, false);
    journalPersistedInodes(persistedInodes, journalContext);
    RenameEntry rename = RenameEntry.newBuilder().setId(srcInode.getId()).setDstPath(dstInodePath.getUri().getPath()).setOpTimeMs(options.getOperationTimeMs()).build();
    appendJournalEntry(JournalEntry.newBuilder().setRename(rename).build(), journalContext);
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) RenameEntry(alluxio.proto.journal.File.RenameEntry) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) Inode(alluxio.master.file.meta.Inode) InvalidPathException(alluxio.exception.InvalidPathException)

Example 7 with FileAlreadyExistsException

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

the class InodeTreeTest method createPathTest.

/**
   * Tests the {@link InodeTree#createPath(LockedInodePath, CreatePathOptions)} method.
   */
@Test
public void createPathTest() throws Exception {
    // save the last mod time of the root
    long lastModTime = mTree.getRoot().getLastModificationTimeMs();
    // sleep to ensure a different last modification time
    CommonUtils.sleepMs(10);
    // Need to use updated options to set the correct last mod time.
    CreateDirectoryOptions dirOptions = CreateDirectoryOptions.defaults().setOwner(TEST_OWNER).setGroup(TEST_GROUP).setMode(TEST_DIR_MODE).setRecursive(true);
    // create nested directory
    InodeTree.CreatePathResult createResult = createPath(mTree, NESTED_URI, dirOptions);
    List<Inode<?>> modified = createResult.getModified();
    List<Inode<?>> created = createResult.getCreated();
    // 1 modified directory
    Assert.assertEquals(1, modified.size());
    Assert.assertEquals("", modified.get(0).getName());
    Assert.assertNotEquals(lastModTime, modified.get(0).getLastModificationTimeMs());
    // 2 created directories
    Assert.assertEquals(2, created.size());
    Assert.assertEquals("nested", created.get(0).getName());
    Assert.assertEquals("test", created.get(1).getName());
    // save the last mod time of 'test'
    lastModTime = created.get(1).getLastModificationTimeMs();
    // sleep to ensure a different last modification time
    CommonUtils.sleepMs(10);
    // creating the directory path again results in no new inodes.
    try {
        createPath(mTree, NESTED_URI, dirOptions);
        Assert.assertTrue("createPath should throw FileAlreadyExistsException", false);
    } catch (FileAlreadyExistsException e) {
        Assert.assertEquals(e.getMessage(), ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(NESTED_URI));
    }
    // create a file
    CreateFileOptions options = CreateFileOptions.defaults().setBlockSizeBytes(Constants.KB).setRecursive(true);
    createResult = createPath(mTree, NESTED_FILE_URI, options);
    modified = createResult.getModified();
    created = createResult.getCreated();
    // test directory was modified
    Assert.assertEquals(1, modified.size());
    Assert.assertEquals("test", modified.get(0).getName());
    Assert.assertNotEquals(lastModTime, modified.get(0).getLastModificationTimeMs());
    // file was created
    Assert.assertEquals(1, created.size());
    Assert.assertEquals("file", created.get(0).getName());
}
Also used : CreateFileOptions(alluxio.master.file.options.CreateFileOptions) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) CreateDirectoryOptions(alluxio.master.file.options.CreateDirectoryOptions) Test(org.junit.Test)

Example 8 with FileAlreadyExistsException

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

the class MountTableTest method uri.

/**
   * Tests the different methods of the {@link MountTable} class with a URI.
   */
@Test
public void uri() throws Exception {
    // Test add()
    mMountTable.add(new AlluxioURI("alluxio://localhost:1234/mnt/foo"), new AlluxioURI("file://localhost:5678/foo"), mDefaultOptions);
    mMountTable.add(new AlluxioURI("alluxio://localhost:1234/mnt/bar"), new AlluxioURI("file://localhost:5678/bar"), mDefaultOptions);
    try {
        mMountTable.add(new AlluxioURI("alluxio://localhost:1234/mnt/foo"), new AlluxioURI("hdfs://localhost:5678/foo2"), mDefaultOptions);
    } catch (FileAlreadyExistsException e) {
        // Exception expected
        Assert.assertEquals(ExceptionMessage.MOUNT_POINT_ALREADY_EXISTS.getMessage("/mnt/foo"), e.getMessage());
    }
    try {
        mMountTable.add(new AlluxioURI("alluxio://localhost:1234/mnt/bar/baz"), new AlluxioURI("hdfs://localhost:5678/baz"), mDefaultOptions);
    } catch (InvalidPathException e) {
        Assert.assertEquals(ExceptionMessage.MOUNT_POINT_PREFIX_OF_ANOTHER.getMessage("/mnt/bar", "/mnt/bar/baz"), e.getMessage());
    }
    // Test resolve()
    Assert.assertEquals(new AlluxioURI("file://localhost:5678/foo"), mMountTable.resolve(new AlluxioURI("alluxio://localhost:1234/mnt/foo")).getUri());
    Assert.assertEquals(new AlluxioURI("file://localhost:5678/bar"), mMountTable.resolve(new AlluxioURI("alluxio://localhost:1234/mnt/bar")).getUri());
    Assert.assertEquals(new AlluxioURI("file://localhost:5678/bar/y"), mMountTable.resolve(new AlluxioURI("alluxio://localhost:1234/mnt/bar/y")).getUri());
    Assert.assertEquals(new AlluxioURI("file://localhost:5678/bar/baz"), mMountTable.resolve(new AlluxioURI("alluxio://localhost:1234/mnt/bar/baz")).getUri());
    // Test getMountPoint()
    Assert.assertEquals("/mnt/foo", mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/foo")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/bar")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/bar/y")));
    Assert.assertEquals("/mnt/bar", mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/bar/baz")));
    Assert.assertNull(mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt")));
    Assert.assertNull(mMountTable.getMountPoint(new AlluxioURI("alluxio://localhost:1234/")));
    // Test isMountPoint()
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/")));
    Assert.assertTrue(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/foo")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/foo/bar")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/foo2")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/foo3")));
    Assert.assertTrue(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/bar")));
    Assert.assertFalse(mMountTable.isMountPoint(new AlluxioURI("alluxio://localhost:1234/mnt/bar/baz")));
    // Test delete()
    Assert.assertTrue(mMountTable.delete(new AlluxioURI("alluxio://localhost:1234/mnt/bar")));
    Assert.assertTrue(mMountTable.delete(new AlluxioURI("alluxio://localhost:1234/mnt/foo")));
    Assert.assertFalse(mMountTable.delete(new AlluxioURI("alluxio://localhost:1234/mnt/foo")));
    Assert.assertFalse(mMountTable.delete(new AlluxioURI("alluxio://localhost:1234/")));
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 9 with FileAlreadyExistsException

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

the class FileSystemIntegrationTest method mkdir.

@Test
public void mkdir() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    CreateDirectoryOptions options = CreateDirectoryOptions.defaults().setRecursive(true);
    for (int k = 0; k < 10; k++) {
        mFileSystem.createDirectory(new AlluxioURI(uniqPath + k), options);
        try {
            mFileSystem.createDirectory(new AlluxioURI(uniqPath + k), options);
            Assert.fail("createDirectory should throw FileAlreadyExistsException");
        } catch (FileAlreadyExistsException e) {
            Assert.assertEquals(e.getMessage(), ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(uniqPath + k));
        }
    }
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) CreateDirectoryOptions(alluxio.client.file.options.CreateDirectoryOptions) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 10 with FileAlreadyExistsException

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

the class FileSystemIntegrationTest method createFileWithFileAlreadyExistsException.

@Test
public void createFileWithFileAlreadyExistsException() throws Exception {
    AlluxioURI uri = new AlluxioURI(PathUtils.uniqPath());
    mFileSystem.createFile(uri, mWriteBoth).close();
    Assert.assertNotNull(mFileSystem.getStatus(uri));
    try {
        mFileSystem.createFile(uri, mWriteBoth).close();
    } catch (AlluxioException e) {
        Assert.assertTrue(e instanceof FileAlreadyExistsException);
    }
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException) 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