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.chaos.monkies.PolicyBasedChaosMonkey;
27  import org.apache.hadoop.hbase.client.HBaseAdmin;
28  import org.apache.hadoop.hbase.util.Bytes;
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 byte[] tableNameBytes;
35    private final int majorRatio;
36    private final long sleepTime;
37    private final String tableName;
38  
39    public CompactRandomRegionOfTableAction(
40        String tableName, float majorRatio) {
41      this(-1, tableName, majorRatio);
42    }
43  
44    public CompactRandomRegionOfTableAction(
45        int sleepTime, String tableName, float majorRatio) {
46      this.tableNameBytes = Bytes.toBytes(tableName);
47      this.majorRatio = (int) (100 * majorRatio);
48      this.sleepTime = sleepTime;
49      this.tableName = tableName;
50    }
51  
52    @Override
53    public void perform() throws Exception {
54      HBaseTestingUtility util = context.getHBaseIntegrationTestingUtility();
55      HBaseAdmin admin = util.getHBaseAdmin();
56      boolean major = RandomUtils.nextInt(100) < majorRatio;
57  
58      LOG.info("Performing action: Compact random region of table "
59        + tableName + ", major=" + major);
60      List<HRegionInfo> regions = admin.getTableRegions(tableNameBytes);
61      if (regions == null || regions.isEmpty()) {
62        LOG.info("Table " + tableName + " doesn't have regions to compact");
63        return;
64      }
65  
66      HRegionInfo region = PolicyBasedChaosMonkey.selectRandomItem(
67        regions.toArray(new HRegionInfo[regions.size()]));
68  
69      try {
70        if (major) {
71          LOG.debug("Major compacting region " + region.getRegionNameAsString());
72          admin.majorCompact(region.getRegionName());
73        } else {
74          LOG.debug("Compacting region " + region.getRegionNameAsString());
75          admin.compact(region.getRegionName());
76        }
77      } catch (Exception ex) {
78        LOG.warn("Compaction failed, might be caused by other chaos: " + ex.getMessage());
79      }
80      if (sleepTime > 0) {
81        Thread.sleep(sleepTime);
82      }
83    }
84  }