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