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.Assert.assertEquals; 021 022import java.util.Optional; 023import org.apache.commons.io.IOUtils; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.HRegionInfo; 027import org.apache.hadoop.hbase.ServerName; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.Waiter.ExplainingPredicate; 030import org.apache.hadoop.hbase.master.balancer.BaseLoadBalancer; 031import org.apache.hadoop.hbase.testclassification.ClientTests; 032import org.apache.hadoop.hbase.testclassification.MediumTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.AfterClass; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039 040@Category({ MediumTests.class, ClientTests.class }) 041public class TestAsyncMetaRegionLocator { 042 043 @ClassRule 044 public static final HBaseClassTestRule CLASS_RULE = 045 HBaseClassTestRule.forClass(TestAsyncMetaRegionLocator.class); 046 047 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 048 049 private static AsyncRegistry REGISTRY; 050 051 private static AsyncMetaRegionLocator LOCATOR; 052 053 @BeforeClass 054 public static void setUp() throws Exception { 055 TEST_UTIL.getConfiguration().set(BaseLoadBalancer.TABLES_ON_MASTER, "none"); 056 TEST_UTIL.startMiniCluster(3); 057 TEST_UTIL.waitUntilAllSystemRegionsAssigned(); 058 TEST_UTIL.getAdmin().setBalancerRunning(false, true); 059 REGISTRY = AsyncRegistryFactory.getRegistry(TEST_UTIL.getConfiguration()); 060 LOCATOR = new AsyncMetaRegionLocator(REGISTRY); 061 } 062 063 @AfterClass 064 public static void tearDown() throws Exception { 065 IOUtils.closeQuietly(REGISTRY); 066 TEST_UTIL.shutdownMiniCluster(); 067 } 068 069 private Optional<ServerName> getRSCarryingMeta() { 070 return TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream() 071 .map(t -> t.getRegionServer()) 072 .filter(rs -> !rs.getRegions(TableName.META_TABLE_NAME).isEmpty()).findAny() 073 .map(rs -> rs.getServerName()); 074 } 075 076 @Test 077 public void testReload() throws Exception { 078 ServerName serverName = getRSCarryingMeta().get(); 079 assertEquals(serverName, LOCATOR.getRegionLocation(false).get().getServerName()); 080 081 ServerName newServerName = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream() 082 .map(t -> t.getRegionServer().getServerName()).filter(sn -> !sn.equals(serverName)) 083 .findAny().get(); 084 TEST_UTIL.getAdmin().move(HRegionInfo.FIRST_META_REGIONINFO.getEncodedNameAsBytes(), 085 Bytes.toBytes(newServerName.getServerName())); 086 TEST_UTIL.waitFor(30000, new ExplainingPredicate<Exception>() { 087 088 @Override 089 public boolean evaluate() throws Exception { 090 Optional<ServerName> newServerName = getRSCarryingMeta(); 091 return newServerName.isPresent() && !newServerName.get().equals(serverName); 092 } 093 094 @Override 095 public String explainFailure() throws Exception { 096 return HRegionInfo.FIRST_META_REGIONINFO.getRegionNameAsString() + " is still on " + 097 serverName; 098 } 099 }); 100 // The cached location will not change 101 assertEquals(serverName, LOCATOR.getRegionLocation(false).get().getServerName()); 102 // should get the new location when reload = true 103 assertEquals(newServerName, LOCATOR.getRegionLocation(true).get().getServerName()); 104 // the cached location should be replaced 105 assertEquals(newServerName, LOCATOR.getRegionLocation(false).get().getServerName()); 106 } 107}