Search in sources :

Example 11 with InvalidPathException

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

the class CpCommand method copy.

/**
   * Copies a file or a directory in the Alluxio filesystem.
   *
   * @param srcPath the source {@link AlluxioURI} (could be a file or a directory)
   * @param dstPath the {@link AlluxioURI} of the destination path in the Alluxio filesystem
   * @param recursive indicates whether directories should be copied recursively
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copy(AlluxioURI srcPath, AlluxioURI dstPath, boolean recursive) throws AlluxioException, IOException {
    URIStatus srcStatus = mFileSystem.getStatus(srcPath);
    URIStatus dstStatus = null;
    try {
        dstStatus = mFileSystem.getStatus(dstPath);
    } catch (FileDoesNotExistException e) {
    // if the destination does not exist, it will be created
    }
    if (!srcStatus.isFolder()) {
        if (dstStatus != null && dstStatus.isFolder()) {
            dstPath = new AlluxioURI(PathUtils.concatPath(dstPath.getPath(), srcPath.getName()));
        }
        copyFile(srcPath, dstPath);
    } else {
        if (!recursive) {
            throw new IOException(srcPath.getPath() + " is a directory, to copy it please use \"cp -R <src> <dst>\"");
        }
        List<URIStatus> statuses;
        statuses = mFileSystem.listStatus(srcPath);
        if (dstStatus != null) {
            if (!dstStatus.isFolder()) {
                throw new InvalidPathException(ExceptionMessage.DESTINATION_CANNOT_BE_FILE.getMessage());
            }
            // subdirectory of the destination
            if (srcStatus.isFolder()) {
                dstPath = new AlluxioURI(PathUtils.concatPath(dstPath.getPath(), srcPath.getName()));
                mFileSystem.createDirectory(dstPath);
                System.out.println("Created directory: " + dstPath);
            }
        }
        if (dstStatus == null) {
            mFileSystem.createDirectory(dstPath);
            System.out.println("Created directory: " + dstPath);
        }
        List<String> errorMessages = new ArrayList<>();
        for (URIStatus status : statuses) {
            try {
                copy(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), status.getPath()), new AlluxioURI(dstPath.getScheme(), dstPath.getAuthority(), PathUtils.concatPath(dstPath.getPath(), status.getName())), recursive);
            } catch (IOException e) {
                errorMessages.add(e.getMessage());
            }
        }
        if (errorMessages.size() != 0) {
            throw new IOException(Joiner.on('\n').join(errorMessages));
        }
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 12 with InvalidPathException

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

the class CpCommand method run.

@Override
public void run(CommandLine cl) throws AlluxioException, IOException {
    String[] args = cl.getArgs();
    AlluxioURI srcPath = new AlluxioURI(args[0]);
    AlluxioURI dstPath = new AlluxioURI(args[1]);
    if ((dstPath.getScheme() == null || isAlluxio(dstPath.getScheme())) && isFile(srcPath.getScheme())) {
        List<File> srcFiles = AlluxioShellUtils.getFiles(srcPath.getPath());
        if (srcFiles.size() == 0) {
            throw new IOException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(srcPath));
        }
        if (srcPath.containsWildcard()) {
            List<AlluxioURI> srcPaths = new ArrayList<>();
            for (File srcFile : srcFiles) {
                srcPaths.add(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()));
            }
            copyFromLocalWildcard(srcPaths, dstPath);
        } else {
            copyFromLocal(srcPath, dstPath);
        }
    } else if ((srcPath.getScheme() == null || isAlluxio(srcPath.getScheme())) && isFile(dstPath.getScheme())) {
        List<AlluxioURI> srcPaths = AlluxioShellUtils.getAlluxioURIs(mFileSystem, srcPath);
        if (srcPaths.size() == 0) {
            throw new IOException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(srcPath));
        }
        if (srcPath.containsWildcard()) {
            copyWildcardToLocal(srcPaths, dstPath);
        } else {
            copyToLocal(srcPath, dstPath);
        }
    } else if ((srcPath.getScheme() == null || isAlluxio(srcPath.getScheme())) && (dstPath.getScheme() == null || isAlluxio(dstPath.getScheme()))) {
        List<AlluxioURI> srcPaths = AlluxioShellUtils.getAlluxioURIs(mFileSystem, srcPath);
        if (srcPaths.size() == 0) {
            throw new FileDoesNotExistException(ExceptionMessage.PATH_DOES_NOT_EXIST.getMessage(srcPath.getPath()));
        }
        if (srcPath.containsWildcard()) {
            copyWildcard(srcPaths, dstPath, cl.hasOption("R"));
        } else {
            copy(srcPath, dstPath, cl.hasOption("R"));
        }
    } else {
        throw new InvalidPathException("Schemes must be either file or alluxio, and at most one file scheme is allowed.");
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) File(java.io.File) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI)

Example 13 with InvalidPathException

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

the class CpCommand method copyWildcard.

/**
   * Copies a list of files or directories specified by srcPaths to the destination specified by
   * dstPath. This method is used when the original source path contains wildcards.
   *
   * @param srcPaths a list of files or directories in the Alluxio filesystem
   * @param dstPath the destination in the Alluxio filesystem
   * @param recursive indicates whether directories should be copied recursively
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyWildcard(List<AlluxioURI> srcPaths, AlluxioURI dstPath, boolean recursive) throws AlluxioException, IOException {
    URIStatus dstStatus = null;
    try {
        dstStatus = mFileSystem.getStatus(dstPath);
    } catch (FileDoesNotExistException e) {
    // if the destination does not exist, it will be created
    }
    if (dstStatus != null && !dstStatus.isFolder()) {
        throw new InvalidPathException(ExceptionMessage.DESTINATION_CANNOT_BE_FILE.getMessage());
    }
    if (dstStatus == null) {
        mFileSystem.createDirectory(dstPath);
        System.out.println("Created directory: " + dstPath);
    }
    List<String> errorMessages = new ArrayList<>();
    for (AlluxioURI srcPath : srcPaths) {
        try {
            copy(srcPath, new AlluxioURI(dstPath.getScheme(), dstPath.getAuthority(), PathUtils.concatPath(dstPath.getPath(), srcPath.getName())), recursive);
        } catch (AlluxioException | IOException e) {
            errorMessages.add(e.getMessage());
        }
    }
    if (errorMessages.size() != 0) {
        throw new IOException(Joiner.on('\n').join(errorMessages));
    }
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

Example 14 with InvalidPathException

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

the class DefaultFileSystemMaster method renameInternal.

/**
 * Implements renaming.
 *
 * @param rpcContext the rpc context
 * @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 context method options
 */
