1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.List;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.GnuParser;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.hbase.MasterNotRunningException;
35 import org.apache.hadoop.hbase.ZNodeClearer;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.LocalHBaseCluster;
38 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
39 import org.apache.hadoop.hbase.client.HBaseAdmin;
40 import org.apache.hadoop.hbase.regionserver.HRegionServer;
41 import org.apache.hadoop.hbase.util.JVMClusterUtil;
42 import org.apache.hadoop.hbase.util.ServerCommandLine;
43 import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
44 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
45 import org.apache.zookeeper.KeeperException;
46
47 @InterfaceAudience.Private
48 public class HMasterCommandLine extends ServerCommandLine {
49 private static final Log LOG = LogFactory.getLog(HMasterCommandLine.class);
50
51 private static final String USAGE =
52 "Usage: Master [opts] start|stop|clear\n" +
53 " start Start Master. If local mode, start Master and RegionServer in same JVM\n" +
54 " stop Start cluster shutdown; Master signals RegionServer shutdown\n" +
55 " clear Delete the master znode in ZooKeeper after a master crashes\n "+
56 " where [opts] are:\n" +
57 " --minRegionServers=<servers> Minimum RegionServers needed to host user tables.\n" +
58 " --localRegionServers=<servers> " +
59 "RegionServers to start in master process when in standalone mode.\n" +
60 " --masters=<servers> Masters to start in this process.\n" +
61 " --backup Master should start in backup mode";
62
63 private final Class<? extends HMaster> masterClass;
64
65 public HMasterCommandLine(Class<? extends HMaster> masterClass) {
66 this.masterClass = masterClass;
67 }
68
69 protected String getUsage() {
70 return USAGE;
71 }
72
73
74 public int run(String args[]) throws Exception {
75 Options opt = new Options();
76 opt.addOption("localRegionServers", true,
77 "RegionServers to start in master process when running standalone");
78 opt.addOption("masters", true, "Masters to start in this process");
79 opt.addOption("minRegionServers", true, "Minimum RegionServers needed to host user tables");
80 opt.addOption("backup", false, "Do not try to become HMaster until the primary fails");
81
82 CommandLine cmd;
83 try {
84 cmd = new GnuParser().parse(opt, args);
85 } catch (ParseException e) {
86 LOG.error("Could not parse: ", e);
87 usage(null);
88 return 1;
89 }
90
91
92 if (cmd.hasOption("minRegionServers")) {
93 String val = cmd.getOptionValue("minRegionServers");
94 getConf().setInt("hbase.regions.server.count.min",
95 Integer.valueOf(val));
96 LOG.debug("minRegionServers set to " + val);
97 }
98
99
100 if (cmd.hasOption("minServers")) {
101 String val = cmd.getOptionValue("minServers");
102 getConf().setInt("hbase.regions.server.count.min",
103 Integer.valueOf(val));
104 LOG.debug("minServers set to " + val);
105 }
106
107
108 if (cmd.hasOption("backup")) {
109 getConf().setBoolean(HConstants.MASTER_TYPE_BACKUP, true);
110 }
111
112
113
114 if (cmd.hasOption("localRegionServers")) {
115 String val = cmd.getOptionValue("localRegionServers");
116 getConf().setInt("hbase.regionservers", Integer.valueOf(val));
117 LOG.debug("localRegionServers set to " + val);
118 }
119
120 if (cmd.hasOption("masters")) {
121 String val = cmd.getOptionValue("masters");
122 getConf().setInt("hbase.masters", Integer.valueOf(val));
123 LOG.debug("masters set to " + val);
124 }
125
126 List<String> remainingArgs = cmd.getArgList();
127 if (remainingArgs.size() != 1) {
128 usage(null);
129 return 1;
130 }
131
132 String command = remainingArgs.get(0);
133
134 if ("start".equals(command)) {
135 return startMaster();
136 } else if ("stop".equals(command)) {
137 return stopMaster();
138 } else if ("clear".equals(command)) {
139 return (ZNodeClearer.clear(getConf()) ? 0 : 1);
140 } else {
141 usage("Invalid command: " + command);
142 return 1;
143 }
144 }
145
146 private int startMaster() {
147 Configuration conf = getConf();
148 try {
149
150
151 if (LocalHBaseCluster.isLocal(conf)) {
152 final MiniZooKeeperCluster zooKeeperCluster = new MiniZooKeeperCluster(conf);
153 File zkDataPath = new File(conf.get(HConstants.ZOOKEEPER_DATA_DIR));
154 int zkClientPort = conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, 0);
155 if (zkClientPort == 0) {
156 throw new IOException("No config value for "
157 + HConstants.ZOOKEEPER_CLIENT_PORT);
158 }
159 zooKeeperCluster.setDefaultClientPort(zkClientPort);
160
161
162 ZKUtil.loginServer(conf, "hbase.zookeeper.server.keytab.file",
163 "hbase.zookeeper.server.kerberos.principal", null);
164
165 int clientPort = zooKeeperCluster.startup(zkDataPath);
166 if (clientPort != zkClientPort) {
167 String errorMsg = "Could not start ZK at requested port of " +
168 zkClientPort + ". ZK was started at port: " + clientPort +
169 ". Aborting as clients (e.g. shell) will not be able to find " +
170 "this ZK quorum.";
171 System.err.println(errorMsg);
172 throw new IOException(errorMsg);
173 }
174 conf.set(HConstants.ZOOKEEPER_CLIENT_PORT,
175 Integer.toString(clientPort));
176 int localZKClusterSessionTimeout =
177 conf.getInt(HConstants.ZK_SESSION_TIMEOUT + ".localHBaseCluster", 10*1000);
178 conf.setInt(HConstants.ZK_SESSION_TIMEOUT, localZKClusterSessionTimeout);
179
180
181 LocalHBaseCluster cluster = new LocalHBaseCluster(conf, conf.getInt("hbase.masters", 1),
182 conf.getInt("hbase.regionservers", 1), LocalHMaster.class, HRegionServer.class);
183 ((LocalHMaster)cluster.getMaster(0)).setZKCluster(zooKeeperCluster);
184 cluster.startup();
185 waitOnMasterThreads(cluster);
186 } else {
187 logProcessInfo(getConf());
188 HMaster master = HMaster.constructMaster(masterClass, conf);
189 if (master.isStopped()) {
190 LOG.info("Won't bring the Master up as a shutdown is requested");
191 return 1;
192 }
193 master.start();
194 master.join();
195 if(master.isAborted())
196 throw new RuntimeException("HMaster Aborted");
197 }
198 } catch (Throwable t) {
199 LOG.error("Master exiting", t);
200 return 1;
201 }
202 return 0;
203 }
204
205 private int stopMaster() {
206 HBaseAdmin adm = null;
207 try {
208 Configuration conf = getConf();
209
210 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
211 adm = new HBaseAdmin(getConf());
212 } catch (MasterNotRunningException e) {
213 LOG.error("Master not running");
214 return 1;
215 } catch (ZooKeeperConnectionException e) {
216 LOG.error("ZooKeeper not available");
217 return 1;
218 } catch (IOException e) {
219 LOG.error("Got IOException: " +e.getMessage(), e);
220 return 1;
221 }
222 try {
223 adm.shutdown();
224 } catch (Throwable t) {
225 LOG.error("Failed to stop master", t);
226 return 1;
227 }
228 return 0;
229 }
230
231 private void waitOnMasterThreads(LocalHBaseCluster cluster) throws InterruptedException{
232 List<JVMClusterUtil.MasterThread> masters = cluster.getMasters();
233 List<JVMClusterUtil.RegionServerThread> regionservers = cluster.getRegionServers();
234
235 if (masters != null) {
236 for (JVMClusterUtil.MasterThread t : masters) {
237 t.join();
238 if(t.getMaster().isAborted()) {
239 closeAllRegionServerThreads(regionservers);
240 throw new RuntimeException("HMaster Aborted");
241 }
242 }
243 }
244 }
245
246 private static void closeAllRegionServerThreads(List<JVMClusterUtil.RegionServerThread> regionservers) {
247 for(JVMClusterUtil.RegionServerThread t : regionservers){
248 t.getRegionServer().stop("HMaster Aborted; Bringing down regions servers");
249 }
250 }
251
252
253
254
255 public static class LocalHMaster extends HMaster {
256 private MiniZooKeeperCluster zkcluster = null;
257
258 public LocalHMaster(Configuration conf)
259 throws IOException, KeeperException, InterruptedException {
260 super(conf);
261 }
262
263 @Override
264 public void run() {
265 super.run();
266 if (this.zkcluster != null) {
267 try {
268 this.zkcluster.shutdown();
269 } catch (IOException e) {
270 e.printStackTrace();
271 }
272 }
273 }
274
275 void setZKCluster(final MiniZooKeeperCluster zkcluster) {
276 this.zkcluster = zkcluster;
277 }
278 }
279 }