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