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;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.client.Admin;
022import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
023import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
024import org.apache.hadoop.hbase.client.TableDescriptor;
025import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
026import org.apache.hadoop.hbase.testclassification.IntegrationTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
029import org.apache.hadoop.hbase.util.RegionSplitter;
030import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
031import org.junit.jupiter.api.AfterEach;
032import org.junit.jupiter.api.BeforeEach;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * An integration test to detect regressions in HBASE-7220. Create a table with many regions and
040 * verify it completes within a reasonable amount of time.
041 * @see <a href="https://issues.apache.org/jira/browse/HBASE-7220">HBASE-7220</a>
042 */
043@Tag(IntegrationTests.TAG)
044public class IntegrationTestManyRegions {
045  private static final String CLASS_NAME = IntegrationTestManyRegions.class.getSimpleName();
046
047  protected static final Logger LOG = LoggerFactory.getLogger(IntegrationTestManyRegions.class);
048  protected static final TableName TABLE_NAME = TableName.valueOf(CLASS_NAME);
049  protected static final String REGION_COUNT_KEY = String.format("hbase.%s.regions", CLASS_NAME);
050  protected static final String REGIONSERVER_COUNT_KEY =
051    String.format("hbase.%s.regionServers", CLASS_NAME);
052  protected static final String TIMEOUT_MINUTES_KEY =
053    String.format("hbase.%s.timeoutMinutes", CLASS_NAME);
054
055  protected static final IntegrationTestingUtility UTIL = new IntegrationTestingUtility();
056
057  protected static final int DEFAULT_REGION_COUNT = 1000;
058  protected static final int REGION_COUNT =
059    UTIL.getConfiguration().getInt(REGION_COUNT_KEY, DEFAULT_REGION_COUNT);
060  protected static final int DEFAULT_REGIONSERVER_COUNT = 1;
061  protected static final int REGION_SERVER_COUNT =
062    UTIL.getConfiguration().getInt(REGIONSERVER_COUNT_KEY, DEFAULT_REGIONSERVER_COUNT);
063  // running on laptop, consistently takes about 2.5 minutes.
064  // A timeout of 5 minutes should be reasonably safe.
065  protected static final int DEFAULT_TIMEOUT_MINUTES = 5;
066  protected static final int TIMEOUT_MINUTES =
067    UTIL.getConfiguration().getInt(TIMEOUT_MINUTES_KEY, DEFAULT_TIMEOUT_MINUTES);
068
069  private Admin admin;
070
071  @BeforeEach
072  public void setUp() throws Exception {
073    LOG.info(String.format("Initializing cluster with %d region servers.", REGION_SERVER_COUNT));
074    UTIL.initializeCluster(REGION_SERVER_COUNT);
075    LOG.info("Cluster initialized");
076
077    admin = UTIL.getAdmin();
078    if (admin.tableExists(TABLE_NAME)) {
079      LOG.info(String.format("Deleting existing table %s.", TABLE_NAME));
080      if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
081      admin.deleteTable(TABLE_NAME);
082      LOG.info(String.format("Existing table %s deleted.", TABLE_NAME));
083    }
084    LOG.info("Cluster ready");
085  }
086
087  @AfterEach
088  public void tearDown() throws IOException {
089    LOG.info("Cleaning up after test.");
090    if (admin.tableExists(TABLE_NAME)) {
091      if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
092      admin.deleteTable(TABLE_NAME);
093    }
094    LOG.info("Restoring cluster.");
095    UTIL.restoreCluster();
096    LOG.info("Cluster restored.");
097  }
098
099  @Test
100  public void testCreateTableWithRegions() throws Exception {
101    ColumnFamilyDescriptor columnFamilyDescriptor =
102      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf")).build();
103    TableDescriptor tableDescriptor =
104      TableDescriptorBuilder.newBuilder(TABLE_NAME).setColumnFamily(columnFamilyDescriptor).build();
105
106    SplitAlgorithm algo = new RegionSplitter.HexStringSplit();
107    byte[][] splits = algo.split(REGION_COUNT);
108
109    LOG.info(String.format("Creating table %s with %d splits.", TABLE_NAME, REGION_COUNT));
110    long startTime = EnvironmentEdgeManager.currentTime();
111    try {
112      admin.createTable(tableDescriptor, splits);
113      LOG.info(String.format("Pre-split table created successfully in %dms.",
114        (EnvironmentEdgeManager.currentTime() - startTime)));
115    } catch (IOException e) {
116      LOG.error("Failed to create table", e);
117    }
118  }
119}