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