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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
035import org.apache.hadoop.hbase.client.RegionInfo;
036import org.apache.hadoop.hbase.client.TableDescriptor;
037import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
038import org.apache.hadoop.hbase.favored.FavoredNodesManager;
039import org.apache.hadoop.hbase.master.HMaster;
040import org.apache.hadoop.hbase.testclassification.MediumTests;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.hbase.util.Threads;
043import org.junit.After;
044import org.junit.ClassRule;
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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    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    TableDescriptor tableDescriptor =
089      TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName))
090        .setColumnFamily(ColumnFamilyDescriptorBuilder.of(HConstants.CATALOG_FAMILY)).build();
091    admin.createTable(tableDescriptor, Bytes.toBytes("a"), Bytes.toBytes("z"), REGION_NUM);
092    UTIL.waitTableAvailable(tableDescriptor.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(tableDescriptor.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<RegionInfo> regionsOfTable = admin.getRegions(TableName.valueOf(tableName));
116    for (RegionInfo 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}