Search in sources :

Example 21 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class PersistMultipleMountsIntegrationTest method syncMultipleMountsDefaultPersist.

@Test
public void syncMultipleMountsDefaultPersist() throws Exception {
    // Skip non-local and non-HDFS UFSs.
    Assume.assumeTrue(UnderFileSystemUtils.isLocal(mUfs) || UnderFileSystemUtils.isHdfs(mUfs));
    String path = PathUtils.uniqPath();
    AlluxioURI filePath = new AlluxioURI(path);
    FileOutStream os = mFileSystem.createFile(filePath, CreateFileOptions.defaults().setWriteType(WriteType.CACHE_THROUGH));
    os.write((byte) 0);
    os.write((byte) 1);
    os.close();
    // Check the file is persisted
    URIStatus status = mFileSystem.getStatus(filePath);
    Assert.assertEquals(PersistenceState.PERSISTED.toString(), status.getPersistenceState());
    Assert.assertTrue(status.isCompleted());
    Assert.assertTrue(mUfs.exists(PathUtils.concatPath(mUfsRoot, path)));
    Assert.assertFalse(mMountedUfs.exists(PathUtils.concatPath(mMountedUfsRoot, path)));
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) URIStatus(alluxio.client.file.URIStatus) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 22 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class PersistMultipleMountsIntegrationTest method syncMultipleMountsMountedPersist.

@Test
public void syncMultipleMountsMountedPersist() throws Exception {
    // Skip non-local and non-HDFS UFSs.
    Assume.assumeTrue(UnderFileSystemUtils.isLocal(mUfs) || UnderFileSystemUtils.isHdfs(mUfs));
    String path = PathUtils.uniqPath();
    AlluxioURI filePath = new AlluxioURI(MOUNT_PATH + path);
    FileOutStream os = mFileSystem.createFile(filePath, CreateFileOptions.defaults().setWriteType(WriteType.CACHE_THROUGH));
    os.write((byte) 0);
    os.write((byte) 1);
    os.close();
    // Check the file is persisted
    URIStatus status = mFileSystem.getStatus(filePath);
    Assert.assertEquals(PersistenceState.PERSISTED.toString(), status.getPersistenceState());
    Assert.assertTrue(status.isCompleted());
    Assert.assertFalse(mUfs.exists(PathUtils.concatPath(mUfsRoot, path)));
    Assert.assertTrue(mMountedUfs.exists(PathUtils.concatPath(mMountedUfsRoot, path)));
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) URIStatus(alluxio.client.file.URIStatus) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 23 with FileOutStream

use of alluxio.client.file.FileOutStream 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)

Example 24 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class RemoteBlockInStreamIntegrationTest method readMultiBlockFile.

/**
   * Tests that reading a file consisting of more than one block from the underfs works.
   */
@Test
public void readMultiBlockFile() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    int blockSizeByte = 10;
    int numBlocks = 10;
    AlluxioURI uri = new AlluxioURI(uniqPath);
    FileOutStream os = mFileSystem.createFile(uri, mWriteUnderStore);
    for (int i = 0; i < numBlocks; i++) {
        for (int j = 0; j < blockSizeByte; j++) {
            os.write((byte) (i * blockSizeByte + j));
        }
    }
    os.close();
    FileInStream is = mFileSystem.openFile(uri, mReadCache);
    for (int i = 0; i < blockSizeByte * numBlocks; i++) {
        Assert.assertEquals((byte) i, is.read());
    }
    is.close();
    Assert.assertTrue(mFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 25 with FileOutStream

use of alluxio.client.file.FileOutStream in project alluxio by Alluxio.

the class UnderFileSystemBlockInStreamIntegrationTest method readMultiBlockFile.

/**
   * Tests that reading a file consisting of more than one block from the underfs works.
   */
@Test
public void readMultiBlockFile() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    int blockSizeByte = 10;
    int numBlocks = 10;
    AlluxioURI uri = new AlluxioURI(uniqPath);
    FileOutStream os = mFileSystem.createFile(uri, mWriteUnderStore);
    for (int i = 0; i < numBlocks; i++) {
        for (int j = 0; j < blockSizeByte; j++) {
            os.write((byte) (i * blockSizeByte + j));
        }
    }
    os.close();
    FileInStream is = mFileSystem.openFile(uri, mReadCache);
    for (int i = 0; i < blockSizeByte * numBlocks; i++) {
        Assert.assertEquals((byte) i, is.read());
    }
    is.close();
    Assert.assertTrue(mFileSystem.getStatus(uri).getInMemoryPercentage() == 100);
}
Also used : FileOutStream(alluxio.client.file.FileOutStream) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

FileOutStream (alluxio.client.file.FileOutStream)39 AlluxioURI (alluxio.AlluxioURI)28 Test (org.junit.Test)25 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)11 FileInStream (alluxio.client.file.FileInStream)9 URIStatus (alluxio.client.file.URIStatus)9 AlluxioException (alluxio.exception.AlluxioException)5 IOException (java.io.IOException)5 FileSystem (alluxio.client.file.FileSystem)4 ByteBuffer (java.nio.ByteBuffer)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 LocalFirstPolicy (alluxio.client.file.policy.LocalFirstPolicy)2 LineageFileSystem (alluxio.client.lineage.LineageFileSystem)2 LineageMasterClient (alluxio.client.lineage.LineageMasterClient)2 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)2 Closer (com.google.common.io.Closer)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ExpectedException (org.junit.rules.ExpectedException)2 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)1