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 static org.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.hasItem;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024
025import java.util.Set;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseRpcServicesBase;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.regionserver.BootstrapNodeManager;
032import org.apache.hadoop.hbase.security.UserProvider;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.RegionServerTests;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
042
043/**
044 * Make sure that we can update the bootstrap server from master to region server, and region server
045 * could also contact each other to update the bootstrap nodes.
046 */
047@Category({ RegionServerTests.class, MediumTests.class })
048public class TestBootstrapNodeUpdate {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestBootstrapNodeUpdate.class);
053
054  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
055
056  private static RpcConnectionRegistry REGISTRY;
057
058  @BeforeClass
059  public static void setUpBeforeClass() throws Exception {
060    Configuration conf = UTIL.getConfiguration();
061    conf.setLong(BootstrapNodeManager.REQUEST_MASTER_INTERVAL_SECS, 5);
062    conf.setLong(BootstrapNodeManager.REQUEST_MASTER_MIN_INTERVAL_SECS, 1);
063    conf.setLong(BootstrapNodeManager.REQUEST_REGIONSERVER_INTERVAL_SECS, 1);
064    conf.setInt(HBaseRpcServicesBase.CLIENT_BOOTSTRAP_NODE_LIMIT, 2);
065    conf.setLong(RpcConnectionRegistry.INITIAL_REFRESH_DELAY_SECS, 5);
066    conf.setLong(RpcConnectionRegistry.PERIODIC_REFRESH_INTERVAL_SECS, 1);
067    conf.setLong(RpcConnectionRegistry.MIN_SECS_BETWEEN_REFRESHES, 1);
068    UTIL.startMiniCluster(3);
069    REGISTRY = new RpcConnectionRegistry(conf, UserProvider.instantiate(conf).getCurrent());
070  }
071
072  @AfterClass
073  public static void tearDownAfterClass() throws Exception {
074    Closeables.close(REGISTRY, true);
075    UTIL.shutdownMiniCluster();
076  }
077
078  @Test
079  public void testUpdate() throws Exception {
080    ServerName activeMasterServerName = REGISTRY.getActiveMaster().get();
081    ServerName masterInConf = ServerName.valueOf(activeMasterServerName.getHostname(),
082      activeMasterServerName.getPort(), -1);
083    // we should have master in the beginning
084    assertThat(REGISTRY.getParsedServers(), hasItem(masterInConf));
085    // and after refreshing, we will switch to use region servers
086    UTIL.waitFor(15000, () -> !REGISTRY.getParsedServers().contains(masterInConf)
087      && !REGISTRY.getParsedServers().contains(activeMasterServerName));
088    Set<ServerName> parsedServers = REGISTRY.getParsedServers();
089    assertEquals(2, parsedServers.size());
090    // now kill one region server
091    ServerName serverToKill = parsedServers.iterator().next();
092    UTIL.getMiniHBaseCluster().killRegionServer(serverToKill);
093    // wait until the region server disappears
094    // since the min node limit is 2, this means region server will still contact each other for
095    // getting bootstrap nodes, instead of requesting master directly, so this assert can make sure
096    // that the getAllBootstrapNodes works fine, and also the client can communicate with region
097    // server to update bootstrap nodes
098    UTIL.waitFor(30000, () -> !REGISTRY.getParsedServers().contains(serverToKill));
099    // should still have 2 servers, the remaining 2 live region servers
100    assertEquals(2, parsedServers.size());
101    // make sure the registry still works fine
102    assertNotNull(REGISTRY.getClusterId().get());
103  }
104}