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.CoreMatchers.hasItems;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.junit.Assert.assertEquals;
023
024import java.io.IOException;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.List;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.HRegionLocation;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.master.HMaster;
034import org.apache.hadoop.hbase.regionserver.RSRpcServices;
035import org.apache.hadoop.hbase.testclassification.ClientTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.junit.After;
038import org.junit.AfterClass;
039import org.junit.Before;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
046
047@Category({ MediumTests.class, ClientTests.class })
048public class TestRpcConnectionRegistry {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestRpcConnectionRegistry.class);
053
054  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
055
056  private RpcConnectionRegistry registry;
057
058  @BeforeClass
059  public static void setUpBeforeClass() throws Exception {
060    // allow refresh immediately so we will switch to use region servers soon.
061    UTIL.getConfiguration().setLong(RpcConnectionRegistry.INITIAL_REFRESH_DELAY_SECS, 1);
062    UTIL.getConfiguration().setLong(RpcConnectionRegistry.PERIODIC_REFRESH_INTERVAL_SECS, 1);
063    UTIL.getConfiguration().setLong(RpcConnectionRegistry.MIN_SECS_BETWEEN_REFRESHES, 0);
064    UTIL.startMiniCluster(3);
065    HBaseTestingUtility.setReplicas(UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
066  }
067
068  @AfterClass
069  public static void tearDownAfterClass() throws Exception {
070    UTIL.shutdownMiniCluster();
071  }
072
073  @Before
074  public void setUp() throws IOException {
075    registry = new RpcConnectionRegistry(UTIL.getConfiguration());
076  }
077
078  @After
079  public void tearDown() throws IOException {
080    Closeables.close(registry, true);
081  }
082
083  private void setMaxNodeCount(int count) {
084    UTIL.getMiniHBaseCluster().getMasterThreads().stream()
085      .map(t -> t.getMaster().getConfiguration())
086      .forEach(conf -> conf.setInt(RSRpcServices.CLIENT_BOOTSTRAP_NODE_LIMIT, count));
087    UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
088      .map(t -> t.getRegionServer().getConfiguration())
089      .forEach(conf -> conf.setInt(RSRpcServices.CLIENT_BOOTSTRAP_NODE_LIMIT, count));
090  }
091
092  @Test
093  public void testRegistryRPCs() throws Exception {
094    HMaster activeMaster = UTIL.getHBaseCluster().getMaster();
095    // sleep 3 seconds, since our initial delay is 1 second, we should have refreshed the endpoints
096    Thread.sleep(3000);
097    assertThat(registry.getParsedServers(),
098      hasItems(activeMaster.getServerManager().getOnlineServersList().toArray(new ServerName[0])));
099
100    // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
101    // because not all replicas had made it up before test started.
102    RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry);
103
104    assertEquals(registry.getClusterId().get(), activeMaster.getClusterId());
105    assertEquals(registry.getActiveMaster().get(), activeMaster.getServerName());
106    List<HRegionLocation> metaLocations =
107      Arrays.asList(registry.getMetaRegionLocations().get().getRegionLocations());
108    List<HRegionLocation> actualMetaLocations =
109      activeMaster.getMetaRegionLocationCache().getMetaRegionLocations().get();
110    Collections.sort(metaLocations);
111    Collections.sort(actualMetaLocations);
112    assertEquals(actualMetaLocations, metaLocations);
113
114    // test that the node count config works
115    setMaxNodeCount(1);
116    UTIL.waitFor(10000, () -> registry.getParsedServers().size() == 1);
117  }
118}