|
32 | 32 | import org.apache.hadoop.fs.BlockLocation; |
33 | 33 | import org.apache.hadoop.fs.FileStatus; |
34 | 34 | import org.apache.hadoop.fs.FileSystem; |
| 35 | +import org.apache.hadoop.fs.HdfsBlockLocation; |
35 | 36 | import org.apache.hadoop.fs.LocatedFileStatus; |
36 | 37 | import org.apache.hadoop.fs.Path; |
37 | 38 | import org.apache.hadoop.fs.PathFilter; |
38 | 39 | import org.apache.hadoop.fs.RawLocalFileSystem; |
39 | 40 | import org.apache.hadoop.fs.RemoteIterator; |
| 41 | +import org.apache.hadoop.hdfs.protocol.DatanodeID; |
| 42 | +import org.apache.hadoop.hdfs.protocol.DatanodeInfo; |
| 43 | +import org.apache.hadoop.hdfs.protocol.ExtendedBlock; |
| 44 | +import org.apache.hadoop.hdfs.protocol.LocatedBlock; |
| 45 | +import org.apache.hadoop.hdfs.server.blockmanagement.DatanodeDescriptor; |
40 | 46 | import org.apache.hadoop.mapred.SplitLocationInfo; |
41 | 47 | import org.apache.hadoop.mapreduce.InputSplit; |
42 | 48 | import org.apache.hadoop.mapreduce.Job; |
@@ -238,6 +244,50 @@ public void testListStatusErrorOnNonExistantDir() throws IOException { |
238 | 244 | } |
239 | 245 | } |
240 | 246 |
|
| 247 | + @Test |
| 248 | + public void testShrinkStatus() throws IOException { |
| 249 | + Configuration conf = getConfiguration(); |
| 250 | + MockFileSystem mockFs = |
| 251 | + (MockFileSystem) new Path("test:///").getFileSystem(conf); |
| 252 | + Path dir1 = new Path("test:/a1"); |
| 253 | + RemoteIterator<LocatedFileStatus> statuses = mockFs.listLocatedStatus(dir1); |
| 254 | + boolean verified = false; |
| 255 | + while (statuses.hasNext()) { |
| 256 | + LocatedFileStatus orig = statuses.next(); |
| 257 | + LocatedFileStatus shrink = |
| 258 | + (LocatedFileStatus)FileInputFormat.shrinkStatus(orig); |
| 259 | + Assert.assertTrue(orig.equals(shrink)); |
| 260 | + if (shrink.getBlockLocations() != null) { |
| 261 | + Assert.assertEquals(orig.getBlockLocations().length, |
| 262 | + shrink.getBlockLocations().length); |
| 263 | + for (int i = 0; i < shrink.getBlockLocations().length; i++) { |
| 264 | + verified = true; |
| 265 | + BlockLocation location = shrink.getBlockLocations()[i]; |
| 266 | + BlockLocation actual = orig.getBlockLocations()[i]; |
| 267 | + Assert.assertNotNull(((HdfsBlockLocation)actual).getLocatedBlock()); |
| 268 | + Assert.assertEquals(BlockLocation.class.getName(), |
| 269 | + location.getClass().getName()); |
| 270 | + Assert.assertArrayEquals(actual.getHosts(), location.getHosts()); |
| 271 | + Assert.assertArrayEquals(actual.getCachedHosts(), |
| 272 | + location.getCachedHosts()); |
| 273 | + Assert.assertArrayEquals(actual.getStorageIds(), |
| 274 | + location.getStorageIds()); |
| 275 | + Assert.assertArrayEquals(actual.getStorageTypes(), |
| 276 | + location.getStorageTypes()); |
| 277 | + Assert.assertArrayEquals(actual.getTopologyPaths(), |
| 278 | + location.getTopologyPaths()); |
| 279 | + Assert.assertArrayEquals(actual.getNames(), location.getNames()); |
| 280 | + Assert.assertEquals(actual.getLength(), location.getLength()); |
| 281 | + Assert.assertEquals(actual.getOffset(), location.getOffset()); |
| 282 | + Assert.assertEquals(actual.isCorrupt(), location.isCorrupt()); |
| 283 | + } |
| 284 | + } else { |
| 285 | + Assert.assertTrue(orig.getBlockLocations() == null); |
| 286 | + } |
| 287 | + } |
| 288 | + Assert.assertTrue(verified); |
| 289 | + } |
| 290 | + |
241 | 291 | public static List<Path> configureTestSimple(Configuration conf, FileSystem localFs) |
242 | 292 | throws IOException { |
243 | 293 | Path base1 = new Path(TEST_ROOT_DIR, "input1"); |
@@ -437,10 +487,31 @@ public FileStatus[] listStatus(Path f, PathFilter filter) |
437 | 487 | @Override |
438 | 488 | public BlockLocation[] getFileBlockLocations(FileStatus file, long start, long len) |
439 | 489 | throws IOException { |
440 | | - return new BlockLocation[] { |
441 | | - new BlockLocation(new String[] { "localhost:9866", "otherhost:9866" }, |
442 | | - new String[] { "localhost", "otherhost" }, new String[] { "localhost" }, |
443 | | - new String[0], 0, len, false) }; } |
| 490 | + DatanodeInfo[] ds = new DatanodeInfo[2]; |
| 491 | + ds[0] = new DatanodeDescriptor( |
| 492 | + new DatanodeID("127.0.0.1", "localhost", "abcd", |
| 493 | + 9866, 9867, 9868, 9869)); |
| 494 | + ds[1] = new DatanodeDescriptor( |
| 495 | + new DatanodeID("1.0.0.1", "otherhost", "efgh", |
| 496 | + 9866, 9867, 9868, 9869)); |
| 497 | + long blockLen = len / 3; |
| 498 | + ExtendedBlock b1 = new ExtendedBlock("bpid", 0, blockLen, 0); |
| 499 | + ExtendedBlock b2 = new ExtendedBlock("bpid", 1, blockLen, 1); |
| 500 | + ExtendedBlock b3 = new ExtendedBlock("bpid", 2, len - 2 * blockLen, 2); |
| 501 | + String[] names = new String[]{ "localhost:9866", "otherhost:9866" }; |
| 502 | + String[] hosts = new String[]{ "localhost", "otherhost" }; |
| 503 | + String[] cachedHosts = {"localhost"}; |
| 504 | + BlockLocation loc1 = new BlockLocation(names, hosts, cachedHosts, |
| 505 | + new String[0], 0, blockLen, false); |
| 506 | + BlockLocation loc2 = new BlockLocation(names, hosts, cachedHosts, |
| 507 | + new String[0], blockLen, blockLen, false); |
| 508 | + BlockLocation loc3 = new BlockLocation(names, hosts, cachedHosts, |
| 509 | + new String[0], 2 * blockLen, len - 2 * blockLen, false); |
| 510 | + return new BlockLocation[]{ |
| 511 | + new HdfsBlockLocation(loc1, new LocatedBlock(b1, ds)), |
| 512 | + new HdfsBlockLocation(loc2, new LocatedBlock(b2, ds)), |
| 513 | + new HdfsBlockLocation(loc3, new LocatedBlock(b3, ds)) }; |
| 514 | + } |
444 | 515 |
|
445 | 516 | @Override |
446 | 517 | protected RemoteIterator<LocatedFileStatus> listLocatedStatus(Path f, |
|
0 commit comments