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    @Override protected Logger getLogger() {
50      return LOG;
51    }
52  
53    public MoveRegionsOfTableAction(long sleepTime, long maxSleepTime, TableName tableName) {
54      this.sleepTime = sleepTime;
55      this.tableName = tableName;
56      this.maxTime = maxSleepTime;
57    }
58  
59    @Override
60    public void perform() throws Exception {
61      if (sleepTime > 0) {
62        Thread.sleep(sleepTime);
63      }
64  
65      Admin admin = this.context.getHBaseIntegrationTestingUtility().getHBaseAdmin();
66      Collection<ServerName> serversList = admin.getClusterStatus().getServers();
67      ServerName[] servers = serversList.toArray(new ServerName[serversList.size()]);
68  
69      getLogger().info("Performing action: Move regions of table " + tableName);
70      List<HRegionInfo> regions = admin.getTableRegions(tableName);
71      if (regions == null || regions.isEmpty()) {
72        getLogger().info("Table " + tableName + " doesn't have regions to move");
73        return;
74      }
75  
76      Collections.shuffle(regions);
77  
78      long start = System.currentTimeMillis();
79      for (HRegionInfo regionInfo:regions) {
80  
81        // Don't try the move if we're stopping
82        if (context.isStopping()) {
83          return;
84        }
85  
86        try {
87          String destServerName =
88            servers[RandomUtils.nextInt(servers.length)].getServerName();
89          getLogger().debug("Moving " + regionInfo.getRegionNameAsString() + " to " + destServerName);
90          admin.move(regionInfo.getEncodedNameAsBytes(), Bytes.toBytes(destServerName));
91        } catch (Exception ex) {
92          getLogger().warn("Move failed, might be caused by other chaos: " + ex.getMessage());
93        }
94        if (sleepTime > 0) {
95          Thread.sleep(sleepTime);
96        }
97  
98        // put a limit on max num regions. Otherwise, this won't finish
99        // with a sleep time of 10sec, 100 regions will finish in 16min
100       if (System.currentTimeMillis() - start > maxTime) {
101         break;
102       }
103     }
104   }
105 }