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