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;
025
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.regionserver.HRegion;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.FSUtils;
037import org.apache.hadoop.hbase.util.HFileArchiveTestingUtil;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.junit.rules.TestName;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048/**
049 * Test the master filesystem in a local cluster
050 */
051@Category({MasterTests.class, MediumTests.class})
052public class TestMasterFileSystem {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestMasterFileSystem.class);
057
058  @Rule
059  public TestName name = new TestName();
060
061  private static final Logger LOG = LoggerFactory.getLogger(TestMasterFileSystem.class);
062
063  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
064
065  @BeforeClass
066  public static void setupTest() throws Exception {
067    UTIL.startMiniCluster();
068  }
069
070  @AfterClass
071  public static void teardownTest() throws Exception {
072    UTIL.shutdownMiniCluster();
073  }
074
075  @Test
076  public void testFsUriSetProperly() throws Exception {
077    HMaster master = UTIL.getMiniHBaseCluster().getMaster();
078    MasterFileSystem fs = master.getMasterFileSystem();
079    Path masterRoot = FSUtils.getRootDir(fs.getConfiguration());
080    Path rootDir = FSUtils.getRootDir(fs.getFileSystem().getConf());
081    // make sure the fs and the found root dir have the same scheme
082    LOG.debug("from fs uri:" + FileSystem.getDefaultUri(fs.getFileSystem().getConf()));
083    LOG.debug("from configuration uri:" + FileSystem.getDefaultUri(fs.getConfiguration()));
084    // make sure the set uri matches by forcing it.
085    assertEquals(masterRoot, rootDir);
086  }
087
088  @Test
089  public void testCheckTempDir() throws Exception {
090    final MasterFileSystem masterFileSystem =
091      UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem();
092
093    final TableName tableName = TableName.valueOf(name.getMethodName());
094    final byte[] FAM = Bytes.toBytes("fam");
095    final byte[][] splitKeys = new byte[][] {
096      Bytes.toBytes("b"), Bytes.toBytes("c"), Bytes.toBytes("d")
097    };
098
099    UTIL.createTable(tableName, FAM, splitKeys);
100
101    // get the current store files for the regions
102    List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
103    // make sure we have 4 regions serving this table
104    assertEquals(4, regions.size());
105
106    // load the table
107    try (Table table = UTIL.getConnection().getTable(tableName)) {
108      UTIL.loadTable(table, FAM);
109    }
110
111    // disable the table so that we can manipulate the files
112    UTIL.getAdmin().disableTable(tableName);
113
114    final Path tableDir = FSUtils.getTableDir(masterFileSystem.getRootDir(), tableName);
115    final Path tempDir = masterFileSystem.getTempDir();
116    final Path tempTableDir = FSUtils.getTableDir(tempDir, tableName);
117    final FileSystem fs = masterFileSystem.getFileSystem();
118
119    // move the table to the temporary directory
120    if (!fs.rename(tableDir, tempTableDir)) {
121      fail();
122    }
123
124    masterFileSystem.checkTempDir(tempDir, UTIL.getConfiguration(), fs);
125
126    // check if the temporary directory exists and is empty
127    assertTrue(fs.exists(tempDir));
128    assertEquals(0, fs.listStatus(tempDir).length);
129
130    // check for the existence of the archive directory
131    for (HRegion region : regions) {
132      Path archiveDir = HFileArchiveTestingUtil.getRegionArchiveDir(UTIL.getConfiguration(),
133        region);
134      assertTrue(fs.exists(archiveDir));
135    }
136
137    UTIL.deleteTable(tableName);
138  }
139}