1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
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
46
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
53
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
66
67
68
69
70 void runCmdLine() throws KeeperException, IOException, InterruptedException {
71 processCmd(this.cl);
72 System.exit(0);
73 }
74 }
75
76
77
78
79
80 private static boolean hasServer(final String args[]) {
81 return args.length > 0 && args[0].equals(SERVER_ARG);
82 }
83
84
85
86
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
98
99
100 public static void main(String args[]) throws Exception {
101 String [] newArgs = args;
102 if (!hasServer(args)) {
103
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
114
115
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 }