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.fs; |
---|
19 | |
---|
20 | import java.io.File; |
---|
21 | import java.io.IOException; |
---|
22 | |
---|
23 | import org.apache.hadoop.conf.Configuration; |
---|
24 | import org.apache.hadoop.util.Shell; |
---|
25 | |
---|
26 | import junit.framework.TestCase; |
---|
27 | |
---|
28 | /** This test LocalDirAllocator works correctly; |
---|
29 | * Every test case uses different buffer dirs to |
---|
30 | * enforce the AllocatorPerContext initialization. |
---|
31 | * This test does not run on Cygwin because under Cygwin |
---|
32 | * a directory can be created in a read-only directory |
---|
33 | * which breaks this test. |
---|
34 | */ |
---|
35 | public class TestLocalDirAllocator extends TestCase { |
---|
36 | final static private Configuration conf = new Configuration(); |
---|
37 | final static private String BUFFER_DIR_ROOT = "build/test/temp"; |
---|
38 | final static private Path BUFFER_PATH_ROOT = new Path(BUFFER_DIR_ROOT); |
---|
39 | final static private File BUFFER_ROOT = new File(BUFFER_DIR_ROOT); |
---|
40 | final static private String BUFFER_DIR[] = new String[] { |
---|
41 | BUFFER_DIR_ROOT+"/tmp0", BUFFER_DIR_ROOT+"/tmp1", BUFFER_DIR_ROOT+"/tmp2", |
---|
42 | BUFFER_DIR_ROOT+"/tmp3", BUFFER_DIR_ROOT+"/tmp4", BUFFER_DIR_ROOT+"/tmp5", |
---|
43 | BUFFER_DIR_ROOT+"/tmp6"}; |
---|
44 | final static private Path BUFFER_PATH[] = new Path[] { |
---|
45 | new Path(BUFFER_DIR[0]), new Path(BUFFER_DIR[1]), new Path(BUFFER_DIR[2]), |
---|
46 | new Path(BUFFER_DIR[3]), new Path(BUFFER_DIR[4]), new Path(BUFFER_DIR[5]), |
---|
47 | new Path(BUFFER_DIR[6])}; |
---|
48 | final static private String CONTEXT = "dfs.client.buffer.dir"; |
---|
49 | final static private String FILENAME = "block"; |
---|
50 | final static private LocalDirAllocator dirAllocator = |
---|
51 | new LocalDirAllocator(CONTEXT); |
---|
52 | static LocalFileSystem localFs; |
---|
53 | final static private boolean isWindows = |
---|
54 | System.getProperty("os.name").startsWith("Windows"); |
---|
55 | final static int SMALL_FILE_SIZE = 100; |
---|
56 | static { |
---|
57 | try { |
---|
58 | localFs = FileSystem.getLocal(conf); |
---|
59 | rmBufferDirs(); |
---|
60 | } catch(IOException e) { |
---|
61 | System.out.println(e.getMessage()); |
---|
62 | e.printStackTrace(); |
---|
63 | System.exit(-1); |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | private static void rmBufferDirs() throws IOException { |
---|
68 | assertTrue(!localFs.exists(BUFFER_PATH_ROOT) || |
---|
69 | localFs.delete(BUFFER_PATH_ROOT)); |
---|
70 | } |
---|
71 | |
---|
72 | private void validateTempDirCreation(int i) throws IOException { |
---|
73 | File result = createTempFile(SMALL_FILE_SIZE); |
---|
74 | assertTrue("Checking for " + BUFFER_DIR[i] + " in " + result + " - FAILED!", |
---|
75 | result.getPath().startsWith(new File(BUFFER_DIR[i], FILENAME).getPath())); |
---|
76 | } |
---|
77 | |
---|
78 | private File createTempFile() throws IOException { |
---|
79 | File result = dirAllocator.createTmpFileForWrite(FILENAME, -1, conf); |
---|
80 | result.delete(); |
---|
81 | return result; |
---|
82 | } |
---|
83 | |
---|
84 | private File createTempFile(long size) throws IOException { |
---|
85 | File result = dirAllocator.createTmpFileForWrite(FILENAME, size, conf); |
---|
86 | result.delete(); |
---|
87 | return result; |
---|
88 | } |
---|
89 | |
---|
90 | /** Two buffer dirs. The first dir does not exist & is on a read-only disk; |
---|
91 | * The second dir exists & is RW |
---|
92 | * @throws Exception |
---|
93 | */ |
---|
94 | public void test0() throws Exception { |
---|
95 | if (isWindows) return; |
---|
96 | try { |
---|
97 | conf.set(CONTEXT, BUFFER_DIR[0]+","+BUFFER_DIR[1]); |
---|
98 | assertTrue(localFs.mkdirs(BUFFER_PATH[1])); |
---|
99 | BUFFER_ROOT.setReadOnly(); |
---|
100 | validateTempDirCreation(1); |
---|
101 | validateTempDirCreation(1); |
---|
102 | } finally { |
---|
103 | Shell.execCommand(new String[]{"chmod", "u+w", BUFFER_DIR_ROOT}); |
---|
104 | rmBufferDirs(); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** Two buffer dirs. The first dir exists & is on a read-only disk; |
---|
109 | * The second dir exists & is RW |
---|
110 | * @throws Exception |
---|
111 | */ |
---|
112 | public void test1() throws Exception { |
---|
113 | if (isWindows) return; |
---|
114 | try { |
---|
115 | conf.set(CONTEXT, BUFFER_DIR[1]+","+BUFFER_DIR[2]); |
---|
116 | assertTrue(localFs.mkdirs(BUFFER_PATH[2])); |
---|
117 | BUFFER_ROOT.setReadOnly(); |
---|
118 | validateTempDirCreation(2); |
---|
119 | validateTempDirCreation(2); |
---|
120 | } finally { |
---|
121 | Shell.execCommand(new String[]{"chmod", "u+w", BUFFER_DIR_ROOT}); |
---|
122 | rmBufferDirs(); |
---|
123 | } |
---|
124 | } |
---|
125 | /** Two buffer dirs. Both do not exist but on a RW disk. |
---|
126 | * Check if tmp dirs are allocated in a round-robin |
---|
127 | */ |
---|
128 | public void test2() throws Exception { |
---|
129 | if (isWindows) return; |
---|
130 | try { |
---|
131 | conf.set(CONTEXT, BUFFER_DIR[2]+","+BUFFER_DIR[3]); |
---|
132 | |
---|
133 | // create the first file, and then figure the round-robin sequence |
---|
134 | createTempFile(SMALL_FILE_SIZE); |
---|
135 | int firstDirIdx = (dirAllocator.getCurrentDirectoryIndex() == 0) ? 2 : 3; |
---|
136 | int secondDirIdx = (firstDirIdx == 2) ? 3 : 2; |
---|
137 | |
---|
138 | // check if tmp dirs are allocated in a round-robin manner |
---|
139 | validateTempDirCreation(firstDirIdx); |
---|
140 | validateTempDirCreation(secondDirIdx); |
---|
141 | validateTempDirCreation(firstDirIdx); |
---|
142 | } finally { |
---|
143 | rmBufferDirs(); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /** Two buffer dirs. Both exists and on a R/W disk. |
---|
148 | * Later disk1 becomes read-only. |
---|
149 | * @throws Exception |
---|
150 | */ |
---|
151 | public void test3() throws Exception { |
---|
152 | if (isWindows) return; |
---|
153 | try { |
---|
154 | conf.set(CONTEXT, BUFFER_DIR[3]+","+BUFFER_DIR[4]); |
---|
155 | assertTrue(localFs.mkdirs(BUFFER_PATH[3])); |
---|
156 | assertTrue(localFs.mkdirs(BUFFER_PATH[4])); |
---|
157 | |
---|
158 | // create the first file with size, and then figure the round-robin sequence |
---|
159 | createTempFile(SMALL_FILE_SIZE); |
---|
160 | |
---|
161 | int nextDirIdx = (dirAllocator.getCurrentDirectoryIndex() == 0) ? 3 : 4; |
---|
162 | validateTempDirCreation(nextDirIdx); |
---|
163 | |
---|
164 | // change buffer directory 2 to be read only |
---|
165 | new File(BUFFER_DIR[4]).setReadOnly(); |
---|
166 | validateTempDirCreation(3); |
---|
167 | validateTempDirCreation(3); |
---|
168 | } finally { |
---|
169 | rmBufferDirs(); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Two buffer dirs, on read-write disk. |
---|
175 | * |
---|
176 | * Try to create a whole bunch of files. |
---|
177 | * Verify that they do indeed all get created where they should. |
---|
178 | * |
---|
179 | * Would ideally check statistical properties of distribution, but |
---|
180 | * we don't have the nerve to risk false-positives here. |
---|
181 | * |
---|
182 | * @throws Exception |
---|
183 | */ |
---|
184 | static final int TRIALS = 100; |
---|
185 | public void test4() throws Exception { |
---|
186 | if (isWindows) return; |
---|
187 | try { |
---|
188 | |
---|
189 | conf.set(CONTEXT, BUFFER_DIR[5]+","+BUFFER_DIR[6]); |
---|
190 | assertTrue(localFs.mkdirs(BUFFER_PATH[5])); |
---|
191 | assertTrue(localFs.mkdirs(BUFFER_PATH[6])); |
---|
192 | |
---|
193 | int inDir5=0, inDir6=0; |
---|
194 | for(int i = 0; i < TRIALS; ++i) { |
---|
195 | File result = createTempFile(); |
---|
196 | if(result.getPath().startsWith(new File(BUFFER_DIR[5], FILENAME).getPath())) { |
---|
197 | inDir5++; |
---|
198 | } else if(result.getPath().startsWith(new File(BUFFER_DIR[6], FILENAME).getPath())) { |
---|
199 | inDir6++; |
---|
200 | } |
---|
201 | result.delete(); |
---|
202 | } |
---|
203 | |
---|
204 | assertTrue( inDir5 + inDir6 == TRIALS); |
---|
205 | |
---|
206 | } finally { |
---|
207 | rmBufferDirs(); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|