|
| 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.hdfs.server.namenode; |
| 19 | + |
| 20 | +import static org.junit.Assert.fail; |
| 21 | +import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | + |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.hadoop.fs.FileContext; |
| 30 | +import org.apache.hadoop.fs.FileStatus; |
| 31 | +import org.apache.hadoop.fs.FileUtil; |
| 32 | +import org.apache.hadoop.fs.Path; |
| 33 | +import org.apache.hadoop.hdfs.DFSTestUtil; |
| 34 | +import org.apache.hadoop.hdfs.HdfsConfiguration; |
| 35 | +import org.apache.hadoop.hdfs.MiniDFSCluster; |
| 36 | +import org.apache.hadoop.hdfs.server.common.Util; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests the CreateEditsLog utility. |
| 40 | + */ |
| 41 | +public class TestCreateEditsLog { |
| 42 | + |
| 43 | + private static final File HDFS_DIR = new File( |
| 44 | + MiniDFSCluster.getBaseDirectory()).getAbsoluteFile(); |
| 45 | + private static final File TEST_DIR = new File( |
| 46 | + System.getProperty("test.build.data", "build/test/data"), |
| 47 | + "TestCreateEditsLog").getAbsoluteFile(); |
| 48 | + |
| 49 | + private MiniDFSCluster cluster; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() throws Exception { |
| 53 | + deleteIfExists(HDFS_DIR); |
| 54 | + deleteIfExists(TEST_DIR); |
| 55 | + } |
| 56 | + |
| 57 | + @After |
| 58 | + public void tearDown() { |
| 59 | + if (cluster != null) { |
| 60 | + cluster.shutdown(); |
| 61 | + cluster = null; |
| 62 | + } |
| 63 | + deleteIfExists(HDFS_DIR); |
| 64 | + deleteIfExists(TEST_DIR); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tests that an edits log created using CreateEditsLog is valid and can be |
| 69 | + * loaded successfully by a namenode. |
| 70 | + */ |
| 71 | + @Test(timeout=60000) |
| 72 | + public void testCanLoadCreatedEditsLog() throws Exception { |
| 73 | + // Format namenode. |
| 74 | + HdfsConfiguration conf = new HdfsConfiguration(); |
| 75 | + File nameDir = new File(HDFS_DIR, "name"); |
| 76 | + conf.set(DFS_NAMENODE_NAME_DIR_KEY, Util.fileAsURI(nameDir).toString()); |
| 77 | + DFSTestUtil.formatNameNode(conf); |
| 78 | + |
| 79 | + // Call CreateEditsLog and move the resulting edits to the name dir. |
| 80 | + CreateEditsLog.main(new String[] { "-f", "1000", "0", "1", "-d", |
| 81 | + TEST_DIR.getAbsolutePath() }); |
| 82 | + Path editsWildcard = new Path(TEST_DIR.getAbsolutePath(), "*"); |
| 83 | + FileContext localFc = FileContext.getLocalFSFileContext(); |
| 84 | + for (FileStatus edits: localFc.util().globStatus(editsWildcard)) { |
| 85 | + Path src = edits.getPath(); |
| 86 | + Path dst = new Path(new File(nameDir, "current").getAbsolutePath(), |
| 87 | + src.getName()); |
| 88 | + localFc.rename(src, dst); |
| 89 | + } |
| 90 | + |
| 91 | + // Start a namenode to try to load the edits. |
| 92 | + cluster = new MiniDFSCluster.Builder(conf) |
| 93 | + .format(false) |
| 94 | + .manageNameDfsDirs(false) |
| 95 | + .waitSafeMode(false) |
| 96 | + .build(); |
| 97 | + cluster.waitClusterUp(); |
| 98 | + |
| 99 | + // Test successful, because no exception thrown. |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Fully delete the given directory if it exists. |
| 104 | + * |
| 105 | + * @param file File to delete |
| 106 | + */ |
| 107 | + private static void deleteIfExists(File file) { |
| 108 | + if (file.exists() && !FileUtil.fullyDelete(file)) { |
| 109 | + fail("Could not delete '" + file + "'"); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments