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 */
018
019package org.apache.hadoop.hbase.zookeeper;
020
021import java.io.IOException;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.HBaseInterfaceAudience;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.zookeeper.ZooKeeperMain;
028import org.apache.zookeeper.cli.CliException;
029
030
031/**
032 * Tool for running ZookeeperMain from HBase by  reading a ZooKeeper server
033 * from HBase XML configuration.
034 */
035@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
036public class ZKMainServer {
037  private static final String SERVER_ARG = "-server";
038
039  public String parse(final Configuration c) {
040    return ZKConfig.getZKQuorumServersString(c);
041  }
042
043  /**
044   * ZooKeeper 3.4.6 broke being able to pass commands on command line.
045   * See ZOOKEEPER-1897.  This class is a hack to restore this faclity.
046   */
047  private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
048    public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
049      throws IOException, InterruptedException {
050      super(args);
051      // Make sure we are connected before we proceed. Can take a while on some systems. If we
052      // run the command without being connected, we get ConnectionLoss KeeperErrorConnection...
053      // Make it 30seconds. We dont' have a config in this context and zk doesn't have
054      // a timeout until after connection. 30000ms is default for zk.
055      ZooKeeperHelper.ensureConnectedZooKeeper(this.zk, 30000);
056    }
057
058    /**
059     * Run the command-line args passed.  Calls System.exit when done.
060     * @throws IOException in case of a network failure
061     * @throws InterruptedException if the ZooKeeper client closes
062     * @throws CliException if the ZooKeeper exception happens in cli command
063     */
064    void runCmdLine() throws IOException, InterruptedException, CliException {
065      processCmd(this.cl);
066      System.exit(0);
067    }
068  }
069
070  /**
071   * @param args the arguments to check
072   * @return True if argument strings have a '-server' in them.
073   */
074  private static boolean hasServer(final String[] args) {
075    return args.length > 0 && args[0].equals(SERVER_ARG);
076  }
077
078  /**
079   * @param args the arguments to check for command-line arguments
080   * @return True if command-line arguments were passed.
081   */
082  private static boolean hasCommandLineArguments(final String[] args) {
083    if (hasServer(args)) {
084      if (args.length < 2) {
085        throw new IllegalStateException("-server param but no value");
086      }
087
088      return args.length > 2;
089    }
090
091    return args.length > 0;
092  }
093
094  /**
095   * Run the tool.
096   * @param args Command line arguments. First arg is path to zookeepers file.
097   */
098  public static void main(String[] args) throws Exception {
099    String [] newArgs = args;
100    if (!hasServer(args)) {
101      // Add the zk ensemble from configuration if none passed on command-line.
102      Configuration conf = HBaseConfiguration.create();
103      String hostport = new ZKMainServer().parse(conf);
104      if (hostport != null && hostport.length() > 0) {
105        newArgs = new String[args.length + 2];
106        System.arraycopy(args, 0, newArgs, 2, args.length);
107        newArgs[0] = "-server";
108        newArgs[1] = hostport;
109      }
110    }
111    // If command-line arguments, run our hack so they are executed.
112    // ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say
113    // 3.4.6 breaks command-processing; TODO.
114    if (hasCommandLineArguments(args)) {
115      HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
116        new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
117      zkm.runCmdLine();
118    } else {
119      ZooKeeperMain.main(newArgs);
120    }
121  }
122}