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.master.balancer.BalancerConditionalsTestUtil.validateAssertionsWithRetries; 021 022import java.util.List; 023import org.apache.hadoop.hbase.HBaseClassTestRule; 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.HConstants; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Admin; 028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 029import org.apache.hadoop.hbase.client.Connection; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.client.TableDescriptor; 032import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 033import org.apache.hadoop.hbase.testclassification.LargeTests; 034import org.apache.hadoop.hbase.testclassification.MasterTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil; 037import org.junit.After; 038import org.junit.Before; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045@Category({ LargeTests.class, MasterTests.class }) 046public class TestReplicaDistributionBalancerConditional { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestReplicaDistributionBalancerConditional.class); 051 052 private static final Logger LOG = 053 LoggerFactory.getLogger(TestReplicaDistributionBalancerConditional.class); 054 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 055 private static final int REPLICAS = 3; 056 private static final int NUM_SERVERS = REPLICAS; 057 private static final int REGIONS_PER_SERVER = 5; 058 059 @Before 060 public void setUp() throws Exception { 061 DistributeReplicasTestConditional 062 .enableConditionalReplicaDistributionForTest(TEST_UTIL.getConfiguration()); 063 TEST_UTIL.getConfiguration() 064 .setBoolean(ServerRegionReplicaUtil.REGION_REPLICA_REPLICATION_CONF_KEY, true); 065 TEST_UTIL.getConfiguration().setLong(HConstants.HBASE_BALANCER_PERIOD, 1000L); 066 TEST_UTIL.getConfiguration().setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true); 067 068 // turn off replica cost functions 069 TEST_UTIL.getConfiguration() 070 .setLong("hbase.master.balancer.stochastic.regionReplicaRackCostKey", 0); 071 TEST_UTIL.getConfiguration() 072 .setLong("hbase.master.balancer.stochastic.regionReplicaHostCostKey", 0); 073 074 TEST_UTIL.startMiniCluster(NUM_SERVERS); 075 } 076 077 @After 078 public void tearDown() throws Exception { 079 TEST_UTIL.shutdownMiniCluster(); 080 } 081 082 @Test 083 public void testReplicaDistribution() throws Exception { 084 Connection connection = TEST_UTIL.getConnection(); 085 Admin admin = connection.getAdmin(); 086 087 // Create a "replicated_table" with region replicas 088 TableName replicatedTableName = TableName.valueOf("replicated_table"); 089 TableDescriptor replicatedTableDescriptor = 090 TableDescriptorBuilder.newBuilder(replicatedTableName) 091 .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("0")).build()) 092 .setRegionReplication(REPLICAS).build(); 093 admin.createTable(replicatedTableDescriptor, 094 BalancerConditionalsTestUtil.generateSplits(REGIONS_PER_SERVER * NUM_SERVERS)); 095 096 // Pause the balancer 097 admin.balancerSwitch(false, true); 098 099 // Collect all region replicas and place them on one RegionServer 100 List<RegionInfo> allRegions = admin.getRegions(replicatedTableName); 101 String targetServer = 102 TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName().getServerName(); 103 104 for (RegionInfo region : allRegions) { 105 admin.move(region.getEncodedNameAsBytes(), Bytes.toBytes(targetServer)); 106 } 107 108 BalancerConditionalsTestUtil.printRegionLocations(TEST_UTIL.getConnection()); 109 validateAssertionsWithRetries(TEST_UTIL, false, () -> BalancerConditionalsTestUtil 110 .validateReplicaDistribution(connection, replicatedTableName, false)); 111 112 // Unpause the balancer and trigger balancing 113 admin.balancerSwitch(true, true); 114 admin.balance(); 115 116 validateAssertionsWithRetries(TEST_UTIL, true, () -> BalancerConditionalsTestUtil 117 .validateReplicaDistribution(connection, replicatedTableName, true)); 118 BalancerConditionalsTestUtil.printRegionLocations(TEST_UTIL.getConnection()); 119 } 120}