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  
30  /**
31   * Region that queues a compaction of a random region from the table.
32   */
33  public class CompactRandomRegionOfTableAction extends Action {
34    private final int majorRatio;
35    private final long sleepTime;
36    private final TableName tableName;
37  
38    public CompactRandomRegionOfTableAction(
39        TableName tableName, float majorRatio) {
40      this(-1, tableName, majorRatio);
41    }
42  
43    public CompactRandomRegionOfTableAction(
44        int sleepTime, TableName tableName, float majorRatio) {
45      this.majorRatio = (int) (100 * majorRatio);
46      this.sleepTime = sleepTime;
47      this.tableName = tableName;
48    }
49  
50    @Override
51    public void perform() throws Exception {
52      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
53      Admin admin = util.getHBaseAdmin();
54      boolean major = RandomUtils.nextInt(100) < majorRatio;
55  
56      LOG.info("Performing action: Compact random region of table "
57        + tableName + ", major=" + major);
58      List<HRegionInfo> regions = admin.getTableRegions(tableName);
59      if (regions == null || regions.isEmpty()) {
60        LOG.info("Table " + tableName + " doesn't have regions to compact");
61        return;
62      }
63  
64      HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
65        regions.toArray(new HRegionInfo[regions.size()]));
66  
67      try {
68        if (major) {
69          LOG.debug("Major compacting region " + region.getRegionNameAsString());
70          admin.majorCompactRegion(region.getRegionName());
71        } else {
72          LOG.debug("Compacting region " + region.getRegionNameAsString());
73          admin.compactRegion(region.getRegionName());
74        }
75      } catch (Exception ex) {
76        LOG.warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
77      }
78      if (sleepTime > 0) {
79        Thread.sleep(sleepTime);
80      }
81    }
82  }