1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.PosixParser;
27 import org.apache.commons.cli.ParseException;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.rest.filter.GzipFilter;
34 import org.apache.hadoop.hbase.security.User;
35 import org.apache.hadoop.hbase.util.InfoServer;
36 import org.apache.hadoop.hbase.util.Strings;
37 import org.apache.hadoop.hbase.util.VersionInfo;
38 import org.apache.hadoop.net.DNS;
39
40 import java.util.List;
41 import java.util.ArrayList;
42
43 import org.mortbay.jetty.Connector;
44 import org.mortbay.jetty.Server;
45 import org.mortbay.jetty.nio.SelectChannelConnector;
46 import org.mortbay.jetty.servlet.Context;
47 import org.mortbay.jetty.servlet.ServletHolder;
48 import org.mortbay.thread.QueuedThreadPool;
49
50 import com.sun.jersey.spi.container.servlet.ServletContainer;
51
52
53
54
55
56
57
58
59
60
61 public class Main implements Constants {
62
63 private static void printUsageAndExit(Options options, int exitCode) {
64 HelpFormatter formatter = new HelpFormatter();
65 formatter.printHelp("bin/hbase rest start", "", options,
66 "\nTo run the REST server as a daemon, execute " +
67 "bin/hbase-daemon.sh start|stop rest [--infoport <port>] [-p <port>] [-ro]\n", true);
68 System.exit(exitCode);
69 }
70
71
72
73
74
75
76 public static void main(String[] args) throws Exception {
77 Log LOG = LogFactory.getLog("RESTServer");
78
79 VersionInfo.logVersion();
80 Configuration conf = HBaseConfiguration.create();
81
82 if (User.isSecurityEnabled() && User.isHBaseSecurityEnabled(conf)) {
83 String machineName = Strings.domainNamePointerToHostName(
84 DNS.getDefaultHost(conf.get("hbase.rest.dns.interface", "default"),
85 conf.get("hbase.rest.dns.nameserver", "default")));
86 User.login(conf, "hbase.rest.keytab.file", "hbase.rest.kerberos.principal",
87 machineName);
88 }
89
90 RESTServlet servlet = RESTServlet.getInstance(conf);
91
92 Options options = new Options();
93 options.addOption("p", "port", true, "Port to bind to [default: 8080]");
94 options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
95 "method requests [default: false]");
96 options.addOption(null, "infoport", true, "Port for web UI");
97
98 CommandLine commandLine = null;
99 try {
100 commandLine = new PosixParser().parse(options, args);
101 } catch (ParseException e) {
102 LOG.error("Could not parse: ", e);
103 printUsageAndExit(options, -1);
104 }
105
106
107 if (commandLine != null && commandLine.hasOption("port")) {
108 String val = commandLine.getOptionValue("port");
109 servlet.getConfiguration()
110 .setInt("hbase.rest.port", Integer.valueOf(val));
111 LOG.debug("port set to " + val);
112 }
113
114
115 if (commandLine != null && commandLine.hasOption("readonly")) {
116 servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
117 LOG.debug("readonly set to true");
118 }
119
120
121 if (commandLine != null && commandLine.hasOption("infoport")) {
122 String val = commandLine.getOptionValue("infoport");
123 servlet.getConfiguration()
124 .setInt("hbase.rest.info.port", Integer.valueOf(val));
125 LOG.debug("Web UI port set to " + val);
126 }
127
128 @SuppressWarnings("unchecked")
129 List<String> remainingArgs = commandLine != null ?
130 commandLine.getArgList() : new ArrayList<String>();
131 if (remainingArgs.size() != 1) {
132 printUsageAndExit(options, 1);
133 }
134
135 String command = remainingArgs.get(0);
136 if ("start".equals(command)) {
137
138 } else if ("stop".equals(command)) {
139 System.exit(1);
140 } else {
141 printUsageAndExit(options, 1);
142 }
143
144
145 ServletHolder sh = new ServletHolder(ServletContainer.class);
146 sh.setInitParameter(
147 "com.sun.jersey.config.property.resourceConfigClass",
148 ResourceConfig.class.getCanonicalName());
149 sh.setInitParameter("com.sun.jersey.config.property.packages",
150 "jetty");
151
152
153
154 Server server = new Server();
155
156 Connector connector = new SelectChannelConnector();
157 connector.setPort(servlet.getConfiguration().getInt("hbase.rest.port", 8080));
158 connector.setHost(servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0"));
159
160 server.addConnector(connector);
161
162
163
164
165
166
167 int maxThreads = servlet.getConfiguration().getInt("hbase.rest.threads.max", 100);
168 int minThreads = servlet.getConfiguration().getInt("hbase.rest.threads.min", 2);
169 QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
170 threadPool.setMinThreads(minThreads);
171 server.setThreadPool(threadPool);
172
173 server.setSendServerVersion(false);
174 server.setSendDateHeader(false);
175 server.setStopAtShutdown(true);
176
177
178 Context context = new Context(server, "/", Context.SESSIONS);
179 context.addServlet(sh, "/*");
180 context.addFilter(GzipFilter.class, "/*", 0);
181
182
183 int port = conf.getInt("hbase.rest.info.port", 8085);
184 if (port >= 0) {
185 conf.setLong("startcode", System.currentTimeMillis());
186 String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
187 InfoServer infoServer = new InfoServer("rest", a, port, false, conf);
188 infoServer.setAttribute("hbase.conf", conf);
189 infoServer.start();
190 }
191
192
193 server.start();
194 server.join();
195 }
196 }