View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.util.List;
22  
23  import org.apache.commons.lang.math.RandomUtils;
24  import org.apache.hadoop.hbase.HBaseTestingUtility;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Region that queues a compaction of a random region from the table.
34   */
35  public class CompactRandomRegionOfTableAction extends Action {
36    private final int majorRatio;
37    private final long sleepTime;
38    private final TableName tableName;
39    private static final Logger LOG =
40        LoggerFactory.getLogger(CompactRandomRegionOfTableAction.class);
41  
42    public CompactRandomRegionOfTableAction(
43        TableName tableName, float majorRatio) {
44      this(-1, tableName, majorRatio);
45    }
46  
47    public CompactRandomRegionOfTableAction(
48        int sleepTime, TableName tableName, float majorRatio) {
49      this.majorRatio = (int) (100 * majorRatio);
50      this.sleepTime = sleepTime;
51      this.tableName = tableName;
52    }
53  
54    @Override
55    public void perform() throws Exception {
56      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
57      Admin admin = util.getHBaseAdmin();
58      boolean major = RandomUtils.nextInt(100) < majorRatio;
59  
60      LOG.info("Performing action: Compact random region of table "
61        + tableName + ", major=" + major);
62      List<HRegionInfo> regions = admin.getTableRegions(tableName);
63      if (regions == null || regions.isEmpty()) {
64        LOG.info("Table " + tableName + " doesn't have regions to compact");
65        return;
66      }
67  
68      HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
69        regions.toArray(new HRegionInfo[regions.size()]));
70  
71      try {
72        if (major) {
73          LOG.debug("Major compacting region " + region.getRegionNameAsString());
74          admin.majorCompactRegion(region.getRegionName());
75        } else {
76          LOG.debug("Compacting region " + region.getRegionNameAsString());
77          admin.compactRegion(region.getRegionName());
78        }
79      } catch (Exception ex) {
80        LOG.warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
81      }
82      if (sleepTime > 0) {
83        Thread.sleep(sleepTime);
84      }
85    }
86  }