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