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