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