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