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