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