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 static final Logger LOG = LoggerFactory.getLogger(CompactRandomRegionOfTableAction.class);
37  
38    private final int majorRatio;
39    private final long sleepTime;
40    private final TableName tableName;
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 protected Logger getLogger() {
55      return LOG;
56    }
57  
58    @Override
59    public void perform() throws Exception {
60      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
61      Admin admin = util.getHBaseAdmin();
62      boolean major = RandomUtils.nextInt(100) < majorRatio;
63  
64      getLogger().info("Performing action: Compact random region of table "
65        + tableName + ", major=" + major);
66      List<HRegionInfo> regions = admin.getTableRegions(tableName);
67      if (regions == null || regions.isEmpty()) {
68        getLogger().info("Table " + tableName + " doesn't have regions to compact");
69        return;
70      }
71  
72      HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
73        regions.toArray(new HRegionInfo[0]));
74  
75      try {
76        if (major) {
77          getLogger().debug("Major compacting region " + region.getRegionNameAsString());
78          admin.majorCompactRegion(region.getRegionName());
79        } else {
80          getLogger().debug("Compacting region " + region.getRegionNameAsString());
81          admin.compactRegion(region.getRegionName());
82        }
83      } catch (Exception ex) {
84        getLogger().warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
85      }
86      if (sleepTime > 0) {
87        Thread.sleep(sleepTime);
88      }
89    }
90  }