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