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
65
66 ZKUtil.loginServer(conf, "hbase.zookeeper.server.keytab.file",
67 "hbase.zookeeper.server.kerberos.principal",
68 zkConfig.getClientPortAddress().getHostName());
69
70 runZKServer(zkConfig);
71 } catch (Exception e) {
72 e.printStackTrace();
73 System.exit(-1);
74 }
75 }
76
77 private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException {
78 if (zkConfig.isDistributed()) {
79 QuorumPeerMain qp = new QuorumPeerMain();
80 qp.runFromConfig(zkConfig);
81 } else {
82 ZooKeeperServerMain zk = new ZooKeeperServerMain();
83 ServerConfig serverConfig = new ServerConfig();
84 serverConfig.readFrom(zkConfig);
85 zk.runFromConfig(serverConfig);
86 }
87 }
88
89 private static boolean addressIsLocalHost(String address) {
90 return address.equals("localhost") || address.equals("127.0.0.1");
91 }
92
93 static void writeMyID(Properties properties) throws IOException {
94 long myId = -1;
95
96 Configuration conf = HBaseConfiguration.create();
97 String myAddress = Strings.domainNamePointerToHostName(DNS.getDefaultHost(
98 conf.get("hbase.zookeeper.dns.interface","default"),
99 conf.get("hbase.zookeeper.dns.nameserver","default")));
100
101 List<String> ips = new ArrayList<String>();
102
103
104 ips.add(myAddress.contains(".") ?
105 myAddress :
106 StringUtils.simpleHostname(myAddress));
107
108
109 Enumeration<?> nics = NetworkInterface.getNetworkInterfaces();
110 while(nics.hasMoreElements()) {
111 Enumeration<?> rawAdrs =
112 ((NetworkInterface)nics.nextElement()).getInetAddresses();
113 while(rawAdrs.hasMoreElements()) {
114 InetAddress inet = (InetAddress) rawAdrs.nextElement();
115 ips.add(StringUtils.simpleHostname(inet.getHostName()));
116 ips.add(inet.getHostAddress());
117 }
118 }
119
120 for (Entry<Object, Object> entry : properties.entrySet()) {
121 String key = entry.getKey().toString().trim();
122 String value = entry.getValue().toString().trim();
123 if (key.startsWith("server.")) {
124 int dot = key.indexOf('.');
125 long id = Long.parseLong(key.substring(dot + 1));
126 String[] parts = value.split(":");
127 String address = parts[0];
128 if (addressIsLocalHost(address) || ips.contains(address)) {
129 myId = id;
130 break;
131 }
132 }
133 }
134
135
136 properties.setProperty("maxSessionTimeout",
137 conf.get("zookeeper.session.timeout", "180000"));
138
139 if (myId == -1) {
140 throw new IOException("Could not find my address: " + myAddress +
141 " in list of ZooKeeper quorum servers");
142 }
143
144 String dataDirStr = properties.get("dataDir").toString().trim();
145 File dataDir = new File(dataDirStr);
146 if (!dataDir.isDirectory()) {
147 if (!dataDir.mkdirs()) {
148 throw new IOException("Unable to create data dir " + dataDir);
149 }
150 }
151
152 File myIdFile = new File(dataDir, "myid");
153 PrintWriter w = new PrintWriter(myIdFile);
154 w.println(myId);
155 w.close();
156 }
157 }