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.security.PrivilegedExceptionAction;
24 import java.util.List;
25 import com.google.common.base.Throwables;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.hbase.master.HMaster;
32 import org.apache.hadoop.hbase.regionserver.HRegionServer;
33 import org.apache.hadoop.hdfs.DistributedFileSystem;
34 import org.apache.hadoop.security.UserGroupInformation;
35
36
37
38
39 public class JVMClusterUtil {
40 private static final Log LOG = LogFactory.getLog(JVMClusterUtil.class);
41
42 private final static UserGroupInformation UGI;
43 static {
44 try {
45 UGI = UserGroupInformation.getCurrentUser();
46 } catch (IOException ioe) {
47 throw new RuntimeException("Error getting current user", ioe);
48 }
49 }
50
51 private static UserGroupInformation getDifferentUser(final Configuration c,
52 int index)
53 throws IOException {
54 FileSystem currentfs = FileSystem.get(c);
55 if (!(currentfs instanceof DistributedFileSystem)) return UGI;
56
57
58 String username = UGI.getShortUserName() + ".hrs." + index;
59 return UserGroupInformation.createUserForTesting(username,
60 new String[]{"supergroup"});
61 }
62
63
64
65
66 public static class RegionServerThread extends Thread {
67 private final HRegionServer regionServer;
68 private final UserGroupInformation serverUser;
69
70 public RegionServerThread(final HRegionServer r,
71 final UserGroupInformation ugi, final int index) {
72 super(r, "RegionServer:" + index);
73 this.regionServer = r;
74 this.serverUser = ugi;
75 }
76
77
78 public HRegionServer getRegionServer() {
79 return this.regionServer;
80 }
81
82
83
84
85
86 public void waitForServerOnline() {
87
88
89
90
91 while (!this.regionServer.isOnline() &&
92 !this.regionServer.isStopRequested()) {
93 try {
94 Thread.sleep(1000);
95 } catch (InterruptedException e) {
96
97 }
98 }
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111 public static JVMClusterUtil.RegionServerThread createRegionServerThread(final Configuration c,
112 final Class<? extends HRegionServer> hrsc, final int index)
113 throws IOException {
114 HRegionServer server;
115 UserGroupInformation serverUser;
116 if (UserGroupInformation.isSecurityEnabled()) {
117 serverUser = HRegionServer.loginFromKeytab(c);
118 } else {
119 serverUser = getDifferentUser(c, index);
120 }
121
122 try {
123 server = serverUser.doAs( new PrivilegedExceptionAction<HRegionServer>(){
124 public HRegionServer run() throws Exception {
125 return hrsc.getConstructor(Configuration.class).newInstance(c);
126 }
127 });
128 } catch (Exception e) {
129 IOException ioe = new IOException();
130 ioe.initCause(e);
131 throw ioe;
132 }
133 return new JVMClusterUtil.RegionServerThread(server, serverUser, index);
134 }
135
136
137
138
139
140
141
142 public static String startup(final HMaster m,
143 final List<JVMClusterUtil.RegionServerThread> regionservers) {
144 if (m != null) m.start();
145 if (regionservers != null) {
146 for (JVMClusterUtil.RegionServerThread t: regionservers) {
147 t.start();
148 }
149 }
150 return m == null? null: m.getMasterAddress().toString();
151 }
152
153
154
155
156
157 public static void shutdown(final HMaster master,
158 final List<RegionServerThread> regionservers) {
159 LOG.debug("Shutting down HBase Cluster");
160 if (master != null) {
161 master.shutdown();
162 }
163
164
165 for(Thread t: regionservers) {
166 if (t.isAlive()) {
167 try {
168 t.join();
169 } catch (InterruptedException e) {
170
171 }
172 }
173 }
174 if (master != null) {
175 while (master.isAlive()) {
176 try {
177
178
179
180 Threads.threadDumpingIsAlive(master);
181 } catch(InterruptedException e) {
182
183 }
184 }
185 }
186 LOG.info("Shutdown " +
187 ((regionservers != null)? master.getName(): "0 masters") +
188 " " + regionservers.size() + " region server(s)");
189 }
190 }