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.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.Comparator;
028import java.util.List;
029import java.util.Set;
030import org.apache.hadoop.conf.Configuration;
031import org.apache.hadoop.hbase.HBaseTestingUtil;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.HRegionLocation;
034import org.apache.hadoop.hbase.ServerName;
035import org.apache.hadoop.hbase.StartTestingClusterOption;
036import org.apache.hadoop.hbase.TableName;
037import org.apache.hadoop.hbase.Waiter;
038import org.apache.hadoop.hbase.master.HMaster;
039import org.apache.hadoop.hbase.security.User;
040import org.apache.hadoop.hbase.testclassification.ClientTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.junit.jupiter.api.AfterAll;
043import org.junit.jupiter.api.BeforeAll;
044import org.junit.jupiter.api.Tag;
045import org.junit.jupiter.api.Test;
046
047import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
048
049@Tag(MediumTests.TAG)
050@Tag(ClientTests.TAG)
051public class TestMasterRegistry {
052
053  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
054
055  @BeforeAll
056  public static void setUp() throws Exception {
057    StartTestingClusterOption.Builder builder = StartTestingClusterOption.builder();
058    builder.numMasters(3).numRegionServers(3);
059    TEST_UTIL.startMiniCluster(builder.build());
060    HBaseTestingUtil.setReplicas(TEST_UTIL.getAdmin(), TableName.META_TABLE_NAME, 3);
061  }
062
063  @AfterAll
064  public static void tearDown() throws Exception {
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  /**
069   * Generates a string of dummy master addresses in host:port format. Every other hostname won't
070   * have a port number.
071   */
072  private static String generateDummyMastersList(int size) {
073    List<String> masters = new ArrayList<>();
074    for (int i = 0; i < size; i++) {
075      masters.add(" localhost" + (i % 2 == 0 ? ":" + (1000 + i) : ""));
076    }
077    return String.join(",", masters);
078  }
079
080  /**
081   * Makes sure the master registry parses the master end points in the configuration correctly.
082   */
083  @Test
084  public void testMasterAddressParsing() throws Exception {
085    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
086    int numMasters = 10;
087    conf.set(HConstants.MASTER_ADDRS_KEY, generateDummyMastersList(numMasters));
088    List<ServerName> parsedMasters = new ArrayList<>(MasterRegistry.parseMasterAddrs(conf));
089    // Half of them would be without a port, duplicates are removed.
090    assertEquals(numMasters / 2 + 1, parsedMasters.size());
091    // Sort in the increasing order of port numbers.
092    Collections.sort(parsedMasters, Comparator.comparingInt(ServerName::getPort));
093    for (int i = 0; i < parsedMasters.size(); i++) {
094      ServerName sn = parsedMasters.get(i);
095      assertEquals("localhost", sn.getHostname());
096      if (i == parsedMasters.size() - 1) {
097        // Last entry should be the one with default port.
098        assertEquals(HConstants.DEFAULT_MASTER_PORT, sn.getPort());
099      } else {
100        assertEquals(1000 + (2 * i), sn.getPort());
101      }
102    }
103  }
104
105  @Test
106  public void testMasterPortDefaults() throws Exception {
107    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
108    conf.set(HConstants.MASTER_ADDRS_KEY, "localhost");
109    List<ServerName> parsedMasters = new ArrayList<>(MasterRegistry.parseMasterAddrs(conf));
110    ServerName sn = parsedMasters.get(0);
111    assertEquals(HConstants.DEFAULT_MASTER_PORT, sn.getPort());
112    final int CUSTOM_MASTER_PORT = 9999;
113    conf.setInt(HConstants.MASTER_PORT, CUSTOM_MASTER_PORT);
114    parsedMasters = new ArrayList<>(MasterRegistry.parseMasterAddrs(conf));
115    sn = parsedMasters.get(0);
116    assertEquals(CUSTOM_MASTER_PORT, sn.getPort());
117  }
118
119  @Test
120  public void testRegistryRPCs() throws Exception {
121    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
122    HMaster activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
123    final int size = activeMaster.getMetaLocations().size();
124    for (int numHedgedReqs = 1; numHedgedReqs <= size; numHedgedReqs++) {
125      conf.setInt(MasterRegistry.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY, numHedgedReqs);
126      try (MasterRegistry registry = new MasterRegistry(conf, User.getCurrent())) {
127        // Add wait on all replicas being assigned before proceeding w/ test. Failed on occasion
128        // because not all replicas had made it up before test started.
129        RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(TEST_UTIL, registry);
130        assertEquals(registry.getClusterId().get(), activeMaster.getClusterId());
131        assertEquals(registry.getActiveMaster().get(), activeMaster.getServerName());
132        List<HRegionLocation> metaLocations =
133          Arrays.asList(registry.getMetaRegionLocations().get().getRegionLocations());
134        List<HRegionLocation> actualMetaLocations = activeMaster.getMetaLocations();
135        Collections.sort(metaLocations);
136        Collections.sort(actualMetaLocations);
137        assertEquals(actualMetaLocations, metaLocations);
138      }
139    }
140  }
141
142  /**
143   * Tests that the list of masters configured in the MasterRegistry is dynamically refreshed in the
144   * event of errors.
145   */
146  @Test
147  public void testDynamicMasterConfigurationRefresh() throws Exception {
148    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
149    String currentMasterAddrs = Preconditions.checkNotNull(conf.get(HConstants.MASTER_ADDRS_KEY));
150    HMaster activeMaster = TEST_UTIL.getHBaseCluster().getMaster();
151    String clusterId = activeMaster.getClusterId();
152    // Add a non-working master
153    ServerName badServer = ServerName.valueOf("localhost", 1234, -1);
154    conf.set(HConstants.MASTER_ADDRS_KEY, badServer.toShortString() + "," + currentMasterAddrs);
155    // Set the hedging fan out so that all masters are queried.
156    conf.setInt(MasterRegistry.MASTER_REGISTRY_HEDGED_REQS_FANOUT_KEY, 4);
157    // Do not limit the number of refreshes during the test run.
158    conf.setLong(MasterRegistry.MASTER_REGISTRY_MIN_SECS_BETWEEN_REFRESHES, 0);
159    try (MasterRegistry registry = new MasterRegistry(conf, User.getCurrent())) {
160      final Set<ServerName> masters = registry.getParsedServers();
161      assertTrue(masters.contains(badServer));
162      // Make a registry RPC, this should trigger a refresh since one of the hedged RPC fails.
163      assertEquals(registry.getClusterId().get(), clusterId);
164      // Wait for new set of masters to be populated.
165      TEST_UTIL.waitFor(5000,
166        (Waiter.Predicate<Exception>) () -> !registry.getParsedServers().equals(masters));
167      // new set of masters should not include the bad server
168      final Set<ServerName> newMasters = registry.getParsedServers();
169      // Bad one should be out.
170      assertEquals(3, newMasters.size());
171      assertFalse(newMasters.contains(badServer));
172      // Kill the active master
173      activeMaster.stopMaster();
174      TEST_UTIL.waitFor(10000,
175        () -> TEST_UTIL.getMiniHBaseCluster().getLiveMasterThreads().size() == 2);
176      TEST_UTIL.getMiniHBaseCluster().waitForActiveAndReadyMaster(10000);
177      // Wait until the killed master de-registered. This should also trigger another refresh.
178      TEST_UTIL.waitFor(10000, () -> registry.getMasters().get().size() == 2);
179      TEST_UTIL.waitFor(20000, () -> registry.getParsedServers().size() == 2);
180      final Set<ServerName> newMasters2 = registry.getParsedServers();
181      assertEquals(2, newMasters2.size());
182      assertFalse(newMasters2.contains(activeMaster.getServerName()));
183    } finally {
184      // Reset the state, add a killed master.
185      TEST_UTIL.getMiniHBaseCluster().startMaster();
186    }
187  }
188}