Skip to content

Commit

Permalink
HBASE-2980. Refactor region server command line to a new class
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@996633 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
toddlipcon committed Sep 13, 2010
1 parent d48a3d0 commit 02cd4a9
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ Release 0.21.0 - Unreleased
HBASE-2976 Running HFile tool passing fully-qualified filename I get
'IllegalArgumentException: Wrong FS'
HBASE-2977 Refactor master command line to a new class
HBASE-2980 Refactor region server command line to a new class

NEW FEATURES
HBASE-1961 HBase EC2 scripts
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/org/apache/hadoop/hbase/master/HMaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -865,24 +865,11 @@ public static HMaster constructMaster(Class<? extends HMaster> masterClass,
}
}

protected static void doMain(String [] args,
Class<? extends HMaster> masterClass) throws Exception {
int ret = ToolRunner.run(
HBaseConfiguration.create(),
new HMasterCommandLine(masterClass),
args);
if (ret != 0) {
System.exit(ret);
}
// Otherwise exit gracefully so other threads clean up
}

/**
* Main program
* @param args
* @throws IOException
* @see org.apache.hadoop.hbase.master.HMasterCommandLine
*/
public static void main(String [] args) throws Exception {
doMain(args, HMaster.class);
new HMasterCommandLine(HMaster.class).doMain(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.io.IOException;
import java.io.File;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ManagementFactory;
import java.util.List;

import org.apache.commons.cli.CommandLine;
Expand All @@ -32,19 +30,18 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.LocalHBaseCluster;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.regionserver.HRegionServer;
import org.apache.hadoop.hbase.util.ServerCommandLine;
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
import org.apache.zookeeper.KeeperException;

public class HMasterCommandLine extends Configured implements Tool {
public class HMasterCommandLine extends ServerCommandLine {
private static final Log LOG = LogFactory.getLog(HMasterCommandLine.class);

private static final String USAGE =
Expand All @@ -57,29 +54,14 @@ public class HMasterCommandLine extends Configured implements Tool {

private final Class<? extends HMaster> masterClass;


public HMasterCommandLine(Class<? extends HMaster> masterClass) {
this.masterClass = masterClass;
}

private void usage(String message) {
if (message != null) {
System.err.println(message);
System.err.println("\n");
}

System.err.println(USAGE);
protected String getUsage() {
return USAGE;
}

private static void logJVMInfo() {
// Print out vm stats before starting up.
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
if (runtime != null) {
LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
LOG.info("vmInputArguments=" + runtime.getInputArguments());
}
}

public int run(String args[]) throws Exception {
Options opt = new Options();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -1939,6 +1935,10 @@ public NavigableSet<HRegionInfo> getOnlineRegions() {
return sortedset;
}

public int getNumberOfOnlineRegions() {
return onlineRegions.size();
}

/**
* For tests and web ui.
* This method will only work if HRegionServer is in the same JVM as client;
Expand Down Expand Up @@ -2321,18 +2321,6 @@ public static Thread startRegionServer(final HRegionServer hrs,
return t;
}

private static void printUsageAndExit() {
printUsageAndExit(null);
}

private static void printUsageAndExit(final String message) {
if (message != null) {
System.err.println(message);
}
System.err.println("Usage: java org.apache.hbase.HRegionServer start|stop [-D <conf.param=value>]");
System.exit(0);
}

/**
* Utility for constructing an instance of the passed HRegionServer class.
*
Expand Down Expand Up @@ -2360,80 +2348,17 @@ public void replicateLogEntries(final HLog.Entry[] entries)
this.replicationHandler.replicateLogEntries(entries);
}

/**
* Do class main.
*
* @param args
* @param regionServerClass
* HRegionServer to instantiate.
*/
protected static void doMain(final String[] args,
final Class<? extends HRegionServer> regionServerClass) {
Configuration conf = HBaseConfiguration.create();

Options opt = new Options();
opt.addOption("D", true, "Override HBase Configuration Settings");
try {
CommandLine cmd = new GnuParser().parse(opt, args);

if (cmd.hasOption("D")) {
for (String confOpt : cmd.getOptionValues("D")) {
String[] kv = confOpt.split("=", 2);
if (kv.length == 2) {
conf.set(kv[0], kv[1]);
LOG.debug("-D configuration override: " + kv[0] + "=" + kv[1]);
} else {
throw new ParseException("-D option format invalid: " + confOpt);
}
}
}

if (cmd.getArgList().contains("start")) {
try {
// If 'local', don't start a region server here. Defer to
// LocalHBaseCluster. It manages 'local' clusters.
if (LocalHBaseCluster.isLocal(conf)) {
LOG.warn("Not starting a distinct region server because "
+ HConstants.CLUSTER_DISTRIBUTED + " is false");
} else {
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
if (runtime != null) {
LOG.info("vmInputArguments=" + runtime.getInputArguments());
}
HRegionServer hrs = constructRegionServer(regionServerClass, conf);
startRegionServer(hrs);
}
} catch (Throwable t) {
LOG.error( "Can not start region server because "+
StringUtils.stringifyException(t) );
System.exit(-1);
}
} else if (cmd.getArgList().contains("stop")) {
throw new ParseException("To shutdown the regionserver run " +
"bin/hbase-daemon.sh stop regionserver or send a kill signal to" +
"the regionserver pid");
} else {
throw new ParseException("Unknown argument(s): " +
org.apache.commons.lang.StringUtils.join(cmd.getArgs(), " "));
}
} catch (ParseException e) {
LOG.error("Could not parse", e);
printUsageAndExit();
}
}

/**
* @param args
* @see org.apache.hadoop.hbase.regionserver.HRegionServerCommandLine
*/
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
@SuppressWarnings("unchecked")
Class<? extends HRegionServer> regionServerClass = (Class<? extends HRegionServer>) conf
.getClass(HConstants.REGION_SERVER_IMPL, HRegionServer.class);
doMain(args, regionServerClass);
}

public int getNumberOfOnlineRegions() {
return onlineRegions.size();
new HRegionServerCommandLine(regionServerClass).doMain(args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright 2010 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.regionserver;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.LocalHBaseCluster;
import org.apache.hadoop.hbase.util.ServerCommandLine;

/**
* Class responsible for parsing the command line and starting the
* RegionServer.
*/
public class HRegionServerCommandLine extends ServerCommandLine {
private static final Log LOG = LogFactory.getLog(HRegionServerCommandLine.class);

private final Class<? extends HRegionServer> regionServerClass;

private static final String USAGE =
"Usage: HRegionServer [-D conf.param=value] start";

public HRegionServerCommandLine(Class<? extends HRegionServer> clazz) {
this.regionServerClass = clazz;
}

protected String getUsage() {
return USAGE;
}

private int start() throws Exception {
Configuration conf = getConf();

// If 'local', don't start a region server here. Defer to
// LocalHBaseCluster. It manages 'local' clusters.
if (LocalHBaseCluster.isLocal(conf)) {
LOG.warn("Not starting a distinct region server because "
+ HConstants.CLUSTER_DISTRIBUTED + " is false");
} else {
logJVMInfo();
HRegionServer hrs = HRegionServer.constructRegionServer(regionServerClass, conf);
HRegionServer.startRegionServer(hrs);
}
return 0;
}

public int run(String args[]) throws Exception {
if (args.length != 1) {
usage(null);
return -1;
}

String cmd = args[0];

if ("start".equals(cmd)) {
return start();
} else if ("stop".equals(cmd)) {
System.err.println(
"To shutdown the regionserver run " +
"bin/hbase-daemon.sh stop regionserver or send a kill signal to" +
"the regionserver pid");
return -1;
} else {
usage("Unknown command: " + args[0]);
return -1;
}
}
}
82 changes: 82 additions & 0 deletions src/main/java/org/apache/hadoop/hbase/util/ServerCommandLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright 2010 The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.util;

import java.lang.management.RuntimeMXBean;
import java.lang.management.ManagementFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
* Base class for command lines that start up various HBase daemons.
*/
public abstract class ServerCommandLine extends Configured implements Tool {
private static final Log LOG = LogFactory.getLog(ServerCommandLine.class);

/**
* Implementing subclasses should return a usage string to print out.
*/
protected abstract String getUsage();

/**
* Print usage information for this command line.
*
* @param message if not null, print this message before the usage info.
*/
protected void usage(String message) {
if (message != null) {
System.err.println(message);
System.err.println("");
}

System.err.println(getUsage());
}

/**
* Log information about the currently running JVM.
*/
public static void logJVMInfo() {
// Print out vm stats before starting up.
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
if (runtime != null) {
LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
LOG.info("vmInputArguments=" + runtime.getInputArguments());
}
}

/**
* Parse and run the given command line. This may exit the JVM if
* a nonzero exit code is returned from <code>run()</code>.
*/
public void doMain(String args[]) throws Exception {
int ret = ToolRunner.run(
HBaseConfiguration.create(), this, args);
if (ret != 0) {
System.exit(ret);
}
}
}
Loading

0 comments on commit 02cd4a9

Please sign in to comment.