[120] | 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 | |
---|
| 23 | import org.apache.commons.logging.Log; |
---|
| 24 | import org.apache.commons.logging.LogFactory; |
---|
| 25 | import org.apache.hadoop.conf.Configuration; |
---|
| 26 | import org.apache.hadoop.fs.FileSystem; |
---|
| 27 | import org.apache.hadoop.fs.Path; |
---|
| 28 | import org.apache.hadoop.hdfs.protocol.FSConstants.SafeModeAction; |
---|
| 29 | import org.apache.hadoop.hdfs.server.namenode.NameNode; |
---|
| 30 | |
---|
| 31 | import junit.framework.TestCase; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * This test makes sure that if SafeMode is manually entered, NameNode does not |
---|
| 35 | * come out of safe mode even after the startup safemode conditions are met. |
---|
| 36 | */ |
---|
| 37 | public class TestSafeMode extends TestCase { |
---|
| 38 | |
---|
| 39 | static Log LOG = LogFactory.getLog(TestSafeMode.class); |
---|
| 40 | |
---|
| 41 | public void testManualSafeMode() throws IOException { |
---|
| 42 | MiniDFSCluster cluster = null; |
---|
| 43 | FileSystem fs = null; |
---|
| 44 | try { |
---|
| 45 | Configuration conf = new Configuration(); |
---|
| 46 | // disable safemode extension to make the test run faster. |
---|
| 47 | conf.set("dfs.safemode.extension", "1"); |
---|
| 48 | cluster = new MiniDFSCluster(conf, 1, true, null); |
---|
| 49 | cluster.waitActive(); |
---|
| 50 | |
---|
| 51 | fs = cluster.getFileSystem(); |
---|
| 52 | Path file1 = new Path("/tmp/testManualSafeMode/file1"); |
---|
| 53 | Path file2 = new Path("/tmp/testManualSafeMode/file2"); |
---|
| 54 | |
---|
| 55 | LOG.info("Created file1 and file2."); |
---|
| 56 | |
---|
| 57 | // create two files with one block each. |
---|
| 58 | DFSTestUtil.createFile(fs, file1, 1000, (short)1, 0); |
---|
| 59 | DFSTestUtil.createFile(fs, file2, 2000, (short)1, 0); |
---|
| 60 | cluster.shutdown(); |
---|
| 61 | |
---|
| 62 | // now bring up just the NameNode. |
---|
| 63 | cluster = new MiniDFSCluster(conf, 0, false, null); |
---|
| 64 | cluster.waitActive(); |
---|
| 65 | |
---|
| 66 | LOG.info("Restarted cluster with just the NameNode"); |
---|
| 67 | |
---|
| 68 | NameNode namenode = cluster.getNameNode(); |
---|
| 69 | |
---|
| 70 | assertTrue("No datanode is started. Should be in SafeMode", |
---|
| 71 | namenode.isInSafeMode()); |
---|
| 72 | |
---|
| 73 | // manually set safemode. |
---|
| 74 | namenode.setSafeMode(SafeModeAction.SAFEMODE_ENTER); |
---|
| 75 | |
---|
| 76 | // now bring up the datanode and wait for it to be active. |
---|
| 77 | cluster.startDataNodes(conf, 1, true, null, null); |
---|
| 78 | cluster.waitActive(); |
---|
| 79 | |
---|
| 80 | LOG.info("Datanode is started."); |
---|
| 81 | |
---|
| 82 | try { |
---|
| 83 | Thread.sleep(2000); |
---|
| 84 | } catch (InterruptedException ignored) {} |
---|
| 85 | |
---|
| 86 | assertTrue("should still be in SafeMode", namenode.isInSafeMode()); |
---|
| 87 | |
---|
| 88 | namenode.setSafeMode(SafeModeAction.SAFEMODE_LEAVE); |
---|
| 89 | assertFalse("should not be in SafeMode", namenode.isInSafeMode()); |
---|
| 90 | } finally { |
---|
| 91 | if(fs != null) fs.close(); |
---|
| 92 | if(cluster!= null) cluster.shutdown(); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | } |
---|