Search in sources :

Example 76 with FileInStream

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

the class MultiUfsMountIntegrationTest method mountAfterWorkerRestart.

@Test
public void mountAfterWorkerRestart() throws Exception {
    String ufsFile1 = PathUtils.concatPath(mUfsUri1, "file1");
    String ufsFile2 = PathUtils.concatPath(mUfsUri2, "file2");
    UnderFileSystemUtils.touch(mLocalUfs, ufsFile1);
    UnderFileSystemUtils.touch(mLocalUfs, ufsFile2);
    mLocalAlluxioCluster.stopWorkers();
    mLocalAlluxioCluster.startWorkers();
    AlluxioURI file1 = mMountPoint1.join("file1");
    AlluxioURI file2 = mMountPoint2.join("file2");
    Assert.assertTrue(mFileSystem.exists(file1));
    Assert.assertTrue(mFileSystem.exists(file2));
    FileInStream inStream1 = mFileSystem.openFile(file1);
    Assert.assertNotNull(inStream1);
    inStream1.close();
    FileInStream inStream2 = mFileSystem.openFile(file2);
    Assert.assertNotNull(inStream2);
    inStream2.close();
}
Also used : FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI) BaseIntegrationTest(alluxio.testutils.BaseIntegrationTest) Test(org.junit.Test)

Example 77 with FileInStream

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

the class S3ClientRestApiTest method putObjectTest.

private void putObjectTest(String bucket, String objectKey, byte[] object, Long uploadId, Integer partNumber) throws Exception {
    final String fullObjectKey = bucket + AlluxioURI.SEPARATOR + objectKey;
    createObject(fullObjectKey, object, uploadId, partNumber);
    // Verify the object is created for the new bucket.
    AlluxioURI bucketURI = new AlluxioURI(AlluxioURI.SEPARATOR + bucket);
    AlluxioURI objectURI = new AlluxioURI(AlluxioURI.SEPARATOR + fullObjectKey);
    if (uploadId != null) {
        String tmpDir = S3RestUtils.getMultipartTemporaryDirForObject(bucketURI.getPath(), objectKey);
        bucketURI = new AlluxioURI(tmpDir);
        objectURI = new AlluxioURI(tmpDir + AlluxioURI.SEPARATOR + partNumber.toString());
    }
    List<FileInfo> fileInfos = mFileSystemMaster.listStatus(bucketURI, ListStatusContext.defaults());
    Assert.assertEquals(1, fileInfos.size());
    Assert.assertEquals(objectURI.getPath(), fileInfos.get(0).getPath());
    // Verify the object's content.
    FileInStream is = mFileSystem.openFile(objectURI);
    byte[] writtenObjectContent = IOUtils.toString(is).getBytes();
    is.close();
    Assert.assertArrayEquals(object, writtenObjectContent);
}
Also used : FileInfo(alluxio.wire.FileInfo) FileInStream(alluxio.client.file.FileInStream) AlluxioURI(alluxio.AlluxioURI)

Example 78 with FileInStream

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

the class WebInterfaceDownloadServlet method downloadFile.

/**
   * This function prepares for downloading a file.
   *
   * @param path the path of the file to download
   * @param request the {@link HttpServletRequest} object
   * @param response the {@link HttpServletResponse} object
   * @throws FileDoesNotExistException if the file does not exist
   * @throws IOException if an I/O error occurs
   * @throws InvalidPathException if an invalid path is encountered
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   */
private void downloadFile(AlluxioURI path, HttpServletRequest request, HttpServletResponse response) throws FileDoesNotExistException, IOException, InvalidPathException, AlluxioException {
    FileSystem alluxioClient = FileSystem.Factory.get();
    URIStatus status = alluxioClient.getStatus(path);
    long len = status.getLength();
    String fileName = path.getName();
    response.setContentType("application/octet-stream");
    if (len <= Integer.MAX_VALUE) {
        response.setContentLength((int) len);
    } else {
        response.addHeader("Content-Length", Long.toString(len));
    }
    response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
    FileInStream is = null;
    ServletOutputStream out = null;
    try {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        is = alluxioClient.openFile(path, options);
        out = response.getOutputStream();
        ByteStreams.copy(is, out);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
        }
        if (is != null) {
            is.close();
        }
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) FileSystem(alluxio.client.file.FileSystem) FileInStream(alluxio.client.file.FileInStream) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus)

