/** * @return the default {@link FileLocationOptions} */ public static FileLocationOptions defaults() { return new FileLocationOptions(); }
@Override public List<String> get(long blockId, AlluxioURI fileUri, long offset) { List<String> locations = mCache.getIfPresent(blockId); if (locations != null) { return locations; } try { MountTable.Resolution resolution = mMountTable.resolve(fileUri); String ufsUri = resolution.getUri().toString(); try (CloseableResource<UnderFileSystem> ufsResource = resolution.acquireUfsResource()) { UnderFileSystem ufs = ufsResource.get(); locations = ufs.getFileLocations(ufsUri, FileLocationOptions.defaults().setOffset(offset)); } if (locations != null) { mCache.put(blockId, locations); return locations; } } catch (InvalidPathException | IOException e) { LOG.warn("Failed to get locations for block {} in file {} with offset {}: {}", blockId, fileUri, offset, e); } return null; } }
@Override public List<String> getFileLocations(String path) throws IOException { return getFileLocations(path, FileLocationOptions.defaults()); }
@Override @Nullable public List<String> getFileLocations(String path, FileLocationOptions options) throws IOException { // If the user has hinted the underlying storage nodes are not co-located with Alluxio // workers, short circuit without querying the locations. if (Boolean.valueOf(mUfsConf.get(PropertyKey.UNDERFS_HDFS_REMOTE))) { return null; } FileSystem hdfs = getFs(); List<String> ret = new ArrayList<>(); try { // The only usage of fileStatus is to get the path in getFileBlockLocations. // In HDFS 2, there is an API getFileBlockLocation(Path path, long offset, long len), // but in HDFS 1, the only API is // getFileBlockLocation(FileStatus stat, long offset, long len). // By constructing the file status manually, we can save one RPC call to getFileStatus. FileStatus fileStatus = new FileStatus(0L, false, 0, 0L, 0L, 0L, null, null, null, new Path(path)); BlockLocation[] bLocations = hdfs.getFileBlockLocations(fileStatus, options.getOffset(), 1); if (bLocations.length > 0) { String[] names = bLocations[0].getHosts(); Collections.addAll(ret, names); } } catch (IOException e) { LOG.warn("Unable to get file location for {} : {}", path, e.getMessage()); } return ret; }
@Override public List<String> get(long blockId, AlluxioURI fileUri, long offset) { List<String> locations = mCache.getIfPresent(blockId); if (locations != null) { return locations; } try { MountTable.Resolution resolution = mMountTable.resolve(fileUri); String ufsUri = resolution.getUri().toString(); try (CloseableResource<UnderFileSystem> ufsResource = resolution.acquireUfsResource()) { UnderFileSystem ufs = ufsResource.get(); locations = ufs.getFileLocations(ufsUri, FileLocationOptions.defaults().setOffset(offset)); } if (locations != null) { mCache.put(blockId, locations); return locations; } } catch (InvalidPathException | IOException e) { LOG.warn("Failed to get locations for block {} in file {} with offset {}: {}", blockId, fileUri, offset, e); } return null; } }
@Override public List<String> getFileLocations(String path) throws IOException { return getFileLocations(path, FileLocationOptions.defaults()); }
@Override public List<String> getFileLocations(String path, FileLocationOptions options) throws IOException { // If the user has hinted the underlying storage nodes are not co-located with Alluxio // workers, short circuit without querying the locations if (Boolean.valueOf(mUfsConf.getValue(PropertyKey.UNDERFS_HDFS_REMOTE))) { return null; } List<String> ret = new ArrayList<>(); try { FileStatus fStatus = mFileSystem.getFileStatus(new Path(path)); BlockLocation[] bLocations = mFileSystem.getFileBlockLocations(fStatus, options.getOffset(), 1); if (bLocations.length > 0) { String[] names = bLocations[0].getHosts(); Collections.addAll(ret, names); } } catch (IOException e) { LOG.warn("Unable to get file location for {} : {}", path, e.getMessage()); } return ret; }
/** * @return the default {@link FileLocationOptions} */ public static FileLocationOptions defaults() { return new FileLocationOptions(); }