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 java.util.ArrayList;
021import java.util.List;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.StartTestingClusterOption;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
027import org.apache.hadoop.hbase.client.Put;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.MiscTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.AfterClass;
036import org.junit.Assert;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042@Category({ MiscTests.class, MediumTests.class })
043public class TestRegionPlansWithThrottle {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestRegionPlansWithThrottle.class);
048
049  private static HMaster hMaster;
050
051  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
052
053  @BeforeClass
054  public static void setUp() throws Exception {
055    UTIL.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(2).build());
056    hMaster = UTIL.getMiniHBaseCluster().getMaster();
057  }
058
059  @AfterClass
060  public static void tearDown() throws Exception {
061    UTIL.shutdownMiniCluster();
062  }
063
064  @Test
065  public void testExecuteRegionPlansWithThrottling() throws Exception {
066    final TableName tableName = TableName.valueOf("testExecuteRegionPlansWithThrottling");
067
068    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName)
069      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("cf"))).build();
070
071    UTIL.getAdmin().createTable(tableDescriptor);
072    Table table = UTIL.getConnection().getTable(tableName);
073
074    List<Put> puts = new ArrayList<>();
075    for (int i = 0; i < 100; i++) {
076      Put put = new Put(Bytes.toBytes("row" + i));
077      byte[] q = Bytes.toBytes("q1");
078      byte[] v = Bytes.toBytes("v" + i);
079      put.addColumn(Bytes.toBytes("cf"), q, v);
080      puts.add(put);
081    }
082    table.put(puts);
083    UTIL.getAdmin().flush(tableName);
084    UTIL.getAdmin().split(tableName, Bytes.toBytes("v5"));
085
086    List<RegionPlan> plans = new ArrayList<>();
087    List<RegionInfo> regionInfos = UTIL.getAdmin().getRegions(tableName);
088    for (RegionInfo regionInfo : regionInfos) {
089      plans
090        .add(new RegionPlan(regionInfo, UTIL.getHBaseCluster().getRegionServer(0).getServerName(),
091          UTIL.getHBaseCluster().getRegionServer(1).getServerName()));
092    }
093    List<RegionPlan> successPlans = hMaster.executeRegionPlansWithThrottling(plans);
094    Assert.assertEquals(regionInfos.size(), successPlans.size());
095  }
096
097}