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