-
Notifications
You must be signed in to change notification settings - Fork 9.1k
HDFS-6874. Add GETFILEBLOCKLOCATIONS operation to HttpFS #4750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ | |
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectReader; | ||
|
||
import org.apache.hadoop.classification.VisibleForTesting; | ||
import org.apache.hadoop.fs.BlockLocation; | ||
import org.apache.hadoop.util.Preconditions; | ||
import org.apache.hadoop.thirdparty.com.google.common.collect.Maps; | ||
import org.apache.hadoop.fs.ContentSummary; | ||
|
@@ -965,4 +968,53 @@ private static SnapshotStatus toSnapshotStatus( | |
SnapshotStatus.getParentPath(fullPath))); | ||
return snapshotStatus; | ||
} | ||
|
||
@VisibleForTesting | ||
public static BlockLocation[] toBlockLocationArray(Map<?, ?> json) | ||
throws IOException { | ||
final Map<?, ?> rootmap = | ||
(Map<?, ?>) json.get(BlockLocation.class.getSimpleName() + "s"); | ||
final List<?> array = | ||
JsonUtilClient.getList(rootmap, BlockLocation.class.getSimpleName()); | ||
Preconditions.checkNotNull(array); | ||
final BlockLocation[] locations = new BlockLocation[array.size()]; | ||
int i = 0; | ||
for (Object object : array) { | ||
final Map<?, ?> m = (Map<?, ?>) object; | ||
locations[i++] = JsonUtilClient.toBlockLocation(m); | ||
} | ||
return locations; | ||
} | ||
|
||
/** Convert a Json map to BlockLocation. **/ | ||
private static BlockLocation toBlockLocation(Map<?, ?> m) throws IOException { | ||
if (m == null) { | ||
return null; | ||
} | ||
long length = ((Number) m.get("length")).longValue(); | ||
long offset = ((Number) m.get("offset")).longValue(); | ||
boolean corrupt = Boolean.getBoolean(m.get("corrupt").toString()); | ||
String[] storageIds = toStringArray(getList(m, "storageIds")); | ||
ashutoshcipher marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want use this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for suggestion. I think current way looks fine too. Any specific reason to modify? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just think that it's an invalidate code line. Because the serialized code didn't serialize it. Not block this PR. |
||
String[] cachedHosts = toStringArray(getList(m, "cachedHosts")); | ||
String[] hosts = toStringArray(getList(m, "hosts")); | ||
String[] names = toStringArray(getList(m, "names")); | ||
String[] topologyPaths = toStringArray(getList(m, "topologyPaths")); | ||
StorageType[] storageTypes = toStorageTypeArray(getList(m, "storageTypes")); | ||
return new BlockLocation(names, hosts, cachedHosts, topologyPaths, | ||
storageIds, storageTypes, offset, length, corrupt); | ||
} | ||
|
||
@VisibleForTesting | ||
static String[] toStringArray(List<?> list) { | ||
if (list == null) { | ||
return null; | ||
} else { | ||
final String[] array = new String[list.size()]; | ||
int i = 0; | ||
for (Object object : list) { | ||
array[i++] = object.toString(); | ||
} | ||
return array; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,8 @@ public class WebHdfsFileSystem extends FileSystem | |
private KeyProvider testProvider; | ||
private boolean isTLSKrb; | ||
|
||
private boolean isServerHCFSCompatible = true; | ||
|
||
/** | ||
* Return the protocol scheme for the FileSystem. | ||
* | ||
|
@@ -1882,18 +1884,51 @@ public BlockLocation[] getFileBlockLocations(final FileStatus status, | |
} | ||
|
||
@Override | ||
public BlockLocation[] getFileBlockLocations(final Path p, | ||
final long offset, final long length) throws IOException { | ||
public BlockLocation[] getFileBlockLocations(final Path p, final long offset, | ||
final long length) throws IOException { | ||
statistics.incrementReadOps(1); | ||
storageStatistics.incrementOpCounter(OpType.GET_FILE_BLOCK_LOCATIONS); | ||
BlockLocation[] locations; | ||
try { | ||
if (isServerHCFSCompatible) { | ||
locations = getFileBlockLocations(GetOpParam.Op.GETFILEBLOCKLOCATIONS, p, offset, length); | ||
} else { | ||
locations = getFileBlockLocations(GetOpParam.Op.GET_BLOCK_LOCATIONS, p, offset, length); | ||
} | ||
} catch (RemoteException e) { | ||
// parsing the exception is needed only if the client thinks the service is compatible | ||
if (isServerHCFSCompatible && isGetFileBlockLocationsException(e)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can add one UT to test this fallback? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
LOG.warn("Server does not appear to support GETFILEBLOCKLOCATIONS." + | ||
"Fallback to the old GET_BLOCK_LOCATIONS. Exception: {}", | ||
e.getMessage()); | ||
isServerHCFSCompatible = false; | ||
locations = getFileBlockLocations(GetOpParam.Op.GET_BLOCK_LOCATIONS, p, offset, length); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
return locations; | ||
} | ||
|
||
final HttpOpParam.Op op = GetOpParam.Op.GET_BLOCK_LOCATIONS; | ||
return new FsPathResponseRunner<BlockLocation[]>(op, p, | ||
private boolean isGetFileBlockLocationsException(RemoteException e) { | ||
return e.getMessage() != null && e.getMessage().contains("Invalid value for webhdfs parameter") | ||
&& e.getMessage().contains(GetOpParam.Op.GETFILEBLOCKLOCATIONS.toString()); | ||
} | ||
|
||
private BlockLocation[] getFileBlockLocations(final GetOpParam.Op operation, | ||
final Path p, final long offset, final long length) throws IOException { | ||
return new FsPathResponseRunner<BlockLocation[]>(operation, p, | ||
new OffsetParam(offset), new LengthParam(length)) { | ||
@Override | ||
BlockLocation[] decodeResponse(Map<?,?> json) throws IOException { | ||
return DFSUtilClient.locatedBlocks2Locations( | ||
JsonUtilClient.toLocatedBlocks(json)); | ||
BlockLocation[] decodeResponse(Map<?, ?> json) throws IOException { | ||
switch (operation) { | ||
case GETFILEBLOCKLOCATIONS: | ||
ashutoshcipher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return JsonUtilClient.toBlockLocationArray(json); | ||
case GET_BLOCK_LOCATIONS: | ||
return DFSUtilClient.locatedBlocks2Locations(JsonUtilClient.toLocatedBlocks(json)); | ||
default: | ||
throw new IOException("Unknown operation " + operation.name()); | ||
} | ||
} | ||
}.run(); | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.