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