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