View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.io.IOException;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.zookeeper.KeeperException;
30  import org.apache.zookeeper.ZooKeeperMain;
31  
32  /**
33   * Tool for running ZookeeperMain from HBase by  reading a ZooKeeper server
34   * from HBase XML configuration.
35   */
36  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
37  public class ZooKeeperMainServer {
38    private static final String SERVER_ARG = "-server";
39  
40    public String parse(final Configuration c) {
41      return ZKConfig.getZKQuorumServersString(c);
42    }
43  
44    /**
45     * ZooKeeper 3.4.6 broke being able to pass commands on command line.
46     * See ZOOKEEPER-1897.  This class is a hack to restore this faclity.
47     */
48    private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
49      public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
50      throws IOException, InterruptedException {
51        super(args);
52        // Make sure we are connected before we proceed. Can take a while on some systems. If we
53        // run the command without being connected, we get ConnectionLoss KeeperErrorConnection...
54        long startTime = System.currentTimeMillis();
55        while (!this.zk.getState().isConnected()) {
56          Thread.sleep(1);
57          long elapsed = System.currentTimeMillis() - startTime;
58          if (elapsed > 30000) {
59            throw new InterruptedException("Failed connect after " + elapsed + "ms; " + this.zk);
60          }
61        }
62      }
63  
64      /**
65       * Run the command-line args passed.  Calls System.exit when done.
66       * @throws KeeperException
67       * @throws IOException
68       * @throws InterruptedException
69       */
70      void runCmdLine() throws KeeperException, IOException, InterruptedException {
71        processCmd(this.cl);
72        System.exit(0);
73      }
74    }
75  
76    /**
77     * @param args
78     * @return True if argument strings have a '-server' in them.
79     */
80    private static boolean hasServer(final String args[]) {
81      return args.length > 0 && args[0].equals(SERVER_ARG);
82    }
83  
84    /**
85     * @param args
86     * @return True if command-line arguments were passed.
87     */
88    private static boolean hasCommandLineArguments(final String args[]) {
89      if (hasServer(args)) {
90        if (args.length < 2) throw new IllegalStateException("-server param but no value");
91        return args.length > 2;
92      }
93      return args.length > 0;
94    }
95  
96    /**
97     * Run the tool.
98     * @param args Command line arguments. First arg is path to zookeepers file.
99     */
100   public static void main(String args[]) throws Exception {
101     String [] newArgs = args;
102     if (!hasServer(args)) {
103       // Add the zk ensemble from configuration if none passed on command-line.
104       Configuration conf = HBaseConfiguration.create();
105       String hostport = new ZooKeeperMainServer().parse(conf);
106       if (hostport != null && hostport.length() > 0) {
107         newArgs = new String[args.length + 2];
108         System.arraycopy(args, 0, newArgs, 2, args.length);
109         newArgs[0] = "-server";
110         newArgs[1] = hostport;
111       }
112     }
113     // If command-line arguments, run our hack so they are executed.
114     // ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say
115     // 3.4.6 breaks command-processing; TODO.
116     if (hasCommandLineArguments(args)) {
117       HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
118         new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
119       zkm.runCmdLine();
120     } else {
121       ZooKeeperMain.main(newArgs);
122     }
123   }
124 }