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