Commit 31e56082 authored by Mahadev Konar's avatar Mahadev Konar
Browse files

ZOOKEEPER-217. Fix errors in config to be thrown as Exceptions. (mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/branches/branch-3.0@718659 13f79535-47bb-0310-9956-ffa450edef68
parent d7157cd6
......@@ -15,6 +15,8 @@ zookeeper.h (pat via mahadev)
ZOOKEEPER-219. fix events.poll timeout in watcher test to be longer. (pat
via mahadev)
ZOOKEEPER-217. Fix errors in config to be thrown as Exceptions. (mahadev)
Release 3.0.0 - 2008-10-21
......
......@@ -23,6 +23,7 @@ import static org.apache.zookeeper.server.ServerConfig.getClientPort;
import java.io.File;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.apache.zookeeper.jmx.server.ConnectionMXBean;
import org.apache.zookeeper.jmx.server.DataTreeMXBean;
import org.apache.zookeeper.jmx.server.ZooKeeperServerMXBean;
......@@ -53,6 +54,7 @@ import org.apache.zookeeper.server.util.ZooKeeperObserverManager;
*/
public class ManagedZooKeeperServerMain extends ZooKeeperServerMain {
private static final Logger LOG = Logger.getLogger(ManagedZooKeeperServerMain.class);
/**
* To start the server specify the client port number and the data directory
* on the command line.
......@@ -60,7 +62,12 @@ public class ManagedZooKeeperServerMain extends ZooKeeperServerMain {
* @param args command line parameters.
*/
public static void main(String[] args) {
ServerConfig.parse(args);
try {
ServerConfig.parse(args);
} catch(Exception e) {
LOG.fatal("Error in config", e);
System.exit(2);
}
ZooKeeperObserverManager.setAsConcrete();
runStandalone(new ZooKeeperServer.Factory() {
public NIOServerCnxn.Factory createConnectionFactory()throws IOException {
......
......@@ -85,7 +85,12 @@ public class ManagedQuorumPeerMain {
ManagedZooKeeperServerMain.main(args);
return;
}
QuorumPeerConfig.parse(args);
try {
QuorumPeerConfig.parse(args);
} catch(Exception e) {
LOG.fatal("Error in config ", e);
System.exit(2);
}
if (!QuorumPeerConfig.isStandalone()) {
ZooKeeperObserverManager.setAsConcrete();
runPeer(new QuorumPeer.Factory() {
......
......@@ -51,18 +51,16 @@ public class ServerConfig {
protected static ServerConfig instance=null;
public static void parse(String[] args) {
public static void parse(String[] args) throws Exception {
if(instance!=null)
return;
if (args.length != 2) {
System.err.println("USAGE: ZooKeeperServer port datadir\n");
System.exit(2);
throw new IllegalArgumentException("Invalid usage.");
}
try {
instance=new ServerConfig(Integer.parseInt(args[0]),args[1],args[1]);
} catch (NumberFormatException e) {
System.err.println(args[0] + " is not a valid port number");
System.exit(2);
throw new IllegalArgumentException(args[0] + " is not a valid port number");
}
}
......
......@@ -31,14 +31,20 @@ import org.apache.zookeeper.server.ZooKeeperServer.BasicDataTreeBuilder;
public class ZooKeeperServerMain {
private static final Logger LOG = Logger.getLogger(ZooKeeperServerMain.class);
private static final String USAGE = "Usage: ZooKeeperServerMain port datadir";
/*
* Start up the ZooKeeper server.
*
* @param args the port and data directory
*/
public static void main(String[] args) {
ServerConfig.parse(args);
try {
ServerConfig.parse(args);
} catch(Exception e) {
LOG.fatal("Error in config", e);
LOG.info(USAGE);
System.exit(2);
}
runStandalone(new ZooKeeperServer.Factory() {
public NIOServerCnxn.Factory createConnectionFactory() throws IOException {
return new NIOServerCnxn.Factory(ServerConfig.getClientPort());
......
......@@ -47,145 +47,137 @@ public class QuorumPeerConfig extends ServerConfig {
super(port, dataDir, dataLogDir);
}
public static void parse(String[] args) {
if(instance!=null)
public static void parse(String[] args) throws Exception {
if (instance != null)
return;
if (args.length != 1) {
throw new IllegalArgumentException("Invalid usage.");
}
File zooCfgFile = new File(args[0]);
if (!zooCfgFile.exists()) {
throw new IllegalArgumentException(zooCfgFile.toString()
+ " file is missing");
}
Properties cfg = new Properties();
FileInputStream zooCfgStream = new FileInputStream(zooCfgFile);
try {
if (args.length != 1) {
System.err.println("USAGE: configFile");
System.exit(2);
}
File zooCfgFile = new File(args[0]);
if (!zooCfgFile.exists()) {
LOG.error(zooCfgFile.toString() + " file is missing");
System.exit(2);
}
Properties cfg = new Properties();
FileInputStream zooCfgStream = new FileInputStream(zooCfgFile);
try {
cfg.load(zooCfgStream);
} finally {
zooCfgStream.close();
}
HashMap<Long,QuorumServer> servers = new HashMap<Long,QuorumServer>();
String dataDir = null;
String dataLogDir = null;
int clientPort = 0;
int tickTime = 0;
int initLimit = 0;
int syncLimit = 0;
int electionAlg = 3;
int electionPort = 2182;
for (Entry<Object, Object> entry : cfg.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if (key.equals("dataDir")) {
dataDir = value;
} else if (key.equals("dataLogDir")) {
dataLogDir = value;
} else if (key.equals("clientPort")) {
clientPort = Integer.parseInt(value);
} else if (key.equals("tickTime")) {
tickTime = Integer.parseInt(value);
} else if (key.equals("initLimit")) {
initLimit = Integer.parseInt(value);
} else if (key.equals("syncLimit")) {
syncLimit = Integer.parseInt(value);
} else if (key.equals("electionAlg")) {
electionAlg = Integer.parseInt(value);
} else if (key.startsWith("server.")) {
int dot = key.indexOf('.');
long sid = Long.parseLong(key.substring(dot + 1));
String parts[] = value.split(":");
if ((parts.length != 2) &&
(parts.length != 3)){
LOG.error(value
+ " does not have the form host:port or host:port:port");
}
InetSocketAddress addr = new InetSocketAddress(parts[0],
Integer.parseInt(parts[1]));
if(parts.length == 2)
servers.put(Long.valueOf(sid), new QuorumServer(sid, addr));
else if(parts.length == 3){
InetSocketAddress electionAddr = new InetSocketAddress(parts[0],
Integer.parseInt(parts[2]));
servers.put(Long.valueOf(sid), new QuorumServer(sid, addr, electionAddr));
}
} else {
System.setProperty("zookeeper." + key, value);
cfg.load(zooCfgStream);
} finally {
zooCfgStream.close();
}
HashMap<Long, QuorumServer> servers = new HashMap<Long, QuorumServer>();
String dataDir = null;
String dataLogDir = null;
int clientPort = 0;
int tickTime = 0;
int initLimit = 0;
int syncLimit = 0;
int electionAlg = 3;
int electionPort = 2182;
for (Entry<Object, Object> entry : cfg.entrySet()) {
String key = entry.getKey().toString();
String value = entry.getValue().toString();
if (key.equals("dataDir")) {
dataDir = value;
} else if (key.equals("dataLogDir")) {
dataLogDir = value;
} else if (key.equals("clientPort")) {
clientPort = Integer.parseInt(value);
} else if (key.equals("tickTime")) {
tickTime = Integer.parseInt(value);
} else if (key.equals("initLimit")) {
initLimit = Integer.parseInt(value);
} else if (key.equals("syncLimit")) {
syncLimit = Integer.parseInt(value);
} else if (key.equals("electionAlg")) {
electionAlg = Integer.parseInt(value);
} else if (key.startsWith("server.")) {
int dot = key.indexOf('.');
long sid = Long.parseLong(key.substring(dot + 1));
String parts[] = value.split(":");
if ((parts.length != 2) && (parts.length != 3)) {
LOG
.error(value
+ " does not have the form host:port or host:port:port");
}
}
if (dataDir == null) {
LOG.error("dataDir is not set");
System.exit(2);
}
if (dataLogDir == null) {
dataLogDir = dataDir;
} else {
if (!new File(dataLogDir).isDirectory()) {
LOG.error("dataLogDir " + dataLogDir+ " is missing.");
System.exit(2);
InetSocketAddress addr = new InetSocketAddress(parts[0],
Integer.parseInt(parts[1]));
if (parts.length == 2)
servers.put(Long.valueOf(sid), new QuorumServer(sid, addr));
else if (parts.length == 3) {
InetSocketAddress electionAddr = new InetSocketAddress(
parts[0], Integer.parseInt(parts[2]));
servers.put(Long.valueOf(sid), new QuorumServer(sid, addr,
electionAddr));
}
} else {
System.setProperty("zookeeper." + key, value);
}
if (clientPort == 0) {
LOG.error("clientPort is not set");
System.exit(2);
}
if (dataDir == null) {
throw new IllegalArgumentException("dataDir is not set");
}
if (dataLogDir == null) {
dataLogDir = dataDir;
} else {
if (!new File(dataLogDir).isDirectory()) {
throw new IllegalArgumentException("dataLogDir " + dataLogDir
+ " is missing.");
}
if (tickTime == 0) {
LOG.error("tickTime is not set");
System.exit(2);
}
if (clientPort == 0) {
throw new IllegalArgumentException("clientPort is not set");
}
if (tickTime == 0) {
throw new IllegalArgumentException("tickTime is not set");
}
if (servers.size() > 1 && initLimit == 0) {
throw new IllegalArgumentException("initLimit is not set");
}
if (servers.size() > 1 && syncLimit == 0) {
throw new IllegalArgumentException("syncLimit is not set");
}
QuorumPeerConfig conf = new QuorumPeerConfig(clientPort, dataDir,
dataLogDir);
conf.tickTime = tickTime;
conf.initLimit = initLimit;
conf.syncLimit = syncLimit;
conf.electionAlg = electionAlg;
conf.servers = servers;
if (servers.size() > 1) {
/*
* If using FLE, then every server requires a separate election
* port.
*/
if (electionAlg != 0) {
for (QuorumServer s : servers.values()) {
if (s.electionAddr == null)
throw new IllegalArgumentException(
"Missing election port for server: " + s.id);
}
}
if (servers.size() > 1 && initLimit == 0) {
LOG.error("initLimit is not set");
System.exit(2);
File myIdFile = new File(dataDir, "myid");
if (!myIdFile.exists()) {
throw new IllegalArgumentException(myIdFile.toString()
+ " file is missing");
}
if (servers.size() > 1 && syncLimit == 0) {
LOG.error("syncLimit is not set");
System.exit(2);
BufferedReader br = new BufferedReader(new FileReader(myIdFile));
String myIdString;
try {
myIdString = br.readLine();
} finally {
br.close();
}
QuorumPeerConfig conf = new QuorumPeerConfig(clientPort, dataDir,
dataLogDir);
conf.tickTime = tickTime;
conf.initLimit = initLimit;
conf.syncLimit = syncLimit;
conf.electionAlg = electionAlg;
conf.servers = servers;
if (servers.size() > 1) {
/*
* If using FLE, then every server requires a separate election port.
*/
if(electionAlg != 0){
for(QuorumServer s : servers.values()){
if(s.electionAddr == null)
LOG.error("Missing election port for server: " + s.id);
}
}
File myIdFile = new File(dataDir, "myid");
if (!myIdFile.exists()) {
LOG.error(myIdFile.toString() + " file is missing");
System.exit(2);
}
BufferedReader br = new BufferedReader(new FileReader(myIdFile));
String myIdString;
try {
myIdString = br.readLine();
} finally {
br.close();
}
try {
conf.serverId = Long.parseLong(myIdString);
} catch (NumberFormatException e) {
LOG.error(myIdString + " is not a number");
System.exit(2);
}
try {
conf.serverId = Long.parseLong(myIdString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("serverid " + myIdString
+ " is not a number");
}
instance=conf;
} catch (Exception e) {
LOG.error("FIXMSG",e);
System.exit(2);
}
instance = conf;
}
@Override
......
......@@ -67,7 +67,12 @@ public class QuorumPeerMain {
ZooKeeperServerMain.main(args);
return;
}
QuorumPeerConfig.parse(args);
try {
QuorumPeerConfig.parse(args);
} catch(Exception e) {
LOG.fatal("Error in config", e);
System.exit(2);
}
if (!QuorumPeerConfig.isStandalone()) {
runPeer(new QuorumPeer.Factory() {
public QuorumPeer create(NIOServerCnxn.Factory cnxnFactory) throws IOException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment