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 | |
---|
19 | package org.apache.hadoop.hdfs; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.net.URI; |
---|
23 | import java.util.Random; |
---|
24 | |
---|
25 | import org.apache.commons.logging.impl.Log4JLogger; |
---|
26 | import org.apache.hadoop.conf.Configuration; |
---|
27 | import org.apache.hadoop.fs.FSDataInputStream; |
---|
28 | import org.apache.hadoop.fs.FSDataOutputStream; |
---|
29 | import org.apache.hadoop.fs.FileChecksum; |
---|
30 | import org.apache.hadoop.fs.FileSystem; |
---|
31 | import org.apache.hadoop.fs.Path; |
---|
32 | import org.apache.log4j.Level; |
---|
33 | |
---|
34 | public class TestDistributedFileSystem extends junit.framework.TestCase { |
---|
35 | private static final Random RAN = new Random(); |
---|
36 | |
---|
37 | public void testFileSystemCloseAll() throws Exception { |
---|
38 | Configuration conf = new Configuration(); |
---|
39 | MiniDFSCluster cluster = new MiniDFSCluster(conf, 0, true, null); |
---|
40 | URI address = FileSystem.getDefaultUri(conf); |
---|
41 | |
---|
42 | try { |
---|
43 | FileSystem.closeAll(); |
---|
44 | |
---|
45 | conf = new Configuration(); |
---|
46 | FileSystem.setDefaultUri(conf, address); |
---|
47 | FileSystem.get(conf); |
---|
48 | FileSystem.get(conf); |
---|
49 | FileSystem.closeAll(); |
---|
50 | } |
---|
51 | finally { |
---|
52 | if (cluster != null) {cluster.shutdown();} |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Tests DFSClient.close throws no ConcurrentModificationException if |
---|
58 | * multiple files are open. |
---|
59 | */ |
---|
60 | public void testDFSClose() throws Exception { |
---|
61 | Configuration conf = new Configuration(); |
---|
62 | MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
63 | FileSystem fileSys = cluster.getFileSystem(); |
---|
64 | |
---|
65 | try { |
---|
66 | // create two files |
---|
67 | fileSys.create(new Path("/test/dfsclose/file-0")); |
---|
68 | fileSys.create(new Path("/test/dfsclose/file-1")); |
---|
69 | |
---|
70 | fileSys.close(); |
---|
71 | } |
---|
72 | finally { |
---|
73 | if (cluster != null) {cluster.shutdown();} |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | public void testDFSClient() throws Exception { |
---|
78 | Configuration conf = new Configuration(); |
---|
79 | MiniDFSCluster cluster = null; |
---|
80 | |
---|
81 | try { |
---|
82 | cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
83 | final Path filepath = new Path("/test/LeaseChecker/foo"); |
---|
84 | final long millis = System.currentTimeMillis(); |
---|
85 | |
---|
86 | { |
---|
87 | DistributedFileSystem dfs = (DistributedFileSystem)cluster.getFileSystem(); |
---|
88 | assertFalse(dfs.dfs.isLeaseCheckerStarted()); |
---|
89 | |
---|
90 | //create a file |
---|
91 | FSDataOutputStream out = dfs.create(filepath); |
---|
92 | assertTrue(dfs.dfs.isLeaseCheckerStarted()); |
---|
93 | |
---|
94 | //write something and close |
---|
95 | out.writeLong(millis); |
---|
96 | assertTrue(dfs.dfs.isLeaseCheckerStarted()); |
---|
97 | out.close(); |
---|
98 | assertTrue(dfs.dfs.isLeaseCheckerStarted()); |
---|
99 | dfs.close(); |
---|
100 | } |
---|
101 | |
---|
102 | { |
---|
103 | DistributedFileSystem dfs = (DistributedFileSystem)cluster.getFileSystem(); |
---|
104 | assertFalse(dfs.dfs.isLeaseCheckerStarted()); |
---|
105 | |
---|
106 | //open and check the file |
---|
107 | FSDataInputStream in = dfs.open(filepath); |
---|
108 | assertFalse(dfs.dfs.isLeaseCheckerStarted()); |
---|
109 | assertEquals(millis, in.readLong()); |
---|
110 | assertFalse(dfs.dfs.isLeaseCheckerStarted()); |
---|
111 | in.close(); |
---|
112 | assertFalse(dfs.dfs.isLeaseCheckerStarted()); |
---|
113 | dfs.close(); |
---|
114 | } |
---|
115 | } |
---|
116 | finally { |
---|
117 | if (cluster != null) {cluster.shutdown();} |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | public void testFileChecksum() throws IOException { |
---|
122 | ((Log4JLogger)HftpFileSystem.LOG).getLogger().setLevel(Level.ALL); |
---|
123 | |
---|
124 | final long seed = RAN.nextLong(); |
---|
125 | System.out.println("seed=" + seed); |
---|
126 | RAN.setSeed(seed); |
---|
127 | |
---|
128 | final Configuration conf = new Configuration(); |
---|
129 | conf.set("slave.host.name", "localhost"); |
---|
130 | |
---|
131 | final MiniDFSCluster cluster = new MiniDFSCluster(conf, 2, true, null); |
---|
132 | final FileSystem hdfs = cluster.getFileSystem(); |
---|
133 | final String hftpuri = "hftp://" + conf.get("dfs.http.address"); |
---|
134 | System.out.println("hftpuri=" + hftpuri); |
---|
135 | final FileSystem hftp = new Path(hftpuri).getFileSystem(conf); |
---|
136 | |
---|
137 | final String dir = "/filechecksum"; |
---|
138 | final int block_size = 1024; |
---|
139 | final int buffer_size = conf.getInt("io.file.buffer.size", 4096); |
---|
140 | conf.setInt("io.bytes.per.checksum", 512); |
---|
141 | |
---|
142 | //try different number of blocks |
---|
143 | for(int n = 0; n < 5; n++) { |
---|
144 | //generate random data |
---|
145 | final byte[] data = new byte[RAN.nextInt(block_size/2-1)+n*block_size+1]; |
---|
146 | RAN.nextBytes(data); |
---|
147 | System.out.println("data.length=" + data.length); |
---|
148 | |
---|
149 | //write data to a file |
---|
150 | final Path foo = new Path(dir, "foo" + n); |
---|
151 | { |
---|
152 | final FSDataOutputStream out = hdfs.create(foo, false, buffer_size, |
---|
153 | (short)2, block_size); |
---|
154 | out.write(data); |
---|
155 | out.close(); |
---|
156 | } |
---|
157 | |
---|
158 | //compute checksum |
---|
159 | final FileChecksum hdfsfoocs = hdfs.getFileChecksum(foo); |
---|
160 | System.out.println("hdfsfoocs=" + hdfsfoocs); |
---|
161 | |
---|
162 | final FileChecksum hftpfoocs = hftp.getFileChecksum(foo); |
---|
163 | System.out.println("hftpfoocs=" + hftpfoocs); |
---|
164 | |
---|
165 | final Path qualified = new Path(hftpuri + dir, "foo" + n); |
---|
166 | final FileChecksum qfoocs = hftp.getFileChecksum(qualified); |
---|
167 | System.out.println("qfoocs=" + qfoocs); |
---|
168 | |
---|
169 | //write another file |
---|
170 | final Path bar = new Path(dir, "bar" + n); |
---|
171 | { |
---|
172 | final FSDataOutputStream out = hdfs.create(bar, false, buffer_size, |
---|
173 | (short)2, block_size); |
---|
174 | out.write(data); |
---|
175 | out.close(); |
---|
176 | } |
---|
177 | |
---|
178 | { //verify checksum |
---|
179 | final FileChecksum barcs = hdfs.getFileChecksum(bar); |
---|
180 | final int barhashcode = barcs.hashCode(); |
---|
181 | assertEquals(hdfsfoocs.hashCode(), barhashcode); |
---|
182 | assertEquals(hdfsfoocs, barcs); |
---|
183 | |
---|
184 | assertEquals(hftpfoocs.hashCode(), barhashcode); |
---|
185 | assertEquals(hftpfoocs, barcs); |
---|
186 | |
---|
187 | assertEquals(qfoocs.hashCode(), barhashcode); |
---|
188 | assertEquals(qfoocs, barcs); |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|