forked from apache/hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestFuseDFS.java
More file actions
415 lines (366 loc) · 13 KB
/
Copy pathTestFuseDFS.java
File metadata and controls
415 lines (366 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.concurrent.atomic.*;
import org.slf4j.event.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.*;
import org.apache.hadoop.hdfs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.util.StringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Basic functional tests on a fuse-dfs mount.
*/
public class TestFuseDFS {
private static MiniDFSCluster cluster;
private static FileSystem fs;
private static Process fuseProcess;
private static Runtime r;
private static String mountPoint;
private static final Logger LOG = LoggerFactory.getLogger(TestFuseDFS.class);
{
GenericTestUtils.setLogLevel(LOG, Level.TRACE);
}
/** Dump the given intput stream to stderr */
private static void dumpInputStream(InputStream is) throws IOException {
int len;
do {
byte b[] = new byte[is.available()];
len = is.read(b);
System.out.println("Read "+len+" bytes");
System.out.write(b, 0, b.length);
} while (len > 0);
}
/**
* Wait for the given process to return and check that it exited
* as required. Log if the process failed.
*/
private static void checkProcessRet(Process p, boolean expectPass)
throws IOException {
try {
int ret = p.waitFor();
if (ret != 0) {
dumpInputStream(p.getErrorStream());
}
if (expectPass) {
assertEquals(0, ret);
} else {
assertTrue(ret != 0);
}
} catch (InterruptedException ie) {
fail("Process interrupted: "+ie.getMessage());
}
}
/** Exec the given command and assert it executed successfully */
private static void execWaitRet(String cmd) throws IOException {
LOG.debug("EXEC "+cmd);
Process p = r.exec(cmd);
try {
p.waitFor();
} catch (InterruptedException ie) {
fail("Process interrupted: "+ie.getMessage());
}
}
/** Exec the given command and assert it executed successfully */
private static void execIgnoreRet(String cmd) throws IOException {
LOG.debug("EXEC "+cmd);
r.exec(cmd);
}
/** Exec the given command and assert it executed successfully */
private static void execAssertSucceeds(String cmd) throws IOException {
LOG.debug("EXEC "+cmd);
checkProcessRet(r.exec(cmd), true);
}
/** Exec the given command, assert it returned an error code */
private static void execAssertFails(String cmd) throws IOException {
LOG.debug("EXEC "+cmd);
checkProcessRet(r.exec(cmd), false);
}
/** Create and write the given file */
private static void createFile(File f, String s) throws IOException {
InputStream is = new ByteArrayInputStream(s.getBytes());
FileOutputStream fos = new FileOutputStream(f);
IOUtils.copyBytes(is, fos, s.length(), true);
}
/** Check that the given file exists with the given contents */
private static void checkFile(File f, String expectedContents)
throws IOException {
FileInputStream fi = new FileInputStream(f);
int len = expectedContents.length();
byte[] b = new byte[len];
try {
IOUtils.readFully(fi, b, 0, len);
} catch (IOException ie) {
fail("Reading "+f.getName()+" failed with "+ie.getMessage());
} finally {
fi.close(); // NB: leaving f unclosed prevents unmount
}
String s = new String(b, 0, len);
assertEquals(expectedContents, s, "File content differs");
}
private static class RedirectToStdoutThread extends Thread {
private InputStream is;
RedirectToStdoutThread(InputStream is) {
this.is = is;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
LOG.error("FUSE_LINE:" + line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/** Run a fuse-dfs process to mount the given DFS */
private static Process establishMount(URI uri) throws IOException {
Runtime r = Runtime.getRuntime();
String cp = System.getProperty("java.class.path");
String buildTestDir = System.getProperty("build.test");
String fuseCmd = buildTestDir + "/../fuse_dfs";
String libHdfs = buildTestDir + "/../../../c++/lib";
String arch = System.getProperty("os.arch");
String jvm = System.getProperty("java.home") + "/lib/" + arch + "/server";
String lp = System.getProperty("LD_LIBRARY_PATH")+":"+libHdfs+":"+jvm;
LOG.debug("LD_LIBRARY_PATH=" + lp);
String nameNode =
"dfs://" + uri.getHost() + ":" + String.valueOf(uri.getPort());
// NB: We're mounting via an unprivileged user, therefore
// user_allow_other needs to be set in /etc/fuse.conf, which also
// needs to be world readable.
String mountCmd[] = {
fuseCmd, nameNode, mountPoint,
// "-odebug", // Don't daemonize
"-obig_writes", // Allow >4kb writes
"-oentry_timeout=0.1", // Don't cache dents long
"-oattribute_timeout=0.1", // Don't cache attributes long
"-ononempty", // Don't complain about junk in mount point
"-f", // Don't background the process
"-ordbuffer=32768", // Read buffer size in kb
"-omax_background=100", // Set fuse max_background=100 (12 by default)
"rw"
};
String [] env = {
"CLASSPATH="+cp,
"LD_LIBRARY_PATH="+lp,
"PATH=/usr/bin:/bin"
};
execWaitRet("fusermount -u " + mountPoint);
execAssertSucceeds("rm -rf " + mountPoint);
execAssertSucceeds("mkdir -p " + mountPoint);
// Mount the mini cluster
String cmdStr = "";
for (String c : mountCmd) {
cmdStr += (" " + c);
}
LOG.info("now mounting with:" + cmdStr);
Process fuseProcess = r.exec(mountCmd, env);
RedirectToStdoutThread stdoutThread =
new RedirectToStdoutThread(fuseProcess.getInputStream());
RedirectToStdoutThread stderrThread =
new RedirectToStdoutThread(fuseProcess.getErrorStream());
stdoutThread.start();
stderrThread.start();
// Wait for fusermount to start up, so that we know we're operating on the
// FUSE FS when we run the tests.
try {
Thread.sleep(50000);
} catch (InterruptedException e) {
}
return fuseProcess;
}
/** Tear down the fuse-dfs process and mount */
private static void teardownMount() throws IOException {
execWaitRet("fusermount -u " + mountPoint);
try {
assertEquals(0,
fuseProcess.waitFor()); // fuse_dfs should exit cleanly
} catch (InterruptedException e) {
fail("interrupted while waiting for fuse_dfs process to exit.");
}
}
@BeforeAll
public static void startUp() throws IOException {
Configuration conf = new HdfsConfiguration();
r = Runtime.getRuntime();
mountPoint = System.getProperty("build.test") + "/mnt";
conf.setBoolean(DFSConfigKeys.DFS_PERMISSIONS_ENABLED_KEY, false);
cluster = new MiniDFSCluster.Builder(conf).build();
cluster.waitClusterUp();
fs = cluster.getFileSystem();
fuseProcess = establishMount(fs.getUri());
}
@AfterAll
public static void tearDown() throws IOException {
// Unmount before taking down the mini cluster
// so no outstanding operations hang.
teardownMount();
if (fs != null) {
fs.close();
}
if (cluster != null) {
cluster.shutdown();
}
}
/** Test basic directory creation, access, removal */
@Test
public void testBasicDir() throws IOException {
File d = new File(mountPoint, "dir1");
// Mkdir, access and rm via the mount
execAssertSucceeds("mkdir " + d.getAbsolutePath());
execAssertSucceeds("ls " + d.getAbsolutePath());
execAssertSucceeds("rmdir " + d.getAbsolutePath());
// The dir should no longer exist
execAssertFails("ls " + d.getAbsolutePath());
}
/** Test basic file creation and writing */
@Test
public void testCreate() throws IOException {
final String contents = "hello world";
File f = new File(mountPoint, "file1");
// Create and access via the mount
createFile(f, contents);
// XX avoids premature EOF
try {
Thread.sleep(1000);
} catch (InterruptedException ie) { }
checkFile(f, contents);
// Cat, stat and delete via the mount
execAssertSucceeds("cat " + f.getAbsolutePath());
execAssertSucceeds("stat " + f.getAbsolutePath());
execAssertSucceeds("rm " + f.getAbsolutePath());
// The file should no longer exist
execAssertFails("ls " + f.getAbsolutePath());
}
/** Test creating a file via touch */
@Test
public void testTouch() throws IOException {
File f = new File(mountPoint, "file1");
execAssertSucceeds("touch " + f.getAbsolutePath());
execAssertSucceeds("rm " + f.getAbsolutePath());
}
/** Test random access to a file */
@Test
public void testRandomAccess() throws IOException {
final String contents = "hello world";
File f = new File(mountPoint, "file1");
createFile(f, contents);
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.seek(f.length());
try {
raf.write('b');
} catch (IOException e) {
// Expected: fuse-dfs not yet support append
assertEquals("Operation not supported", e.getMessage());
} finally {
raf.close();
}
raf = new RandomAccessFile(f, "rw");
raf.seek(0);
try {
raf.write('b');
fail("Over-wrote existing bytes");
} catch (IOException e) {
// Expected: can-not overwrite a file
assertEquals("Invalid argument", e.getMessage());
} finally {
raf.close();
}
execAssertSucceeds("rm " + f.getAbsolutePath());
}
/** Test copying a set of files from the mount to itself */
@Test
public void testCopyFiles() throws IOException {
final String contents = "hello world";
File d1 = new File(mountPoint, "dir1");
File d2 = new File(mountPoint, "dir2");
// Create and populate dir1 via the mount
execAssertSucceeds("mkdir " + d1.getAbsolutePath());
for (int i = 0; i < 5; i++) {
createFile(new File(d1, "file"+i), contents);
}
assertEquals(5, d1.listFiles().length);
// Copy dir from the mount to the mount
execAssertSucceeds("cp -r " + d1.getAbsolutePath() +
" " + d2.getAbsolutePath());
assertEquals(5, d2.listFiles().length);
// Access all the files in the dirs and remove them
execAssertSucceeds("find " + d1.getAbsolutePath());
execAssertSucceeds("find " + d2.getAbsolutePath());
execAssertSucceeds("rm -r " + d1.getAbsolutePath());
execAssertSucceeds("rm -r " + d2.getAbsolutePath());
}
/** Test concurrent creation and access of the mount */
@Test
public void testMultipleThreads() throws IOException {
ArrayList<Thread> threads = new ArrayList<Thread>();
final AtomicReference<String> errorMessage = new AtomicReference<String>();
for (int i = 0; i < 10; i++) {
Thread t = new Thread() {
public void run() {
try {
File d = new File(mountPoint, "dir"+getId());
execWaitRet("mkdir " + d.getAbsolutePath());
for (int j = 0; j < 10; j++) {
File f = new File(d, "file"+j);
final String contents = "thread "+getId()+" "+j;
createFile(f, contents);
}
for (int j = 0; j < 10; j++) {
File f = new File(d, "file"+j);
execWaitRet("cat " + f.getAbsolutePath());
execWaitRet("rm " + f.getAbsolutePath());
}
execWaitRet("rmdir " + d.getAbsolutePath());
} catch (IOException ie) {
errorMessage.set(
String.format("Exception %s",
StringUtils.stringifyException(ie)));
}
}
};
t.start();
threads.add(t);
}
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException ie) {
fail("Thread interrupted: "+ie.getMessage());
}
}
assertNull(errorMessage.get(), errorMessage.get());
}
}