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.regionserver;
020
021import org.apache.hadoop.hbase.trace.TraceUtil;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.LocalHBaseCluster;
028import org.apache.hadoop.hbase.util.ServerCommandLine;
029
030/**
031 * Class responsible for parsing the command line and starting the
032 * RegionServer.
033 */
034@InterfaceAudience.Private
035public class HRegionServerCommandLine extends ServerCommandLine {
036  private static final Logger LOG = LoggerFactory.getLogger(HRegionServerCommandLine.class);
037
038  private final Class<? extends HRegionServer> regionServerClass;
039
040  private static final String USAGE =
041    "Usage: HRegionServer [-D conf.param=value] start";
042
043  public HRegionServerCommandLine(Class<? extends HRegionServer> clazz) {
044    this.regionServerClass = clazz;
045  }
046
047  @Override
048  protected String getUsage() {
049    return USAGE;
050  }
051
052  private int start() throws Exception {
053    Configuration conf = getConf();
054    TraceUtil.initTracer(conf);
055    try {
056      // If 'local', don't start a region server here. Defer to
057      // LocalHBaseCluster. It manages 'local' clusters.
058      if (LocalHBaseCluster.isLocal(conf)) {
059        LOG.warn("Not starting a distinct region server because "
060            + HConstants.CLUSTER_DISTRIBUTED + " is false");
061      } else {
062        logProcessInfo(getConf());
063        HRegionServer hrs = HRegionServer.constructRegionServer(regionServerClass, conf);
064        hrs.start();
065        hrs.join();
066        if (hrs.isAborted()) {
067          throw new RuntimeException("HRegionServer Aborted");
068        }
069      }
070    } catch (Throwable t) {
071      LOG.error("Region server exiting", t);
072      return 1;
073    }
074    return 0;
075  }
076
077  @Override
078  public int run(String args[]) throws Exception {
079    if (args.length != 1) {
080      usage(null);
081      return 1;
082    }
083
084    String cmd = args[0];
085
086    if ("start".equals(cmd)) {
087      return start();
088    } else if ("stop".equals(cmd)) {
089      System.err.println(
090        "To shutdown the regionserver run " +
091        "hbase-daemon.sh stop regionserver or send a kill signal to " +
092        "the regionserver pid");
093      return 1;
094    } else {
095      usage("Unknown command: " + args[0]);
096      return 1;
097    }
098  }
099}