001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.zookeeper; 019 020import java.io.IOException; 021import org.apache.hadoop.conf.Configuration; 022import org.apache.hadoop.hbase.HBaseConfiguration; 023import org.apache.hadoop.hbase.HBaseInterfaceAudience; 024import org.apache.yetus.audience.InterfaceAudience; 025import org.apache.zookeeper.ZooKeeperMain; 026import org.apache.zookeeper.cli.CliException; 027 028/** 029 * Tool for running ZookeeperMain from HBase by reading a ZooKeeper server from HBase XML 030 * configuration. 031 */ 032@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS) 033public class ZKMainServer { 034 private static final String SERVER_ARG = "-server"; 035 036 public String parse(final Configuration c) { 037 return ZKConfig.getZKQuorumServersString(c); 038 } 039 040 /** 041 * ZooKeeper 3.4.6 broke being able to pass commands on command line. See ZOOKEEPER-1897. This 042 * class is a hack to restore this faclity. 043 */ 044 private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain { 045 public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args) 046 throws IOException, InterruptedException { 047 super(args); 048 // Make sure we are connected before we proceed. Can take a while on some systems. If we 049 // run the command without being connected, we get ConnectionLoss KeeperErrorConnection... 050 // Make it 30seconds. We dont' have a config in this context and zk doesn't have 051 // a timeout until after connection. 30000ms is default for zk. 052 ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000); 053 } 054 055 /** 056 * Run the command-line args passed. Calls System.exit when done. 057 * @throws IOException in case of a network failure 058 * @throws InterruptedException if the ZooKeeper client closes 059 * @throws CliException if the ZooKeeper exception happens in cli command 060 */ 061 void runCmdLine() throws IOException, InterruptedException, CliException { 062 processCmd(this.cl); 063 System.exit(0); 064 } 065 } 066 067 /** 068 * @param args the arguments to check 069 * @return True if argument strings have a '-server' in them. 070 */ 071 private static boolean hasServer(final String[] args) { 072 return args.length > 0 && args[0].equals(SERVER_ARG); 073 } 074 075 /** 076 * @param args the arguments to check for command-line arguments 077 * @return True if command-line arguments were passed. 078 */ 079 private static boolean hasCommandLineArguments(final String[] args) { 080 if (hasServer(args)) { 081 if (args.length < 2) { 082 throw new IllegalStateException("-server param but no value"); 083 } 084 085 return args.length > 2; 086 } 087 088 return args.length > 0; 089 } 090 091 /** 092 * Run the tool. 093 * @param args Command line arguments. First arg is path to zookeepers file. 094 */ 095 public static void main(String[] args) throws Exception { 096 String[] newArgs = args; 097 if (!hasServer(args)) { 098 // Add the zk ensemble from configuration if none passed on command-line. 099 Configuration conf = HBaseConfiguration.create(); 100 String hostport = new ZKMainServer().parse(conf); 101 if (hostport != null && hostport.length() > 0) { 102 newArgs = new String[args.length + 2]; 103 System.arraycopy(args, 0, newArgs, 2, args.length); 104 newArgs[0] = "-server"; 105 newArgs[1] = hostport; 106 } 107 } 108 // If command-line arguments, run our hack so they are executed. 109 // ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say 110 // 3.4.6 breaks command-processing; TODO. 111 if (hasCommandLineArguments(args)) { 112 HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm = 113 new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs); 114 zkm.runCmdLine(); 115 } else { 116 ZooKeeperMain.main(newArgs); 117 } 118 } 119}