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