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
.assertEquals
;
21 import static org
.junit
.Assert
.assertNotEquals
;
22 import static org
.junit
.Assert
.assertTrue
;
24 import java
.io
.IOException
;
25 import org
.apache
.hadoop
.conf
.Configuration
;
26 import org
.apache
.hadoop
.hbase
.HBaseClassTestRule
;
27 import org
.apache
.hadoop
.hbase
.HBaseTestingUtility
;
28 import org
.apache
.hadoop
.hbase
.HRegionInfo
;
29 import org
.apache
.hadoop
.hbase
.MiniHBaseCluster
;
30 import org
.apache
.hadoop
.hbase
.MiniHBaseCluster
.MiniHBaseClusterRegionServer
;
31 import org
.apache
.hadoop
.hbase
.ServerName
;
32 import org
.apache
.hadoop
.hbase
.StartMiniClusterOption
;
33 import org
.apache
.hadoop
.hbase
.Waiter
;
34 import org
.apache
.hadoop
.hbase
.master
.assignment
.RegionStates
;
35 import org
.apache
.hadoop
.hbase
.testclassification
.MediumTests
;
36 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
37 import org
.apache
.hadoop
.hbase
.zookeeper
.MetaTableLocator
;
38 import org
.apache
.hadoop
.hbase
.zookeeper
.ZKUtil
;
39 import org
.apache
.hadoop
.hbase
.zookeeper
.ZNodePaths
;
40 import org
.apache
.zookeeper
.KeeperException
;
41 import org
.junit
.AfterClass
;
42 import org
.junit
.BeforeClass
;
43 import org
.junit
.ClassRule
;
44 import org
.junit
.Test
;
45 import org
.junit
.experimental
.categories
.Category
;
46 import org
.slf4j
.Logger
;
47 import org
.slf4j
.LoggerFactory
;
50 * Tests handling of meta-carrying region server failover.
52 @Category(MediumTests
.class)
53 public class TestMetaShutdownHandler
{
54 private static final Logger LOG
= LoggerFactory
.getLogger(TestMetaShutdownHandler
.class);
56 public static final HBaseClassTestRule CLASS_RULE
=
57 HBaseClassTestRule
.forClass(TestMetaShutdownHandler
.class);
59 private final static HBaseTestingUtility TEST_UTIL
= new HBaseTestingUtility();
60 final static Configuration conf
= TEST_UTIL
.getConfiguration();
63 public static void setUpBeforeClass() throws Exception
{
64 StartMiniClusterOption option
= StartMiniClusterOption
.builder()
65 .numRegionServers(3).rsClass(MyRegionServer
.class).numDataNodes(3).build();
66 TEST_UTIL
.startMiniCluster(option
);
70 public static void tearDownAfterClass() throws Exception
{
71 TEST_UTIL
.shutdownMiniCluster();
75 * This test will test the expire handling of a meta-carrying
77 * After HBaseMiniCluster is up, we will delete the ephemeral
78 * node of the meta-carrying region server, which will trigger
79 * the expire of this region server on the master.
80 * On the other hand, we will slow down the abort process on
81 * the region server so that it is still up during the master SSH.
82 * We will check that the master SSH is still successfully done.
85 public void testExpireMetaRegionServer() throws Exception
{
86 MiniHBaseCluster cluster
= TEST_UTIL
.getHBaseCluster();
87 HMaster master
= cluster
.getMaster();
88 RegionStates regionStates
= master
.getAssignmentManager().getRegionStates();
89 ServerName metaServerName
= regionStates
.getRegionServerOfRegion(
90 HRegionInfo
.FIRST_META_REGIONINFO
);
91 if (master
.getServerName().equals(metaServerName
) || metaServerName
== null
92 || !metaServerName
.equals(cluster
.getServerHoldingMeta())) {
93 // Move meta off master
95 cluster
.getLiveRegionServerThreads().get(0).getRegionServer().getServerName();
96 master
.move(HRegionInfo
.FIRST_META_REGIONINFO
.getEncodedNameAsBytes(),
97 Bytes
.toBytes(metaServerName
.getServerName()));
98 TEST_UTIL
.waitUntilNoRegionsInTransition(60000);
99 metaServerName
= regionStates
.getRegionServerOfRegion(HRegionInfo
.FIRST_META_REGIONINFO
);
101 RegionState metaState
= MetaTableLocator
.getMetaRegionState(master
.getZooKeeper());
102 assertEquals("Wrong state for meta!", RegionState
.State
.OPEN
, metaState
.getState());
103 assertNotEquals("Meta is on master!", metaServerName
, master
.getServerName());
105 // Delete the ephemeral node of the meta-carrying region server.
106 // This is trigger the expire of this region server on the master.
107 String rsEphemeralNodePath
=
108 ZNodePaths
.joinZNode(master
.getZooKeeper().getZNodePaths().rsZNode
,
109 metaServerName
.toString());
110 ZKUtil
.deleteNode(master
.getZooKeeper(), rsEphemeralNodePath
);
111 LOG
.info("Deleted the znode for the RegionServer hosting hbase:meta; waiting on SSH");
112 // Wait for SSH to finish
113 final ServerManager serverManager
= master
.getServerManager();
114 final ServerName priorMetaServerName
= metaServerName
;
115 TEST_UTIL
.waitFor(120000, 200, new Waiter
.Predicate
<Exception
>() {
117 public boolean evaluate() throws Exception
{
118 return !serverManager
.isServerOnline(priorMetaServerName
)
119 && !serverManager
.areDeadServersInProgress();
122 LOG
.info("Past wait on RIT");
123 TEST_UTIL
.waitUntilNoRegionsInTransition(60000);
124 // Now, make sure meta is assigned
125 assertTrue("Meta should be assigned",
126 regionStates
.isRegionOnline(HRegionInfo
.FIRST_META_REGIONINFO
));
127 // Now, make sure meta is registered in zk
128 metaState
= MetaTableLocator
.getMetaRegionState(master
.getZooKeeper());
129 assertEquals("Meta should not be in transition", RegionState
.State
.OPEN
,
130 metaState
.getState());
131 assertEquals("Meta should be assigned", metaState
.getServerName(),
132 regionStates
.getRegionServerOfRegion(HRegionInfo
.FIRST_META_REGIONINFO
));
133 assertNotEquals("Meta should be assigned on a different server",
134 metaState
.getServerName(), metaServerName
);
137 public static class MyRegionServer
extends MiniHBaseClusterRegionServer
{
139 public MyRegionServer(Configuration conf
) throws IOException
, KeeperException
,
140 InterruptedException
{
145 public void abort(String reason
, Throwable cause
) {
146 // sleep to slow down the region server abort
148 Thread
.sleep(30*1000);
149 } catch (InterruptedException e
) {
152 super.abort(reason
, cause
);