Search in sources :

Example 26 with AlluxioException

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

the class FileSystemUtilsIntegrationTest method waitCompletedTest2.

@Test
public void waitCompletedTest2() throws IOException, AlluxioException, InterruptedException {
    final String uniqPath = PathUtils.uniqPath();
    // random value chosen through a fair dice roll :P
    final int numWrites = 4;
    final AlluxioURI uri = new AlluxioURI(uniqPath);
    final Runnable writer = new Runnable() {

        @Override
        public void run() {
            try {
                FileOutStream os = sFileSystem.createFile(uri, sWriteBoth);
                boolean completed = sFileSystem.getStatus(uri).isCompleted();
                Assert.assertFalse(completed);
                // four writes that will take > 600ms due to the sleeps
                for (int i = 0; i < numWrites; i++) {
                    os.write(42);
                    CommonUtils.sleepMs(200);
                }
                os.close();
                completed = sFileSystem.getStatus(uri).isCompleted();
                Assert.assertTrue(completed);
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }
    };
    final Runnable waiter = new Runnable() {

        @Override
        public void run() {
            try {
                // set the slow default polling period to a more sensible value, in order
                // to speed up the tests artificial waiting times
                String original = Configuration.get(PropertyKey.USER_FILE_WAITCOMPLETED_POLL_MS);
                Configuration.set(PropertyKey.USER_FILE_WAITCOMPLETED_POLL_MS, "100");
                try {
                    // The write will take at most 600ms I am waiting for at most 400ms - epsilon.
                    boolean completed = FileSystemUtils.waitCompleted(sFileSystem, uri, 300, TimeUnit.MILLISECONDS);
                    Assert.assertFalse(completed);
                    completed = sFileSystem.getStatus(uri).isCompleted();
                    Assert.assertFalse(completed);
                } finally {
                    Configuration.set(PropertyKey.USER_FILE_WAITCOMPLETED_POLL_MS, original);
                }
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail(e.getMessage());
            }
        }
    };
    final Thread waitingThread = new Thread(waiter);
    waitingThread.start();
    final Thread writingThread = new Thread(writer);
    writingThread.start();
    waitingThread.join();
    writingThread.join();
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) ExpectedException(org.junit.rules.ExpectedException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 27 with AlluxioException

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

the class FileSystemUtilsIntegrationTest method waitCompletedTest1.

@Test
public void waitCompletedTest1() throws IOException, AlluxioException, InterruptedException {
    final String uniqPath = PathUtils.uniqPath();
    // random value chosen through a fair dice roll :P
    final int numWrites = 4;
    final AlluxioURI uri = new AlluxioURI(uniqPath);
    final Runnable writer = new Runnable() {

        @Override
        public void run() {
            try {
                FileOutStream os = sFileSystem.createFile(uri, sWriteBoth);
                boolean completed = sFileSystem.getStatus(uri).isCompleted();
                Assert.assertFalse(completed);
                for (int i = 0; i < numWrites; i++) {
                    os.write(42);
                    CommonUtils.sleepMs(200);
                }
                os.close();
                completed = sFileSystem.getStatus(uri).isCompleted();
                Assert.assertTrue(completed);
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }
    };
    final Runnable waiter = new Runnable() {

        @Override
        public void run() {
            try {
                boolean completed = FileSystemUtils.waitCompleted(sFileSystem, uri);
                Assert.assertTrue(completed);
                completed = sFileSystem.getStatus(uri).isCompleted();
                Assert.assertTrue(completed);
            } catch (Exception e) {
                e.printStackTrace();
                Assert.fail(e.getMessage());
            }
        }
    };
    final Thread waitingThread = new Thread(waiter);
    waitingThread.start();
    final Thread writingThread = new Thread(writer);
    writingThread.start();
    waitingThread.join();
    writingThread.join();
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) ExpectedException(org.junit.rules.ExpectedException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 28 with AlluxioException

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

the class CpCommand method copyFromLocalWildcard.

/**
   * Copies a list of files or directories specified by srcPaths from the local filesystem to
   * dstPath in the Alluxio filesystem space. This method is used when the input path contains
   * wildcards.
   *
   * @param srcPaths a list of files or directories 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 copyFromLocalWildcard(List<AlluxioURI> srcPaths, AlluxioURI dstPath) throws AlluxioException, IOException {
    boolean dstExistedBefore = mFileSystem.exists(dstPath);
    createDstDir(dstPath);
    List<String> errorMessages = new ArrayList<>();
    int misFiles = 0;
    for (AlluxioURI srcPath : srcPaths) {
        AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcPath.getName()));
        try {
            copyPath(srcPath, newURI);
            System.out.println("Copied " + srcPath + " to " + dstPath);
        } catch (AlluxioException | IOException e) {
            errorMessages.add(e.getMessage());
            if (!mFileSystem.exists(newURI)) {
                misFiles++;
            }
        }
    }
    if (errorMessages.size() != 0) {
        if (misFiles == srcPaths.size()) {
            // If the directory doesn't exist and no files were created, then delete the directory
            if (!dstExistedBefore && mFileSystem.exists(dstPath)) {
                mFileSystem.delete(dstPath);
            }
        }
        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 29 with AlluxioException

use of alluxio.exception.AlluxioException 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 30 with AlluxioException

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

the class CpCommand method copyFromLocalDir.

/**
   * Copies a directory from local to Alluxio filesystem. The destination directory structure
   * maintained as local directory. This method is used when input path is a directory.
   *
   * @param srcPath the {@link AlluxioURI} of the source directory 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 copyFromLocalDir(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    File srcDir = new File(srcPath.getPath());
    boolean dstExistedBefore = mFileSystem.exists(dstPath);
    createDstDir(dstPath);
    List<String> errorMessages = new ArrayList<>();
    File[] fileList = srcDir.listFiles();
    if (fileList == null) {
        String errMsg = String.format("Failed to list files for directory %s", srcDir);
        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 (AlluxioException | 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 (!dstExistedBefore && mFileSystem.exists(dstPath)) {
                mFileSystem.delete(dstPath);
            }
        }
        throw new IOException(Joiner.on('\n').join(errorMessages));
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

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