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.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.commons.lang.math.RandomUtils;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.client.HBaseAdmin;
29  import org.apache.hadoop.hbase.util.Bytes;
30  
31  /**
32  * Action that tries to move every region of a table.
33  */
34  public class MoveRegionsOfTableAction extends Action {
35    private final long sleepTime;
36    private final byte[] tableNameBytes;
37    private final String tableName;
38    private final long maxTime;
39  
40    public MoveRegionsOfTableAction(String tableName) {
41      this(-1, tableName);
42    }
43  
44    public MoveRegionsOfTableAction(long sleepTime, String tableName) {
45      this.sleepTime = sleepTime;
46      this.tableNameBytes = Bytes.toBytes(tableName);
47      this.tableName = tableName;
48      this.maxTime = 10 * 60 * 1000; // 10 min default
49    }
50  
51    @Override
52    public void perform() throws Exception {
53      if (sleepTime > 0) {
54        Thread.sleep(sleepTime);
55      }
56  
57      HBaseAdmin admin = this.context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
58      Collection<ServerName> serversList = admin.getClusterStatus().getServers();
59      ServerName[] servers = serversList.toArray(new ServerName[serversList.size()]);
60  
61      LOG.info("Performing action: Move regions of table " + tableName);
62      List<HRegionInfo> regions = admin.getTableRegions(tableNameBytes);
63      if (regions == null || regions.isEmpty()) {
64        LOG.info("Table " + tableName + " doesn't have regions to move");
65        return;
66      }
67  
68      Collections.shuffle(regions);
69  
70      long start = System.currentTimeMillis();
71      for (HRegionInfo regionInfo:regions) {
72        try {
73          String destServerName =
74            servers[RandomUtils.nextInt(servers.length)].getServerName();
75          LOG.debug("Moving " + regionInfo.getRegionNameAsString() + " to " + destServerName);
76          admin.move(regionInfo.getEncodedNameAsBytes(), Bytes.toBytes(destServerName));
77        } catch (Exception ex) {
78          LOG.warn("Move failed, might be caused by other chaos: " + ex.getMessage());
79        }
80        if (sleepTime > 0) {
81          Thread.sleep(sleepTime);
82        }
83  
84        // put a limit on max num regions. Otherwise, this won't finish
85        // with a sleep time of 10sec, 100 regions will finish in 16min
86        if (System.currentTimeMillis() - start > maxTime) {
87          break;
88        }
89      }
90    }
91  }