1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.hdfs.server.datanode; |
---|
19 | |
---|
20 | import java.io.DataInputStream; |
---|
21 | import java.io.IOException; |
---|
22 | import java.io.InputStream; |
---|
23 | import java.io.OutputStream; |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | import org.apache.hadoop.conf.Configuration; |
---|
27 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
28 | import org.apache.hadoop.hdfs.server.datanode.FSDatasetInterface; |
---|
29 | import org.apache.hadoop.hdfs.server.datanode.SimulatedFSDataset; |
---|
30 | import org.apache.hadoop.util.DataChecksum; |
---|
31 | |
---|
32 | /** |
---|
33 | * this class tests the methods of the SimulatedFSDataset. |
---|
34 | * |
---|
35 | */ |
---|
36 | |
---|
37 | public class TestSimulatedFSDataset extends TestCase { |
---|
38 | |
---|
39 | Configuration conf = null; |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | static final int NUMBLOCKS = 20; |
---|
44 | static final int BLOCK_LENGTH_MULTIPLIER = 79; |
---|
45 | |
---|
46 | protected void setUp() throws Exception { |
---|
47 | super.setUp(); |
---|
48 | conf = new Configuration(); |
---|
49 | conf.setBoolean(SimulatedFSDataset.CONFIG_PROPERTY_SIMULATED, true); |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | protected void tearDown() throws Exception { |
---|
54 | super.tearDown(); |
---|
55 | } |
---|
56 | |
---|
57 | long blockIdToLen(long blkid) { |
---|
58 | return blkid*BLOCK_LENGTH_MULTIPLIER; |
---|
59 | } |
---|
60 | |
---|
61 | int addSomeBlocks(FSDatasetInterface fsdataset, int startingBlockId) throws IOException { |
---|
62 | int bytesAdded = 0; |
---|
63 | for (int i = startingBlockId; i < startingBlockId+NUMBLOCKS; ++i) { |
---|
64 | Block b = new Block(i, 0, 0); // we pass expected len as zero, - fsdataset should use the sizeof actual data written |
---|
65 | OutputStream dataOut = fsdataset.writeToBlock(b, false).dataOut; |
---|
66 | assertEquals(0, fsdataset.getLength(b)); |
---|
67 | for (int j=1; j <= blockIdToLen(i); ++j) { |
---|
68 | dataOut.write(j); |
---|
69 | assertEquals(j, fsdataset.getLength(b)); // correct length even as we write |
---|
70 | bytesAdded++; |
---|
71 | } |
---|
72 | dataOut.close(); |
---|
73 | b.setNumBytes(blockIdToLen(i)); |
---|
74 | fsdataset.finalizeBlock(b); |
---|
75 | assertEquals(blockIdToLen(i), fsdataset.getLength(b)); |
---|
76 | } |
---|
77 | return bytesAdded; |
---|
78 | } |
---|
79 | int addSomeBlocks(FSDatasetInterface fsdataset ) throws IOException { |
---|
80 | return addSomeBlocks(fsdataset, 1); |
---|
81 | } |
---|
82 | |
---|
83 | public void testGetMetaData() throws IOException { |
---|
84 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
85 | Block b = new Block(1, 5, 0); |
---|
86 | try { |
---|
87 | assertFalse(fsdataset.metaFileExists(b)); |
---|
88 | assertTrue("Expected an IO exception", false); |
---|
89 | } catch (IOException e) { |
---|
90 | // ok - as expected |
---|
91 | } |
---|
92 | addSomeBlocks(fsdataset); // Only need to add one but .... |
---|
93 | b = new Block(1, 0, 0); |
---|
94 | InputStream metaInput = fsdataset.getMetaDataInputStream(b); |
---|
95 | DataInputStream metaDataInput = new DataInputStream(metaInput); |
---|
96 | short version = metaDataInput.readShort(); |
---|
97 | assertEquals(FSDataset.METADATA_VERSION, version); |
---|
98 | DataChecksum checksum = DataChecksum.newDataChecksum(metaDataInput); |
---|
99 | assertEquals(DataChecksum.CHECKSUM_NULL, checksum.getChecksumType()); |
---|
100 | assertEquals(0, checksum.getChecksumSize()); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | public void testStorageUsage() throws IOException { |
---|
105 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
106 | assertEquals(fsdataset.getDfsUsed(), 0); |
---|
107 | assertEquals(fsdataset.getRemaining(), fsdataset.getCapacity()); |
---|
108 | int bytesAdded = addSomeBlocks(fsdataset); |
---|
109 | assertEquals(bytesAdded, fsdataset.getDfsUsed()); |
---|
110 | assertEquals(fsdataset.getCapacity()-bytesAdded, fsdataset.getRemaining()); |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | void checkBlockDataAndSize(FSDatasetInterface fsdataset, |
---|
117 | Block b, long expectedLen) throws IOException { |
---|
118 | InputStream input = fsdataset.getBlockInputStream(b); |
---|
119 | long lengthRead = 0; |
---|
120 | int data; |
---|
121 | while ((data = input.read()) != -1) { |
---|
122 | assertEquals(SimulatedFSDataset.DEFAULT_DATABYTE, data); |
---|
123 | lengthRead++; |
---|
124 | } |
---|
125 | assertEquals(expectedLen, lengthRead); |
---|
126 | } |
---|
127 | |
---|
128 | public void testWriteRead() throws IOException { |
---|
129 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
130 | addSomeBlocks(fsdataset); |
---|
131 | for (int i=1; i <= NUMBLOCKS; ++i) { |
---|
132 | Block b = new Block(i, 0, 0); |
---|
133 | assertTrue(fsdataset.isValidBlock(b)); |
---|
134 | assertEquals(blockIdToLen(i), fsdataset.getLength(b)); |
---|
135 | checkBlockDataAndSize(fsdataset, b, blockIdToLen(i)); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | public void testGetBlockReport() throws IOException { |
---|
142 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
143 | Block[] blockReport = fsdataset.getBlockReport(); |
---|
144 | assertEquals(0, blockReport.length); |
---|
145 | int bytesAdded = addSomeBlocks(fsdataset); |
---|
146 | blockReport = fsdataset.getBlockReport(); |
---|
147 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
148 | for (Block b: blockReport) { |
---|
149 | assertNotNull(b); |
---|
150 | assertEquals(blockIdToLen(b.getBlockId()), b.getNumBytes()); |
---|
151 | } |
---|
152 | } |
---|
153 | public void testInjectionEmpty() throws IOException { |
---|
154 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
155 | Block[] blockReport = fsdataset.getBlockReport(); |
---|
156 | assertEquals(0, blockReport.length); |
---|
157 | int bytesAdded = addSomeBlocks(fsdataset); |
---|
158 | blockReport = fsdataset.getBlockReport(); |
---|
159 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
160 | for (Block b: blockReport) { |
---|
161 | assertNotNull(b); |
---|
162 | assertEquals(blockIdToLen(b.getBlockId()), b.getNumBytes()); |
---|
163 | } |
---|
164 | |
---|
165 | // Inject blocks into an empty fsdataset |
---|
166 | // - injecting the blocks we got above. |
---|
167 | |
---|
168 | |
---|
169 | SimulatedFSDataset sfsdataset = new SimulatedFSDataset(conf); |
---|
170 | sfsdataset.injectBlocks(blockReport); |
---|
171 | blockReport = sfsdataset.getBlockReport(); |
---|
172 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
173 | for (Block b: blockReport) { |
---|
174 | assertNotNull(b); |
---|
175 | assertEquals(blockIdToLen(b.getBlockId()), b.getNumBytes()); |
---|
176 | assertEquals(blockIdToLen(b.getBlockId()), sfsdataset.getLength(b)); |
---|
177 | } |
---|
178 | assertEquals(bytesAdded, sfsdataset.getDfsUsed()); |
---|
179 | assertEquals(sfsdataset.getCapacity()-bytesAdded, sfsdataset.getRemaining()); |
---|
180 | } |
---|
181 | |
---|
182 | public void testInjectionNonEmpty() throws IOException { |
---|
183 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
184 | |
---|
185 | Block[] blockReport = fsdataset.getBlockReport(); |
---|
186 | assertEquals(0, blockReport.length); |
---|
187 | int bytesAdded = addSomeBlocks(fsdataset); |
---|
188 | blockReport = fsdataset.getBlockReport(); |
---|
189 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
190 | for (Block b: blockReport) { |
---|
191 | assertNotNull(b); |
---|
192 | assertEquals(blockIdToLen(b.getBlockId()), b.getNumBytes()); |
---|
193 | } |
---|
194 | fsdataset = null; |
---|
195 | |
---|
196 | // Inject blocks into an non-empty fsdataset |
---|
197 | // - injecting the blocks we got above. |
---|
198 | |
---|
199 | |
---|
200 | SimulatedFSDataset sfsdataset = new SimulatedFSDataset(conf); |
---|
201 | // Add come blocks whose block ids do not conflict with |
---|
202 | // the ones we are going to inject. |
---|
203 | bytesAdded += addSomeBlocks(sfsdataset, NUMBLOCKS+1); |
---|
204 | Block[] blockReport2 = sfsdataset.getBlockReport(); |
---|
205 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
206 | blockReport2 = sfsdataset.getBlockReport(); |
---|
207 | assertEquals(NUMBLOCKS, blockReport.length); |
---|
208 | sfsdataset.injectBlocks(blockReport); |
---|
209 | blockReport = sfsdataset.getBlockReport(); |
---|
210 | assertEquals(NUMBLOCKS*2, blockReport.length); |
---|
211 | for (Block b: blockReport) { |
---|
212 | assertNotNull(b); |
---|
213 | assertEquals(blockIdToLen(b.getBlockId()), b.getNumBytes()); |
---|
214 | assertEquals(blockIdToLen(b.getBlockId()), sfsdataset.getLength(b)); |
---|
215 | } |
---|
216 | assertEquals(bytesAdded, sfsdataset.getDfsUsed()); |
---|
217 | assertEquals(sfsdataset.getCapacity()-bytesAdded, sfsdataset.getRemaining()); |
---|
218 | |
---|
219 | |
---|
220 | // Now test that the dataset cannot be created if it does not have sufficient cap |
---|
221 | |
---|
222 | conf.setLong(SimulatedFSDataset.CONFIG_PROPERTY_CAPACITY, 10); |
---|
223 | |
---|
224 | try { |
---|
225 | sfsdataset = new SimulatedFSDataset(conf); |
---|
226 | sfsdataset.injectBlocks(blockReport); |
---|
227 | assertTrue("Expected an IO exception", false); |
---|
228 | } catch (IOException e) { |
---|
229 | // ok - as expected |
---|
230 | } |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | public void checkInvalidBlock(Block b) throws IOException { |
---|
235 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
236 | assertFalse(fsdataset.isValidBlock(b)); |
---|
237 | try { |
---|
238 | fsdataset.getLength(b); |
---|
239 | assertTrue("Expected an IO exception", false); |
---|
240 | } catch (IOException e) { |
---|
241 | // ok - as expected |
---|
242 | } |
---|
243 | |
---|
244 | try { |
---|
245 | fsdataset.getBlockInputStream(b); |
---|
246 | assertTrue("Expected an IO exception", false); |
---|
247 | } catch (IOException e) { |
---|
248 | // ok - as expected |
---|
249 | } |
---|
250 | |
---|
251 | try { |
---|
252 | fsdataset.finalizeBlock(b); |
---|
253 | assertTrue("Expected an IO exception", false); |
---|
254 | } catch (IOException e) { |
---|
255 | // ok - as expected |
---|
256 | } |
---|
257 | |
---|
258 | } |
---|
259 | |
---|
260 | public void testInValidBlocks() throws IOException { |
---|
261 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
262 | Block b = new Block(1, 5, 0); |
---|
263 | checkInvalidBlock(b); |
---|
264 | |
---|
265 | // Now check invlaid after adding some blocks |
---|
266 | addSomeBlocks(fsdataset); |
---|
267 | b = new Block(NUMBLOCKS + 99, 5, 0); |
---|
268 | checkInvalidBlock(b); |
---|
269 | |
---|
270 | } |
---|
271 | |
---|
272 | public void testInvalidate() throws IOException { |
---|
273 | FSDatasetInterface fsdataset = new SimulatedFSDataset(conf); |
---|
274 | int bytesAdded = addSomeBlocks(fsdataset); |
---|
275 | Block[] deleteBlocks = new Block[2]; |
---|
276 | deleteBlocks[0] = new Block(1, 0, 0); |
---|
277 | deleteBlocks[1] = new Block(2, 0, 0); |
---|
278 | fsdataset.invalidate(deleteBlocks); |
---|
279 | checkInvalidBlock(deleteBlocks[0]); |
---|
280 | checkInvalidBlock(deleteBlocks[1]); |
---|
281 | long sizeDeleted = blockIdToLen(1) + blockIdToLen(2); |
---|
282 | assertEquals(bytesAdded-sizeDeleted, fsdataset.getDfsUsed()); |
---|
283 | assertEquals(fsdataset.getCapacity()-bytesAdded+sizeDeleted, fsdataset.getRemaining()); |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | // Now make sure the rest of the blocks are valid |
---|
288 | for (int i=3; i <= NUMBLOCKS; ++i) { |
---|
289 | Block b = new Block(i, 0, 0); |
---|
290 | assertTrue(fsdataset.isValidBlock(b)); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | } |
---|