1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with this |
---|
4 | * work for additional information regarding copyright ownership. The ASF |
---|
5 | * licenses this file to you under the Apache License, Version 2.0 (the |
---|
6 | * "License"); you may not use this file except in compliance with the License. |
---|
7 | * You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
---|
13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
---|
14 | * License for the specific language governing permissions and limitations under |
---|
15 | * the License. |
---|
16 | */ |
---|
17 | |
---|
18 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import java.util.Arrays; |
---|
22 | import java.util.Comparator; |
---|
23 | import java.util.Random; |
---|
24 | |
---|
25 | import junit.framework.TestCase; |
---|
26 | |
---|
27 | import org.apache.hadoop.conf.Configuration; |
---|
28 | |
---|
29 | /** |
---|
30 | * Testing the correctness of FileSystem.getFileBlockLocations. |
---|
31 | */ |
---|
32 | public class TestGetFileBlockLocations extends TestCase { |
---|
33 | private static String TEST_ROOT_DIR = |
---|
34 | System.getProperty("test.build.data", "/tmp/testGetFileBlockLocations"); |
---|
35 | private static final int FileLength = 4 * 1024 * 1024; // 4MB |
---|
36 | private Configuration conf; |
---|
37 | private Path path; |
---|
38 | private FileSystem fs; |
---|
39 | private Random random; |
---|
40 | |
---|
41 | /** |
---|
42 | * @see TestCase#setUp() |
---|
43 | */ |
---|
44 | @Override |
---|
45 | protected void setUp() throws IOException { |
---|
46 | conf = new Configuration(); |
---|
47 | Path rootPath = new Path(TEST_ROOT_DIR); |
---|
48 | path = new Path(rootPath, "TestGetFileBlockLocations"); |
---|
49 | fs = rootPath.getFileSystem(conf); |
---|
50 | FSDataOutputStream fsdos = fs.create(path, true); |
---|
51 | byte[] buffer = new byte[1024]; |
---|
52 | while (fsdos.getPos() < FileLength) { |
---|
53 | fsdos.write(buffer); |
---|
54 | } |
---|
55 | fsdos.close(); |
---|
56 | random = new Random(System.nanoTime()); |
---|
57 | } |
---|
58 | |
---|
59 | private void oneTest(int offBegin, int offEnd, FileStatus status) |
---|
60 | throws IOException { |
---|
61 | if (offBegin > offEnd) { |
---|
62 | int tmp = offBegin; |
---|
63 | offBegin = offEnd; |
---|
64 | offEnd = tmp; |
---|
65 | } |
---|
66 | BlockLocation[] locations = |
---|
67 | fs.getFileBlockLocations(status, offBegin, offEnd - offBegin); |
---|
68 | if (offBegin < status.getLen()) { |
---|
69 | Arrays.sort(locations, new Comparator<BlockLocation>() { |
---|
70 | |
---|
71 | @Override |
---|
72 | public int compare(BlockLocation arg0, BlockLocation arg1) { |
---|
73 | long cmprv = arg0.getOffset() - arg1.getOffset(); |
---|
74 | if (cmprv < 0) return -1; |
---|
75 | if (cmprv > 0) return 1; |
---|
76 | cmprv = arg0.getLength() - arg1.getLength(); |
---|
77 | if (cmprv < 0) return -1; |
---|
78 | if (cmprv > 0) return 1; |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | }); |
---|
83 | offBegin = (int) Math.min(offBegin, status.getLen() - 1); |
---|
84 | offEnd = (int) Math.min(offEnd, status.getLen()); |
---|
85 | BlockLocation first = locations[0]; |
---|
86 | BlockLocation last = locations[locations.length - 1]; |
---|
87 | assertTrue(first.getOffset() <= offBegin); |
---|
88 | assertTrue(offEnd <= last.getOffset() + last.getLength()); |
---|
89 | } else { |
---|
90 | assertTrue(locations.length == 0); |
---|
91 | } |
---|
92 | } |
---|
93 | /** |
---|
94 | * @see TestCase#tearDown() |
---|
95 | */ |
---|
96 | @Override |
---|
97 | protected void tearDown() throws IOException { |
---|
98 | fs.delete(path, true); |
---|
99 | fs.close(); |
---|
100 | } |
---|
101 | |
---|
102 | public void testFailureNegativeParameters() throws IOException { |
---|
103 | FileStatus status = fs.getFileStatus(path); |
---|
104 | try { |
---|
105 | BlockLocation[] locations = fs.getFileBlockLocations(status, -1, 100); |
---|
106 | fail("Expecting exception being throw"); |
---|
107 | } catch (IllegalArgumentException e) { |
---|
108 | |
---|
109 | } |
---|
110 | |
---|
111 | try { |
---|
112 | BlockLocation[] locations = fs.getFileBlockLocations(status, 100, -1); |
---|
113 | fail("Expecting exception being throw"); |
---|
114 | } catch (IllegalArgumentException e) { |
---|
115 | |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | public void testGetFileBlockLocations1() throws IOException { |
---|
120 | FileStatus status = fs.getFileStatus(path); |
---|
121 | oneTest(0, (int) status.getLen(), status); |
---|
122 | oneTest(0, (int) status.getLen() * 2, status); |
---|
123 | oneTest((int) status.getLen() * 2, (int) status.getLen() * 4, status); |
---|
124 | oneTest((int) status.getLen() / 2, (int) status.getLen() * 3, status); |
---|
125 | for (int i = 0; i < 10; ++i) { |
---|
126 | oneTest((int) status.getLen() * i / 10, (int) status.getLen() * (i + 1) |
---|
127 | / 10, status); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | public void testGetFileBlockLocations2() throws IOException { |
---|
132 | FileStatus status = fs.getFileStatus(path); |
---|
133 | for (int i = 0; i < 1000; ++i) { |
---|
134 | int offBegin = random.nextInt((int) (2 * status.getLen())); |
---|
135 | int offEnd = random.nextInt((int) (2 * status.getLen())); |
---|
136 | oneTest(offBegin, offEnd, status); |
---|
137 | } |
---|
138 | } |
---|
139 | } |
---|