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