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.io.IOException;
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Queue;
26
27 import org.apache.commons.lang.math.RandomUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.ServerName;
31 import org.apache.hadoop.hbase.chaos.monkies.PolicyBasedChaosMonkey;
32
33
34
35
36
37
38
39 public class RollingBatchRestartRsAction extends BatchRestartRsAction {
40 private static final Log LOG = LogFactory.getLog(RollingBatchRestartRsAction.class);
41 protected int maxDeadServers;
42
43 public RollingBatchRestartRsAction(long sleepTime, float ratio) {
44 this(sleepTime, ratio, 5);
45 }
46
47 public RollingBatchRestartRsAction(long sleepTime, float ratio, int maxDeadServers) {
48 super(sleepTime, ratio);
49 this.maxDeadServers = maxDeadServers;
50 }
51
52 enum KillOrStart {
53 KILL,
54 START
55 }
56
57 @Override
58 public void perform() throws Exception {
59 LOG.info(String.format("Performing action: Rolling batch restarting %d%% of region servers",
60 (int)(ratio * 100)));
61 List<ServerName> selectedServers = selectServers();
62
63 Queue<ServerName> serversToBeKilled = new LinkedList<ServerName>(selectedServers);
64 Queue<ServerName> deadServers = new LinkedList<ServerName>();
65
66
67 while ((!serversToBeKilled.isEmpty() || !deadServers.isEmpty()) && !context.isStopping()) {
68 KillOrStart action = KillOrStart.KILL;
69
70 if (serversToBeKilled.isEmpty()) {
71 action = KillOrStart.START;
72 } else if (deadServers.isEmpty()) {
73 action = KillOrStart.KILL;
74 } else if (deadServers.size() >= maxDeadServers) {
75
76 action = KillOrStart.START;
77 } else {
78
79 action = RandomUtils.nextBoolean() ? KillOrStart.KILL : KillOrStart.START;
80 }
81
82 ServerName server;
83
84 switch (action) {
85 case KILL:
86 server = serversToBeKilled.remove();
87 try {
88 killRs(server);
89 } catch (org.apache.hadoop.util.Shell.ExitCodeException e) {
90
91
92 LOG.info("Problem killing but presume successful; code=" + e.getExitCode(), e);
93 }
94 deadServers.add(server);
95 break;
96 case START:
97 try {
98 server = deadServers.remove();
99 startRs(server);
100 } catch (org.apache.hadoop.util.Shell.ExitCodeException e) {
101
102
103 LOG.info("Problem starting, will retry; code=" + e.getExitCode(), e);
104 }
105 break;
106 }
107
108 sleep(RandomUtils.nextInt((int)sleepTime));
109 }
110 }
111
112 protected List<ServerName> selectServers() throws IOException {
113 return PolicyBasedChaosMonkey.selectRandomItems(getCurrentServers(), ratio);
114 }
115
116
117
118
119
120
121 public static void main(final String[] args) throws Exception {
122 RollingBatchRestartRsAction action = new RollingBatchRestartRsAction(1, 1.0f) {
123 private int invocations = 0;
124 @Override
125 protected ServerName[] getCurrentServers() throws IOException {
126 final int count = 4;
127 List<ServerName> serverNames = new ArrayList<ServerName>(count);
128 for (int i = 0; i < 4; i++) {
129 serverNames.add(ServerName.valueOf(i + ".example.org", i, i));
130 }
131 return serverNames.toArray(new ServerName[serverNames.size()]);
132 }
133
134 @Override
135 protected void killRs(ServerName server) throws IOException {
136 LOG.info("Killed " + server);
137 if (this.invocations++ % 3 == 0) {
138 throw new org.apache.hadoop.util.Shell.ExitCodeException(-1, "Failed");
139 }
140 }
141
142 @Override
143 protected void startRs(ServerName server) throws IOException {
144 LOG.info("Started " + server);
145 if (this.invocations++ % 3 == 0) {
146 throw new org.apache.hadoop.util.Shell.ExitCodeException(-1, "Failed");
147 }
148 }
149 };
150
151 action.perform();
152 }
153 }