1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.util;
21
22 import java.io.IOException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.hbase.master.HMaster;
31 import org.apache.hadoop.hbase.regionserver.HRegionServer;
32 import org.apache.hadoop.hbase.regionserver.ShutdownHook;
33
34
35
36
37 public class JVMClusterUtil {
38 private static final Log LOG = LogFactory.getLog(JVMClusterUtil.class);
39
40
41
42
43 public static class RegionServerThread extends Thread {
44 private final HRegionServer regionServer;
45
46 public RegionServerThread(final HRegionServer r, final int index) {
47 super(r, "RegionServer:" + index + ";" + r.getServerName());
48 this.regionServer = r;
49 }
50
51
52 public HRegionServer getRegionServer() {
53 return this.regionServer;
54 }
55
56
57
58
59
60 public void waitForServerOnline() {
61
62
63
64
65 regionServer.waitForServerOnline();
66 }
67 }
68
69
70
71
72
73
74
75
76
77
78 public static JVMClusterUtil.RegionServerThread createRegionServerThread(
79 final Configuration c, final Class<? extends HRegionServer> hrsc,
80 final int index)
81 throws IOException {
82 HRegionServer server;
83 try {
84 server = hrsc.getConstructor(Configuration.class).newInstance(c);
85 } catch (InvocationTargetException ite) {
86 Throwable target = ite.getTargetException();
87 throw new RuntimeException("Failed construction of RegionServer: " +
88 hrsc.toString() + ((target.getCause() != null)?
89 target.getCause().getMessage(): ""), target);
90 } catch (Exception e) {
91 IOException ioe = new IOException();
92 ioe.initCause(e);
93 throw ioe;
94 }
95 return new JVMClusterUtil.RegionServerThread(server, index);
96 }
97
98
99
100
101
102 public static class MasterThread extends Thread {
103 private final HMaster master;
104
105 public MasterThread(final HMaster m, final int index) {
106 super(m, "Master:" + index + ";" + m.getServerName());
107 this.master = m;
108 }
109
110
111 public HMaster getMaster() {
112 return this.master;
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125 public static JVMClusterUtil.MasterThread createMasterThread(
126 final Configuration c, final Class<? extends HMaster> hmc,
127 final int index)
128 throws IOException {
129 HMaster server;
130 try {
131 server = hmc.getConstructor(Configuration.class).newInstance(c);
132 } catch (InvocationTargetException ite) {
133 Throwable target = ite.getTargetException();
134 throw new RuntimeException("Failed construction of Master: " +
135 hmc.toString() + ((target.getCause() != null)?
136 target.getCause().getMessage(): ""), target);
137 } catch (Exception e) {
138 IOException ioe = new IOException();
139 ioe.initCause(e);
140 throw ioe;
141 }
142 return new JVMClusterUtil.MasterThread(server, index);
143 }
144
145 private static JVMClusterUtil.MasterThread findActiveMaster(
146 List<JVMClusterUtil.MasterThread> masters) {
147 for (JVMClusterUtil.MasterThread t : masters) {
148 if (t.master.isActiveMaster()) {
149 return t;
150 }
151 }
152
153 return null;
154 }
155
156
157
158
159
160
161
162
163 public static String startup(final List<JVMClusterUtil.MasterThread> masters,
164 final List<JVMClusterUtil.RegionServerThread> regionservers) throws IOException {
165
166 if (masters == null || masters.isEmpty()) {
167 return null;
168 }
169
170 for (JVMClusterUtil.MasterThread t : masters) {
171 t.start();
172 }
173
174
175
176
177 long startTime = System.currentTimeMillis();
178 while (findActiveMaster(masters) == null) {
179 try {
180 Thread.sleep(100);
181 } catch (InterruptedException ignored) {
182 }
183 if (System.currentTimeMillis() > startTime + 30000) {
184 throw new RuntimeException("Master not active after 30 seconds");
185 }
186 }
187
188 if (regionservers != null) {
189 for (JVMClusterUtil.RegionServerThread t: regionservers) {
190 HRegionServer hrs = t.getRegionServer();
191 ShutdownHook.install(hrs.getConfiguration(), FileSystem.get(hrs
192 .getConfiguration()), hrs, t);
193 t.start();
194 }
195 }
196
197
198
199 startTime = System.currentTimeMillis();
200 while (true) {
201 JVMClusterUtil.MasterThread t = findActiveMaster(masters);
202 if (t != null && t.master.isInitialized()) {
203 return t.master.getServerName().toString();
204 }
205 if (System.currentTimeMillis() > startTime + 200000) {
206 throw new RuntimeException("Master not initialized after 200 seconds");
207 }
208 try {
209 Thread.sleep(100);
210 } catch (InterruptedException ignored) {
211
212 }
213 }
214 }
215
216
217
218
219
220 public static void shutdown(final List<MasterThread> masters,
221 final List<RegionServerThread> regionservers) {
222 LOG.debug("Shutting down HBase Cluster");
223 if (masters != null) {
224
225 JVMClusterUtil.MasterThread activeMaster = null;
226 for (JVMClusterUtil.MasterThread t : masters) {
227 if (!t.master.isActiveMaster()) {
228 t.master.stopMaster();
229 } else {
230 activeMaster = t;
231 }
232 }
233
234 if (activeMaster != null) activeMaster.master.shutdown();
235 }
236
237
238 for(RegionServerThread t: regionservers) {
239 if (t.isAlive()) {
240 try {
241 t.getRegionServer().stop("Shutdown requested");
242 t.join();
243 } catch (InterruptedException e) {
244
245 }
246 }
247 }
248 if (masters != null) {
249 for (JVMClusterUtil.MasterThread t : masters) {
250 while (t.master.isAlive()) {
251 try {
252
253
254
255 Threads.threadDumpingIsAlive(t.master.getThread());
256 } catch(InterruptedException e) {
257
258 }
259 }
260 }
261 }
262 LOG.info("Shutdown of " +
263 ((masters != null) ? masters.size() : "0") + " master(s) and " +
264 ((regionservers != null) ? regionservers.size() : "0") +
265 " regionserver(s) complete");
266 }
267 }