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.server.datanode;
20 
21 import java.io.*;
22 import java.util.*;
23 
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.StorageType;
26 import org.junit.Test;
27 
28 import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_DATANODE_DATA_DIR_KEY;
29 import static org.hamcrest.CoreMatchers.is;
30 import static org.junit.Assert.*;
31 import static org.mockito.Mockito.*;
32 
33 import org.apache.hadoop.fs.LocalFileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hdfs.server.datanode.DataNode.DataNodeDiskChecker;
36 
37 public class TestDataDirs {
38 
39   @Test (timeout = 30000)
testDataDirParsing()40   public void testDataDirParsing() throws Throwable {
41     Configuration conf = new Configuration();
42     List<StorageLocation> locations;
43     File dir0 = new File("/dir0");
44     File dir1 = new File("/dir1");
45     File dir2 = new File("/dir2");
46     File dir3 = new File("/dir3");
47     File dir4 = new File("/dir4");
48 
49     // Verify that a valid string is correctly parsed, and that storage
50     // type is not case-sensitive
51     String locations1 = "[disk]/dir0,[DISK]/dir1,[sSd]/dir2,[disK]/dir3,[ram_disk]/dir4";
52     conf.set(DFS_DATANODE_DATA_DIR_KEY, locations1);
53     locations = DataNode.getStorageLocations(conf);
54     assertThat(locations.size(), is(5));
55     assertThat(locations.get(0).getStorageType(), is(StorageType.DISK));
56     assertThat(locations.get(0).getUri(), is(dir0.toURI()));
57     assertThat(locations.get(1).getStorageType(), is(StorageType.DISK));
58     assertThat(locations.get(1).getUri(), is(dir1.toURI()));
59     assertThat(locations.get(2).getStorageType(), is(StorageType.SSD));
60     assertThat(locations.get(2).getUri(), is(dir2.toURI()));
61     assertThat(locations.get(3).getStorageType(), is(StorageType.DISK));
62     assertThat(locations.get(3).getUri(), is(dir3.toURI()));
63     assertThat(locations.get(4).getStorageType(), is(StorageType.RAM_DISK));
64     assertThat(locations.get(4).getUri(), is(dir4.toURI()));
65 
66     // Verify that an unrecognized storage type result in an exception.
67     String locations2 = "[BadMediaType]/dir0,[ssd]/dir1,[disk]/dir2";
68     conf.set(DFS_DATANODE_DATA_DIR_KEY, locations2);
69     try {
70       locations = DataNode.getStorageLocations(conf);
71       fail();
72     } catch(IllegalArgumentException iae) {
73       DataNode.LOG.info("The exception is expected.", iae);
74     }
75 
76     // Assert that a string with no storage type specified is
77     // correctly parsed and the default storage type is picked up.
78     String locations3 = "/dir0,/dir1";
79     conf.set(DFS_DATANODE_DATA_DIR_KEY, locations3);
80     locations = DataNode.getStorageLocations(conf);
81     assertThat(locations.size(), is(2));
82     assertThat(locations.get(0).getStorageType(), is(StorageType.DISK));
83     assertThat(locations.get(0).getUri(), is(dir0.toURI()));
84     assertThat(locations.get(1).getStorageType(), is(StorageType.DISK));
85     assertThat(locations.get(1).getUri(), is(dir1.toURI()));
86   }
87 
88   @Test (timeout = 30000)
testDataDirValidation()89   public void testDataDirValidation() throws Throwable {
90 
91     DataNodeDiskChecker diskChecker = mock(DataNodeDiskChecker.class);
92     doThrow(new IOException()).doThrow(new IOException()).doNothing()
93       .when(diskChecker).checkDir(any(LocalFileSystem.class), any(Path.class));
94     LocalFileSystem fs = mock(LocalFileSystem.class);
95     AbstractList<StorageLocation> locations = new ArrayList<StorageLocation>();
96 
97     locations.add(StorageLocation.parse("file:/p1/"));
98     locations.add(StorageLocation.parse("file:/p2/"));
99     locations.add(StorageLocation.parse("file:/p3/"));
100 
101     List<StorageLocation> checkedLocations =
102         DataNode.checkStorageLocations(locations, fs, diskChecker);
103     assertEquals("number of valid data dirs", 1, checkedLocations.size());
104     String validDir = checkedLocations.iterator().next().getFile().getPath();
105     assertThat("p3 should be valid", new File("/p3/").getPath(), is(validDir));
106   }
107 }
108