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; 023 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.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.Threads; 040import org.junit.After; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046 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.cleanupTestDir(); 070 UTIL.shutdownMiniCluster(); 071 } 072 073 @Test 074 public void testTableCreation() throws Exception { 075 076 conf.set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, StochasticLoadBalancer.class.getName()); 077 078 LOG.info("Starting up cluster"); 079 UTIL.startMiniCluster(SLAVES); 080 while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) { 081 Threads.sleep(1); 082 } 083 Admin admin = UTIL.getAdmin(); 084 admin.setBalancerRunning(false, true); 085 086 String tableName = "testFNImport"; 087 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName)); 088 desc.addFamily(new HColumnDescriptor(HConstants.CATALOG_FAMILY)); 089 admin.createTable(desc, Bytes.toBytes("a"), Bytes.toBytes("z"), REGION_NUM); 090 UTIL.waitTableAvailable(desc.getTableName()); 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 while (!UTIL.getMiniHBaseCluster().getMaster().isInitialized()) { 101 Threads.sleep(1); 102 } 103 admin = UTIL.getAdmin(); 104 105 UTIL.waitTableAvailable(desc.getTableName()); 106 107 FavoredNodesManager fnm = UTIL.getHBaseCluster().getMaster().getFavoredNodesManager(); 108 109 List<HRegionInfo> regionsOfTable = admin.getTableRegions(TableName.valueOf(tableName)); 110 for (HRegionInfo rInfo : regionsOfTable) { 111 Set<ServerName> favNodes = Sets.newHashSet(fnm.getFavoredNodes(rInfo)); 112 assertNotNull(favNodes); 113 assertEquals("Required no of favored nodes not found.", FAVORED_NODES_NUM, favNodes.size()); 114 for (ServerName fn : favNodes) { 115 assertEquals("StartCode invalid for:" + fn, ServerName.NON_STARTCODE, fn.getStartcode()); 116 } 117 } 118 } 119}