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;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.List;
024import java.util.Map;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
032import org.apache.hadoop.hbase.master.assignment.RegionStates;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.junit.After;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Rule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.rules.TestName;
042
043/**
044 * Test balancer with disabled table
045 */
046@Category({ MasterTests.class, LargeTests.class })
047public class TestBalancer {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestBalancer.class);
052
053  private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
054
055  @Rule
056  public TestName name = new TestName();
057
058  @Before
059  public void before() throws Exception {
060    TEST_UTIL.startMiniCluster();
061  }
062
063  @After
064  public void after() throws Exception {
065    TEST_UTIL.shutdownMiniCluster();
066  }
067
068  @Test
069  public void testAssignmentsForBalancer() throws Exception {
070    final TableName tableName = TableName.valueOf(name.getMethodName());
071    TEST_UTIL.createMultiRegionTable(tableName, HConstants.CATALOG_FAMILY, 10);
072    // disable table
073    final TableName disableTableName = TableName.valueOf("testDisableTable");
074    TEST_UTIL.createMultiRegionTable(disableTableName, HConstants.CATALOG_FAMILY, 10);
075    TEST_UTIL.getAdmin().disableTable(disableTableName);
076
077    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
078    AssignmentManager assignmentManager = master.getAssignmentManager();
079    RegionStates regionStates = assignmentManager.getRegionStates();
080    ServerName sn1 = ServerName.parseServerName("asf903.gq1.ygridcore.net,52690,1517835491385");
081    regionStates.createServer(sn1);
082
083    TableStateManager tableStateManager = master.getTableStateManager();
084    ServerManager serverManager = master.getServerManager();
085    Map<TableName, Map<ServerName, List<RegionInfo>>> assignments =
086      assignmentManager.getRegionStates().getAssignmentsForBalancer(tableStateManager,
087        serverManager.getOnlineServersList());
088    assertFalse(assignments.containsKey(disableTableName));
089    assertTrue(assignments.containsKey(tableName));
090    assertFalse(assignments.get(tableName).containsKey(sn1));
091  }
092}