001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.master;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.util.List;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.regionserver.HRegion;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.CommonFSUtils;
036import org.apache.hadoop.hbase.util.HFileArchiveTestingUtil;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047/**
048 * Test the master filesystem in a local cluster
049 */
050@Category({MasterTests.class, MediumTests.class})
051public class TestMasterFileSystem {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055      HBaseClassTestRule.forClass(TestMasterFileSystem.class);
056
057  @Rule
058  public TestName name = new TestName();
059
060  private static final Logger LOG = LoggerFactory.getLogger(TestMasterFileSystem.class);
061
062  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
063
064  @BeforeClass
065  public static void setupTest() throws Exception {
066    UTIL.startMiniCluster();
067  }
068
069  @AfterClass
070  public static void teardownTest() throws Exception {
071    UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testFsUriSetProperly() throws Exception {
076    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
077    MasterFileSystem fs = master.getMasterFileSystem();
078    Path masterRoot = CommonFSUtils.getRootDir(fs.getConfiguration());
079    Path rootDir = CommonFSUtils.getRootDir(fs.getFileSystem().getConf());
080    // make sure the fs and the found root dir have the same scheme
081    LOG.debug("from fs uri:" + FileSystem.getDefaultUri(fs.getFileSystem().getConf()));
082    LOG.debug("from configuration uri:" + FileSystem.getDefaultUri(fs.getConfiguration()));
083    // make sure the set uri matches by forcing it.
084    assertEquals(masterRoot, rootDir);
085  }
086
087  @Test
088  public void testCheckTempDir() throws Exception {
089    final MasterFileSystem masterFileSystem =
090      UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
091
092    final TableName tableName = TableName.valueOf(name.getMethodName());
093    final byte[] FAM = Bytes.toBytes("fam");
094    final byte[][] splitKeys = new byte[][] {
095      Bytes.toBytes("b"), Bytes.toBytes("c"), Bytes.toBytes("d")
096    };
097
098    UTIL.createTable(tableName, FAM, splitKeys);
099
100    // get the current store files for the regions
101    List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
102    // make sure we have 4 regions serving this table
103    assertEquals(4, regions.size());
104
105    // load the table
106    try (Table table = UTIL.getConnection().getTable(tableName)) {
107      UTIL.loadTable(table, FAM);
108    }
109
110    // disable the table so that we can manipulate the files
111    UTIL.getAdmin().disableTable(tableName);
112
113    final Path tableDir = CommonFSUtils.getTableDir(masterFileSystem.getRootDir(), tableName);
114    final Path tempDir = masterFileSystem.getTempDir();
115    final Path tempTableDir = CommonFSUtils.getTableDir(tempDir, tableName);
116    final FileSystem fs = masterFileSystem.getFileSystem();
117
118    // move the table to the temporary directory
119    if (!fs.rename(tableDir, tempTableDir)) {
120      fail();
121    }
122
123    masterFileSystem.checkTempDir(tempDir, UTIL.getConfiguration(), fs);
124
125    // check if the temporary directory exists and is empty
126    assertTrue(fs.exists(tempDir));
127    assertEquals(0, fs.listStatus(tempDir).length);
128
129    // check for the existence of the archive directory
130    for (HRegion region : regions) {
131      Path archiveDir = HFileArchiveTestingUtil.getRegionArchiveDir(UTIL.getConfiguration(),
132        region);
133      assertTrue(fs.exists(archiveDir));
134    }
135
136    UTIL.deleteTable(tableName);
137  }
138}