Search in sources :

Example 31 with AlluxioException

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

the class LoadCommand method load.

/**
   * Loads a file or directory in Alluxio space, makes it resident in memory.
   *
   * @param filePath The {@link AlluxioURI} path to load into Alluxio memory
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void load(AlluxioURI filePath) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(filePath);
    if (status.isFolder()) {
        List<URIStatus> statuses = mFileSystem.listStatus(filePath);
        for (URIStatus uriStatus : statuses) {
            AlluxioURI newPath = new AlluxioURI(uriStatus.getPath());
            load(newPath);
        }
    } else {
        if (status.getInMemoryPercentage() == 100) {
            // The file has already been fully loaded into Alluxio memory.
            return;
        }
        Closer closer = Closer.create();
        try {
            OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.CACHE_PROMOTE);
            FileInStream in = closer.register(mFileSystem.openFile(filePath, options));
            byte[] buf = new byte[8 * Constants.MB];
            while (in.read(buf) != -1) {
            }
        } catch (Exception e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }
    System.out.println(filePath + " loaded");
}
Also used : Closer(com.google.common.io.Closer) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) AlluxioURI(alluxio.AlluxioURI)

Example 32 with AlluxioException

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

the class WithWildCardPathCommand method run.

@Override
public void run(CommandLine cl) throws AlluxioException, IOException {
    String[] args = cl.getArgs();
    AlluxioURI inputPath = new AlluxioURI(args[0]);
    List<AlluxioURI> paths = AlluxioShellUtils.getAlluxioURIs(mFileSystem, inputPath);
    if (paths.size() == 0) {
        // A unified sanity check on the paths
        throw new IOException(inputPath + " does not exist.");
    }
    Collections.sort(paths, createAlluxioURIComparator());
    List<String> errorMessages = new ArrayList<>();
    for (AlluxioURI path : paths) {
        try {
            runCommand(path, cl);
        } catch (AlluxioException | IOException e) {
            errorMessages.add(e.getMessage());
        }
    }
    if (errorMessages.size() != 0) {
        throw new IOException(Joiner.on('\n').join(errorMessages));
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

Example 33 with AlluxioException

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

the class CountCommand method countHelper.

private long[] countHelper(AlluxioURI path) throws AlluxioException, IOException {
    URIStatus status = mFileSystem.getStatus(path);
    if (!status.isFolder()) {
        return new long[] { 1L, 0L, status.getLength() };
    }
    long[] rtn = new long[] { 0L, 1L, 0L };
    List<URIStatus> statuses;
    try {
        statuses = mFileSystem.listStatus(path);
    } catch (AlluxioException e) {
        throw new IOException(e.getMessage());
    }
    for (URIStatus uriStatus : statuses) {
        long[] toAdd = countHelper(new AlluxioURI(uriStatus.getPath()));
        rtn[0] += toAdd[0];
        rtn[1] += toAdd[1];
        rtn[2] += toAdd[2];
    }
    return rtn;
}
Also used : IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) AlluxioException(alluxio.exception.AlluxioException) AlluxioURI(alluxio.AlluxioURI)

Example 34 with AlluxioException

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

the class CpCommand method copyToLocal.

/**
   * Copies a file or a directory from the Alluxio filesystem to the local filesystem.
   *
   * @param srcPath the source {@link AlluxioURI} (could be a file or a directory)
   * @param dstPath the {@link AlluxioURI} of the destination in the local filesystem
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyToLocal(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    URIStatus srcStatus = mFileSystem.getStatus(srcPath);
    File dstFile = new File(dstPath.getPath());
    if (srcStatus.isFolder()) {
        // make a local directory
        if (!dstFile.exists()) {
            if (!dstFile.mkdirs()) {
                throw new IOException("mkdir failure for directory: " + dstPath);
            } else {
                System.out.println("Create directory: " + dstPath);
            }
        }
        List<URIStatus> statuses;
        try {
            statuses = mFileSystem.listStatus(srcPath);
        } catch (AlluxioException e) {
            throw new IOException(e.getMessage());
        }
        List<String> errorMessages = new ArrayList<>();
        for (URIStatus status : statuses) {
            try {
                File subDstFile = new File(dstFile.getAbsolutePath(), status.getName());
                copyToLocal(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), status.getPath()), new AlluxioURI(dstPath.getScheme(), dstPath.getAuthority(), subDstFile.getPath()));
            } catch (IOException e) {
                errorMessages.add(e.getMessage());
            }
        }
        if (errorMessages.size() != 0) {
            throw new IOException(Joiner.on('\n').join(errorMessages));
        }
    } else {
        copyFileToLocal(srcPath, dstPath);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) File(java.io.File) AlluxioException(alluxio.exception.AlluxioException) AlluxioURI(alluxio.AlluxioURI)

Example 35 with AlluxioException

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

the class CpCommand method copyPath.

/**
   * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the
   * Alluxio filesystem space.
   *
   * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem
   * @param dstPath the {@link AlluxioURI} of the destination
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File src = new File(srcPath.getPath());
    if (!src.isDirectory()) {
        // src will be copied to.
        if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) {
            dstPath = dstPath.join(src.getName());
        }
        FileOutStream os = null;
        try (Closer closer = Closer.create()) {
            os = closer.register(mFileSystem.createFile(dstPath));
            FileInputStream in = closer.register(new FileInputStream(src));
            FileChannel channel = closer.register(in.getChannel());
            ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB);
            while (channel.read(buf) != -1) {
                buf.flip();
                os.write(buf.array(), 0, buf.limit());
            }
        } catch (Exception e) {
            // around.
            if (os != null) {
                os.cancel();
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw e;
        }
    } else {
        mFileSystem.createDirectory(dstPath);
        List<String> errorMessages = new ArrayList<>();
        File[] fileList = src.listFiles();
        if (fileList == null) {
            String errMsg = String.format("Failed to list files for directory %s", src);
            errorMessages.add(errMsg);
            fileList = new File[0];
        }
        int misFiles = 0;
        for (File srcFile : fileList) {
            AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName()));
            try {
                copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()), newURI);
            } catch (IOException e) {
                errorMessages.add(e.getMessage());
                if (!mFileSystem.exists(newURI)) {
                    misFiles++;
                }
            }
        }
        if (errorMessages.size() != 0) {
            if (misFiles == fileList.length) {
                // If the directory doesn't exist and no files were created, then delete the directory
                if (mFileSystem.exists(dstPath)) {
                    mFileSystem.delete(dstPath);
                }
            }
            throw new IOException(Joiner.on('\n').join(errorMessages));
        }
    }
}
Also used : Closer(com.google.common.io.Closer) FileChannel(java.nio.channels.FileChannel) FileOutStream(alluxio.client.file.FileOutStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) InvalidPathException(alluxio.exception.InvalidPathException) FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) File(java.io.File) AlluxioURI(alluxio.AlluxioURI)

Aggregations

AlluxioException (alluxio.exception.AlluxioException)56 IOException (java.io.IOException)54 AlluxioURI (alluxio.AlluxioURI)33 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)15 URIStatus (alluxio.client.file.URIStatus)13 ArrayList (java.util.ArrayList)11 InvalidPathException (alluxio.exception.InvalidPathException)9 Closer (com.google.common.io.Closer)8 FileOutStream (alluxio.client.file.FileOutStream)5 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)5 WorkerNetAddress (alluxio.wire.WorkerNetAddress)5 LockBlockResult (alluxio.wire.LockBlockResult)4 BlockWorkerClient (alluxio.client.block.BlockWorkerClient)3 Mode (alluxio.security.authorization.Mode)3 AlluxioTException (alluxio.thrift.AlluxioTException)3 ThriftIOException (alluxio.thrift.ThriftIOException)3 File (java.io.File)3 WorkerStorageTierAssoc (alluxio.WorkerStorageTierAssoc)2 LockBlockOptions (alluxio.client.block.options.LockBlockOptions)2 SetAttributeOptions (alluxio.client.file.options.SetAttributeOptions)2