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          if ((System.currentTimeMillis() - startTime) > 10000) {
58            throw new InterruptedException("Failed connect " + this.zk);
59          }
60        }
61      }
62  
63      /**
64       * Run the command-line args passed.  Calls System.exit when done.
65       * @throws KeeperException
66       * @throws IOException
67       * @throws InterruptedException
68       */
69      void runCmdLine() throws IOException, InterruptedException {
70        try {
71          processCmd(this.cl);
72        } catch (IOException | InterruptedException e) {
73          throw e;
74        } catch (Exception e) {
75          // The current signature proposes we throw IOException or
76          // InterruptedException. ZK 3.6 throws another type of checked
77          // exception, which causes a compilation error. Therefore we catch
78          // that and potentially others, and wrap it into an IOE. Adding a
79          // new checked exception to the signature would cause a compilation 
80          // problem with 3.4.
81          throw new IOException(e);
82        }
83        System.exit(0);
84      }
85    }
86  
87    /**
88     * @param args
89     * @return True if argument strings have a '-server' in them.
90     */
91    private static boolean hasServer(final String args[]) {
92      return args.length > 0 && args[0].equals(SERVER_ARG);
93    }
94  
95    /**
96     * @param args
97     * @return True if command-line arguments were passed.
98     */
99    private static boolean hasCommandLineArguments(final String args[]) {
100     if (hasServer(args)) {
101       if (args.length < 2) throw new IllegalStateException("-server param but no value");
102       return args.length > 2;
103     }
104     return args.length > 0;
105   }
106 
107   /**
108    * Run the tool.
109    * @param args Command line arguments. First arg is path to zookeepers file.
110    */
111   public static void main(String args[]) throws Exception {
112     String [] newArgs = args;
113     if (!hasServer(args)) {
114       // Add the zk ensemble from configuration if none passed on command-line.
115       Configuration conf = HBaseConfiguration.create();
116       String hostport = new ZooKeeperMainServer().parse(conf);
117       if (hostport != null && hostport.length() > 0) {
118         newArgs = new String[args.length + 2];
119         System.arraycopy(args, 0, newArgs, 2, args.length);
120         newArgs[0] = "-server";
121         newArgs[1] = hostport;
122       }
123     }
124     // If command-line arguments, run our hack so they are executed.
125     // ZOOKEEPER-1897 was committed to zookeeper-3.4.6 but elsewhere in this class we say
126     // 3.4.6 breaks command-processing; TODO.
127     if (hasCommandLineArguments(args)) {
128       HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
129         new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
130       zkm.runCmdLine();
131     } else {
132       ZooKeeperMain.main(newArgs);
133     }
134   }
135 }