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