private void renameInternal(RpcContext rpcContext, LockedInodePath srcInodePath, LockedInodePath dstInodePath, boolean replayed, RenameContext context) throws FileDoesNotExistException, InvalidPathException, IOException, AccessControlException {
    // 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);
    if (dstInodePath.fullPathExists()) {
        throw new InvalidPathException("Destination path: " + dstPath + " already exists.");
    }
    mInodeTree.rename(rpcContext, RenameEntry.newBuilder().setId(srcInode.getId()).setOpTimeMs(context.getOperationTimeMs()).setNewParentId(dstParentInode.getId()).setNewName(dstName).setPath(srcPath.getPath()).setNewPath(dstPath.getPath()).build());
    // If the source file is persisted, rename it in the UFS.
    try {
        if (!replayed && srcInode.isPersisted()) {
            // Check if ufs is writable
            checkUfsMode(srcPath, OperationType.WRITE);
            checkUfsMode(dstPath, OperationType.WRITE);
            MountTable.Resolution resolution = mMountTable.resolve(srcPath);
            // Persist ancestor directories from top to the bottom. We cannot use recursive create
            // parents here because the permission for the ancestors can be different.
            // inodes from the same mount point as the dst
            Stack<InodeDirectory> sameMountDirs = new Stack<>();
            List<Inode> dstInodeList = dstInodePath.getInodeList();
            for (int i = dstInodeList.size() - 1; i >= 0; i--) {
                // Since dstInodePath is guaranteed not to be a full path, all inodes in the incomplete
                // path are guaranteed to be a directory.
                InodeDirectory dir = dstInodeList.get(i).asDirectory();
                sameMountDirs.push(dir);
                if (dir.isMountPoint()) {
                    break;
                }
            }
            while (!sameMountDirs.empty()) {
                InodeDirectory dir = sameMountDirs.pop();
                if (!dir.isPersisted()) {
                    mInodeTree.syncPersistExistingDirectory(rpcContext, dir);
                }
            }
            String ufsSrcPath = resolution.getUri().toString();
            try (CloseableResource<UnderFileSystem> ufsResource = resolution.acquireUfsResource()) {
                UnderFileSystem ufs = ufsResource.get();
                String ufsDstUri = mMountTable.resolve(dstPath).getUri().toString();
                boolean success;
                if (srcInode.isFile()) {
                    success = ufs.renameRenamableFile(ufsSrcPath, ufsDstUri);
                } else {
                    success = ufs.renameRenamableDirectory(ufsSrcPath, ufsDstUri);
                }
                if (!success) {
                    throw new IOException(ExceptionMessage.FAILED_UFS_RENAME.getMessage(ufsSrcPath, ufsDstUri));
                }
            }
            // The destination was persisted in ufs.
            mUfsAbsentPathCache.processExisting(dstPath);
        }
    } catch (Throwable t) {
        // On failure, revert changes and throw exception.
        mInodeTree.rename(rpcContext, RenameEntry.newBuilder().setId(srcInode.getId()).setOpTimeMs(context.getOperationTimeMs()).setNewName(srcName).setNewParentId(srcParentInode.getId()).setPath(dstPath.getPath()).setNewPath(srcPath.getPath()).build());
        throw t;
    }
    Metrics.PATHS_RENAMED.inc();
}
Also used : IOException(java.io.IOException) MountTable(alluxio.master.file.meta.MountTable) InvalidPathException(alluxio.exception.InvalidPathException) Fingerprint(alluxio.underfs.Fingerprint) Stack(java.util.Stack) InodeDirectory(alluxio.master.file.meta.InodeDirectory) Inode(alluxio.master.file.meta.Inode) UnderFileSystem(alluxio.underfs.UnderFileSystem) AlluxioURI(alluxio.AlluxioURI)

