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.jupiter.api.Assertions.assertNotNull;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022import static org.junit.jupiter.api.Assertions.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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.StartTestingClusterOption;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.AsyncTable;
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.LargeTests;
041import org.apache.hadoop.hbase.testclassification.MasterTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.junit.jupiter.api.AfterEach;
044import org.junit.jupiter.api.BeforeEach;
045import org.junit.jupiter.api.Tag;
046import org.junit.jupiter.api.Test;
047import org.junit.jupiter.api.TestInfo;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051@Tag(MasterTests.TAG)
052@Tag(LargeTests.TAG)
053public class TestMasterRepairMode {
054  private String testMethodName;
055
056  @BeforeEach
057  public void setTestMethod(TestInfo testInfo) {
058    testMethodName = testInfo.getTestMethod().get().getName();
059  }
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 HBaseTestingUtil TEST_UTIL;
066
067  @BeforeEach
068  public void setUp() throws Exception {
069    TEST_UTIL = new HBaseTestingUtil();
070  }
071
072  @AfterEach
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(
088      StartTestingClusterOption.builder().numRegionServers(0).numDataNodes(3).build());
089
090    Connection conn = TEST_UTIL.getConnection();
091    assertTrue(conn.getAdmin().isMasterInMaintenanceMode());
092
093    try (Table table = conn.getTable(TableName.META_TABLE_NAME);
094      ResultScanner scanner = table.getScanner(new Scan())) {
095      assertNotNull(scanner.next(), "Could not read meta.");
096    }
097  }
098
099  @Test
100  public void testExistingCluster() throws Exception {
101    TableName testRepairMode = TableName.valueOf(testMethodName);
102
103    TEST_UTIL.startMiniCluster();
104    Table t = TEST_UTIL.createTable(testRepairMode, FAMILYNAME);
105    Put p = new Put(Bytes.toBytes("r"));
106    p.addColumn(FAMILYNAME, Bytes.toBytes("c"), new byte[0]);
107    t.put(p);
108
109    TEST_UTIL.shutdownMiniHBaseCluster();
110
111    LOG.info("Starting master-only");
112
113    enableMaintenanceMode();
114    TEST_UTIL.startMiniHBaseCluster(
115      StartTestingClusterOption.builder().numRegionServers(0).createRootDir(false).build());
116
117    Connection conn = TEST_UTIL.getConnection();
118    assertTrue(conn.getAdmin().isMasterInMaintenanceMode());
119
120    try (Table table = conn.getTable(TableName.META_TABLE_NAME);
121      ResultScanner scanner = table.getScanner(HConstants.TABLE_FAMILY);
122      Stream<Result> results = StreamSupport.stream(scanner.spliterator(), false)) {
123      assertTrue(results.anyMatch(r -> Arrays.equals(r.getRow(), testRepairMode.getName())),
124        "Did not find user table records while reading hbase:meta");
125    }
126    // use async table so we can set the timeout and retry value to let the operation fail fast
127    AsyncTable<?> table = conn.toAsyncConnection().getTableBuilder(testRepairMode)
128      .setScanTimeout(5, TimeUnit.SECONDS).setMaxRetries(2).build();
129    assertThrows(Exception.class, () -> {
130      try (ResultScanner scanner = table.getScanner(new Scan())) {
131        scanner.next();
132      }
133    }, "Should not be able to access user-space tables in repair mode.");
134  }
135}