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.File;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.net.InetAddress;
26 import java.net.NetworkInterface;
27 import java.net.UnknownHostException;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.Map.Entry;
33
34 import org.apache.hadoop.conf.Configuration;
35 import org.apache.hadoop.hbase.HBaseConfiguration;
36 import org.apache.hadoop.hbase.util.Strings;
37 import org.apache.hadoop.net.DNS;
38 import org.apache.hadoop.util.StringUtils;
39 import org.apache.zookeeper.server.ServerConfig;
40 import org.apache.zookeeper.server.ZooKeeperServerMain;
41 import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
42 import org.apache.zookeeper.server.quorum.QuorumPeerMain;
43
44
45
46
47
48
49
50
51 public class HQuorumPeer {
52
53
54
55
56
57 public static void main(String[] args) {
58 Configuration conf = HBaseConfiguration.create();
59 try {
60 Properties zkProperties = ZKConfig.makeZKProps(conf);
61 writeMyID(zkProperties);
62 QuorumPeerConfig zkConfig = new QuorumPeerConfig();
63 zkConfig.parseProperties(zkProperties);
64 runZKServer(zkConfig);
65 } catch (Exception e) {
66 e.printStackTrace();
67 System.exit(-1);
68 }
69 }
70
71 private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException {
72 if (zkConfig.isDistributed()) {
73 QuorumPeerMain qp = new QuorumPeerMain();
74 qp.runFromConfig(zkConfig);
75 } else {
76 ZooKeeperServerMain zk = new ZooKeeperServerMain();
77 ServerConfig serverConfig = new ServerConfig();
78 serverConfig.readFrom(zkConfig);
79 zk.runFromConfig(serverConfig);
80 }
81 }
82
83 private static boolean addressIsLocalHost(String address) {
84 return address.equals("localhost") || address.equals("127.0.0.1");
85 }
86
87 static void writeMyID(Properties properties) throws IOException {
88 long myId = -1;
89
90 Configuration conf = HBaseConfiguration.create();
91 String myAddress = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
92 conf.get("hbase.zookeeper.dns.interface","default"),
93 conf.get("hbase.zookeeper.dns.nameserver","default")));
94
95 List<String> ips = new ArrayList<String>();
96
97
98 ips.add(myAddress.contains(".") ?
99 myAddress :
100 StringUtils.simpleHostname(myAddress));
101
102
103 Enumeration<?> nics = NetworkInterface.getNetworkInterfaces();
104 while(nics.hasMoreElements()) {
105 Enumeration<?> rawAdrs =
106 ((NetworkInterface)nics.nextElement()).getInetAddresses();
107 while(rawAdrs.hasMoreElements()) {
108 InetAddress inet = (InetAddress) rawAdrs.nextElement();
109 ips.add(StringUtils.simpleHostname(inet.getHostName()));
110 ips.add(inet.getHostAddress());
111 }
112 }
113
114 for (Entry<Object, Object> entry : properties.entrySet()) {
115 String key = entry.getKey().toString().trim();
116 String value = entry.getValue().toString().trim();
117 if (key.startsWith("server.")) {
118 int dot = key.indexOf('.');
119 long id = Long.parseLong(key.substring(dot + 1));
120 String[] parts = value.split(":");
121 String address = parts[0];
122 if (addressIsLocalHost(address) || ips.contains(address)) {
123 myId = id;
124 break;
125 }
126 }
127 }
128
129
130 properties.setProperty("maxSessionTimeout",
131 conf.get("zookeeper.session.timeout", "180000"));
132
133 if (myId == -1) {
134 throw new IOException("Could not find my address: " + myAddress +
135 " in list of ZooKeeper quorum servers");
136 }
137
138 String dataDirStr = properties.get("dataDir").toString().trim();
139 File dataDir = new File(dataDirStr);
140 if (!dataDir.isDirectory()) {
141 if (!dataDir.mkdirs()) {
142 throw new IOException("Unable to create data dir " + dataDir);
143 }
144 }
145
146 File myIdFile = new File(dataDir, "myid");
147 PrintWriter w = new PrintWriter(myIdFile);
148 w.println(myId);
149 w.close();
150 }
151 }