1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
86
87 if (System.currentTimeMillis() - start > maxTime) {
88 break;
89 }
90 }
91 }
92 }