use of alluxio.client.file.FileSystem 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();
}
}
}
use of alluxio.client.file.FileSystem 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));
}
use of alluxio.client.file.FileSystem in project alluxio by Alluxio.
the class MultiWorkerIntegrationTest method writeLargeFile.
@Test
public void writeLargeFile() throws Exception {
int fileSize = NUM_WORKERS * WORKER_MEMORY_SIZE_BYTES;
AlluxioURI file = new AlluxioURI("/test");
FileSystem fs = mResource.get().getClient();
// Write a file large enough to fill all the memory of all the workers.
FileSystemTestUtils.createByteFile(fs, file.getPath(), fileSize, CreateFileOptions.defaults().setWriteType(WriteType.MUST_CACHE).setLocationPolicy(new RoundRobinPolicy()));
URIStatus status = fs.getStatus(file);
assertEquals(100, status.getInMemoryPercentage());
try (FileInStream inStream = fs.openFile(file)) {
assertEquals(fileSize, IOUtils.toByteArray(inStream).length);
}
}
use of alluxio.client.file.FileSystem in project alluxio by Alluxio.
the class KeyValueSystemIntegrationTest method createMultiPartitions.
/**
* Tests creating and opening a store with a number of keys, while each key-value pair is large
* enough to take a separate key-value partition.
*/
@Test
public void createMultiPartitions() throws Exception {
// Each partition is at most 1 MB
final long maxPartitionSize = Constants.MB;
final int numKeys = 10;
// 4Byte key
final int keyLength = 4;
// 500KB value
final int valueLength = 500 * Constants.KB;
FileSystem fs = FileSystem.Factory.get();
AlluxioURI storeUri = createStoreOfMultiplePartitions(numKeys, null);
List<URIStatus> files = fs.listStatus(storeUri);
Assert.assertEquals(numKeys, files.size());
for (URIStatus info : files) {
Assert.assertTrue(info.getLength() <= maxPartitionSize);
}
mReader = sKeyValueSystem.openStore(storeUri);
for (int i = 0; i < numKeys; i++) {
byte[] key = BufferUtils.getIncreasingByteArray(i, keyLength);
byte[] value = mReader.get(key);
Assert.assertTrue(BufferUtils.equalIncreasingByteArray(i, valueLength, value));
}
Assert.assertNull(mReader.get(KEY1));
Assert.assertNull(mReader.get(KEY2));
mReader.close();
}
use of alluxio.client.file.FileSystem in project alluxio by Alluxio.
the class FileDataManager method prepareUfsFilePath.
/**
* Prepares the destination file path of the given file id. Also creates the parent folder if it
* does not exist.
*
* @param fileId the file id
* @return the path for persistence
* @throws AlluxioException if an unexpected Alluxio exception is thrown
* @throws IOException if the folder creation fails
*/
private String prepareUfsFilePath(long fileId) throws AlluxioException, IOException {
FileInfo fileInfo = mBlockWorker.getFileInfo(fileId);
AlluxioURI alluxioPath = new AlluxioURI(fileInfo.getPath());
FileSystem fs = FileSystem.Factory.get();
URIStatus status = fs.getStatus(alluxioPath);
String ufsPath = status.getUfsPath();
UnderFileSystem ufs = UnderFileSystem.Factory.get(ufsPath);
UnderFileSystemUtils.prepareFilePath(alluxioPath, ufsPath, fs, ufs);
return ufsPath;
}
Aggregations