View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  
27  import org.apache.commons.cli.CommandLine;
28  import org.apache.commons.cli.HelpFormatter;
29  import org.apache.commons.cli.Options;
30  import org.apache.commons.cli.ParseException;
31  import org.apache.commons.cli.PosixParser;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.hadoop.classification.InterfaceAudience;
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.hbase.HBaseConfiguration;
37  import org.apache.hadoop.hbase.rest.filter.AuthFilter;
38  import org.apache.hadoop.hbase.rest.filter.GzipFilter;
39  import org.apache.hadoop.hbase.security.User;
40  import org.apache.hadoop.hbase.util.InfoServer;
41  import org.apache.hadoop.hbase.util.Strings;
42  import org.apache.hadoop.hbase.util.VersionInfo;
43  import org.apache.hadoop.net.DNS;
44  import org.apache.hadoop.security.SecurityUtil;
45  import org.apache.hadoop.security.UserGroupInformation;
46  import org.mortbay.jetty.Connector;
47  import org.mortbay.jetty.Server;
48  import org.mortbay.jetty.nio.SelectChannelConnector;
49  import org.mortbay.jetty.security.SslSelectChannelConnector;
50  import org.mortbay.jetty.servlet.Context;
51  import org.mortbay.jetty.servlet.FilterHolder;
52  import org.mortbay.jetty.servlet.ServletHolder;
53  import org.mortbay.thread.QueuedThreadPool;
54  
55  import com.google.common.base.Preconditions;
56  import com.sun.jersey.api.json.JSONConfiguration;
57  import com.sun.jersey.spi.container.servlet.ServletContainer;
58  
59  /**
60   * Main class for launching REST gateway as a servlet hosted by Jetty.
61   * <p>
62   * The following options are supported:
63   * <ul>
64   * <li>-p --port : service port</li>
65   * <li>-ro --readonly : server mode</li>
66   * </ul>
67   */
68  @InterfaceAudience.Private
69  public class RESTServer implements Constants {
70  
71    private static void printUsageAndExit(Options options, int exitCode) {
72      HelpFormatter formatter = new HelpFormatter();
73      formatter.printHelp("bin/hbase rest start", "", options,
74        "\nTo run the REST server as a daemon, execute " +
75        "bin/hbase-daemon.sh start|stop rest [--infoport <port>] [-p <port>] [-ro]\n", true);
76      System.exit(exitCode);
77    }
78  
79    /**
80     * The main method for the HBase rest server.
81     * @param args command-line arguments
82     * @throws Exception exception
83     */
84    public static void main(String[] args) throws Exception {
85      Log LOG = LogFactory.getLog("RESTServer");
86  
87      VersionInfo.logVersion();
88      FilterHolder authFilter = null;
89      UserGroupInformation realUser = null;
90      Configuration conf = HBaseConfiguration.create();
91      Class<? extends ServletContainer> containerClass = ServletContainer.class;
92  
93      // login the server principal (if using secure Hadoop)
94      if (User.isSecurityEnabled() && User.isHBaseSecurityEnabled(conf)) {
95        String machineName = Strings.domainNamePointerToHostName(
96          DNS.getDefaultHost(conf.get(REST_DNS_INTERFACE, "default"),
97            conf.get(REST_DNS_NAMESERVER, "default")));
98        String keytabFilename = conf.get(REST_KEYTAB_FILE);
99        Preconditions.checkArgument(keytabFilename != null && !keytabFilename.isEmpty(),
100         REST_KEYTAB_FILE + " should be set if security is enabled");
101       String principalConfig = conf.get(REST_KERBEROS_PRINCIPAL);
102       Preconditions.checkArgument(principalConfig != null && !principalConfig.isEmpty(),
103         REST_KERBEROS_PRINCIPAL + " should be set if security is enabled");
104       User.login(conf, REST_KEYTAB_FILE, REST_KERBEROS_PRINCIPAL, machineName);
105       realUser = User.getCurrent().getUGI();
106       if (conf.get(REST_AUTHENTICATION_TYPE) != null) {
107         containerClass = RESTServletContainer.class;
108         authFilter = new FilterHolder();
109         authFilter.setClassName(AuthFilter.class.getName());
110         authFilter.setName("AuthenticationFilter");
111       }
112     }
113 
114     RESTServlet servlet = RESTServlet.getInstance(conf, realUser);
115 
116     Options options = new Options();
117     options.addOption("p", "port", true, "Port to bind to [default: 8080]");
118     options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
119       "method requests [default: false]");
120     options.addOption(null, "infoport", true, "Port for web UI");
121 
122     CommandLine commandLine = null;
123     try {
124       commandLine = new PosixParser().parse(options, args);
125     } catch (ParseException e) {
126       LOG.error("Could not parse: ", e);
127       printUsageAndExit(options, -1);
128     }
129 
130     // check for user-defined port setting, if so override the conf
131     if (commandLine != null && commandLine.hasOption("port")) {
132       String val = commandLine.getOptionValue("port");
133       servlet.getConfiguration()
134           .setInt("hbase.rest.port", Integer.valueOf(val));
135       LOG.debug("port set to " + val);
136     }
137 
138     // check if server should only process GET requests, if so override the conf
139     if (commandLine != null && commandLine.hasOption("readonly")) {
140       servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
141       LOG.debug("readonly set to true");
142     }
143 
144     // check for user-defined info server port setting, if so override the conf
145     if (commandLine != null && commandLine.hasOption("infoport")) {
146       String val = commandLine.getOptionValue("infoport");
147       servlet.getConfiguration()
148           .setInt("hbase.rest.info.port", Integer.valueOf(val));
149       LOG.debug("Web UI port set to " + val);
150     }
151 
152     @SuppressWarnings("unchecked")
153     List<String> remainingArgs = commandLine != null ?
154         commandLine.getArgList() : new ArrayList<String>();
155     if (remainingArgs.size() != 1) {
156       printUsageAndExit(options, 1);
157     }
158 
159     String command = remainingArgs.get(0);
160     if ("start".equals(command)) {
161       // continue and start container
162     } else if ("stop".equals(command)) {
163       System.exit(1);
164     } else {
165       printUsageAndExit(options, 1);
166     }
167 
168     // set up the Jersey servlet container for Jetty
169     ServletHolder sh = new ServletHolder(containerClass);
170     sh.setInitParameter(
171       "com.sun.jersey.config.property.resourceConfigClass",
172       ResourceConfig.class.getCanonicalName());
173     sh.setInitParameter("com.sun.jersey.config.property.packages",
174       "jetty");
175     // The servlet holder below is instantiated to only handle the case
176     // of the /status/cluster returning arrays of nodes (live/dead). Without
177     // this servlet holder, the problem is that the node arrays in the response
178     // are collapsed to single nodes. We want to be able to treat the
179     // node lists as POJO in the response to /status/cluster servlet call,
180     // but not change the behavior for any of the other servlets
181     // Hence we don't use the servlet holder for all servlets / paths
182     ServletHolder shPojoMap = new ServletHolder(containerClass);
183     @SuppressWarnings("unchecked")
184     Map<String, String> shInitMap = sh.getInitParameters();
185     for (Entry<String, String> e : shInitMap.entrySet()) {
186       shPojoMap.setInitParameter(e.getKey(), e.getValue());
187     }
188     shPojoMap.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
189 
190     // set up Jetty and run the embedded server
191 
192     Server server = new Server();
193 
194     Connector connector = new SelectChannelConnector();
195     if(conf.getBoolean(REST_SSL_ENABLED, false)) {
196       SslSelectChannelConnector sslConnector = new SslSelectChannelConnector();
197       String keystore = conf.get(REST_SSL_KEYSTORE_STORE);
198       String password = conf.get(REST_SSL_KEYSTORE_PASSWORD);
199       String keyPassword = conf.get(REST_SSL_KEYSTORE_KEYPASSWORD, password);
200       sslConnector.setKeystore(keystore);
201       sslConnector.setPassword(password);
202       sslConnector.setKeyPassword(keyPassword);
203       connector = sslConnector;
204     }
205     connector.setPort(servlet.getConfiguration().getInt("hbase.rest.port", 8080));
206     connector.setHost(servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0"));
207 
208     server.addConnector(connector);
209 
210     // Set the default max thread number to 100 to limit
211     // the number of concurrent requests so that REST server doesn't OOM easily.
212     // Jetty set the default max thread number to 250, if we don't set it.
213     //
214     // Our default min thread number 2 is the same as that used by Jetty.
215     int maxThreads = servlet.getConfiguration().getInt("hbase.rest.threads.max", 100);
216     int minThreads = servlet.getConfiguration().getInt("hbase.rest.threads.min", 2);
217     QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads);
218     threadPool.setMinThreads(minThreads);
219     server.setThreadPool(threadPool);
220 
221     server.setSendServerVersion(false);
222     server.setSendDateHeader(false);
223     server.setStopAtShutdown(true);
224       // set up context
225     Context context = new Context(server, "/", Context.SESSIONS);
226     context.addServlet(shPojoMap, "/status/cluster");
227     context.addServlet(sh, "/*");
228     if (authFilter != null) {
229       context.addFilter(authFilter, "/*", 1);
230     }
231 
232     context.addFilter(GzipFilter.class, "/*", 0);
233 
234     // Put up info server.
235     int port = conf.getInt("hbase.rest.info.port", 8085);
236     if (port >= 0) {
237       conf.setLong("startcode", System.currentTimeMillis());
238       String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
239       InfoServer infoServer = new InfoServer("rest", a, port, false, conf);
240       infoServer.setAttribute("hbase.conf", conf);
241       infoServer.start();
242     }
243 
244     // start server
245     server.start();
246     server.join();
247   }
248 }