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