001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.thrift2;
019
020import static org.apache.hadoop.hbase.thrift.Constants.READONLY_OPTION;
021import static org.apache.hadoop.hbase.thrift.Constants.THRIFT_READONLY_ENABLED;
022import static org.apache.hadoop.hbase.thrift.Constants.THRIFT_READONLY_ENABLED_DEFAULT;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseConfiguration;
027import org.apache.hadoop.hbase.HBaseInterfaceAudience;
028import org.apache.hadoop.hbase.security.UserProvider;
029import org.apache.hadoop.hbase.thrift.HBaseServiceHandler;
030import org.apache.hadoop.hbase.thrift.HbaseHandlerMetricsProxy;
031import org.apache.hadoop.hbase.thrift.ThriftMetrics;
032import org.apache.hadoop.hbase.thrift2.generated.THBaseService;
033import org.apache.hadoop.util.Shell;
034import org.apache.hadoop.util.ToolRunner;
035import org.apache.thrift.TProcessor;
036import org.apache.yetus.audience.InterfaceAudience;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
041import org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter;
042import org.apache.hbase.thirdparty.org.apache.commons.cli.Options;
043
044/**
045 * ThriftServer - this class starts up a Thrift server which implements the HBase API specified in
046 * the HbaseClient.thrift IDL file.
047 */
048@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS",
049    justification = "Change the name will be an incompatible change, will do it later")
050@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
051@SuppressWarnings({ "rawtypes", "unchecked" })
052public class ThriftServer extends org.apache.hadoop.hbase.thrift.ThriftServer {
053  private static final Logger log = LoggerFactory.getLogger(ThriftServer.class);
054
055  public ThriftServer(Configuration conf) {
056    super(conf);
057  }
058
059  @Override
060  protected void printUsageAndExit(Options options, int exitCode) throws Shell.ExitCodeException {
061    HelpFormatter formatter = new HelpFormatter();
062    formatter.printHelp("Thrift", null, options,
063      "To start the Thrift server run 'hbase-daemon.sh start thrift2' or " + "'hbase thrift2'\n"
064        + "To shutdown the thrift server run 'hbase-daemon.sh stop thrift2' or"
065        + " send a kill signal to the thrift server pid",
066      true);
067    throw new Shell.ExitCodeException(exitCode, "");
068  }
069
070  @Override
071  protected HBaseServiceHandler createHandler(Configuration conf, UserProvider userProvider)
072    throws IOException {
073    return new ThriftHBaseServiceHandler(conf, userProvider);
074  }
075
076  @Override
077  protected ThriftMetrics createThriftMetrics(Configuration conf) {
078    return new ThriftMetrics(conf, ThriftMetrics.ThriftServerType.TWO);
079  }
080
081  @Override
082  protected TProcessor createProcessor() {
083    return new THBaseService.Processor<>(HbaseHandlerMetricsProxy
084      .newInstance((THBaseService.Iface) hbaseServiceHandler, metrics, conf));
085  }
086
087  @Override
088  protected void addOptions(Options options) {
089    super.addOptions(options);
090    options.addOption("ro", READONLY_OPTION, false,
091      "Respond only to read method requests [default: false]");
092  }
093
094  @Override
095  protected void parseCommandLine(CommandLine cmd, Options options) throws Shell.ExitCodeException {
096    super.parseCommandLine(cmd, options);
097    boolean readOnly = THRIFT_READONLY_ENABLED_DEFAULT;
098    if (cmd.hasOption(READONLY_OPTION)) {
099      readOnly = true;
100    }
101    conf.setBoolean(THRIFT_READONLY_ENABLED, readOnly);
102  }
103
104  /**
105   * Start up the Thrift2 server.
106   */
107  public static void main(String[] args) throws Exception {
108    final Configuration conf = HBaseConfiguration.create();
109    // for now, only time we return is on an argument error.
110    final int status = ToolRunner.run(conf, new ThriftServer(conf), args);
111    System.exit(status);
112  }
113
114}