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