Search in sources :

Example 11 with FileInStream

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

the class IsolatedFileSystemIntegrationTest method lockBlockTest3.

@Test
public void lockBlockTest3() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    FileInStream is;
    ByteBuffer buf;
    int numOfFiles = 5;
    int fileSize = WORKER_CAPACITY_BYTES / numOfFiles;
    List<AlluxioURI> files = new ArrayList<>();
    for (int k = 0; k < numOfFiles; k++) {
        FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + k, fileSize, mWriteBoth);
        files.add(new AlluxioURI(uniqPath + k));
    }
    for (int k = 0; k < numOfFiles; k++) {
        URIStatus info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
        is = mFileSystem.openFile(files.get(k), FileSystemTestUtils.toOpenFileOptions(mWriteBoth));
        buf = ByteBuffer.allocate((int) info.getBlockSizeBytes());
        int r = is.read(buf.array());
        if (k < numOfFiles - 1) {
            Assert.assertTrue(r != -1);
        }
        is.close();
    }
    FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + numOfFiles, fileSize, mWriteBoth);
    files.add(new AlluxioURI(uniqPath + numOfFiles));
    HeartbeatScheduler.execute(HeartbeatContext.WORKER_BLOCK_SYNC);
    URIStatus info = mFileSystem.getStatus(files.get(0));
    Assert.assertFalse(info.getInMemoryPercentage() == 100);
    for (int k = 1; k <= numOfFiles; k++) {
        info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream) ArrayList(java.util.ArrayList) URIStatus(alluxio.client.file.URIStatus) ByteBuffer(java.nio.ByteBuffer) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 12 with FileInStream

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

the class IsolatedFileSystemIntegrationTest method unlockBlockTest3.

@Test
public void unlockBlockTest3() throws Exception {
    String uniqPath = PathUtils.uniqPath();
    FileInStream is;
    ByteBuffer buf1;
    ByteBuffer buf2;
    int numOfFiles = 5;
    int fileSize = WORKER_CAPACITY_BYTES / numOfFiles;
    List<AlluxioURI> files = new ArrayList<>();
    for (int k = 0; k < numOfFiles; k++) {
        FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + k, fileSize, mWriteBoth);
        files.add(new AlluxioURI(uniqPath + k));
    }
    for (int k = 0; k < numOfFiles; k++) {
        URIStatus info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
        is = mFileSystem.openFile(files.get(k), FileSystemTestUtils.toOpenFileOptions(mWriteBoth));
        buf1 = ByteBuffer.allocate((int) info.getBlockSizeBytes());
        Assert.assertTrue(is.read(buf1.array()) != -1);
        buf2 = ByteBuffer.allocate((int) info.getBlockSizeBytes());
        is.seek(0);
        Assert.assertTrue(is.read(buf2.array()) != -1);
        is.close();
    }
    FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + numOfFiles, fileSize, mWriteBoth);
    files.add(new AlluxioURI(uniqPath + numOfFiles));
    HeartbeatScheduler.execute(HeartbeatContext.WORKER_BLOCK_SYNC);
    URIStatus info = mFileSystem.getStatus(files.get(0));
    Assert.assertFalse(info.getInMemoryPercentage() == 100);
    for (int k = 1; k <= numOfFiles; k++) {
        URIStatus in = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(in.getInMemoryPercentage() == 100);
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream) ArrayList(java.util.ArrayList) URIStatus(alluxio.client.file.URIStatus) ByteBuffer(java.nio.ByteBuffer) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 13 with FileInStream

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

the class CpCommand method copyFile.

/**
   * Copies a file in the Alluxio filesystem.
   *
   * @param srcPath the source {@link AlluxioURI} (has to be a file)
   * @param dstPath the destination path in the Alluxio filesystem
   * @throws AlluxioException when Alluxio exception occurs
   * @throws IOException when non-Alluxio exception occurs
   */
private void copyFile(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException {
    try (Closer closer = Closer.create()) {
        OpenFileOptions openFileOptions = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        FileInStream is = closer.register(mFileSystem.openFile(srcPath, openFileOptions));
        CreateFileOptions createFileOptions = CreateFileOptions.defaults();
        FileOutStream os = closer.register(mFileSystem.createFile(dstPath, createFileOptions));
        IOUtils.copy(is, os);
        System.out.println("Copied " + srcPath + " to " + dstPath);
    }
}
Also used : Closer(com.google.common.io.Closer) CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) FileOutStream(alluxio.client.file.FileOutStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions)

Example 14 with FileInStream

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

the class AbstractFileOutStreamIntegrationTest method checkFileInAlluxio.

/**
   * Checks the given file exists in Alluxio storage and expects its content to be an increasing
   * array of the given length.
   *
   * @param filePath path of the tmp file
   * @param fileLen length of the file
   */
protected void checkFileInAlluxio(AlluxioURI filePath, int fileLen) throws Exception {
    URIStatus status = mFileSystem.getStatus(filePath);
    Assert.assertEquals(fileLen, status.getLength());
    try (FileInStream is = mFileSystem.openFile(filePath, OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE))) {
        byte[] res = new byte[(int) status.getLength()];
        Assert.assertEquals((int) status.getLength(), is.read(res));
        Assert.assertTrue(BufferUtils.equalIncreasingByteArray(fileLen, res));
    }
}
Also used : FileInStream(alluxio.client.file.FileInStream) URIStatus(alluxio.client.file.URIStatus)

Example 15 with FileInStream

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

the class BufferedBlockInStreamIntegrationTest method readTest3.

/**
   * Tests {@link alluxio.client.block.BufferedBlockInStream#read(byte[], int, int)}.
   */
@Test
public void readTest3() throws IOException, AlluxioException {
    for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) {
        for (CreateFileOptions op : getOptionSet()) {
            AlluxioURI path = new AlluxioURI(sTestPath + "/file_" + k + "_" + op.hashCode());
            FileInStream is = sFileSystem.openFile(path, FileSystemTestUtils.toOpenFileOptions(op));
            byte[] ret = new byte[k / 2];
            Assert.assertEquals(k / 2, is.read(ret, 0, k / 2));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k / 2, ret));
            is.close();
            is = sFileSystem.openFile(path, FileSystemTestUtils.toOpenFileOptions(op));
            ret = new byte[k];
            Assert.assertEquals(k, is.read(ret, 0, k));
            Assert.assertTrue(BufferUtils.equalIncreasingByteArray(k, ret));
            is.close();
        }
    }
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

FileInStream (alluxio.client.file.FileInStream)179 AlluxioURI (alluxio.AlluxioURI)148 Test (org.junit.Test)132 BaseIntegrationTest (alluxio.testutils.BaseIntegrationTest)67 URIStatus (alluxio.client.file.URIStatus)44 CreateFilePOptions (alluxio.grpc.CreateFilePOptions)31 FileOutStream (alluxio.client.file.FileOutStream)25 CreateFileOptions (alluxio.client.file.options.CreateFileOptions)21 FileSystem (alluxio.client.file.FileSystem)17 ByteBuffer (java.nio.ByteBuffer)16 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)11 File (java.io.File)11 OpenFileOptions (alluxio.client.file.options.OpenFileOptions)10 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)8 FileInfo (alluxio.wire.FileInfo)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InstancedConfiguration (alluxio.conf.InstancedConfiguration)5 FileIncompleteException (alluxio.exception.FileIncompleteException)5