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; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import java.net.InetSocketAddress; |
---|
22 | import java.util.*; |
---|
23 | |
---|
24 | import org.apache.hadoop.conf.Configuration; |
---|
25 | import org.apache.hadoop.hdfs.protocol.Block; |
---|
26 | import org.apache.hadoop.hdfs.protocol.DatanodeInfo; |
---|
27 | import org.apache.hadoop.hdfs.protocol.LocatedBlock; |
---|
28 | import org.apache.hadoop.hdfs.server.common.GenerationStamp; |
---|
29 | import org.apache.hadoop.hdfs.server.namenode.NameNode; |
---|
30 | import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocol; |
---|
31 | import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations.BlockWithLocations; |
---|
32 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
33 | import org.apache.hadoop.fs.FileSystem; |
---|
34 | import org.apache.hadoop.fs.Path; |
---|
35 | import org.apache.hadoop.ipc.RPC; |
---|
36 | import org.apache.hadoop.ipc.RemoteException; |
---|
37 | import org.apache.hadoop.net.NetUtils; |
---|
38 | import org.apache.hadoop.security.UnixUserGroupInformation; |
---|
39 | |
---|
40 | import junit.framework.TestCase; |
---|
41 | /** |
---|
42 | * This class tests if block replacement request to data nodes work correctly. |
---|
43 | */ |
---|
44 | public class TestGetBlocks extends TestCase { |
---|
45 | /** test getBlocks */ |
---|
46 | public void testGetBlocks() throws Exception { |
---|
47 | final Configuration CONF = new Configuration(); |
---|
48 | |
---|
49 | final short REPLICATION_FACTOR = (short)2; |
---|
50 | final int DEFAULT_BLOCK_SIZE = 1024; |
---|
51 | final Random r = new Random(); |
---|
52 | |
---|
53 | CONF.setLong("dfs.block.size", DEFAULT_BLOCK_SIZE); |
---|
54 | MiniDFSCluster cluster = new MiniDFSCluster( |
---|
55 | CONF, REPLICATION_FACTOR, true, null ); |
---|
56 | try { |
---|
57 | cluster.waitActive(); |
---|
58 | |
---|
59 | // create a file with two blocks |
---|
60 | FileSystem fs = cluster.getFileSystem(); |
---|
61 | FSDataOutputStream out = fs.create(new Path("/tmp.txt"), |
---|
62 | REPLICATION_FACTOR); |
---|
63 | byte [] data = new byte[1024]; |
---|
64 | long fileLen = 2*DEFAULT_BLOCK_SIZE; |
---|
65 | long bytesToWrite = fileLen; |
---|
66 | while( bytesToWrite > 0 ) { |
---|
67 | r.nextBytes(data); |
---|
68 | int bytesToWriteNext = (1024<bytesToWrite)?1024:(int)bytesToWrite; |
---|
69 | out.write(data, 0, bytesToWriteNext); |
---|
70 | bytesToWrite -= bytesToWriteNext; |
---|
71 | } |
---|
72 | out.close(); |
---|
73 | |
---|
74 | // get blocks & data nodes |
---|
75 | List<LocatedBlock> locatedBlocks; |
---|
76 | DatanodeInfo[] dataNodes=null; |
---|
77 | boolean notWritten; |
---|
78 | do { |
---|
79 | final DFSClient dfsclient = new DFSClient(NameNode.getAddress(CONF), CONF); |
---|
80 | locatedBlocks = dfsclient.namenode. |
---|
81 | getBlockLocations("/tmp.txt", 0, fileLen).getLocatedBlocks(); |
---|
82 | assertEquals(2, locatedBlocks.size()); |
---|
83 | notWritten = false; |
---|
84 | for(int i=0; i<2; i++) { |
---|
85 | dataNodes = locatedBlocks.get(i).getLocations(); |
---|
86 | if(dataNodes.length != REPLICATION_FACTOR) { |
---|
87 | notWritten = true; |
---|
88 | try { |
---|
89 | Thread.sleep(10); |
---|
90 | } catch(InterruptedException e) { |
---|
91 | } |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | } while(notWritten); |
---|
96 | |
---|
97 | // get RPC client to namenode |
---|
98 | InetSocketAddress addr = new InetSocketAddress("localhost", |
---|
99 | cluster.getNameNodePort()); |
---|
100 | NamenodeProtocol namenode = (NamenodeProtocol) RPC.getProxy( |
---|
101 | NamenodeProtocol.class, NamenodeProtocol.versionID, addr, |
---|
102 | UnixUserGroupInformation.login(CONF), CONF, |
---|
103 | NetUtils.getDefaultSocketFactory(CONF)); |
---|
104 | |
---|
105 | // get blocks of size fileLen from dataNodes[0] |
---|
106 | BlockWithLocations[] locs; |
---|
107 | locs = namenode.getBlocks(dataNodes[0], fileLen).getBlocks(); |
---|
108 | assertEquals(locs.length, 2); |
---|
109 | assertEquals(locs[0].getDatanodes().length, 2); |
---|
110 | assertEquals(locs[1].getDatanodes().length, 2); |
---|
111 | |
---|
112 | // get blocks of size BlockSize from dataNodes[0] |
---|
113 | locs = namenode.getBlocks(dataNodes[0], DEFAULT_BLOCK_SIZE).getBlocks(); |
---|
114 | assertEquals(locs.length, 1); |
---|
115 | assertEquals(locs[0].getDatanodes().length, 2); |
---|
116 | |
---|
117 | // get blocks of size 1 from dataNodes[0] |
---|
118 | locs = namenode.getBlocks(dataNodes[0], 1).getBlocks(); |
---|
119 | assertEquals(locs.length, 1); |
---|
120 | assertEquals(locs[0].getDatanodes().length, 2); |
---|
121 | |
---|
122 | // get blocks of size 0 from dataNodes[0] |
---|
123 | getBlocksWithException(namenode, dataNodes[0], 0); |
---|
124 | |
---|
125 | // get blocks of size -1 from dataNodes[0] |
---|
126 | getBlocksWithException(namenode, dataNodes[0], -1); |
---|
127 | |
---|
128 | // get blocks of size BlockSize from a non-existent datanode |
---|
129 | getBlocksWithException(namenode, new DatanodeInfo(), 2); |
---|
130 | } finally { |
---|
131 | cluster.shutdown(); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | private void getBlocksWithException(NamenodeProtocol namenode, |
---|
136 | DatanodeInfo datanode, |
---|
137 | long size) throws IOException { |
---|
138 | boolean getException = false; |
---|
139 | try { |
---|
140 | namenode.getBlocks(new DatanodeInfo(), 2); |
---|
141 | } catch(RemoteException e) { |
---|
142 | getException = true; |
---|
143 | assertTrue(e.getMessage().contains("IllegalArgumentException")); |
---|
144 | } |
---|
145 | assertTrue(getException); |
---|
146 | } |
---|
147 | |
---|
148 | public void testGenerationStampWildCard() { |
---|
149 | Map<Block, Long> map = new HashMap<Block, Long>(); |
---|
150 | final Random RAN = new Random(); |
---|
151 | final long seed = RAN.nextLong(); |
---|
152 | System.out.println("seed=" + seed); |
---|
153 | RAN.setSeed(seed); |
---|
154 | |
---|
155 | long[] blkids = new long[10]; |
---|
156 | for(int i = 0; i < blkids.length; i++) { |
---|
157 | blkids[i] = 1000L + RAN.nextInt(100000); |
---|
158 | map.put(new Block(blkids[i], 0, blkids[i]), blkids[i]); |
---|
159 | } |
---|
160 | System.out.println("map=" + map.toString().replace(",", "\n ")); |
---|
161 | |
---|
162 | for(int i = 0; i < blkids.length; i++) { |
---|
163 | Block b = new Block(blkids[i], 0, GenerationStamp.WILDCARD_STAMP); |
---|
164 | Long v = map.get(b); |
---|
165 | System.out.println(b + " => " + v); |
---|
166 | assertEquals(blkids[i], v.longValue()); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * @param args |
---|
172 | */ |
---|
173 | public static void main(String[] args) throws Exception { |
---|
174 | (new TestGetBlocks()).testGetBlocks(); |
---|
175 | } |
---|
176 | |
---|
177 | } |
---|