hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From git-site-r...@apache.org
Subject [09/41] hbase-site git commit: Published site at d8b550fabcacf3050583b514c4081adf5d087d65.
Date Thu, 29 Mar 2018 14:47:57 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/2a4c7d89/devapidocs/src-html/org/apache/hadoop/hbase/zookeeper/ZKConfig.ZKClusterKey.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/zookeeper/ZKConfig.ZKClusterKey.html b/devapidocs/src-html/org/apache/hadoop/hbase/zookeeper/ZKConfig.ZKClusterKey.html
index 9b42e6b..6c6f61c 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/zookeeper/ZKConfig.ZKClusterKey.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/zookeeper/ZKConfig.ZKClusterKey.html
@@ -32,293 +32,313 @@
 <span class="sourceLineNo">024</span><a name="line.24"></a>
 <span class="sourceLineNo">025</span>import org.apache.hadoop.conf.Configuration;<a name="line.25"></a>
 <span class="sourceLineNo">026</span>import org.apache.hadoop.hbase.HConstants;<a name="line.26"></a>
-<span class="sourceLineNo">027</span>import org.apache.yetus.audience.InterfaceAudience;<a name="line.27"></a>
-<span class="sourceLineNo">028</span><a name="line.28"></a>
-<span class="sourceLineNo">029</span>import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;<a name="line.29"></a>
-<span class="sourceLineNo">030</span><a name="line.30"></a>
-<span class="sourceLineNo">031</span>/**<a name="line.31"></a>
-<span class="sourceLineNo">032</span> * Utility methods for reading, and building the ZooKeeper configuration.<a name="line.32"></a>
-<span class="sourceLineNo">033</span> *<a name="line.33"></a>
-<span class="sourceLineNo">034</span> * The order and priority for reading the config are as follows:<a name="line.34"></a>
-<span class="sourceLineNo">035</span> * (1). Property with "hbase.zookeeper.property." prefix from HBase XML<a name="line.35"></a>
-<span class="sourceLineNo">036</span> * (2). other zookeeper related properties in HBASE XML<a name="line.36"></a>
-<span class="sourceLineNo">037</span> */<a name="line.37"></a>
-<span class="sourceLineNo">038</span>@InterfaceAudience.Private<a name="line.38"></a>
-<span class="sourceLineNo">039</span>public final class ZKConfig {<a name="line.39"></a>
-<span class="sourceLineNo">040</span><a name="line.40"></a>
-<span class="sourceLineNo">041</span>  private static final String VARIABLE_START = "${";<a name="line.41"></a>
-<span class="sourceLineNo">042</span><a name="line.42"></a>
-<span class="sourceLineNo">043</span>  private ZKConfig() {<a name="line.43"></a>
-<span class="sourceLineNo">044</span>  }<a name="line.44"></a>
-<span class="sourceLineNo">045</span><a name="line.45"></a>
-<span class="sourceLineNo">046</span>  /**<a name="line.46"></a>
-<span class="sourceLineNo">047</span>   * Make a Properties object holding ZooKeeper config.<a name="line.47"></a>
-<span class="sourceLineNo">048</span>   * Parses the corresponding config options from the HBase XML configs<a name="line.48"></a>
-<span class="sourceLineNo">049</span>   * and generates the appropriate ZooKeeper properties.<a name="line.49"></a>
-<span class="sourceLineNo">050</span>   * @param conf Configuration to read from.<a name="line.50"></a>
-<span class="sourceLineNo">051</span>   * @return Properties holding mappings representing ZooKeeper config file.<a name="line.51"></a>
-<span class="sourceLineNo">052</span>   */<a name="line.52"></a>
-<span class="sourceLineNo">053</span>  public static Properties makeZKProps(Configuration conf) {<a name="line.53"></a>
-<span class="sourceLineNo">054</span>    return makeZKPropsFromHbaseConfig(conf);<a name="line.54"></a>
-<span class="sourceLineNo">055</span>  }<a name="line.55"></a>
-<span class="sourceLineNo">056</span><a name="line.56"></a>
-<span class="sourceLineNo">057</span>  /**<a name="line.57"></a>
-<span class="sourceLineNo">058</span>   * Make a Properties object holding ZooKeeper config.<a name="line.58"></a>
-<span class="sourceLineNo">059</span>   * Parses the corresponding config options from the HBase XML configs<a name="line.59"></a>
-<span class="sourceLineNo">060</span>   * and generates the appropriate ZooKeeper properties.<a name="line.60"></a>
-<span class="sourceLineNo">061</span>   *<a name="line.61"></a>
-<span class="sourceLineNo">062</span>   * @param conf Configuration to read from.<a name="line.62"></a>
-<span class="sourceLineNo">063</span>   * @return Properties holding mappings representing ZooKeeper config file.<a name="line.63"></a>
-<span class="sourceLineNo">064</span>   */<a name="line.64"></a>
-<span class="sourceLineNo">065</span>  private static Properties makeZKPropsFromHbaseConfig(Configuration conf) {<a name="line.65"></a>
-<span class="sourceLineNo">066</span>    Properties zkProperties = new Properties();<a name="line.66"></a>
-<span class="sourceLineNo">067</span><a name="line.67"></a>
-<span class="sourceLineNo">068</span>    // Directly map all of the hbase.zookeeper.property.KEY properties.<a name="line.68"></a>
-<span class="sourceLineNo">069</span>    // Synchronize on conf so no loading of configs while we iterate<a name="line.69"></a>
-<span class="sourceLineNo">070</span>    synchronized (conf) {<a name="line.70"></a>
-<span class="sourceLineNo">071</span>      for (Entry&lt;String, String&gt; entry : conf) {<a name="line.71"></a>
-<span class="sourceLineNo">072</span>        String key = entry.getKey();<a name="line.72"></a>
-<span class="sourceLineNo">073</span>        if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {<a name="line.73"></a>
-<span class="sourceLineNo">074</span>          String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);<a name="line.74"></a>
-<span class="sourceLineNo">075</span>          String value = entry.getValue();<a name="line.75"></a>
-<span class="sourceLineNo">076</span>          // If the value has variables substitutions, need to do a get.<a name="line.76"></a>
-<span class="sourceLineNo">077</span>          if (value.contains(VARIABLE_START)) {<a name="line.77"></a>
-<span class="sourceLineNo">078</span>            value = conf.get(key);<a name="line.78"></a>
-<span class="sourceLineNo">079</span>          }<a name="line.79"></a>
-<span class="sourceLineNo">080</span>          zkProperties.setProperty(zkKey, value);<a name="line.80"></a>
-<span class="sourceLineNo">081</span>        }<a name="line.81"></a>
-<span class="sourceLineNo">082</span>      }<a name="line.82"></a>
-<span class="sourceLineNo">083</span>    }<a name="line.83"></a>
-<span class="sourceLineNo">084</span><a name="line.84"></a>
-<span class="sourceLineNo">085</span>    // If clientPort is not set, assign the default.<a name="line.85"></a>
-<span class="sourceLineNo">086</span>    if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {<a name="line.86"></a>
-<span class="sourceLineNo">087</span>      zkProperties.put(HConstants.CLIENT_PORT_STR,<a name="line.87"></a>
-<span class="sourceLineNo">088</span>          HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT);<a name="line.88"></a>
-<span class="sourceLineNo">089</span>    }<a name="line.89"></a>
-<span class="sourceLineNo">090</span><a name="line.90"></a>
-<span class="sourceLineNo">091</span>    // Create the server.X properties.<a name="line.91"></a>
-<span class="sourceLineNo">092</span>    int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);<a name="line.92"></a>
-<span class="sourceLineNo">093</span>    int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);<a name="line.93"></a>
-<span class="sourceLineNo">094</span><a name="line.94"></a>
-<span class="sourceLineNo">095</span>    final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,<a name="line.95"></a>
-<span class="sourceLineNo">096</span>                                                 HConstants.LOCALHOST);<a name="line.96"></a>
-<span class="sourceLineNo">097</span>    String serverHost;<a name="line.97"></a>
-<span class="sourceLineNo">098</span>    String address;<a name="line.98"></a>
-<span class="sourceLineNo">099</span>    String key;<a name="line.99"></a>
-<span class="sourceLineNo">100</span>    for (int i = 0; i &lt; serverHosts.length; ++i) {<a name="line.100"></a>
-<span class="sourceLineNo">101</span>      if (serverHosts[i].contains(":")) {<a name="line.101"></a>
-<span class="sourceLineNo">102</span>        serverHost = serverHosts[i].substring(0, serverHosts[i].indexOf(':'));<a name="line.102"></a>
-<span class="sourceLineNo">103</span>      } else {<a name="line.103"></a>
-<span class="sourceLineNo">104</span>        serverHost = serverHosts[i];<a name="line.104"></a>
-<span class="sourceLineNo">105</span>      }<a name="line.105"></a>
-<span class="sourceLineNo">106</span>      address = serverHost + ":" + peerPort + ":" + leaderPort;<a name="line.106"></a>
-<span class="sourceLineNo">107</span>      key = "server." + i;<a name="line.107"></a>
-<span class="sourceLineNo">108</span>      zkProperties.put(key, address);<a name="line.108"></a>
-<span class="sourceLineNo">109</span>    }<a name="line.109"></a>
-<span class="sourceLineNo">110</span><a name="line.110"></a>
-<span class="sourceLineNo">111</span>    return zkProperties;<a name="line.111"></a>
-<span class="sourceLineNo">112</span>  }<a name="line.112"></a>
-<span class="sourceLineNo">113</span><a name="line.113"></a>
-<span class="sourceLineNo">114</span>  /**<a name="line.114"></a>
-<span class="sourceLineNo">115</span>   * Return the ZK Quorum servers string given the specified configuration<a name="line.115"></a>
-<span class="sourceLineNo">116</span>   *<a name="line.116"></a>
-<span class="sourceLineNo">117</span>   * @param conf<a name="line.117"></a>
-<span class="sourceLineNo">118</span>   * @return Quorum servers String<a name="line.118"></a>
-<span class="sourceLineNo">119</span>   */<a name="line.119"></a>
-<span class="sourceLineNo">120</span>  private static String getZKQuorumServersStringFromHbaseConfig(Configuration conf) {<a name="line.120"></a>
-<span class="sourceLineNo">121</span>    String defaultClientPort = Integer.toString(<a name="line.121"></a>
-<span class="sourceLineNo">122</span>        conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT));<a name="line.122"></a>
-<span class="sourceLineNo">123</span><a name="line.123"></a>
-<span class="sourceLineNo">124</span>    // Build the ZK quorum server string with "server:clientport" list, separated by ','<a name="line.124"></a>
-<span class="sourceLineNo">125</span>    final String[] serverHosts =<a name="line.125"></a>
-<span class="sourceLineNo">126</span>        conf.getStrings(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST);<a name="line.126"></a>
-<span class="sourceLineNo">127</span>    return buildZKQuorumServerString(serverHosts, defaultClientPort);<a name="line.127"></a>
-<span class="sourceLineNo">128</span>  }<a name="line.128"></a>
-<span class="sourceLineNo">129</span><a name="line.129"></a>
-<span class="sourceLineNo">130</span>  /**<a name="line.130"></a>
-<span class="sourceLineNo">131</span>   * Return the ZK Quorum servers string given the specified configuration.<a name="line.131"></a>
-<span class="sourceLineNo">132</span>   * @return Quorum servers<a name="line.132"></a>
-<span class="sourceLineNo">133</span>   */<a name="line.133"></a>
-<span class="sourceLineNo">134</span>  public static String getZKQuorumServersString(Configuration conf) {<a name="line.134"></a>
-<span class="sourceLineNo">135</span>    return getZKQuorumServersStringFromHbaseConfig(conf);<a name="line.135"></a>
-<span class="sourceLineNo">136</span>  }<a name="line.136"></a>
-<span class="sourceLineNo">137</span><a name="line.137"></a>
-<span class="sourceLineNo">138</span>  /**<a name="line.138"></a>
-<span class="sourceLineNo">139</span>   * Build the ZK quorum server string with "server:clientport" list, separated by ','<a name="line.139"></a>
-<span class="sourceLineNo">140</span>   *<a name="line.140"></a>
-<span class="sourceLineNo">141</span>   * @param serverHosts a list of servers for ZK quorum<a name="line.141"></a>
-<span class="sourceLineNo">142</span>   * @param clientPort the default client port<a name="line.142"></a>
-<span class="sourceLineNo">143</span>   * @return the string for a list of "server:port" separated by ","<a name="line.143"></a>
-<span class="sourceLineNo">144</span>   */<a name="line.144"></a>
-<span class="sourceLineNo">145</span>  public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) {<a name="line.145"></a>
-<span class="sourceLineNo">146</span>    StringBuilder quorumStringBuilder = new StringBuilder();<a name="line.146"></a>
-<span class="sourceLineNo">147</span>    String serverHost;<a name="line.147"></a>
-<span class="sourceLineNo">148</span>    for (int i = 0; i &lt; serverHosts.length; ++i) {<a name="line.148"></a>
-<span class="sourceLineNo">149</span>      if (serverHosts[i].contains(":")) {<a name="line.149"></a>
-<span class="sourceLineNo">150</span>        serverHost = serverHosts[i]; // just use the port specified from the input<a name="line.150"></a>
-<span class="sourceLineNo">151</span>      } else {<a name="line.151"></a>
-<span class="sourceLineNo">152</span>        serverHost = serverHosts[i] + ":" + clientPort;<a name="line.152"></a>
-<span class="sourceLineNo">153</span>      }<a name="line.153"></a>
-<span class="sourceLineNo">154</span>      if (i &gt; 0) {<a name="line.154"></a>
-<span class="sourceLineNo">155</span>        quorumStringBuilder.append(',');<a name="line.155"></a>
-<span class="sourceLineNo">156</span>      }<a name="line.156"></a>
-<span class="sourceLineNo">157</span>      quorumStringBuilder.append(serverHost);<a name="line.157"></a>
-<span class="sourceLineNo">158</span>    }<a name="line.158"></a>
-<span class="sourceLineNo">159</span>    return quorumStringBuilder.toString();<a name="line.159"></a>
-<span class="sourceLineNo">160</span>  }<a name="line.160"></a>
-<span class="sourceLineNo">161</span><a name="line.161"></a>
-<span class="sourceLineNo">162</span>  /**<a name="line.162"></a>
-<span class="sourceLineNo">163</span>   * Verifies that the given key matches the expected format for a ZooKeeper cluster key.<a name="line.163"></a>
-<span class="sourceLineNo">164</span>   * The Quorum for the ZK cluster can have one the following formats (see examples below):<a name="line.164"></a>
-<span class="sourceLineNo">165</span>   *<a name="line.165"></a>
-<span class="sourceLineNo">166</span>   * &lt;ol&gt;<a name="line.166"></a>
-<span class="sourceLineNo">167</span>   *   &lt;li&gt;s1,s2,s3 (no client port in the list, the client port could be obtained from<a name="line.167"></a>
-<span class="sourceLineNo">168</span>   *       clientPort)&lt;/li&gt;<a name="line.168"></a>
-<span class="sourceLineNo">169</span>   *   &lt;li&gt;s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,<a name="line.169"></a>
-<span class="sourceLineNo">170</span>   *       in this case, the clientPort would be ignored)&lt;/li&gt;<a name="line.170"></a>
-<span class="sourceLineNo">171</span>   *   &lt;li&gt;s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use<a name="line.171"></a>
-<span class="sourceLineNo">172</span>   *       the clientPort; otherwise, it would use the specified port)&lt;/li&gt;<a name="line.172"></a>
-<span class="sourceLineNo">173</span>   * &lt;/ol&gt;<a name="line.173"></a>
-<span class="sourceLineNo">174</span>   *<a name="line.174"></a>
-<span class="sourceLineNo">175</span>   * @param key the cluster key to validate<a name="line.175"></a>
-<span class="sourceLineNo">176</span>   * @throws IOException if the key could not be parsed<a name="line.176"></a>
-<span class="sourceLineNo">177</span>   */<a name="line.177"></a>
-<span class="sourceLineNo">178</span>  public static void validateClusterKey(String key) throws IOException {<a name="line.178"></a>
-<span class="sourceLineNo">179</span>    transformClusterKey(key);<a name="line.179"></a>
-<span class="sourceLineNo">180</span>  }<a name="line.180"></a>
-<span class="sourceLineNo">181</span><a name="line.181"></a>
-<span class="sourceLineNo">182</span>  /**<a name="line.182"></a>
-<span class="sourceLineNo">183</span>   * Separate the given key into the three configurations it should contain:<a name="line.183"></a>
-<span class="sourceLineNo">184</span>   * hbase.zookeeper.quorum, hbase.zookeeper.client.port<a name="line.184"></a>
-<span class="sourceLineNo">185</span>   * and zookeeper.znode.parent<a name="line.185"></a>
-<span class="sourceLineNo">186</span>   * @param key<a name="line.186"></a>
-<span class="sourceLineNo">187</span>   * @return the three configuration in the described order<a name="line.187"></a>
-<span class="sourceLineNo">188</span>   * @throws IOException<a name="line.188"></a>
-<span class="sourceLineNo">189</span>   */<a name="line.189"></a>
-<span class="sourceLineNo">190</span>  public static ZKClusterKey transformClusterKey(String key) throws IOException {<a name="line.190"></a>
-<span class="sourceLineNo">191</span>    String[] parts = key.split(":");<a name="line.191"></a>
-<span class="sourceLineNo">192</span><a name="line.192"></a>
-<span class="sourceLineNo">193</span>    if (parts.length == 3) {<a name="line.193"></a>
-<span class="sourceLineNo">194</span>      if (!parts[2].matches("/.*[^/]")) {<a name="line.194"></a>
-<span class="sourceLineNo">195</span>        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +<a name="line.195"></a>
-<span class="sourceLineNo">196</span>            HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.196"></a>
-<span class="sourceLineNo">197</span>            + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.197"></a>
-<span class="sourceLineNo">198</span>      }<a name="line.198"></a>
-<span class="sourceLineNo">199</span>      return new ZKClusterKey(parts [0], Integer.parseInt(parts [1]), parts [2]);<a name="line.199"></a>
-<span class="sourceLineNo">200</span>    }<a name="line.200"></a>
-<span class="sourceLineNo">201</span><a name="line.201"></a>
-<span class="sourceLineNo">202</span>    if (parts.length &gt; 3) {<a name="line.202"></a>
-<span class="sourceLineNo">203</span>      // The quorum could contain client port in server:clientport format, try to transform more.<a name="line.203"></a>
-<span class="sourceLineNo">204</span>      String zNodeParent = parts [parts.length - 1];<a name="line.204"></a>
-<span class="sourceLineNo">205</span>      if (!zNodeParent.matches("/.*[^/]")) {<a name="line.205"></a>
-<span class="sourceLineNo">206</span>        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:"<a name="line.206"></a>
-<span class="sourceLineNo">207</span>            + HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.207"></a>
-<span class="sourceLineNo">208</span>            + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.208"></a>
-<span class="sourceLineNo">209</span>      }<a name="line.209"></a>
-<span class="sourceLineNo">210</span><a name="line.210"></a>
-<span class="sourceLineNo">211</span>      String clientPort = parts [parts.length - 2];<a name="line.211"></a>
-<span class="sourceLineNo">212</span><a name="line.212"></a>
-<span class="sourceLineNo">213</span>      // The first part length is the total length minus the lengths of other parts and minus 2 ":"<a name="line.213"></a>
-<span class="sourceLineNo">214</span>      int endQuorumIndex = key.length() - zNodeParent.length() - clientPort.length() - 2;<a name="line.214"></a>
-<span class="sourceLineNo">215</span>      String quorumStringInput = key.substring(0, endQuorumIndex);<a name="line.215"></a>
-<span class="sourceLineNo">216</span>      String[] serverHosts = quorumStringInput.split(",");<a name="line.216"></a>
-<span class="sourceLineNo">217</span><a name="line.217"></a>
-<span class="sourceLineNo">218</span>      // The common case is that every server has its own client port specified - this means<a name="line.218"></a>
-<span class="sourceLineNo">219</span>      // that (total parts - the ZNodeParent part - the ClientPort part) is equal to<a name="line.219"></a>
-<span class="sourceLineNo">220</span>      // (the number of "," + 1) - "+ 1" because the last server has no ",".<a name="line.220"></a>
-<span class="sourceLineNo">221</span>      if ((parts.length - 2) == (serverHosts.length + 1)) {<a name="line.221"></a>
-<span class="sourceLineNo">222</span>        return new ZKClusterKey(quorumStringInput, Integer.parseInt(clientPort), zNodeParent);<a name="line.222"></a>
-<span class="sourceLineNo">223</span>      }<a name="line.223"></a>
-<span class="sourceLineNo">224</span><a name="line.224"></a>
-<span class="sourceLineNo">225</span>      // For the uncommon case that some servers has no port specified, we need to build the<a name="line.225"></a>
-<span class="sourceLineNo">226</span>      // server:clientport list using default client port for servers without specified port.<a name="line.226"></a>
-<span class="sourceLineNo">227</span>      return new ZKClusterKey(<a name="line.227"></a>
-<span class="sourceLineNo">228</span>          buildZKQuorumServerString(serverHosts, clientPort),<a name="line.228"></a>
-<span class="sourceLineNo">229</span>          Integer.parseInt(clientPort),<a name="line.229"></a>
-<span class="sourceLineNo">230</span>          zNodeParent);<a name="line.230"></a>
-<span class="sourceLineNo">231</span>    }<a name="line.231"></a>
-<span class="sourceLineNo">232</span><a name="line.232"></a>
-<span class="sourceLineNo">233</span>    throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +<a name="line.233"></a>
-<span class="sourceLineNo">234</span>        HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.234"></a>
-<span class="sourceLineNo">235</span>        + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.235"></a>
-<span class="sourceLineNo">236</span>  }<a name="line.236"></a>
-<span class="sourceLineNo">237</span><a name="line.237"></a>
-<span class="sourceLineNo">238</span>  /**<a name="line.238"></a>
-<span class="sourceLineNo">239</span>   * Get the key to the ZK ensemble for this configuration without<a name="line.239"></a>
-<span class="sourceLineNo">240</span>   * adding a name at the end<a name="line.240"></a>
-<span class="sourceLineNo">241</span>   * @param conf Configuration to use to build the key<a name="line.241"></a>
-<span class="sourceLineNo">242</span>   * @return ensemble key without a name<a name="line.242"></a>
-<span class="sourceLineNo">243</span>   */<a name="line.243"></a>
-<span class="sourceLineNo">244</span>  public static String getZooKeeperClusterKey(Configuration conf) {<a name="line.244"></a>
-<span class="sourceLineNo">245</span>    return getZooKeeperClusterKey(conf, null);<a name="line.245"></a>
-<span class="sourceLineNo">246</span>  }<a name="line.246"></a>
-<span class="sourceLineNo">247</span><a name="line.247"></a>
-<span class="sourceLineNo">248</span>  /**<a name="line.248"></a>
-<span class="sourceLineNo">249</span>   * Get the key to the ZK ensemble for this configuration and append<a name="line.249"></a>
-<span class="sourceLineNo">250</span>   * a name at the end<a name="line.250"></a>
-<span class="sourceLineNo">251</span>   * @param conf Configuration to use to build the key<a name="line.251"></a>
-<span class="sourceLineNo">252</span>   * @param name Name that should be appended at the end if not empty or null<a name="line.252"></a>
-<span class="sourceLineNo">253</span>   * @return ensemble key with a name (if any)<a name="line.253"></a>
-<span class="sourceLineNo">254</span>   */<a name="line.254"></a>
-<span class="sourceLineNo">255</span>  public static String getZooKeeperClusterKey(Configuration conf, String name) {<a name="line.255"></a>
-<span class="sourceLineNo">256</span>    String ensemble = conf.get(HConstants.ZOOKEEPER_QUORUM).replaceAll(<a name="line.256"></a>
-<span class="sourceLineNo">257</span>        "[\\t\\n\\x0B\\f\\r]", "");<a name="line.257"></a>
-<span class="sourceLineNo">258</span>    StringBuilder builder = new StringBuilder(ensemble);<a name="line.258"></a>
-<span class="sourceLineNo">259</span>    builder.append(":");<a name="line.259"></a>
-<span class="sourceLineNo">260</span>    builder.append(conf.get(HConstants.ZOOKEEPER_CLIENT_PORT));<a name="line.260"></a>
-<span class="sourceLineNo">261</span>    builder.append(":");<a name="line.261"></a>
-<span class="sourceLineNo">262</span>    builder.append(conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));<a name="line.262"></a>
-<span class="sourceLineNo">263</span>    if (name != null &amp;&amp; !name.isEmpty()) {<a name="line.263"></a>
-<span class="sourceLineNo">264</span>      builder.append(",");<a name="line.264"></a>
-<span class="sourceLineNo">265</span>      builder.append(name);<a name="line.265"></a>
-<span class="sourceLineNo">266</span>    }<a name="line.266"></a>
-<span class="sourceLineNo">267</span>    return builder.toString();<a name="line.267"></a>
-<span class="sourceLineNo">268</span>  }<a name="line.268"></a>
-<span class="sourceLineNo">269</span><a name="line.269"></a>
-<span class="sourceLineNo">270</span>  /**<a name="line.270"></a>
-<span class="sourceLineNo">271</span>   * Standardize the ZK quorum string: make it a "server:clientport" list, separated by ','<a name="line.271"></a>
-<span class="sourceLineNo">272</span>   * @param quorumStringInput a string contains a list of servers for ZK quorum<a name="line.272"></a>
-<span class="sourceLineNo">273</span>   * @param clientPort the default client port<a name="line.273"></a>
-<span class="sourceLineNo">274</span>   * @return the string for a list of "server:port" separated by ","<a name="line.274"></a>
-<span class="sourceLineNo">275</span>   */<a name="line.275"></a>
-<span class="sourceLineNo">276</span>  @VisibleForTesting<a name="line.276"></a>
-<span class="sourceLineNo">277</span>  public static String standardizeZKQuorumServerString(String quorumStringInput,<a name="line.277"></a>
-<span class="sourceLineNo">278</span>      String clientPort) {<a name="line.278"></a>
-<span class="sourceLineNo">279</span>    String[] serverHosts = quorumStringInput.split(",");<a name="line.279"></a>
-<span class="sourceLineNo">280</span>    return buildZKQuorumServerString(serverHosts, clientPort);<a name="line.280"></a>
-<span class="sourceLineNo">281</span>  }<a name="line.281"></a>
-<span class="sourceLineNo">282</span><a name="line.282"></a>
-<span class="sourceLineNo">283</span>  // The Quorum for the ZK cluster can have one the following format (see examples below):<a name="line.283"></a>
-<span class="sourceLineNo">284</span>  // (1). s1,s2,s3 (no client port in the list, the client port could be obtained from clientPort)<a name="line.284"></a>
-<span class="sourceLineNo">285</span>  // (2). s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,<a name="line.285"></a>
-<span class="sourceLineNo">286</span>  //      in this case, the clientPort would be ignored)<a name="line.286"></a>
-<span class="sourceLineNo">287</span>  // (3). s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use<a name="line.287"></a>
-<span class="sourceLineNo">288</span>  //      the clientPort; otherwise, it would use the specified port)<a name="line.288"></a>
-<span class="sourceLineNo">289</span>  @VisibleForTesting<a name="line.289"></a>
-<span class="sourceLineNo">290</span>  public static class ZKClusterKey {<a name="line.290"></a>
-<span class="sourceLineNo">291</span>    private String quorumString;<a name="line.291"></a>
-<span class="sourceLineNo">292</span>    private int clientPort;<a name="line.292"></a>
-<span class="sourceLineNo">293</span>    private String znodeParent;<a name="line.293"></a>
-<span class="sourceLineNo">294</span><a name="line.294"></a>
-<span class="sourceLineNo">295</span>    ZKClusterKey(String quorumString, int clientPort, String znodeParent) {<a name="line.295"></a>
-<span class="sourceLineNo">296</span>      this.quorumString = quorumString;<a name="line.296"></a>
-<span class="sourceLineNo">297</span>      this.clientPort = clientPort;<a name="line.297"></a>
-<span class="sourceLineNo">298</span>      this.znodeParent = znodeParent;<a name="line.298"></a>
-<span class="sourceLineNo">299</span>    }<a name="line.299"></a>
-<span class="sourceLineNo">300</span><a name="line.300"></a>
-<span class="sourceLineNo">301</span>    public String getQuorumString() {<a name="line.301"></a>
-<span class="sourceLineNo">302</span>      return quorumString;<a name="line.302"></a>
-<span class="sourceLineNo">303</span>    }<a name="line.303"></a>
-<span class="sourceLineNo">304</span><a name="line.304"></a>
-<span class="sourceLineNo">305</span>    public int getClientPort() {<a name="line.305"></a>
-<span class="sourceLineNo">306</span>      return clientPort;<a name="line.306"></a>
-<span class="sourceLineNo">307</span>    }<a name="line.307"></a>
-<span class="sourceLineNo">308</span><a name="line.308"></a>
-<span class="sourceLineNo">309</span>    public String getZnodeParent() {<a name="line.309"></a>
-<span class="sourceLineNo">310</span>      return znodeParent;<a name="line.310"></a>
-<span class="sourceLineNo">311</span>    }<a name="line.311"></a>
-<span class="sourceLineNo">312</span>  }<a name="line.312"></a>
-<span class="sourceLineNo">313</span>}<a name="line.313"></a>
+<span class="sourceLineNo">027</span>import org.apache.hadoop.util.StringUtils;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import org.apache.yetus.audience.InterfaceAudience;<a name="line.28"></a>
+<span class="sourceLineNo">029</span><a name="line.29"></a>
+<span class="sourceLineNo">030</span>import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;<a name="line.30"></a>
+<span class="sourceLineNo">031</span><a name="line.31"></a>
+<span class="sourceLineNo">032</span>/**<a name="line.32"></a>
+<span class="sourceLineNo">033</span> * Utility methods for reading, and building the ZooKeeper configuration.<a name="line.33"></a>
+<span class="sourceLineNo">034</span> *<a name="line.34"></a>
+<span class="sourceLineNo">035</span> * The order and priority for reading the config are as follows:<a name="line.35"></a>
+<span class="sourceLineNo">036</span> * (1). Property with "hbase.zookeeper.property." prefix from HBase XML<a name="line.36"></a>
+<span class="sourceLineNo">037</span> * (2). other zookeeper related properties in HBASE XML<a name="line.37"></a>
+<span class="sourceLineNo">038</span> */<a name="line.38"></a>
+<span class="sourceLineNo">039</span>@InterfaceAudience.Private<a name="line.39"></a>
+<span class="sourceLineNo">040</span>public final class ZKConfig {<a name="line.40"></a>
+<span class="sourceLineNo">041</span><a name="line.41"></a>
+<span class="sourceLineNo">042</span>  private static final String VARIABLE_START = "${";<a name="line.42"></a>
+<span class="sourceLineNo">043</span><a name="line.43"></a>
+<span class="sourceLineNo">044</span>  private ZKConfig() {<a name="line.44"></a>
+<span class="sourceLineNo">045</span>  }<a name="line.45"></a>
+<span class="sourceLineNo">046</span><a name="line.46"></a>
+<span class="sourceLineNo">047</span>  /**<a name="line.47"></a>
+<span class="sourceLineNo">048</span>   * Make a Properties object holding ZooKeeper config.<a name="line.48"></a>
+<span class="sourceLineNo">049</span>   * Parses the corresponding config options from the HBase XML configs<a name="line.49"></a>
+<span class="sourceLineNo">050</span>   * and generates the appropriate ZooKeeper properties.<a name="line.50"></a>
+<span class="sourceLineNo">051</span>   * @param conf Configuration to read from.<a name="line.51"></a>
+<span class="sourceLineNo">052</span>   * @return Properties holding mappings representing ZooKeeper config file.<a name="line.52"></a>
+<span class="sourceLineNo">053</span>   */<a name="line.53"></a>
+<span class="sourceLineNo">054</span>  public static Properties makeZKProps(Configuration conf) {<a name="line.54"></a>
+<span class="sourceLineNo">055</span>    return makeZKPropsFromHbaseConfig(conf);<a name="line.55"></a>
+<span class="sourceLineNo">056</span>  }<a name="line.56"></a>
+<span class="sourceLineNo">057</span><a name="line.57"></a>
+<span class="sourceLineNo">058</span>  /**<a name="line.58"></a>
+<span class="sourceLineNo">059</span>   * Make a Properties object holding ZooKeeper config.<a name="line.59"></a>
+<span class="sourceLineNo">060</span>   * Parses the corresponding config options from the HBase XML configs<a name="line.60"></a>
+<span class="sourceLineNo">061</span>   * and generates the appropriate ZooKeeper properties.<a name="line.61"></a>
+<span class="sourceLineNo">062</span>   *<a name="line.62"></a>
+<span class="sourceLineNo">063</span>   * @param conf Configuration to read from.<a name="line.63"></a>
+<span class="sourceLineNo">064</span>   * @return Properties holding mappings representing ZooKeeper config file.<a name="line.64"></a>
+<span class="sourceLineNo">065</span>   */<a name="line.65"></a>
+<span class="sourceLineNo">066</span>  private static Properties makeZKPropsFromHbaseConfig(Configuration conf) {<a name="line.66"></a>
+<span class="sourceLineNo">067</span>    Properties zkProperties = new Properties();<a name="line.67"></a>
+<span class="sourceLineNo">068</span><a name="line.68"></a>
+<span class="sourceLineNo">069</span>    // Directly map all of the hbase.zookeeper.property.KEY properties.<a name="line.69"></a>
+<span class="sourceLineNo">070</span>    // Synchronize on conf so no loading of configs while we iterate<a name="line.70"></a>
+<span class="sourceLineNo">071</span>    synchronized (conf) {<a name="line.71"></a>
+<span class="sourceLineNo">072</span>      for (Entry&lt;String, String&gt; entry : conf) {<a name="line.72"></a>
+<span class="sourceLineNo">073</span>        String key = entry.getKey();<a name="line.73"></a>
+<span class="sourceLineNo">074</span>        if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) {<a name="line.74"></a>
+<span class="sourceLineNo">075</span>          String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN);<a name="line.75"></a>
+<span class="sourceLineNo">076</span>          String value = entry.getValue();<a name="line.76"></a>
+<span class="sourceLineNo">077</span>          // If the value has variables substitutions, need to do a get.<a name="line.77"></a>
+<span class="sourceLineNo">078</span>          if (value.contains(VARIABLE_START)) {<a name="line.78"></a>
+<span class="sourceLineNo">079</span>            value = conf.get(key);<a name="line.79"></a>
+<span class="sourceLineNo">080</span>          }<a name="line.80"></a>
+<span class="sourceLineNo">081</span>          zkProperties.setProperty(zkKey, value);<a name="line.81"></a>
+<span class="sourceLineNo">082</span>        }<a name="line.82"></a>
+<span class="sourceLineNo">083</span>      }<a name="line.83"></a>
+<span class="sourceLineNo">084</span>    }<a name="line.84"></a>
+<span class="sourceLineNo">085</span><a name="line.85"></a>
+<span class="sourceLineNo">086</span>    // If clientPort is not set, assign the default.<a name="line.86"></a>
+<span class="sourceLineNo">087</span>    if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) {<a name="line.87"></a>
+<span class="sourceLineNo">088</span>      zkProperties.put(HConstants.CLIENT_PORT_STR,<a name="line.88"></a>
+<span class="sourceLineNo">089</span>          HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT);<a name="line.89"></a>
+<span class="sourceLineNo">090</span>    }<a name="line.90"></a>
+<span class="sourceLineNo">091</span><a name="line.91"></a>
+<span class="sourceLineNo">092</span>    // Create the server.X properties.<a name="line.92"></a>
+<span class="sourceLineNo">093</span>    int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888);<a name="line.93"></a>
+<span class="sourceLineNo">094</span>    int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888);<a name="line.94"></a>
+<span class="sourceLineNo">095</span><a name="line.95"></a>
+<span class="sourceLineNo">096</span>    final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM,<a name="line.96"></a>
+<span class="sourceLineNo">097</span>                                                 HConstants.LOCALHOST);<a name="line.97"></a>
+<span class="sourceLineNo">098</span>    String serverHost;<a name="line.98"></a>
+<span class="sourceLineNo">099</span>    String address;<a name="line.99"></a>
+<span class="sourceLineNo">100</span>    String key;<a name="line.100"></a>
+<span class="sourceLineNo">101</span>    for (int i = 0; i &lt; serverHosts.length; ++i) {<a name="line.101"></a>
+<span class="sourceLineNo">102</span>      if (serverHosts[i].contains(":")) {<a name="line.102"></a>
+<span class="sourceLineNo">103</span>        serverHost = serverHosts[i].substring(0, serverHosts[i].indexOf(':'));<a name="line.103"></a>
+<span class="sourceLineNo">104</span>      } else {<a name="line.104"></a>
+<span class="sourceLineNo">105</span>        serverHost = serverHosts[i];<a name="line.105"></a>
+<span class="sourceLineNo">106</span>      }<a name="line.106"></a>
+<span class="sourceLineNo">107</span>      address = serverHost + ":" + peerPort + ":" + leaderPort;<a name="line.107"></a>
+<span class="sourceLineNo">108</span>      key = "server." + i;<a name="line.108"></a>
+<span class="sourceLineNo">109</span>      zkProperties.put(key, address);<a name="line.109"></a>
+<span class="sourceLineNo">110</span>    }<a name="line.110"></a>
+<span class="sourceLineNo">111</span><a name="line.111"></a>
+<span class="sourceLineNo">112</span>    return zkProperties;<a name="line.112"></a>
+<span class="sourceLineNo">113</span>  }<a name="line.113"></a>
+<span class="sourceLineNo">114</span><a name="line.114"></a>
+<span class="sourceLineNo">115</span>  /**<a name="line.115"></a>
+<span class="sourceLineNo">116</span>   * Return the ZK Quorum servers string given the specified configuration<a name="line.116"></a>
+<span class="sourceLineNo">117</span>   *<a name="line.117"></a>
+<span class="sourceLineNo">118</span>   * @param conf<a name="line.118"></a>
+<span class="sourceLineNo">119</span>   * @return Quorum servers String<a name="line.119"></a>
+<span class="sourceLineNo">120</span>   */<a name="line.120"></a>
+<span class="sourceLineNo">121</span>  private static String getZKQuorumServersStringFromHbaseConfig(Configuration conf) {<a name="line.121"></a>
+<span class="sourceLineNo">122</span>    String defaultClientPort = Integer.toString(<a name="line.122"></a>
+<span class="sourceLineNo">123</span>        conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT));<a name="line.123"></a>
+<span class="sourceLineNo">124</span><a name="line.124"></a>
+<span class="sourceLineNo">125</span>    // Build the ZK quorum server string with "server:clientport" list, separated by ','<a name="line.125"></a>
+<span class="sourceLineNo">126</span>    final String[] serverHosts =<a name="line.126"></a>
+<span class="sourceLineNo">127</span>        conf.getStrings(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST);<a name="line.127"></a>
+<span class="sourceLineNo">128</span>    return buildZKQuorumServerString(serverHosts, defaultClientPort);<a name="line.128"></a>
+<span class="sourceLineNo">129</span>  }<a name="line.129"></a>
+<span class="sourceLineNo">130</span><a name="line.130"></a>
+<span class="sourceLineNo">131</span>  /**<a name="line.131"></a>
+<span class="sourceLineNo">132</span>   * Return the ZK Quorum servers string given the specified configuration.<a name="line.132"></a>
+<span class="sourceLineNo">133</span>   * @return Quorum servers<a name="line.133"></a>
+<span class="sourceLineNo">134</span>   */<a name="line.134"></a>
+<span class="sourceLineNo">135</span>  public static String getZKQuorumServersString(Configuration conf) {<a name="line.135"></a>
+<span class="sourceLineNo">136</span>    return getZKQuorumServersStringFromHbaseConfig(conf);<a name="line.136"></a>
+<span class="sourceLineNo">137</span>  }<a name="line.137"></a>
+<span class="sourceLineNo">138</span><a name="line.138"></a>
+<span class="sourceLineNo">139</span>  /**<a name="line.139"></a>
+<span class="sourceLineNo">140</span>   * Build the ZK quorum server string with "server:clientport" list, separated by ','<a name="line.140"></a>
+<span class="sourceLineNo">141</span>   *<a name="line.141"></a>
+<span class="sourceLineNo">142</span>   * @param serverHosts a list of servers for ZK quorum<a name="line.142"></a>
+<span class="sourceLineNo">143</span>   * @param clientPort the default client port<a name="line.143"></a>
+<span class="sourceLineNo">144</span>   * @return the string for a list of "server:port" separated by ","<a name="line.144"></a>
+<span class="sourceLineNo">145</span>   */<a name="line.145"></a>
+<span class="sourceLineNo">146</span>  public static String buildZKQuorumServerString(String[] serverHosts, String clientPort) {<a name="line.146"></a>
+<span class="sourceLineNo">147</span>    StringBuilder quorumStringBuilder = new StringBuilder();<a name="line.147"></a>
+<span class="sourceLineNo">148</span>    String serverHost;<a name="line.148"></a>
+<span class="sourceLineNo">149</span>    for (int i = 0; i &lt; serverHosts.length; ++i) {<a name="line.149"></a>
+<span class="sourceLineNo">150</span>      if (serverHosts[i].contains(":")) {<a name="line.150"></a>
+<span class="sourceLineNo">151</span>        serverHost = serverHosts[i]; // just use the port specified from the input<a name="line.151"></a>
+<span class="sourceLineNo">152</span>      } else {<a name="line.152"></a>
+<span class="sourceLineNo">153</span>        serverHost = serverHosts[i] + ":" + clientPort;<a name="line.153"></a>
+<span class="sourceLineNo">154</span>      }<a name="line.154"></a>
+<span class="sourceLineNo">155</span>      if (i &gt; 0) {<a name="line.155"></a>
+<span class="sourceLineNo">156</span>        quorumStringBuilder.append(',');<a name="line.156"></a>
+<span class="sourceLineNo">157</span>      }<a name="line.157"></a>
+<span class="sourceLineNo">158</span>      quorumStringBuilder.append(serverHost);<a name="line.158"></a>
+<span class="sourceLineNo">159</span>    }<a name="line.159"></a>
+<span class="sourceLineNo">160</span>    return quorumStringBuilder.toString();<a name="line.160"></a>
+<span class="sourceLineNo">161</span>  }<a name="line.161"></a>
+<span class="sourceLineNo">162</span><a name="line.162"></a>
+<span class="sourceLineNo">163</span>  /**<a name="line.163"></a>
+<span class="sourceLineNo">164</span>   * Verifies that the given key matches the expected format for a ZooKeeper cluster key.<a name="line.164"></a>
+<span class="sourceLineNo">165</span>   * The Quorum for the ZK cluster can have one the following formats (see examples below):<a name="line.165"></a>
+<span class="sourceLineNo">166</span>   *<a name="line.166"></a>
+<span class="sourceLineNo">167</span>   * &lt;ol&gt;<a name="line.167"></a>
+<span class="sourceLineNo">168</span>   *   &lt;li&gt;s1,s2,s3 (no client port in the list, the client port could be obtained from<a name="line.168"></a>
+<span class="sourceLineNo">169</span>   *       clientPort)&lt;/li&gt;<a name="line.169"></a>
+<span class="sourceLineNo">170</span>   *   &lt;li&gt;s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,<a name="line.170"></a>
+<span class="sourceLineNo">171</span>   *       in this case, the clientPort would be ignored)&lt;/li&gt;<a name="line.171"></a>
+<span class="sourceLineNo">172</span>   *   &lt;li&gt;s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use<a name="line.172"></a>
+<span class="sourceLineNo">173</span>   *       the clientPort; otherwise, it would use the specified port)&lt;/li&gt;<a name="line.173"></a>
+<span class="sourceLineNo">174</span>   * &lt;/ol&gt;<a name="line.174"></a>
+<span class="sourceLineNo">175</span>   *<a name="line.175"></a>
+<span class="sourceLineNo">176</span>   * @param key the cluster key to validate<a name="line.176"></a>
+<span class="sourceLineNo">177</span>   * @throws IOException if the key could not be parsed<a name="line.177"></a>
+<span class="sourceLineNo">178</span>   */<a name="line.178"></a>
+<span class="sourceLineNo">179</span>  public static void validateClusterKey(String key) throws IOException {<a name="line.179"></a>
+<span class="sourceLineNo">180</span>    transformClusterKey(key);<a name="line.180"></a>
+<span class="sourceLineNo">181</span>  }<a name="line.181"></a>
+<span class="sourceLineNo">182</span><a name="line.182"></a>
+<span class="sourceLineNo">183</span>  /**<a name="line.183"></a>
+<span class="sourceLineNo">184</span>   * Separate the given key into the three configurations it should contain:<a name="line.184"></a>
+<span class="sourceLineNo">185</span>   * hbase.zookeeper.quorum, hbase.zookeeper.client.port<a name="line.185"></a>
+<span class="sourceLineNo">186</span>   * and zookeeper.znode.parent<a name="line.186"></a>
+<span class="sourceLineNo">187</span>   * @param key<a name="line.187"></a>
+<span class="sourceLineNo">188</span>   * @return the three configuration in the described order<a name="line.188"></a>
+<span class="sourceLineNo">189</span>   * @throws IOException<a name="line.189"></a>
+<span class="sourceLineNo">190</span>   */<a name="line.190"></a>
+<span class="sourceLineNo">191</span>  public static ZKClusterKey transformClusterKey(String key) throws IOException {<a name="line.191"></a>
+<span class="sourceLineNo">192</span>    String[] parts = key.split(":");<a name="line.192"></a>
+<span class="sourceLineNo">193</span><a name="line.193"></a>
+<span class="sourceLineNo">194</span>    if (parts.length == 3) {<a name="line.194"></a>
+<span class="sourceLineNo">195</span>      if (!parts[2].matches("/.*[^/]")) {<a name="line.195"></a>
+<span class="sourceLineNo">196</span>        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +<a name="line.196"></a>
+<span class="sourceLineNo">197</span>            HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.197"></a>
+<span class="sourceLineNo">198</span>            + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.198"></a>
+<span class="sourceLineNo">199</span>      }<a name="line.199"></a>
+<span class="sourceLineNo">200</span>      return new ZKClusterKey(parts [0], Integer.parseInt(parts [1]), parts [2]);<a name="line.200"></a>
+<span class="sourceLineNo">201</span>    }<a name="line.201"></a>
+<span class="sourceLineNo">202</span><a name="line.202"></a>
+<span class="sourceLineNo">203</span>    if (parts.length &gt; 3) {<a name="line.203"></a>
+<span class="sourceLineNo">204</span>      // The quorum could contain client port in server:clientport format, try to transform more.<a name="line.204"></a>
+<span class="sourceLineNo">205</span>      String zNodeParent = parts [parts.length - 1];<a name="line.205"></a>
+<span class="sourceLineNo">206</span>      if (!zNodeParent.matches("/.*[^/]")) {<a name="line.206"></a>
+<span class="sourceLineNo">207</span>        throw new IOException("Cluster key passed " + key + " is invalid, the format should be:"<a name="line.207"></a>
+<span class="sourceLineNo">208</span>            + HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.208"></a>
+<span class="sourceLineNo">209</span>            + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.209"></a>
+<span class="sourceLineNo">210</span>      }<a name="line.210"></a>
+<span class="sourceLineNo">211</span><a name="line.211"></a>
+<span class="sourceLineNo">212</span>      String clientPort = parts [parts.length - 2];<a name="line.212"></a>
+<span class="sourceLineNo">213</span><a name="line.213"></a>
+<span class="sourceLineNo">214</span>      // The first part length is the total length minus the lengths of other parts and minus 2 ":"<a name="line.214"></a>
+<span class="sourceLineNo">215</span>      int endQuorumIndex = key.length() - zNodeParent.length() - clientPort.length() - 2;<a name="line.215"></a>
+<span class="sourceLineNo">216</span>      String quorumStringInput = key.substring(0, endQuorumIndex);<a name="line.216"></a>
+<span class="sourceLineNo">217</span>      String[] serverHosts = quorumStringInput.split(",");<a name="line.217"></a>
+<span class="sourceLineNo">218</span><a name="line.218"></a>
+<span class="sourceLineNo">219</span>      // The common case is that every server has its own client port specified - this means<a name="line.219"></a>
+<span class="sourceLineNo">220</span>      // that (total parts - the ZNodeParent part - the ClientPort part) is equal to<a name="line.220"></a>
+<span class="sourceLineNo">221</span>      // (the number of "," + 1) - "+ 1" because the last server has no ",".<a name="line.221"></a>
+<span class="sourceLineNo">222</span>      if ((parts.length - 2) == (serverHosts.length + 1)) {<a name="line.222"></a>
+<span class="sourceLineNo">223</span>        return new ZKClusterKey(quorumStringInput, Integer.parseInt(clientPort), zNodeParent);<a name="line.223"></a>
+<span class="sourceLineNo">224</span>      }<a name="line.224"></a>
+<span class="sourceLineNo">225</span><a name="line.225"></a>
+<span class="sourceLineNo">226</span>      // For the uncommon case that some servers has no port specified, we need to build the<a name="line.226"></a>
+<span class="sourceLineNo">227</span>      // server:clientport list using default client port for servers without specified port.<a name="line.227"></a>
+<span class="sourceLineNo">228</span>      return new ZKClusterKey(<a name="line.228"></a>
+<span class="sourceLineNo">229</span>          buildZKQuorumServerString(serverHosts, clientPort),<a name="line.229"></a>
+<span class="sourceLineNo">230</span>          Integer.parseInt(clientPort),<a name="line.230"></a>
+<span class="sourceLineNo">231</span>          zNodeParent);<a name="line.231"></a>
+<span class="sourceLineNo">232</span>    }<a name="line.232"></a>
+<span class="sourceLineNo">233</span><a name="line.233"></a>
+<span class="sourceLineNo">234</span>    throw new IOException("Cluster key passed " + key + " is invalid, the format should be:" +<a name="line.234"></a>
+<span class="sourceLineNo">235</span>        HConstants.ZOOKEEPER_QUORUM + ":" + HConstants.ZOOKEEPER_CLIENT_PORT + ":"<a name="line.235"></a>
+<span class="sourceLineNo">236</span>        + HConstants.ZOOKEEPER_ZNODE_PARENT);<a name="line.236"></a>
+<span class="sourceLineNo">237</span>  }<a name="line.237"></a>
+<span class="sourceLineNo">238</span><a name="line.238"></a>
+<span class="sourceLineNo">239</span>  /**<a name="line.239"></a>
+<span class="sourceLineNo">240</span>   * Get the key to the ZK ensemble for this configuration without<a name="line.240"></a>
+<span class="sourceLineNo">241</span>   * adding a name at the end<a name="line.241"></a>
+<span class="sourceLineNo">242</span>   * @param conf Configuration to use to build the key<a name="line.242"></a>
+<span class="sourceLineNo">243</span>   * @return ensemble key without a name<a name="line.243"></a>
+<span class="sourceLineNo">244</span>   */<a name="line.244"></a>
+<span class="sourceLineNo">245</span>  public static String getZooKeeperClusterKey(Configuration conf) {<a name="line.245"></a>
+<span class="sourceLineNo">246</span>    return getZooKeeperClusterKey(conf, null);<a name="line.246"></a>
+<span class="sourceLineNo">247</span>  }<a name="line.247"></a>
+<span class="sourceLineNo">248</span><a name="line.248"></a>
+<span class="sourceLineNo">249</span>  /**<a name="line.249"></a>
+<span class="sourceLineNo">250</span>   * Get the key to the ZK ensemble for this configuration and append<a name="line.250"></a>
+<span class="sourceLineNo">251</span>   * a name at the end<a name="line.251"></a>
+<span class="sourceLineNo">252</span>   * @param conf Configuration to use to build the key<a name="line.252"></a>
+<span class="sourceLineNo">253</span>   * @param name Name that should be appended at the end if not empty or null<a name="line.253"></a>
+<span class="sourceLineNo">254</span>   * @return ensemble key with a name (if any)<a name="line.254"></a>
+<span class="sourceLineNo">255</span>   */<a name="line.255"></a>
+<span class="sourceLineNo">256</span>  public static String getZooKeeperClusterKey(Configuration conf, String name) {<a name="line.256"></a>
+<span class="sourceLineNo">257</span>    String ensemble = conf.get(HConstants.ZOOKEEPER_QUORUM).replaceAll(<a name="line.257"></a>
+<span class="sourceLineNo">258</span>        "[\\t\\n\\x0B\\f\\r]", "");<a name="line.258"></a>
+<span class="sourceLineNo">259</span>    StringBuilder builder = new StringBuilder(ensemble);<a name="line.259"></a>
+<span class="sourceLineNo">260</span>    builder.append(":");<a name="line.260"></a>
+<span class="sourceLineNo">261</span>    builder.append(conf.get(HConstants.ZOOKEEPER_CLIENT_PORT));<a name="line.261"></a>
+<span class="sourceLineNo">262</span>    builder.append(":");<a name="line.262"></a>
+<span class="sourceLineNo">263</span>    builder.append(conf.get(HConstants.ZOOKEEPER_ZNODE_PARENT));<a name="line.263"></a>
+<span class="sourceLineNo">264</span>    if (name != null &amp;&amp; !name.isEmpty()) {<a name="line.264"></a>
+<span class="sourceLineNo">265</span>      builder.append(",");<a name="line.265"></a>
+<span class="sourceLineNo">266</span>      builder.append(name);<a name="line.266"></a>
+<span class="sourceLineNo">267</span>    }<a name="line.267"></a>
+<span class="sourceLineNo">268</span>    return builder.toString();<a name="line.268"></a>
+<span class="sourceLineNo">269</span>  }<a name="line.269"></a>
+<span class="sourceLineNo">270</span><a name="line.270"></a>
+<span class="sourceLineNo">271</span>  /**<a name="line.271"></a>
+<span class="sourceLineNo">272</span>   * Standardize the ZK quorum string: make it a "server:clientport" list, separated by ','<a name="line.272"></a>
+<span class="sourceLineNo">273</span>   * @param quorumStringInput a string contains a list of servers for ZK quorum<a name="line.273"></a>
+<span class="sourceLineNo">274</span>   * @param clientPort the default client port<a name="line.274"></a>
+<span class="sourceLineNo">275</span>   * @return the string for a list of "server:port" separated by ","<a name="line.275"></a>
+<span class="sourceLineNo">276</span>   */<a name="line.276"></a>
+<span class="sourceLineNo">277</span>  @VisibleForTesting<a name="line.277"></a>
+<span class="sourceLineNo">278</span>  public static String standardizeZKQuorumServerString(String quorumStringInput,<a name="line.278"></a>
+<span class="sourceLineNo">279</span>      String clientPort) {<a name="line.279"></a>
+<span class="sourceLineNo">280</span>    String[] serverHosts = quorumStringInput.split(",");<a name="line.280"></a>
+<span class="sourceLineNo">281</span>    return buildZKQuorumServerString(serverHosts, clientPort);<a name="line.281"></a>
+<span class="sourceLineNo">282</span>  }<a name="line.282"></a>
+<span class="sourceLineNo">283</span><a name="line.283"></a>
+<span class="sourceLineNo">284</span>  // The Quorum for the ZK cluster can have one the following format (see examples below):<a name="line.284"></a>
+<span class="sourceLineNo">285</span>  // (1). s1,s2,s3 (no client port in the list, the client port could be obtained from clientPort)<a name="line.285"></a>
+<span class="sourceLineNo">286</span>  // (2). s1:p1,s2:p2,s3:p3 (with client port, which could be same or different for each server,<a name="line.286"></a>
+<span class="sourceLineNo">287</span>  //      in this case, the clientPort would be ignored)<a name="line.287"></a>
+<span class="sourceLineNo">288</span>  // (3). s1:p1,s2,s3:p3 (mix of (1) and (2) - if port is not specified in a server, it would use<a name="line.288"></a>
+<span class="sourceLineNo">289</span>  //      the clientPort; otherwise, it would use the specified port)<a name="line.289"></a>
+<span class="sourceLineNo">290</span>  @VisibleForTesting<a name="line.290"></a>
+<span class="sourceLineNo">291</span>  public static class ZKClusterKey {<a name="line.291"></a>
+<span class="sourceLineNo">292</span>    private String quorumString;<a name="line.292"></a>
+<span class="sourceLineNo">293</span>    private int clientPort;<a name="line.293"></a>
+<span class="sourceLineNo">294</span>    private String znodeParent;<a name="line.294"></a>
+<span class="sourceLineNo">295</span><a name="line.295"></a>
+<span class="sourceLineNo">296</span>    ZKClusterKey(String quorumString, int clientPort, String znodeParent) {<a name="line.296"></a>
+<span class="sourceLineNo">297</span>      this.quorumString = quorumString;<a name="line.297"></a>
+<span class="sourceLineNo">298</span>      this.clientPort = clientPort;<a name="line.298"></a>
+<span class="sourceLineNo">299</span>      this.znodeParent = znodeParent;<a name="line.299"></a>
+<span class="sourceLineNo">300</span>    }<a name="line.300"></a>
+<span class="sourceLineNo">301</span><a name="line.301"></a>
+<span class="sourceLineNo">302</span>    public String getQuorumString() {<a name="line.302"></a>
+<span class="sourceLineNo">303</span>      return quorumString;<a name="line.303"></a>
+<span class="sourceLineNo">304</span>    }<a name="line.304"></a>
+<span class="sourceLineNo">305</span><a name="line.305"></a>
+<span class="sourceLineNo">306</span>    public int getClientPort() {<a name="line.306"></a>
+<span class="sourceLineNo">307</span>      return clientPort;<a name="line.307"></a>
+<span class="sourceLineNo">308</span>    }<a name="line.308"></a>
+<span class="sourceLineNo">309</span><a name="line.309"></a>
+<span class="sourceLineNo">310</span>    public String getZnodeParent() {<a name="line.310"></a>
+<span class="sourceLineNo">311</span>      return znodeParent;<a name="line.311"></a>
+<span class="sourceLineNo">312</span>    }<a name="line.312"></a>
+<span class="sourceLineNo">313</span>  }<a name="line.313"></a>
+<span class="sourceLineNo">314</span><a name="line.314"></a>
+<span class="sourceLineNo">315</span>  /**<a name="line.315"></a>
+<span class="sourceLineNo">316</span>   * Get the client ZK Quorum servers string<a name="line.316"></a>
+<span class="sourceLineNo">317</span>   * @param conf the configuration to read<a name="line.317"></a>
+<span class="sourceLineNo">318</span>   * @return Client quorum servers, or null if not specified<a name="line.318"></a>
+<span class="sourceLineNo">319</span>   */<a name="line.319"></a>
+<span class="sourceLineNo">320</span>  public static String getClientZKQuorumServersString(Configuration conf) {<a name="line.320"></a>
+<span class="sourceLineNo">321</span>    String clientQuromServers = conf.get(HConstants.CLIENT_ZOOKEEPER_QUORUM);<a name="line.321"></a>
+<span class="sourceLineNo">322</span>    if (clientQuromServers == null) {<a name="line.322"></a>
+<span class="sourceLineNo">323</span>      return null;<a name="line.323"></a>
+<span class="sourceLineNo">324</span>    }<a name="line.324"></a>
+<span class="sourceLineNo">325</span>    int defaultClientPort =<a name="line.325"></a>
+<span class="sourceLineNo">326</span>        conf.getInt(HConstants.ZOOKEEPER_CLIENT_PORT, HConstants.DEFAULT_ZOOKEEPER_CLIENT_PORT);<a name="line.326"></a>
+<span class="sourceLineNo">327</span>    String clientZkClientPort =<a name="line.327"></a>
+<span class="sourceLineNo">328</span>        Integer.toString(conf.getInt(HConstants.CLIENT_ZOOKEEPER_CLIENT_PORT, defaultClientPort));<a name="line.328"></a>
+<span class="sourceLineNo">329</span>    // Build the ZK quorum server string with "server:clientport" list, separated by ','<a name="line.329"></a>
+<span class="sourceLineNo">330</span>    final String[] serverHosts = StringUtils.getStrings(clientQuromServers);<a name="line.330"></a>
+<span class="sourceLineNo">331</span>    return buildZKQuorumServerString(serverHosts, clientZkClientPort);<a name="line.331"></a>
+<span class="sourceLineNo">332</span>  }<a name="line.332"></a>
+<span class="sourceLineNo">333</span>}<a name="line.333"></a>
 
 
 


Mime
View raw message