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 */
018
019package org.apache.hadoop.hbase.chaos.actions;
020
021import java.util.List;
022
023import org.apache.commons.lang3.RandomUtils;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HRegionInfo;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
028import org.apache.hadoop.hbase.client.Admin;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032/**
033 * Region that queues a compaction of a random region from the table.
034 */
035public class CompactRandomRegionOfTableAction extends Action {
036  private final int majorRatio;
037  private final long sleepTime;
038  private final TableName tableName;
039  private static final Logger LOG =
040      LoggerFactory.getLogger(CompactRandomRegionOfTableAction.class);
041
042  public CompactRandomRegionOfTableAction(
043      TableName tableName, float majorRatio) {
044    this(-1, tableName, majorRatio);
045  }
046
047  public CompactRandomRegionOfTableAction(
048      int sleepTime, TableName tableName, float majorRatio) {
049    this.majorRatio = (int) (100 * majorRatio);
050    this.sleepTime = sleepTime;
051    this.tableName = tableName;
052  }
053
054  @Override
055  public void perform() throws Exception {
056    HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
057    Admin admin = util.getAdmin();
058    boolean major = RandomUtils.nextInt(0, 100) < majorRatio;
059
060    LOG.info("Performing action: Compact random region of table "
061      + tableName + ", major=" + major);
062    List<HRegionInfo> regions = admin.getTableRegions(tableName);
063    if (regions == null || regions.isEmpty()) {
064      LOG.info("Table " + tableName + " doesn't have regions to compact");
065      return;
066    }
067
068    HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
069      regions.toArray(new HRegionInfo[regions.size()]));
070
071    try {
072      if (major) {
073        LOG.debug("Major compacting region " + region.getRegionNameAsString());
074        admin.majorCompactRegion(region.getRegionName());
075      } else {
076        LOG.debug("Compacting region " + region.getRegionNameAsString());
077        admin.compactRegion(region.getRegionName());
078      }
079    } catch (Exception ex) {
080      LOG.warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
081    }
082    if (sleepTime > 0) {
083      Thread.sleep(sleepTime);
084    }
085  }
086}