View Javadoc

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