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.client;
019
020import org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.HConstants;
022import org.apache.hadoop.hbase.HRegionLocation;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.testclassification.MediumTests;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.junit.BeforeClass;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034@Category({ MiscTests.class, MediumTests.class })
035public class TestShutdownOfMetaReplicaHolder extends MetaWithReplicasTestBase {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestShutdownOfMetaReplicaHolder.class);
040
041  private static final Logger LOG = LoggerFactory.getLogger(TestShutdownOfMetaReplicaHolder.class);
042
043  @BeforeClass
044  public static void setUp() throws Exception {
045    startCluster();
046  }
047
048  @Test
049  public void testShutdownOfReplicaHolder() throws Exception {
050    // checks that the when the server holding meta replica is shut down, the meta replica
051    // can be recovered
052    try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
053      RegionLocator locator = conn.getRegionLocator(TableName.META_TABLE_NAME)) {
054      HRegionLocation hrl = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1);
055      ServerName oldServer = hrl.getServerName();
056      TEST_UTIL.getHBaseClusterInterface().killRegionServer(oldServer);
057      LOG.debug("Waiting for the replica {} to come up", hrl.getRegion());
058      TEST_UTIL.waitFor(30000, () -> {
059        HRegionLocation loc = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1);
060        return loc != null && !loc.getServerName().equals(oldServer);
061      });
062      LOG.debug("Replica {} is online on {}, old server is {}", hrl.getRegion(),
063        locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1).getServerName(),
064        oldServer);
065    }
066  }
067}