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.ArrayList;
24  import java.util.List;
25  import java.util.Map.Entry;
26  import java.util.Properties;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.zookeeper.KeeperException;
33  import org.apache.zookeeper.ZooKeeperMain;
34  
35  /**
36   * Tool for running ZookeeperMain from HBase by  reading a ZooKeeper server
37   * from HBase XML configuration.
38   */
39  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
40  public class ZooKeeperMainServer {
41    private static final String SERVER_ARG = "-server";
42  
43    public String parse(final Configuration c) {
44      // Note that we do not simply grab the property
45      // HConstants.ZOOKEEPER_QUORUM from the HBaseConfiguration because the
46      // user may be using a zoo.cfg file.
47      Properties zkProps = ZKConfig.makeZKProps(c);
48      String clientPort = null;
49      List<String> hosts = new ArrayList<String>();
50      for (Entry<Object, Object> entry: zkProps.entrySet()) {
51        String key = entry.getKey().toString().trim();
52        String value = entry.getValue().toString().trim();
53        if (key.startsWith("server.")) {
54          String[] parts = value.split(":");
55          hosts.add(parts[0]);
56        } else if (key.endsWith("clientPort")) {
57          clientPort = value;
58        }
59      }
60      if (hosts.isEmpty() || clientPort == null) return null;
61      StringBuilder host = new StringBuilder();
62      for (int i = 0; i < hosts.size(); i++) {
63        if (i > 0)  host.append("," + hosts.get(i));
64        else host.append(hosts.get(i));
65        host.append(":");
66        host.append(clientPort);
67      }
68      return host.toString();
69    }
70  
71    /**
72     * ZooKeeper 3.4.6 broke being able to pass commands on command line.
73     * See ZOOKEEPER-1897.  This class is a hack to restore this faclity.
74     */
75    private static class HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain extends ZooKeeperMain {
76      public HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(String[] args)
77      throws IOException, InterruptedException {
78        super(args);
79      }
80  
81      /**
82       * Run the command-line args passed.  Calls System.exit when done.
83       * @throws KeeperException
84       * @throws IOException
85       * @throws InterruptedException
86       */
87      void runCmdLine() throws KeeperException, IOException, InterruptedException {
88        processCmd(this.cl);
89        System.exit(0);
90      }
91    }
92  
93    /**
94     * @param args
95     * @return True if argument strings have a '-server' in them.
96     */
97    private static boolean hasServer(final String args[]) {
98      return args.length > 0 && args[0].equals(SERVER_ARG);
99    }
100 
101   /**
102    * @param args
103    * @return True if command-line arguments were passed.
104    */
105   private static boolean hasCommandLineArguments(final String args[]) {
106     if (hasServer(args)) {
107       if (args.length < 2) throw new IllegalStateException("-server param but no value");
108       return args.length > 2;
109     }
110     return args.length > 0;
111   }
112 
113   /**
114    * Run the tool.
115    * @param args Command line arguments. First arg is path to zookeepers file.
116    */
117   public static void main(String args[]) throws Exception {
118     String [] newArgs = args;
119     if (!hasServer(args)) {
120       // Add the zk ensemble from configuration if none passed on command-line.
121       Configuration conf = HBaseConfiguration.create();
122       String hostport = new ZooKeeperMainServer().parse(conf);
123       if (hostport != null && hostport.length() > 0) {
124         newArgs = new String[args.length + 2];
125         System.arraycopy(args, 0, newArgs, 2, args.length);
126         newArgs[0] = "-server";
127         newArgs[1] = hostport;
128       }
129     }
130     // If command-line arguments, run our hack so they are executed.
131     if (hasCommandLineArguments(args)) {
132       HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain zkm =
133         new HACK_UNTIL_ZOOKEEPER_1897_ZooKeeperMain(newArgs);
134       zkm.runCmdLine();
135     } else {
136       ZooKeeperMain.main(newArgs);
137     }
138   }
139 }