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.assertNotNull; 021import static org.junit.Assert.assertTrue; 022import static org.junit.Assert.fail; 023import java.util.Arrays; 024import java.util.stream.Stream; 025import java.util.stream.StreamSupport; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.StartMiniClusterOption; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.Connection; 033import org.apache.hadoop.hbase.client.Put; 034import org.apache.hadoop.hbase.client.Result; 035import org.apache.hadoop.hbase.client.ResultScanner; 036import org.apache.hadoop.hbase.client.Scan; 037import org.apache.hadoop.hbase.client.Table; 038import org.apache.hadoop.hbase.testclassification.LargeTests; 039import org.apache.hadoop.hbase.testclassification.MasterTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.After; 042import org.junit.Before; 043import org.junit.ClassRule; 044import org.junit.Rule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.junit.rules.TestName; 048import org.slf4j.Logger; 049import org.slf4j.LoggerFactory; 050 051@Category({MasterTests.class, LargeTests.class}) 052public class TestMasterRepairMode { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestMasterRepairMode.class); 057 058 @Rule 059 public TestName name = new TestName(); 060 061 private static final Logger LOG = LoggerFactory.getLogger(TestMasterRepairMode.class); 062 063 private static final byte[] FAMILYNAME = Bytes.toBytes("fam"); 064 065 private static HBaseTestingUtility TEST_UTIL; 066 067 @Before 068 public void setUp() throws Exception { 069 TEST_UTIL = new HBaseTestingUtility(); 070 } 071 072 @After 073 public void tearDown() throws Exception { 074 TEST_UTIL.shutdownMiniCluster(); 075 } 076 077 private void enableMaintenanceMode() { 078 Configuration c = TEST_UTIL.getConfiguration(); 079 c.setBoolean(HMaster.MAINTENANCE_MODE, true); 080 c.setInt("hbase.master.init.timeout.localHBaseCluster", 30000); 081 } 082 083 @Test 084 public void testNewCluster() throws Exception { 085 enableMaintenanceMode(); 086 087 TEST_UTIL.startMiniCluster(StartMiniClusterOption.builder() 088 .numRegionServers(0) 089 .numDataNodes(3) 090 .build()); 091 092 Connection conn = TEST_UTIL.getConnection(); 093 assertTrue(conn.getAdmin().isMasterInMaintenanceMode()); 094 095 try (Table table = conn.getTable(TableName.META_TABLE_NAME); 096 ResultScanner scanner = table.getScanner(new Scan())) { 097 assertNotNull("Could not read meta.", scanner.next()); 098 } 099 } 100 101 @Test 102 public void testExistingCluster() throws Exception { 103 TableName testRepairMode = TableName.valueOf(name.getMethodName()); 104 105 TEST_UTIL.startMiniCluster(); 106 Table t = TEST_UTIL.createTable(testRepairMode, FAMILYNAME); 107 Put p = new Put(Bytes.toBytes("r")); 108 p.addColumn(FAMILYNAME, Bytes.toBytes("c"), new byte[0]); 109 t.put(p); 110 111 TEST_UTIL.shutdownMiniHBaseCluster(); 112 113 LOG.info("Starting master-only"); 114 115 enableMaintenanceMode(); 116 TEST_UTIL.startMiniHBaseCluster(StartMiniClusterOption.builder() 117 .numRegionServers(0).createRootDir(false).build()); 118 119 Connection conn = TEST_UTIL.getConnection(); 120 assertTrue(conn.getAdmin().isMasterInMaintenanceMode()); 121 122 try (Table table = conn.getTable(TableName.META_TABLE_NAME); 123 ResultScanner scanner = table.getScanner(HConstants.TABLE_FAMILY); 124 Stream<Result> results = StreamSupport.stream(scanner.spliterator(), false)) { 125 assertTrue("Did not find user table records while reading hbase:meta", 126 results.anyMatch(r -> Arrays.equals(r.getRow(), testRepairMode.getName()))); 127 } 128 129 try (Table table = conn.getTable(testRepairMode); 130 ResultScanner scanner = table.getScanner(new Scan())) { 131 scanner.next(); 132 fail("Should not be able to access user-space tables in repair mode."); 133 } catch (Exception e) { 134 // Expected 135 } 136 } 137}