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