Example 79 with FileInStream

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

the class WebInterfaceBrowseServlet method displayFile.

/**
   * This function displays 5KB of a file from a specific offset if it is in ASCII format.
   *
   * @param path the path of the file to display
   * @param request the {@link HttpServletRequest} object
   * @param offset where the file starts to display
   * @throws FileDoesNotExistException if the file does not exist
   * @throws IOException if an I/O error occurs
   * @throws InvalidPathException if an invalid path is encountered
   * @throws AlluxioException if an unexpected Alluxio exception is thrown
   */
private void displayFile(AlluxioURI path, HttpServletRequest request, long offset) throws FileDoesNotExistException, InvalidPathException, IOException, AlluxioException {
    FileSystem fs = FileSystem.Factory.get();
    String fileData;
    URIStatus status = fs.getStatus(path);
    if (status.isCompleted()) {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        try (FileInStream is = fs.openFile(path, options)) {
            int len = (int) Math.min(5 * Constants.KB, status.getLength() - offset);
            byte[] data = new byte[len];
            long skipped = is.skip(offset);
            if (skipped < 0) {
                // nothing was skipped
                fileData = "Unable to traverse to offset; is file empty?";
            } else if (skipped < offset) {
                // couldn't skip all the way to offset
                fileData = "Unable to traverse to offset; is offset larger than the file?";
            } else {
                // read may not read up to len, so only convert what was read
                int read = is.read(data, 0, len);
                if (read < 0) {
                    // stream couldn't read anything, skip went to EOF?
                    fileData = "Unable to read file";
                } else {
                    fileData = WebUtils.convertByteArrayToStringWithoutEscape(data, 0, read);
                }
            }
        }
    } else {
        fileData = "The requested file is not complete yet.";
    }
    List<UIFileBlockInfo> uiBlockInfo = new ArrayList<>();
    for (FileBlockInfo fileBlockInfo : mMaster.getFileSystemMaster().getFileBlockInfoList(path)) {
        uiBlockInfo.add(new UIFileBlockInfo(fileBlockInfo));
    }
    request.setAttribute("fileBlocks", uiBlockInfo);
    request.setAttribute("fileData", fileData);
    request.setAttribute("highestTierAlias", mMaster.getBlockMaster().getGlobalStorageTierAssoc().getAlias(0));
}
Also used : FileSystem(alluxio.client.file.FileSystem) FileInStream(alluxio.client.file.FileInStream) ArrayList(java.util.ArrayList) OpenFileOptions(alluxio.client.file.options.OpenFileOptions) URIStatus(alluxio.client.file.URIStatus) FileBlockInfo(alluxio.wire.FileBlockInfo)

Example 80 with FileInStream

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

the class IsolatedFileSystemIntegrationTest method lockBlockTest2.

@Test
public void lockBlockTest2() 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());
        Assert.assertTrue(is.read(buf.array()) != -1);
        is.close();
    }
    FileSystemTestUtils.createByteFile(mFileSystem, uniqPath + numOfFiles, fileSize, mWriteBoth);
    files.add(new AlluxioURI(uniqPath + numOfFiles));
    for (int k = 1; k < numOfFiles; k++) {
        URIStatus info = mFileSystem.getStatus(files.get(k));
        Assert.assertTrue(info.getInMemoryPercentage() == 100);
    }
    HeartbeatScheduler.execute(HeartbeatContext.WORKER_BLOCK_SYNC);
    URIStatus info = mFileSystem.getStatus(files.get(numOfFiles));
    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)

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