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.master.balancer; 019 020import static org.apache.hadoop.hbase.favored.FavoredNodeAssignmentHelper.FAVORED_NODES_NUM; 021import static org.junit.Assert.assertEquals; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertTrue; 024import java.util.List; 025import java.util.Set; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.HColumnDescriptor; 030import org.apache.hadoop.hbase.HConstants; 031import org.apache.hadoop.hbase.HRegionInfo; 032import org.apache.hadoop.hbase.HTableDescriptor; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.client.Admin; 036import org.apache.hadoop.hbase.favored.FavoredNodesManager; 037import org.apache.hadoop.hbase.master.HMaster; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.util.Bytes; 040import org.apache.hadoop.hbase.util.Threads; 041import org.junit.After; 042import org.junit.ClassRule; 043import org.junit.Test; 044import org.junit.experimental.categories.Category; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 048 049/* 050 * This case tests a scenario when a cluster with tables is moved from Stochastic Load Balancer 051 * to FavoredStochasticLoadBalancer and the generation of favored nodes after switch. 052 */ 053@Category(MediumTests.class) 054public class TestFavoredNodeTableImport { 055 056 @ClassRule 057 public static final HBaseClassTestRule CLASS_RULE = 058 HBaseClassTestRule.forClass(TestFavoredNodeTableImport.class); 059 060 private static final Logger LOG = LoggerFactory.getLogger(TestFavoredNodeTableImport.class); 061 062 private static final int SLAVES = 3; 063 private static final int REGION_NUM = 20; 064 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 065 private static final Configuration conf = UTIL.getConfiguration(); 066 067 @After 068 public void stopCluster() throws Exception { 069 UTIL.shutdownMiniCluster(); 070 } 071 072 @Test 073 public void testTableCreation() throws Exception { 074 075 conf.set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, StochasticLoadBalancer.class.getName()); 076 077 LOG.info("Starting up cluster"); 078 UTIL.startMiniCluster(SLAVES); 079 while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) { 080 Threads.sleep(1); 081 } 082 Admin admin = UTIL.getAdmin(); 083 admin.balancerSwitch(false, true); 084 085 String tableName = "testFNImport"; 086 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName)); 087 desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY)); 088 admin.createTable(desc, Bytes.toBytes("a"), Bytes.toBytes("z"), REGION_NUM); 089 UTIL.waitTableAvailable(desc.getTableName()); 090 admin.balancerSwitch(true, true); 091 092 LOG.info("Shutting down cluster"); 093 UTIL.shutdownMiniHBaseCluster(); 094 095 Thread.sleep(2000); 096 LOG.info("Starting cluster again with FN Balancer"); 097 UTIL.getConfiguration().set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, 098 FavoredStochasticBalancer.class.getName()); 099 UTIL.restartHBaseCluster(SLAVES); 100 HMaster master = UTIL.getMiniHBaseCluster().getMaster(); 101 while (!master.isInitialized()) { 102 Threads.sleep(1); 103 } 104 UTIL.waitTableAvailable(desc.getTableName()); 105 UTIL.waitUntilNoRegionsInTransition(10000); 106 assertTrue(master.isBalancerOn()); 107 108 FavoredNodesManager fnm = master.getFavoredNodesManager(); 109 assertNotNull(fnm); 110 111 admin = UTIL.getAdmin(); 112 List<HRegionInfo> regionsOfTable = admin.getTableRegions(TableName.valueOf(tableName)); 113 for (HRegionInfo rInfo : regionsOfTable) { 114 assertNotNull(rInfo); 115 assertNotNull(fnm); 116 List<ServerName> fns = fnm.getFavoredNodes(rInfo); 117 LOG.info("FNS {} {}", rInfo, fns); 118 assertNotNull(rInfo.toString(), fns); 119 Set<ServerName> favNodes = Sets.newHashSet(fns); 120 assertNotNull(favNodes); 121 assertEquals("Required no of favored nodes not found.", FAVORED_NODES_NUM, favNodes.size()); 122 for (ServerName fn : favNodes) { 123 assertEquals("StartCode invalid for:" + fn, ServerName.NON_STARTCODE, fn.getStartcode()); 124 } 125 } 126 } 127}