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.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 conf.setInt(HConstants.ZK_SESSION_TIMEOUT, 10 *1000);
177
178
179 LocalHBaseCluster cluster = new LocalHBaseCluster(conf, conf.getInt("hbase.masters", 1),
180 conf.getInt("hbase.regionservers", 1), LocalHMaster.class, HRegionServer.class);
181 ((LocalHMaster)cluster.getMaster(0)).setZKCluster(zooKeeperCluster);
182 cluster.startup();
183 waitOnMasterThreads(cluster);
184 } else {
185 logProcessInfo(getConf());
186 HMaster master = HMaster.constructMaster(masterClass, conf);
187 if (master.isStopped()) {
188 LOG.info("Won't bring the Master up as a shutdown is requested");
189 return 1;
190 }
191 master.start();
192 master.join();
193 if(master.isAborted())
194 throw new RuntimeException("HMaster Aborted");
195 }
196 } catch (Throwable t) {
197 LOG.error("Master exiting", t);
198 return 1;
199 }
200 return 0;
201 }
202
203 private int stopMaster() {
204 HBaseAdmin adm = null;
205 try {
206 Configuration conf = getConf();
207
208 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
209 adm = new HBaseAdmin(getConf());
210 } catch (MasterNotRunningException e) {
211 LOG.error("Master not running");
212 return 1;
213 } catch (ZooKeeperConnectionException e) {
214 LOG.error("ZooKeeper not available");
215 return 1;
216 } catch (IOException e) {
217 LOG.error("Got IOException: " +e.getMessage(), e);
218 return 1;
219 }
220 try {
221 adm.shutdown();
222 } catch (Throwable t) {
223 LOG.error("Failed to stop master", t);
224 return 1;
225 }
226 return 0;
227 }
228
229 private void waitOnMasterThreads(LocalHBaseCluster cluster) throws InterruptedException{
230 List<JVMClusterUtil.MasterThread> masters = cluster.getMasters();
231 List<JVMClusterUtil.RegionServerThread> regionservers = cluster.getRegionServers();
232
233 if (masters != null) {
234 for (JVMClusterUtil.MasterThread t : masters) {
235 t.join();
236 if(t.getMaster().isAborted()) {
237 closeAllRegionServerThreads(regionservers);
238 throw new RuntimeException("HMaster Aborted");
239 }
240 }
241 }
242 }
243
244 private static void closeAllRegionServerThreads(List<JVMClusterUtil.RegionServerThread> regionservers) {
245 for(JVMClusterUtil.RegionServerThread t : regionservers){
246 t.getRegionServer().stop("HMaster Aborted; Bringing down regions servers");
247 }
248 }
249
250
251
252
253 public static class LocalHMaster extends HMaster {
254 private MiniZooKeeperCluster zkcluster = null;
255
256 public LocalHMaster(Configuration conf)
257 throws IOException, KeeperException, InterruptedException {
258 super(conf);
259 }
260
261 @Override
262 public void run() {
263 super.run();
264 if (this.zkcluster != null) {
265 try {
266 this.zkcluster.shutdown();
267 } catch (IOException e) {
268 e.printStackTrace();
269 }
270 }
271 }
272
273 void setZKCluster(final MiniZooKeeperCluster zkcluster) {
274 this.zkcluster = zkcluster;
275 }
276 }
277 }