@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; }
/** * Tests for default {@link FileLocationOptions}. */ @Test public void defaults() throws IOException { FileLocationOptions options = FileLocationOptions.defaults(); Assert.assertEquals(0, options.getOffset()); }
/** * Tests getting and setting fields. */ @Test public void fields() { FileLocationOptions options = FileLocationOptions.defaults(); long[] offsets = {100, 110, 150, 200}; for (long offset : offsets) { options.setOffset(offset); Assert.assertEquals(offset, options.getOffset()); } }
@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; }