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