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");
}
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));
}
}
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;
}
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);
}
}
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));
}
}
}
Aggregations