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