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.thrift;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import org.apache.commons.cli.CommandLine;
25  import org.apache.commons.cli.CommandLineParser;
26  import org.apache.commons.cli.HelpFormatter;
27  import org.apache.commons.cli.Options;
28  import org.apache.commons.cli.PosixParser;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.hbase.HBaseConfiguration;
34  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
35  import org.apache.hadoop.hbase.http.InfoServer;
36  import org.apache.hadoop.hbase.thrift.ThriftServerRunner.ImplType;
37  import org.apache.hadoop.hbase.util.VersionInfo;
38  import org.apache.hadoop.util.Shell.ExitCodeException;
39  
40  /**
41   * ThriftServer- this class starts up a Thrift server which implements the
42   * Hbase API specified in the Hbase.thrift IDL file. The server runs in an
43   * independent process.
44   */
45  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
46  public class ThriftServer {
47  
48    private static final Log LOG = LogFactory.getLog(ThriftServer.class);
49  
50    private static final String MIN_WORKERS_OPTION = "minWorkers";
51    private static final String MAX_WORKERS_OPTION = "workers";
52    private static final String MAX_QUEUE_SIZE_OPTION = "queue";
53    private static final String KEEP_ALIVE_SEC_OPTION = "keepAliveSec";
54    static final String BIND_OPTION = "bind";
55    static final String COMPACT_OPTION = "compact";
56    static final String FRAMED_OPTION = "framed";
57    static final String PORT_OPTION = "port";
58  
59    private static final String DEFAULT_BIND_ADDR = "0.0.0.0";
60    private static final int DEFAULT_LISTEN_PORT = 9090;
61  
62    private Configuration conf;
63    ThriftServerRunner serverRunner;
64  
65    private InfoServer infoServer;
66  
67    //
68    // Main program and support routines
69    //
70  
71    public ThriftServer(Configuration conf) {
72      this.conf = HBaseConfiguration.create(conf);
73    }
74  
75    private static void printUsageAndExit(Options options, int exitCode)
76        throws ExitCodeException {
77      HelpFormatter formatter = new HelpFormatter();
78      formatter.printHelp("Thrift", null, options,
79          "To start the Thrift server run 'bin/hbase-daemon.sh start thrift'\n" +
80          "To shutdown the thrift server run 'bin/hbase-daemon.sh stop " +
81          "thrift' or send a kill signal to the thrift server pid",
82          true);
83      throw new ExitCodeException(exitCode, "");
84    }
85  
86    /**
87     * Start up or shuts down the Thrift server, depending on the arguments.
88     * @param args
89     */
90     void doMain(final String[] args) throws Exception {
91       processOptions(args);
92  
93       serverRunner = new ThriftServerRunner(conf);
94  
95       // Put up info server.
96       int port = conf.getInt("hbase.thrift.info.port", 9095);
97       if (port >= 0) {
98         conf.setLong("startcode", System.currentTimeMillis());
99         String a = conf.get("hbase.thrift.info.bindAddress", "0.0.0.0");
100        infoServer = new InfoServer("thrift", a, port, false, conf);
101        infoServer.setAttribute("hbase.conf", conf);
102        infoServer.start();
103      }
104      serverRunner.run();
105   }
106 
107   /**
108    * Parse the command line options to set parameters the conf.
109    */
110   private void processOptions(final String[] args) throws Exception {
111     Options options = new Options();
112     options.addOption("b", BIND_OPTION, true, "Address to bind " +
113         "the Thrift server to. [default: " + DEFAULT_BIND_ADDR + "]");
114     options.addOption("p", PORT_OPTION, true, "Port to bind to [default: " +
115         DEFAULT_LISTEN_PORT + "]");
116     options.addOption("f", FRAMED_OPTION, false, "Use framed transport");
117     options.addOption("c", COMPACT_OPTION, false, "Use the compact protocol");
118     options.addOption("h", "help", false, "Print help information");
119     options.addOption(null, "infoport", true, "Port for web UI");
120 
121     options.addOption("m", MIN_WORKERS_OPTION, true,
122         "The minimum number of worker threads for " +
123         ImplType.THREAD_POOL.simpleClassName());
124 
125     options.addOption("w", MAX_WORKERS_OPTION, true,
126         "The maximum number of worker threads for " +
127         ImplType.THREAD_POOL.simpleClassName());
128 
129     options.addOption("q", MAX_QUEUE_SIZE_OPTION, true,
130         "The maximum number of queued requests in " +
131         ImplType.THREAD_POOL.simpleClassName());
132 
133     options.addOption("k", KEEP_ALIVE_SEC_OPTION, true,
134         "The amount of time in secods to keep a thread alive when idle in " +
135         ImplType.THREAD_POOL.simpleClassName());
136 
137     options.addOptionGroup(ImplType.createOptionGroup());
138 
139     CommandLineParser parser = new PosixParser();
140     CommandLine cmd = parser.parse(options, args);
141 
142     // This is so complicated to please both bin/hbase and bin/hbase-daemon.
143     // hbase-daemon provides "start" and "stop" arguments
144     // hbase should print the help if no argument is provided
145     List<String> commandLine = Arrays.asList(args);
146     boolean stop = commandLine.contains("stop");
147     boolean start = commandLine.contains("start");
148     boolean invalidStartStop = (start && stop) || (!start && !stop);
149     if (cmd.hasOption("help") || invalidStartStop) {
150       if (invalidStartStop) {
151         LOG.error("Exactly one of 'start' and 'stop' has to be specified");
152       }
153       printUsageAndExit(options, 1);
154     }
155 
156     // Get port to bind to
157     try {
158       if (cmd.hasOption(PORT_OPTION)) {
159         int listenPort = Integer.parseInt(cmd.getOptionValue(PORT_OPTION));
160         conf.setInt(ThriftServerRunner.PORT_CONF_KEY, listenPort);
161       }
162     } catch (NumberFormatException e) {
163       LOG.error("Could not parse the value provided for the port option", e);
164       printUsageAndExit(options, -1);
165     }
166 
167     // check for user-defined info server port setting, if so override the conf
168     try {
169       if (cmd.hasOption("infoport")) {
170         String val = cmd.getOptionValue("infoport");
171         conf.setInt("hbase.thrift.info.port", Integer.valueOf(val));
172         LOG.debug("Web UI port set to " + val);
173       }
174     } catch (NumberFormatException e) {
175       LOG.error("Could not parse the value provided for the infoport option", e);
176       printUsageAndExit(options, -1);
177     }
178 
179     // Make optional changes to the configuration based on command-line options
180     optionToConf(cmd, MIN_WORKERS_OPTION,
181         conf, TBoundedThreadPoolServer.MIN_WORKER_THREADS_CONF_KEY);
182     optionToConf(cmd, MAX_WORKERS_OPTION,
183         conf, TBoundedThreadPoolServer.MAX_WORKER_THREADS_CONF_KEY);
184     optionToConf(cmd, MAX_QUEUE_SIZE_OPTION,
185         conf, TBoundedThreadPoolServer.MAX_QUEUED_REQUESTS_CONF_KEY);
186     optionToConf(cmd, KEEP_ALIVE_SEC_OPTION,
187         conf, TBoundedThreadPoolServer.THREAD_KEEP_ALIVE_TIME_SEC_CONF_KEY);
188 
189     // Set general thrift server options
190     boolean compact = cmd.hasOption(COMPACT_OPTION) ||
191       conf.getBoolean(ThriftServerRunner.COMPACT_CONF_KEY, false);
192     conf.setBoolean(ThriftServerRunner.COMPACT_CONF_KEY, compact);
193     boolean framed = cmd.hasOption(FRAMED_OPTION) ||
194       conf.getBoolean(ThriftServerRunner.FRAMED_CONF_KEY, false);
195     conf.setBoolean(ThriftServerRunner.FRAMED_CONF_KEY, framed);
196     if (cmd.hasOption(BIND_OPTION)) {
197       conf.set(
198           ThriftServerRunner.BIND_CONF_KEY, cmd.getOptionValue(BIND_OPTION));
199     }
200 
201     ImplType.setServerImpl(cmd, conf);
202   }
203 
204   public void stop() {
205     if (this.infoServer != null) {
206       LOG.info("Stopping infoServer");
207       try {
208         this.infoServer.stop();
209       } catch (Exception ex) {
210         ex.printStackTrace();
211       }
212     }
213     serverRunner.shutdown();
214   }
215 
216   private static void optionToConf(CommandLine cmd, String option,
217       Configuration conf, String destConfKey) {
218     if (cmd.hasOption(option)) {
219       String value = cmd.getOptionValue(option);
220       LOG.info("Set configuration key:" + destConfKey + " value:" + value);
221       conf.set(destConfKey, value);
222     }
223   }
224 
225   /**
226    * @param args
227    * @throws Exception
228    */
229   public static void main(String [] args) throws Exception {
230     VersionInfo.logVersion();
231     try {
232       new ThriftServer(HBaseConfiguration.create()).doMain(args);
233     } catch (ExitCodeException ex) {
234       System.exit(ex.getExitCode());
235     }
236   }
237 }