View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.LocalHBaseCluster;
29  import org.apache.hadoop.hbase.CoordinatedStateManager;
30  import org.apache.hadoop.hbase.util.ServerCommandLine;
31  
32  /**
33   * Class responsible for parsing the command line and starting the
34   * RegionServer.
35   */
36  @InterfaceAudience.Private
37  public class HRegionServerCommandLine extends ServerCommandLine {
38    private static final Log LOG = LogFactory.getLog(HRegionServerCommandLine.class);
39  
40    private final Class<? extends HRegionServer> regionServerClass;
41  
42    private static final String USAGE =
43      "Usage: HRegionServer [-D conf.param=value] start";
44  
45    public HRegionServerCommandLine(Class<? extends HRegionServer> clazz) {
46      this.regionServerClass = clazz;
47    }
48  
49    protected String getUsage() {
50      return USAGE;
51    }
52  
53    private int start() throws Exception {
54      Configuration conf = getConf();
55      CoordinatedStateManager cp = CoordinatedStateManagerFactory.getCoordinatedStateManager(conf);
56      try {
57        // If 'local', don't start a region server here. Defer to
58        // LocalHBaseCluster. It manages 'local' clusters.
59        if (LocalHBaseCluster.isLocal(conf)) {
60          LOG.warn("Not starting a distinct region server because "
61              + HConstants.CLUSTER_DISTRIBUTED + " is false");
62        } else {
63          logProcessInfo(getConf());
64          HRegionServer hrs = HRegionServer.constructRegionServer(regionServerClass, conf, cp);
65          hrs.start();
66          hrs.join();
67          if (hrs.isAborted()) {
68            throw new RuntimeException("HRegionServer Aborted");
69          }
70        }
71      } catch (Throwable t) {
72        LOG.error("Region server exiting", t);
73        return 1;
74      }
75      return 0;
76    }
77  
78    public int run(String args[]) throws Exception {
79      if (args.length != 1) {
80        usage(null);
81        return 1;
82      }
83  
84      String cmd = args[0];
85  
86      if ("start".equals(cmd)) {
87        return start();
88      } else if ("stop".equals(cmd)) {
89        System.err.println(
90          "To shutdown the regionserver run " +
91          "bin/hbase-daemon.sh stop regionserver or send a kill signal to" +
92          "the regionserver pid");
93        return 1;
94      } else {
95        usage("Unknown command: " + args[0]);
96        return 1;
97      }
98    }
99  }