View Javadoc

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