Example 15 with InvalidPathException

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

the class DefaultFileSystemMaster method workerHeartbeat.

@Override
public FileSystemCommand workerHeartbeat(long workerId, List<Long> persistedFiles, WorkerHeartbeatContext context) throws IOException {
    List<String> persistedUfsFingerprints = context.getOptions().getPersistedFileFingerprintsList();
    boolean hasPersistedFingerprints = persistedUfsFingerprints.size() == persistedFiles.size();
    for (int i = 0; i < persistedFiles.size(); i++) {
        long fileId = persistedFiles.get(i);
        String ufsFingerprint = hasPersistedFingerprints ? persistedUfsFingerprints.get(i) : Constants.INVALID_UFS_FINGERPRINT;
        try {
            // Permission checking for each file is performed inside setAttribute
            setAttribute(getPath(fileId), SetAttributeContext.mergeFrom(SetAttributePOptions.newBuilder().setPersisted(true)).setUfsFingerprint(ufsFingerprint));
        } catch (FileDoesNotExistException | AccessControlException | InvalidPathException e) {
            LOG.error("Failed to set file {} as persisted, because {}", fileId, e);
        }
    }
    // TODO(zac) Clean up master and worker code since this is taken care of by job service now.
    // Worker should not persist any files. Instead, files are persisted through job service.
    List<PersistFile> filesToPersist = new ArrayList<>();
    FileSystemCommandOptions commandOptions = new FileSystemCommandOptions();
    commandOptions.setPersistOptions(new PersistCommandOptions(filesToPersist));
    return new FileSystemCommand(CommandType.PERSIST, commandOptions);
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) AccessControlException(alluxio.exception.AccessControlException) FileSystemCommandOptions(alluxio.wire.FileSystemCommandOptions) FileSystemCommand(alluxio.wire.FileSystemCommand) Fingerprint(alluxio.underfs.Fingerprint) InvalidPathException(alluxio.exception.InvalidPathException) PersistFile(alluxio.wire.PersistFile) PersistCommandOptions(alluxio.wire.PersistCommandOptions)

Aggregations

InvalidPathException (alluxio.exception.InvalidPathException)82 AlluxioURI (alluxio.AlluxioURI)51 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)44 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)25 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)19 AccessControlException (alluxio.exception.AccessControlException)17 AlluxioException (alluxio.exception.AlluxioException)17 LockedInodePath (alluxio.master.file.meta.LockedInodePath)17 MountTable (alluxio.master.file.meta.MountTable)14 UnderFileSystem (alluxio.underfs.UnderFileSystem)14 Inode (alluxio.master.file.meta.Inode)12 MountInfo (alluxio.master.file.meta.options.MountInfo)11 BlockInfoException (alluxio.exception.BlockInfoException)10 UnavailableException (alluxio.exception.status.UnavailableException)9 LockResource (alluxio.resource.LockResource)9 DirectoryNotEmptyException (alluxio.exception.DirectoryNotEmptyException)8 InodeDirectory (alluxio.master.file.meta.InodeDirectory)8 Test (org.junit.Test)8 URIStatus (alluxio.client.file.URIStatus)7