2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
.master
;
20 import static org
.junit
.Assert
.assertNotNull
;
21 import static org
.junit
.Assert
.assertTrue
;
22 import static org
.junit
.Assert
.fail
;
23 import java
.util
.Arrays
;
24 import java
.util
.stream
.Stream
;
25 import java
.util
.stream
.StreamSupport
;
26 import org
.apache
.hadoop
.conf
.Configuration
;
27 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
28 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
29 import org
.apache
.hadoop
.hbase
.HConstants
;
30 import org
.apache
.hadoop
.hbase
.StartMiniClusterOption
;
31 import org
.apache
.hadoop
.hbase
.TableName
;
32 import org
.apache
.hadoop
.hbase
.client
.Connection
;
33 import org
.apache
.hadoop
.hbase
.client
.Put
;
34 import org
.apache
.hadoop
.hbase
.client
.Result
;
35 import org
.apache
.hadoop
.hbase
.client
.ResultScanner
;
36 import org
.apache
.hadoop
.hbase
.client
.Scan
;
37 import org
.apache
.hadoop
.hbase
.client
.Table
;
38 import org
.apache
.hadoop
.hbase
.testclassification
.LargeTests
;
39 import org
.apache
.hadoop
.hbase
.testclassification
.MasterTests
;
40 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
41 import org
.junit
.After
;
42 import org
.junit
.Before
;
43 import org
.junit
.ClassRule
;
44 import org
.junit
.Rule
;
45 import org
.junit
.Test
;
46 import org
.junit
.experimental
.categories
.Category
;
47 import org
.junit
.rules
.TestName
;
48 import org
.slf4j
.Logger
;
49 import org
.slf4j
.LoggerFactory
;
51 @Category({MasterTests
.class, LargeTests
.class})
52 public class TestMasterRepairMode
{
55 public static final HBaseClassTestRule CLASS_RULE
=
56 HBaseClassTestRule
.forClass(TestMasterRepairMode
.class);
59 public TestName name
= new TestName();
61 private static final Logger LOG
= LoggerFactory
.getLogger(TestMasterRepairMode
.class);
63 private static final byte[] FAMILYNAME
= Bytes
.toBytes("fam");
65 private static HBaseTestingUtility TEST_UTIL
;
68 public void setUp() throws Exception
{
69 TEST_UTIL
= new HBaseTestingUtility();
73 public void tearDown() throws Exception
{
74 TEST_UTIL
.shutdownMiniCluster();
77 private void enableMaintenanceMode() {
78 Configuration c
= TEST_UTIL
.getConfiguration();
79 c
.setBoolean(HMaster
.MAINTENANCE_MODE
, true);
80 c
.setInt("hbase.master.init.timeout.localHBaseCluster", 30000);
84 public void testNewCluster() throws Exception
{
85 enableMaintenanceMode();
87 TEST_UTIL
.startMiniCluster(StartMiniClusterOption
.builder()
92 Connection conn
= TEST_UTIL
.getConnection();
93 assertTrue(conn
.getAdmin().isMasterInMaintenanceMode());
95 try (Table table
= conn
.getTable(TableName
.META_TABLE_NAME
);
96 ResultScanner scanner
= table
.getScanner(new Scan())) {
97 assertNotNull("Could not read meta.", scanner
.next());
102 public void testExistingCluster() throws Exception
{
103 TableName testRepairMode
= TableName
.valueOf(name
.getMethodName());
105 TEST_UTIL
.startMiniCluster();
106 Table t
= TEST_UTIL
.createTable(testRepairMode
, FAMILYNAME
);
107 Put p
= new Put(Bytes
.toBytes("r"));
108 p
.addColumn(FAMILYNAME
, Bytes
.toBytes("c"), new byte[0]);
111 TEST_UTIL
.shutdownMiniHBaseCluster();
113 LOG
.info("Starting master-only");
115 enableMaintenanceMode();
116 TEST_UTIL
.startMiniHBaseCluster(StartMiniClusterOption
.builder()
117 .numRegionServers(0).createRootDir(false).build());
119 Connection conn
= TEST_UTIL
.getConnection();
120 assertTrue(conn
.getAdmin().isMasterInMaintenanceMode());
122 try (Table table
= conn
.getTable(TableName
.META_TABLE_NAME
);
123 ResultScanner scanner
= table
.getScanner(HConstants
.TABLE_FAMILY
);
124 Stream
<Result
> results
= StreamSupport
.stream(scanner
.spliterator(), false)) {
125 assertTrue("Did not find user table records while reading hbase:meta",
126 results
.anyMatch(r
-> Arrays
.equals(r
.getRow(), testRepairMode
.getName())));
129 try (Table table
= conn
.getTable(testRepairMode
);
130 ResultScanner scanner
= table
.getScanner(new Scan())) {
132 fail("Should not be able to access user-space tables in repair mode.");
133 } catch (Exception e
) {