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.assertEquals;
021import static org.junit.Assert.assertNotEquals;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HRegionInfo;
029import org.apache.hadoop.hbase.MiniHBaseCluster;
030import org.apache.hadoop.hbase.MiniHBaseCluster.MiniHBaseClusterRegionServer;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.Waiter;
033import org.apache.hadoop.hbase.master.assignment.RegionStates;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
037import org.apache.hadoop.hbase.zookeeper.ZKUtil;
038import org.apache.hadoop.hbase.zookeeper.ZNodePaths;
039import org.apache.zookeeper.KeeperException;
040import org.junit.AfterClass;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048/**
049 * Tests handling of meta-carrying region server failover.
050 */
051@Category(MediumTests.class)
052public class TestMetaShutdownHandler {
053  private static final Logger LOG = LoggerFactory.getLogger(TestMetaShutdownHandler.class);
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestMetaShutdownHandler.class);
057
058  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
059  final static Configuration conf = TEST_UTIL.getConfiguration();
060
061  @BeforeClass
062  public static void setUpBeforeClass() throws Exception {
063    TEST_UTIL.startMiniCluster(1, 3, null, null, MyRegionServer.class);
064  }
065
066  @AfterClass
067  public static void tearDownAfterClass() throws Exception {
068    TEST_UTIL.shutdownMiniCluster();
069  }
070
071  /**
072   * This test will test the expire handling of a meta-carrying
073   * region server.
074   * After HBaseMiniCluster is up, we will delete the ephemeral
075   * node of the meta-carrying region server, which will trigger
076   * the expire of this region server on the master.
077   * On the other hand, we will slow down the abort process on
078   * the region server so that it is still up during the master SSH.
079   * We will check that the master SSH is still successfully done.
080   */
081  @Test
082  public void testExpireMetaRegionServer() throws Exception {
083    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
084    HMaster master = cluster.getMaster();
085    RegionStates regionStates = master.getAssignmentManager().getRegionStates();
086    ServerName metaServerName = regionStates.getRegionServerOfRegion(
087      HRegionInfo.FIRST_META_REGIONINFO);
088    if (master.getServerName().equals(metaServerName) || metaServerName == null
089        || !metaServerName.equals(cluster.getServerHoldingMeta())) {
090      // Move meta off master
091      metaServerName =
092          cluster.getLiveRegionServerThreads().get(0).getRegionServer().getServerName();
093      master.move(HRegionInfo.FIRST_META_REGIONINFO.getEncodedNameAsBytes(),
094        Bytes.toBytes(metaServerName.getServerName()));
095      TEST_UTIL.waitUntilNoRegionsInTransition(60000);
096      metaServerName = regionStates.getRegionServerOfRegion(HRegionInfo.FIRST_META_REGIONINFO);
097    }
098    RegionState metaState = MetaTableLocator.getMetaRegionState(master.getZooKeeper());
099    assertEquals("Wrong state for meta!", RegionState.State.OPEN, metaState.getState());
100    assertNotEquals("Meta is on master!", metaServerName, master.getServerName());
101
102    // Delete the ephemeral node of the meta-carrying region server.
103    // This is trigger the expire of this region server on the master.
104    String rsEphemeralNodePath =
105        ZNodePaths.joinZNode(master.getZooKeeper().getZNodePaths().rsZNode,
106                metaServerName.toString());
107    ZKUtil.deleteNode(master.getZooKeeper(), rsEphemeralNodePath);
108    LOG.info("Deleted the znode for the RegionServer hosting hbase:meta; waiting on SSH");
109    // Wait for SSH to finish
110    final ServerManager serverManager = master.getServerManager();
111    final ServerName priorMetaServerName = metaServerName;
112    TEST_UTIL.waitFor(120000, 200, new Waiter.Predicate<Exception>() {
113      @Override
114      public boolean evaluate() throws Exception {
115        return !serverManager.isServerOnline(priorMetaServerName)
116            && !serverManager.areDeadServersInProgress();
117      }
118    });
119    LOG.info("Past wait on RIT");
120    TEST_UTIL.waitUntilNoRegionsInTransition(60000);
121    // Now, make sure meta is assigned
122    assertTrue("Meta should be assigned",
123      regionStates.isRegionOnline(HRegionInfo.FIRST_META_REGIONINFO));
124    // Now, make sure meta is registered in zk
125    metaState = MetaTableLocator.getMetaRegionState(master.getZooKeeper());
126    assertEquals("Meta should not be in transition", RegionState.State.OPEN,
127        metaState.getState());
128    assertEquals("Meta should be assigned", metaState.getServerName(),
129      regionStates.getRegionServerOfRegion(HRegionInfo.FIRST_META_REGIONINFO));
130    assertNotEquals("Meta should be assigned on a different server",
131      metaState.getServerName(), metaServerName);
132  }
133
134  public static class MyRegionServer extends MiniHBaseClusterRegionServer {
135
136    public MyRegionServer(Configuration conf) throws IOException, KeeperException,
137        InterruptedException {
138      super(conf);
139    }
140
141    @Override
142    public void abort(String reason, Throwable cause) {
143      // sleep to slow down the region server abort
144      try {
145        Thread.sleep(30*1000);
146      } catch (InterruptedException e) {
147        return;
148      }
149      super.abort(reason, cause);
150    }
151  }
152}