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