/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase;

import java.io.IOException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.testclassification.IntegrationTests;
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
import org.apache.hadoop.hbase.util.RegionSplitter;
import org.apache.hadoop.hbase.util.RegionSplitter.SplitAlgorithm;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An integration test to detect regressions in HBASE-7220. Create a table with many regions and
 * verify it completes within a reasonable amount of time.
 * @see <a href="https://issues.apache.org/jira/browse/HBASE-7220">HBASE-7220</a>
 */
@Tag(IntegrationTests.TAG)
public class IntegrationTestManyRegions {
  private static final String CLASS_NAME = IntegrationTestManyRegions.class.getSimpleName();

  protected static final Logger LOG = LoggerFactory.getLogger(IntegrationTestManyRegions.class);
  protected static final TableName TABLE_NAME = TableName.valueOf(CLASS_NAME);
  protected static final String REGION_COUNT_KEY = String.format("hbase.%s.regions", CLASS_NAME);
  protected static final String REGIONSERVER_COUNT_KEY =
    String.format("hbase.%s.regionServers", CLASS_NAME);
  protected static final String TIMEOUT_MINUTES_KEY =
    String.format("hbase.%s.timeoutMinutes", CLASS_NAME);

  protected static final IntegrationTestingUtility UTIL = new IntegrationTestingUtility();

  protected static final int DEFAULT_REGION_COUNT = 1000;
  protected static final int REGION_COUNT =
    UTIL.getConfiguration().getInt(REGION_COUNT_KEY, DEFAULT_REGION_COUNT);
  protected static final int DEFAULT_REGIONSERVER_COUNT = 1;
  protected static final int REGION_SERVER_COUNT =
    UTIL.getConfiguration().getInt(REGIONSERVER_COUNT_KEY, DEFAULT_REGIONSERVER_COUNT);
  // running on laptop, consistently takes about 2.5 minutes.
  // A timeout of 5 minutes should be reasonably safe.
  protected static final int DEFAULT_TIMEOUT_MINUTES = 5;
  protected static final int TIMEOUT_MINUTES =
    UTIL.getConfiguration().getInt(TIMEOUT_MINUTES_KEY, DEFAULT_TIMEOUT_MINUTES);

  private Admin admin;

  @BeforeEach
  public void setUp() throws Exception {
    LOG.info(String.format("Initializing cluster with %d region servers.", REGION_SERVER_COUNT));
    UTIL.initializeCluster(REGION_SERVER_COUNT);
    LOG.info("Cluster initialized");

    admin = UTIL.getAdmin();
    if (admin.tableExists(TABLE_NAME)) {
      LOG.info(String.format("Deleting existing table %s.", TABLE_NAME));
      if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
      admin.deleteTable(TABLE_NAME);
      LOG.info(String.format("Existing table %s deleted.", TABLE_NAME));
    }
    LOG.info("Cluster ready");
  }

  @AfterEach
  public void tearDown() throws IOException {
    LOG.info("Cleaning up after test.");
    if (admin.tableExists(TABLE_NAME)) {
      if (admin.isTableEnabled(TABLE_NAME)) admin.disableTable(TABLE_NAME);
      admin.deleteTable(TABLE_NAME);
    }
    LOG.info("Restoring cluster.");
    UTIL.restoreCluster();
    LOG.info("Cluster restored.");
  }

  @Test
  public void testCreateTableWithRegions() throws Exception {
    HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
    desc.addFamily(new HColumnDescriptor("cf"));
    SplitAlgorithm algo = new RegionSplitter.HexStringSplit();
    byte[][] splits = algo.split(REGION_COUNT);

    LOG.info(String.format("Creating table %s with %d splits.", TABLE_NAME, REGION_COUNT));
    long startTime = EnvironmentEdgeManager.currentTime();
    try {
      admin.createTable(desc, splits);
      LOG.info(String.format("Pre-split table created successfully in %dms.",
        (EnvironmentEdgeManager.currentTime() - startTime)));
    } catch (IOException e) {
      LOG.error("Failed to create table", e);
    }
  }
}
