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.apache.hadoop.hbase.HConstants.META_REPLICAS_NUM; 021import static org.junit.Assert.assertEquals; 022 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.Arrays; 026import java.util.Collections; 027import java.util.Comparator; 028import java.util.List; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.HRegionLocation; 034import org.apache.hadoop.hbase.ServerName; 035import org.apache.hadoop.hbase.StartMiniClusterOption; 036import org.apache.hadoop.hbase.master.HMaster; 037import org.apache.hadoop.hbase.testclassification.ClientTests; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044 045@Category({ MediumTests.class, ClientTests.class }) 046public class TestMasterRegistry { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestMasterRegistry.class); 051 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 052 053 @BeforeClass 054 public static void setUp() throws Exception { 055 TEST_UTIL.getConfiguration().setInt(META_REPLICAS_NUM, 3); 056 StartMiniClusterOption.Builder builder = StartMiniClusterOption.builder(); 057 builder.numMasters(3).numRegionServers(3); 058 TEST_UTIL.startMiniCluster(builder.build()); 059 } 060 061 @AfterClass 062 public static void tearDown() throws Exception { 063 TEST_UTIL.shutdownMiniCluster(); 064 } 065 066 /** 067 * Generates a string of dummy master addresses in host:port format. Every other hostname won't 068 * have a port number. 069 */ 070 private static String generateDummyMastersList(int size) { 071 List<String> masters = new ArrayList<>(); 072 for (int i = 0; i < size; i++) { 073 masters.add(" localhost" + (i % 2 == 0 ? ":" + (1000 + i) : "")); 074 } 075 return String.join(",", masters); 076 } 077 078 /** 079 * Makes sure the master registry parses the master end points in the configuration correctly. 080 */ 081 @Test 082 public void testMasterAddressParsing() throws IOException { 083 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 084 int numMasters = 10; 085 conf.set(HConstants.MASTER_ADDRS_KEY, generateDummyMastersList(numMasters)); 086 try (MasterRegistry registry = new MasterRegistry(conf)) { 087 List<ServerName> parsedMasters = new ArrayList<>(registry.getParsedMasterServers()); 088 // Half of them would be without a port, duplicates are removed. 089 assertEquals(numMasters / 2 + 1, parsedMasters.size()); 090 // Sort in the increasing order of port numbers. 091 Collections.sort(parsedMasters, Comparator.comparingInt(ServerName::getPort)); 092 for (int i = 0; i < parsedMasters.size(); i++) { 093 ServerName sn = parsedMasters.get(i); 094 assertEquals("localhost", sn.getHostname()); 095 if (i == parsedMasters.size() - 1) { 096 // Last entry should be the one with default port. 097 assertEquals(HConstants.DEFAULT_MASTER_PORT, sn.getPort()); 098 } else { 099 assertEquals(1000 + (2 * i), sn.getPort()); 100 } 101 } 102 } 103 } 104 105 @Test 106 public void testRegistryRPCs() throws Exception { 107 Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); 108 HMaster activeMaster = TEST_UTIL.getHBaseCluster().getMaster(); 109 final int size = 110 activeMaster.getMetaRegionLocationCache().getMetaRegionLocations().get().size(); 111 for (int numHedgedReqs = 1; numHedgedReqs <= size; numHedgedReqs++) { 112 conf.setInt(MasterRegistry.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY, numHedgedReqs); 113 try (MasterRegistry registry = new MasterRegistry(conf)) { 114 // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion 115 // because not all replicas had made it up before test started. 116 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry); 117 assertEquals(registry.getClusterId().get(), activeMaster.getClusterId()); 118 assertEquals(registry.getActiveMaster().get(), activeMaster.getServerName()); 119 List<HRegionLocation> metaLocations = 120 Arrays.asList(registry.getMetaRegionLocations().get().getRegionLocations()); 121 List<HRegionLocation> actualMetaLocations = 122 activeMaster.getMetaRegionLocationCache().getMetaRegionLocations().get(); 123 Collections.sort(metaLocations); 124 Collections.sort(actualMetaLocations); 125 assertEquals(actualMetaLocations, metaLocations); 126 } 127 } 128 } 129}