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