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 while (!this.regionServer.isOnline() &&
66 !this.regionServer.isStopped()) {
67 try {
68 Thread.sleep(1000);
69 } catch (InterruptedException e) {
70
71 }
72 }
73 }
74 }
75
76
77
78
79
80
81
82
83
84
85 public static JVMClusterUtil.RegionServerThread createRegionServerThread(
86 final Configuration c, final Class<? extends HRegionServer> hrsc,
87 final int index)
88 throws IOException {
89 HRegionServer server;
90 try {
91 server = hrsc.getConstructor(Configuration.class).newInstance(c);
92 } catch (InvocationTargetException ite) {
93 Throwable target = ite.getTargetException();
94 throw new RuntimeException("Failed construction of RegionServer: " +
95 hrsc.toString() + ((target.getCause() != null)?
96 target.getCause().getMessage(): ""), target);
97 } catch (Exception e) {
98 IOException ioe = new IOException();
99 ioe.initCause(e);
100 throw ioe;
101 }
102 return new JVMClusterUtil.RegionServerThread(server, index);
103 }
104
105
106
107
108
109 public static class MasterThread extends Thread {
110 private final HMaster master;
111
112 public MasterThread(final HMaster m, final int index) {
113 super(m, "Master:" + index + ";" + m.getServerName());
114 this.master = m;
115 }
116
117
118 public HMaster getMaster() {
119 return this.master;
120 }
121
122
123
124
125
126 public void waitForServerOnline() {
127
128
129 while (!this.master.isMasterRunning() && !this.master.isStopped()) {
130 try {
131 Thread.sleep(1000);
132 } catch (InterruptedException e) {
133
134 }
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148 public static JVMClusterUtil.MasterThread createMasterThread(
149 final Configuration c, final Class<? extends HMaster> hmc,
150 final int index)
151 throws IOException {
152 HMaster server;
153 try {
154 server = hmc.getConstructor(Configuration.class).newInstance(c);
155 } catch (InvocationTargetException ite) {
156 Throwable target = ite.getTargetException();
157 throw new RuntimeException("Failed construction of Master: " +
158 hmc.toString() + ((target.getCause() != null)?
159 target.getCause().getMessage(): ""), target);
160 } catch (Exception e) {
161 IOException ioe = new IOException();
162 ioe.initCause(e);
163 throw ioe;
164 }
165 return new JVMClusterUtil.MasterThread(server, index);
166 }
167
168
169
170
171
172
173
174
175 public static String startup(final List<JVMClusterUtil.MasterThread> masters,
176 final List<JVMClusterUtil.RegionServerThread> regionservers) throws IOException {
177 if (masters != null) {
178 for (JVMClusterUtil.MasterThread t : masters) {
179 t.start();
180 }
181 }
182 if (regionservers != null) {
183 for (JVMClusterUtil.RegionServerThread t: regionservers) {
184 HRegionServer hrs = t.getRegionServer();
185 ShutdownHook.install(hrs.getConfiguration(), FileSystem.get(hrs
186 .getConfiguration()), hrs, t);
187 t.start();
188 }
189 }
190 if (masters == null || masters.isEmpty()) {
191 return null;
192 }
193
194 while (true) {
195 for (JVMClusterUtil.MasterThread t : masters) {
196 if (t.master.isActiveMaster()) {
197 return t.master.getServerName().toString();
198 }
199 }
200 try {
201 Thread.sleep(1000);
202 } catch(InterruptedException e) {
203
204 }
205 }
206 }
207
208
209
210
211
212 public static void shutdown(final List<MasterThread> masters,
213 final List<RegionServerThread> regionservers) {
214 LOG.debug("Shutting down HBase Cluster");
215 if (masters != null) {
216 for (JVMClusterUtil.MasterThread t : masters) {
217 if (t.master.isActiveMaster()) {
218 t.master.shutdown();
219 } else {
220 t.master.stopMaster();
221 }
222 }
223 }
224
225
226 for(RegionServerThread t: regionservers) {
227 if (t.isAlive()) {
228 try {
229 t.getRegionServer().stop("Shutdown requested");
230 t.join();
231 } catch (InterruptedException e) {
232
233 }
234 }
235 }
236 if (masters != null) {
237 for (JVMClusterUtil.MasterThread t : masters) {
238 while (t.master.isAlive()) {
239 try {
240
241
242
243 Threads.threadDumpingIsAlive(t.master.getThread());
244 } catch(InterruptedException e) {
245
246 }
247 }
248 }
249 }
250 LOG.info("Shutdown of " +
251 ((masters != null) ? masters.size() : "0") + " master(s) and " +
252 ((regionservers != null) ? regionservers.size() : "0") +
253 " regionserver(s) complete");
254 }
255 }