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.TableName;
033import org.apache.hadoop.hbase.client.Connection;
034import org.apache.hadoop.hbase.client.Put;
035import org.apache.hadoop.hbase.client.Result;
036import org.apache.hadoop.hbase.client.ResultScanner;
037import org.apache.hadoop.hbase.client.Scan;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.testclassification.MasterTests;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.util.Bytes;
042
043import org.junit.After;
044import org.junit.Before;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054@Category({MasterTests.class, MediumTests.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 HBaseTestingUtility TEST_UTIL;
069
070  @Before
071  public void setUp() throws Exception {
072    TEST_UTIL = new HBaseTestingUtility();
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.startMiniZKCluster();
091    TEST_UTIL.startMiniDFSCluster(3);
092    TEST_UTIL.startMiniHBaseCluster(1, 0);
093
094    Connection conn = TEST_UTIL.getConnection();
095    assertTrue(conn.getAdmin().isMasterInMaintenanceMode());
096
097    try (Table table = conn.getTable(TableName.META_TABLE_NAME);
098        ResultScanner scanner = table.getScanner(new Scan())) {
099      assertNotNull("Could not read meta.", scanner.next());
100    }
101  }
102
103  @Test
104  public void testExistingCluster() throws Exception {
105    TableName testRepairMode = TableName.valueOf(name.getMethodName());
106
107    TEST_UTIL.startMiniCluster();
108    Table t = TEST_UTIL.createTable(testRepairMode, FAMILYNAME);
109    Put p = new Put(Bytes.toBytes("r"));
110    p.addColumn(FAMILYNAME, Bytes.toBytes("c"), new byte[0]);
111    t.put(p);
112
113    TEST_UTIL.shutdownMiniHBaseCluster();
114
115    LOG.info("Starting master-only");
116
117    enableMaintenanceMode();
118    TEST_UTIL.startMiniHBaseCluster(1, 0);
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
130    try (Table table = conn.getTable(testRepairMode);
131        ResultScanner scanner = table.getScanner(new Scan())) {
132      scanner.next();
133      fail("Should not be able to access user-space tables in repair mode.");
134    } catch (Exception e) {
135      // Expected
136    }
137  }
138}