diff --git bin/hbase bin/hbase index d29963b..0751c2b 100755 --- bin/hbase +++ bin/hbase @@ -273,10 +273,7 @@ elif [ "$COMMAND" = "hlog" ] ; then elif [ "$COMMAND" = "hfile" ] ; then CLASS='org.apache.hadoop.hbase.io.hfile.HFile' elif [ "$COMMAND" = "zkcli" ] ; then - # ZooKeeperMainServerArg returns '-server HOST:PORT' or empty string. - SERVER_ARG=`"$bin"/hbase org.apache.hadoop.hbase.zookeeper.ZooKeeperMainServerArg` - CLASS="org.apache.zookeeper.ZooKeeperMain ${SERVER_ARG}" - + CLASS="org.apache.hadoop.hbase.zookeeper.ZooKeeperMainServer" elif [ "$COMMAND" = "master" ] ; then CLASS='org.apache.hadoop.hbase.master.HMaster' if [ "$1" != "stop" ] ; then diff --git bin/hbase.cmd bin/hbase.cmd index 0cc9f78..9e2de13 100644 --- bin/hbase.cmd +++ bin/hbase.cmd @@ -337,9 +337,7 @@ goto :eof goto :eof :zkcli - rem ZooKeeperMainServerArg returns '-server HOST:PORT' or empty string. - set SERVER_ARG=%HADOOP_BIN_PATH%\hbase org.apache.hadoop.hbase.zookeeper.ZooKeeperMainServerArg - set CLASS=org.apache.zookeeper.ZooKeeperMain %SERVER_ARG% + set CLASS=org.apache.hadoop.hbase.zookeeper.ZooKeeperMainServer goto :eof :makeServiceXml diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServer.java hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServer.java new file mode 100644 index 0000000..09efe79 --- /dev/null +++ hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServer.java @@ -0,0 +1,76 @@ +/** + * 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.zookeeper; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Properties; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.zookeeper.ZooKeeperMain; + +/** + * Tool for running ZookeeperMain from HBase by reading a ZooKeeper server + * from HBase XML configuration. + */ +public class ZooKeeperMainServer { + public String parse(final Configuration c) { + // Note that we do not simply grab the property + // HConstants.ZOOKEEPER_QUORUM from the HBaseConfiguration because the + // user may be using a zoo.cfg file. + Properties zkProps = ZKConfig.makeZKProps(c); + String host = null; + String clientPort = null; + List hosts = new ArrayList(); + for (Entry entry: zkProps.entrySet()) { + String key = entry.getKey().toString().trim(); + String value = entry.getValue().toString().trim(); + if (key.startsWith("server.")) { + String[] parts = value.split(":"); + hosts.add(parts[0]); + } else if (key.endsWith("clientPort")) { + clientPort = value; + } + } + if (hosts.isEmpty() || clientPort == null) + return null; + for (int i = 0; i < hosts.size(); i++) { + if (i > 0) + host += "," + hosts.get(i); + else + host = hosts.get(i); + } + return host != null ? host + ":" + clientPort : null; + } + + /** + * Run the tool. + * @param args Command line arguments. First arg is path to zookeepers file. + */ + public static void main(String args[]) throws Exception { + Configuration conf = HBaseConfiguration.create(); + String hostport = new ZooKeeperMainServer().parse(conf); + String zkArg = (hostport == null || hostport.length() == 0)? "": "-server " + hostport; + ZooKeeperMain.main(new String[] {zkArg}); + } +} \ No newline at end of file diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServerArg.java hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServerArg.java deleted file mode 100644 index 26eaab3..0000000 --- hbase-server/src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperMainServerArg.java +++ /dev/null @@ -1,77 +0,0 @@ -/** - * - * 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.zookeeper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map.Entry; -import java.util.Properties; - -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.HBaseConfiguration; - -/** - * Tool for reading a ZooKeeper server from HBase XML configuration producing - * the '-server host:port' argument to pass ZooKeeperMain. This program - * emits either '-server HOST:PORT" where HOST is one of the zk ensemble - * members plus zk client port OR it emits '' if no zk servers found (Yes, - * it emits '-server' too). - */ -public class ZooKeeperMainServerArg { - public String parse(final Configuration c) { - // Note that we do not simply grab the property - // HConstants.ZOOKEEPER_QUORUM from the HBaseConfiguration because the - // user may be using a zoo.cfg file. - Properties zkProps = ZKConfig.makeZKProps(c); - String host = null; - String clientPort = null; - List hosts = new ArrayList(); - for (Entry entry: zkProps.entrySet()) { - String key = entry.getKey().toString().trim(); - String value = entry.getValue().toString().trim(); - if (key.startsWith("server.")) { - String[] parts = value.split(":"); - hosts.add(parts[0]); - } else if (key.endsWith("clientPort")) { - clientPort = value; - } - } - if (hosts.isEmpty() || clientPort == null) - return null; - for (int i = 0; i < hosts.size(); i++) { - if (i > 0) - host += "," + hosts.get(i); - else - host = hosts.get(i); - } - return host != null ? host + ":" + clientPort : null; - } - - /** - * Run the tool. - * @param args Command line arguments. First arg is path to zookeepers file. - */ - public static void main(String args[]) { - Configuration conf = HBaseConfiguration.create(); - String hostport = new ZooKeeperMainServerArg().parse(conf); - System.out.println((hostport == null || hostport.length() == 0)? "": - "-server " + hostport); - } -} diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServer.java hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServer.java new file mode 100644 index 0000000..316bf96 --- /dev/null +++ hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServer.java @@ -0,0 +1,47 @@ +/** + * 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.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.*; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(SmallTests.class) +public class TestZooKeeperMainServer { + private final ZooKeeperMainServer parser = new ZooKeeperMainServer(); + + @Test public void test() { + Configuration c = HBaseConfiguration.create(); + assertEquals("localhost:" + c.get(HConstants.ZOOKEEPER_CLIENT_PORT), + parser.parse(c)); + final String port = "1234"; + c.set(HConstants.ZOOKEEPER_CLIENT_PORT, port); + c.set("hbase.zookeeper.quorum", "example.com"); + assertEquals("example.com:" + port, parser.parse(c)); + c.set("hbase.zookeeper.quorum", "example1.com,example2.com,example3.com"); + assertTrue(port, + parser.parse(c).matches("(example[1-3]\\.com,){2}example[1-3]\\.com:" + port)); + } +} + diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServerArg.java hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServerArg.java deleted file mode 100644 index facc839..0000000 --- hbase-server/src/test/java/org/apache/hadoop/hbase/zookeeper/TestZooKeeperMainServerArg.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * - * 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.zookeeper; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.*; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Category(SmallTests.class) -public class TestZooKeeperMainServerArg { - private final ZooKeeperMainServerArg parser = new ZooKeeperMainServerArg(); - - @Test public void test() { - Configuration c = HBaseConfiguration.create(); - assertEquals("localhost:" + c.get(HConstants.ZOOKEEPER_CLIENT_PORT), - parser.parse(c)); - final String port = "1234"; - c.set(HConstants.ZOOKEEPER_CLIENT_PORT, port); - c.set("hbase.zookeeper.quorum", "example.com"); - assertEquals("example.com:" + port, parser.parse(c)); - c.set("hbase.zookeeper.quorum", "example1.com,example2.com,example3.com"); - assertTrue(port, - parser.parse(c).matches("(example[1-3]\\.com,){2}example[1-3]\\.com:" + port)); - } - -} -