http://git-wip-us.apache.org/repos/asf/hbase-site/blob/6eb695c8/devapidocs/src-html/org/apache/hadoop/hbase/replication/ZKReplicationQueueStorage.html ---------------------------------------------------------------------- diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/replication/ZKReplicationQueueStorage.html b/devapidocs/src-html/org/apache/hadoop/hbase/replication/ZKReplicationQueueStorage.html index 7556544..ce54cbb 100644 --- a/devapidocs/src-html/org/apache/hadoop/hbase/replication/ZKReplicationQueueStorage.html +++ b/devapidocs/src-html/org/apache/hadoop/hbase/replication/ZKReplicationQueueStorage.html @@ -41,520 +41,530 @@ <span class="sourceLineNo">033</span>import org.apache.hadoop.fs.Path;<a name="line.33"></a> <span class="sourceLineNo">034</span>import org.apache.hadoop.hbase.HConstants;<a name="line.34"></a> <span class="sourceLineNo">035</span>import org.apache.hadoop.hbase.ServerName;<a name="line.35"></a> -<span class="sourceLineNo">036</span>import org.apache.hadoop.hbase.exceptions.DeserializationException;<a name="line.36"></a> -<span class="sourceLineNo">037</span>import org.apache.hadoop.hbase.util.Bytes;<a name="line.37"></a> -<span class="sourceLineNo">038</span>import org.apache.hadoop.hbase.util.CollectionUtils;<a name="line.38"></a> -<span class="sourceLineNo">039</span>import org.apache.hadoop.hbase.util.Pair;<a name="line.39"></a> -<span class="sourceLineNo">040</span>import org.apache.hadoop.hbase.zookeeper.ZKUtil;<a name="line.40"></a> -<span class="sourceLineNo">041</span>import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp;<a name="line.41"></a> -<span class="sourceLineNo">042</span>import org.apache.hadoop.hbase.zookeeper.ZKWatcher;<a name="line.42"></a> -<span class="sourceLineNo">043</span>import org.apache.hadoop.hbase.zookeeper.ZNodePaths;<a name="line.43"></a> -<span class="sourceLineNo">044</span>import org.apache.yetus.audience.InterfaceAudience;<a name="line.44"></a> -<span class="sourceLineNo">045</span>import org.apache.zookeeper.KeeperException;<a name="line.45"></a> -<span class="sourceLineNo">046</span>import org.apache.zookeeper.KeeperException.BadVersionException;<a name="line.46"></a> -<span class="sourceLineNo">047</span>import org.apache.zookeeper.KeeperException.NoNodeException;<a name="line.47"></a> -<span class="sourceLineNo">048</span>import org.apache.zookeeper.KeeperException.NodeExistsException;<a name="line.48"></a> -<span class="sourceLineNo">049</span>import org.apache.zookeeper.KeeperException.NotEmptyException;<a name="line.49"></a> -<span class="sourceLineNo">050</span>import org.apache.zookeeper.data.Stat;<a name="line.50"></a> -<span class="sourceLineNo">051</span>import org.slf4j.Logger;<a name="line.51"></a> -<span class="sourceLineNo">052</span>import org.slf4j.LoggerFactory;<a name="line.52"></a> -<span class="sourceLineNo">053</span><a name="line.53"></a> -<span class="sourceLineNo">054</span>import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;<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> * ZK based replication queue storage.<a name="line.57"></a> -<span class="sourceLineNo">058</span> * <p><a name="line.58"></a> -<span class="sourceLineNo">059</span> * The base znode for each regionserver is the regionserver name. For example:<a name="line.59"></a> -<span class="sourceLineNo">060</span> *<a name="line.60"></a> -<span class="sourceLineNo">061</span> * <pre><a name="line.61"></a> -<span class="sourceLineNo">062</span> * /hbase/replication/rs/hostname.example.org,6020,1234<a name="line.62"></a> -<span class="sourceLineNo">063</span> * </pre><a name="line.63"></a> -<span class="sourceLineNo">064</span> *<a name="line.64"></a> -<span class="sourceLineNo">065</span> * Within this znode, the region server maintains a set of WAL replication queues. These queues are<a name="line.65"></a> -<span class="sourceLineNo">066</span> * represented by child znodes named using there give queue id. For example:<a name="line.66"></a> -<span class="sourceLineNo">067</span> *<a name="line.67"></a> -<span class="sourceLineNo">068</span> * <pre><a name="line.68"></a> -<span class="sourceLineNo">069</span> * /hbase/replication/rs/hostname.example.org,6020,1234/1<a name="line.69"></a> -<span class="sourceLineNo">070</span> * /hbase/replication/rs/hostname.example.org,6020,1234/2<a name="line.70"></a> -<span class="sourceLineNo">071</span> * </pre><a name="line.71"></a> -<span class="sourceLineNo">072</span> *<a name="line.72"></a> -<span class="sourceLineNo">073</span> * Each queue has one child znode for every WAL that still needs to be replicated. The value of<a name="line.73"></a> -<span class="sourceLineNo">074</span> * these WAL child znodes is the latest position that has been replicated. This position is updated<a name="line.74"></a> -<span class="sourceLineNo">075</span> * every time a WAL entry is replicated. For example:<a name="line.75"></a> -<span class="sourceLineNo">076</span> *<a name="line.76"></a> -<span class="sourceLineNo">077</span> * <pre><a name="line.77"></a> -<span class="sourceLineNo">078</span> * /hbase/replication/rs/hostname.example.org,6020,1234/1/23522342.23422 [VALUE: 254]<a name="line.78"></a> -<span class="sourceLineNo">079</span> * </pre><a name="line.79"></a> -<span class="sourceLineNo">080</span> */<a name="line.80"></a> -<span class="sourceLineNo">081</span>@InterfaceAudience.Private<a name="line.81"></a> -<span class="sourceLineNo">082</span>class ZKReplicationQueueStorage extends ZKReplicationStorageBase<a name="line.82"></a> -<span class="sourceLineNo">083</span> implements ReplicationQueueStorage {<a name="line.83"></a> -<span class="sourceLineNo">084</span><a name="line.84"></a> -<span class="sourceLineNo">085</span> private static final Logger LOG = LoggerFactory.getLogger(ZKReplicationQueueStorage.class);<a name="line.85"></a> -<span class="sourceLineNo">086</span><a name="line.86"></a> -<span class="sourceLineNo">087</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY =<a name="line.87"></a> -<span class="sourceLineNo">088</span> "zookeeper.znode.replication.hfile.refs";<a name="line.88"></a> -<span class="sourceLineNo">089</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT = "hfile-refs";<a name="line.89"></a> -<span class="sourceLineNo">090</span><a name="line.90"></a> -<span class="sourceLineNo">091</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_REGIONS_KEY =<a name="line.91"></a> -<span class="sourceLineNo">092</span> "zookeeper.znode.replication.regions";<a name="line.92"></a> -<span class="sourceLineNo">093</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_REGIONS_DEFAULT = "regions";<a name="line.93"></a> -<span class="sourceLineNo">094</span><a name="line.94"></a> -<span class="sourceLineNo">095</span> /**<a name="line.95"></a> -<span class="sourceLineNo">096</span> * The name of the znode that contains all replication queues<a name="line.96"></a> -<span class="sourceLineNo">097</span> */<a name="line.97"></a> -<span class="sourceLineNo">098</span> private final String queuesZNode;<a name="line.98"></a> -<span class="sourceLineNo">099</span><a name="line.99"></a> -<span class="sourceLineNo">100</span> /**<a name="line.100"></a> -<span class="sourceLineNo">101</span> * The name of the znode that contains queues of hfile references to be replicated<a name="line.101"></a> -<span class="sourceLineNo">102</span> */<a name="line.102"></a> -<span class="sourceLineNo">103</span> private final String hfileRefsZNode;<a name="line.103"></a> -<span class="sourceLineNo">104</span><a name="line.104"></a> -<span class="sourceLineNo">105</span> private final String regionsZNode;<a name="line.105"></a> -<span class="sourceLineNo">106</span><a name="line.106"></a> -<span class="sourceLineNo">107</span> public ZKReplicationQueueStorage(ZKWatcher zookeeper, Configuration conf) {<a name="line.107"></a> -<span class="sourceLineNo">108</span> super(zookeeper, conf);<a name="line.108"></a> -<span class="sourceLineNo">109</span><a name="line.109"></a> -<span class="sourceLineNo">110</span> String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");<a name="line.110"></a> -<span class="sourceLineNo">111</span> String hfileRefsZNodeName = conf.get(ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY,<a name="line.111"></a> -<span class="sourceLineNo">112</span> ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT);<a name="line.112"></a> -<span class="sourceLineNo">113</span> this.queuesZNode = ZNodePaths.joinZNode(replicationZNode, queuesZNodeName);<a name="line.113"></a> -<span class="sourceLineNo">114</span> this.hfileRefsZNode = ZNodePaths.joinZNode(replicationZNode, hfileRefsZNodeName);<a name="line.114"></a> -<span class="sourceLineNo">115</span> this.regionsZNode = ZNodePaths.joinZNode(replicationZNode, conf<a name="line.115"></a> -<span class="sourceLineNo">116</span> .get(ZOOKEEPER_ZNODE_REPLICATION_REGIONS_KEY, ZOOKEEPER_ZNODE_REPLICATION_REGIONS_DEFAULT));<a name="line.116"></a> -<span class="sourceLineNo">117</span> }<a name="line.117"></a> -<span class="sourceLineNo">118</span><a name="line.118"></a> -<span class="sourceLineNo">119</span> private String getRsNode(ServerName serverName) {<a name="line.119"></a> -<span class="sourceLineNo">120</span> return ZNodePaths.joinZNode(queuesZNode, serverName.getServerName());<a name="line.120"></a> -<span class="sourceLineNo">121</span> }<a name="line.121"></a> -<span class="sourceLineNo">122</span><a name="line.122"></a> -<span class="sourceLineNo">123</span> private String getQueueNode(ServerName serverName, String queueId) {<a name="line.123"></a> -<span class="sourceLineNo">124</span> return ZNodePaths.joinZNode(getRsNode(serverName), queueId);<a name="line.124"></a> -<span class="sourceLineNo">125</span> }<a name="line.125"></a> -<span class="sourceLineNo">126</span><a name="line.126"></a> -<span class="sourceLineNo">127</span> private String getFileNode(String queueNode, String fileName) {<a name="line.127"></a> -<span class="sourceLineNo">128</span> return ZNodePaths.joinZNode(queueNode, fileName);<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> private String getFileNode(ServerName serverName, String queueId, String fileName) {<a name="line.131"></a> -<span class="sourceLineNo">132</span> return getFileNode(getQueueNode(serverName, queueId), fileName);<a name="line.132"></a> -<span class="sourceLineNo">133</span> }<a name="line.133"></a> -<span class="sourceLineNo">134</span><a name="line.134"></a> -<span class="sourceLineNo">135</span> /**<a name="line.135"></a> -<span class="sourceLineNo">136</span> * Put all regions under /hbase/replication/regions znode will lead to too many children because<a name="line.136"></a> -<span class="sourceLineNo">137</span> * of the huge number of regions in real production environment. So here we use hash of encoded<a name="line.137"></a> -<span class="sourceLineNo">138</span> * region name to distribute the znode into multiple znodes. <br><a name="line.138"></a> -<span class="sourceLineNo">139</span> * So the final znode path will be format like this:<a name="line.139"></a> -<span class="sourceLineNo">140</span> *<a name="line.140"></a> -<span class="sourceLineNo">141</span> * <pre><a name="line.141"></a> -<span class="sourceLineNo">142</span> * /hbase/replication/regions/254/dd04e76a6966d4ffa908ed0586764767-100<a name="line.142"></a> -<span class="sourceLineNo">143</span> * </pre><a name="line.143"></a> -<span class="sourceLineNo">144</span> *<a name="line.144"></a> -<span class="sourceLineNo">145</span> * The 254 indicate the hash of encoded region name, the 100 indicate the peer id.<a name="line.145"></a> -<span class="sourceLineNo">146</span> * @param encodedRegionName the encoded region name.<a name="line.146"></a> -<span class="sourceLineNo">147</span> * @param peerId peer id for replication.<a name="line.147"></a> -<span class="sourceLineNo">148</span> * @return ZNode path to persist the max sequence id that we've pushed for the given region and<a name="line.148"></a> -<span class="sourceLineNo">149</span> * peer.<a name="line.149"></a> -<span class="sourceLineNo">150</span> */<a name="line.150"></a> -<span class="sourceLineNo">151</span> private String getSerialReplicationRegionPeerNode(String encodedRegionName, String peerId) {<a name="line.151"></a> -<span class="sourceLineNo">152</span> int hash = encodedRegionName.hashCode() & 0x0000FFFF;<a name="line.152"></a> -<span class="sourceLineNo">153</span> String hashPath = ZNodePaths.joinZNode(regionsZNode, String.valueOf(hash));<a name="line.153"></a> -<span class="sourceLineNo">154</span> return ZNodePaths.joinZNode(hashPath, String.format("%s-%s", encodedRegionName, peerId));<a name="line.154"></a> -<span class="sourceLineNo">155</span> }<a name="line.155"></a> -<span class="sourceLineNo">156</span><a name="line.156"></a> -<span class="sourceLineNo">157</span> @Override<a name="line.157"></a> -<span class="sourceLineNo">158</span> public void removeQueue(ServerName serverName, String queueId) throws ReplicationException {<a name="line.158"></a> -<span class="sourceLineNo">159</span> try {<a name="line.159"></a> -<span class="sourceLineNo">160</span> ZKUtil.deleteNodeRecursively(zookeeper, getQueueNode(serverName, queueId));<a name="line.160"></a> -<span class="sourceLineNo">161</span> } catch (KeeperException e) {<a name="line.161"></a> -<span class="sourceLineNo">162</span> throw new ReplicationException(<a name="line.162"></a> -<span class="sourceLineNo">163</span> "Failed to delete queue (serverName=" + serverName + ", queueId=" + queueId + ")", e);<a name="line.163"></a> -<span class="sourceLineNo">164</span> }<a name="line.164"></a> +<span class="sourceLineNo">036</span>import org.apache.hadoop.hbase.client.RegionInfo;<a name="line.36"></a> +<span class="sourceLineNo">037</span>import org.apache.hadoop.hbase.exceptions.DeserializationException;<a name="line.37"></a> +<span class="sourceLineNo">038</span>import org.apache.hadoop.hbase.util.Bytes;<a name="line.38"></a> +<span class="sourceLineNo">039</span>import org.apache.hadoop.hbase.util.CollectionUtils;<a name="line.39"></a> +<span class="sourceLineNo">040</span>import org.apache.hadoop.hbase.util.Pair;<a name="line.40"></a> +<span class="sourceLineNo">041</span>import org.apache.hadoop.hbase.zookeeper.ZKUtil;<a name="line.41"></a> +<span class="sourceLineNo">042</span>import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp;<a name="line.42"></a> +<span class="sourceLineNo">043</span>import org.apache.hadoop.hbase.zookeeper.ZKWatcher;<a name="line.43"></a> +<span class="sourceLineNo">044</span>import org.apache.hadoop.hbase.zookeeper.ZNodePaths;<a name="line.44"></a> +<span class="sourceLineNo">045</span>import org.apache.yetus.audience.InterfaceAudience;<a name="line.45"></a> +<span class="sourceLineNo">046</span>import org.apache.zookeeper.KeeperException;<a name="line.46"></a> +<span class="sourceLineNo">047</span>import org.apache.zookeeper.KeeperException.BadVersionException;<a name="line.47"></a> +<span class="sourceLineNo">048</span>import org.apache.zookeeper.KeeperException.NoNodeException;<a name="line.48"></a> +<span class="sourceLineNo">049</span>import org.apache.zookeeper.KeeperException.NodeExistsException;<a name="line.49"></a> +<span class="sourceLineNo">050</span>import org.apache.zookeeper.KeeperException.NotEmptyException;<a name="line.50"></a> +<span class="sourceLineNo">051</span>import org.apache.zookeeper.data.Stat;<a name="line.51"></a> +<span class="sourceLineNo">052</span>import org.slf4j.Logger;<a name="line.52"></a> +<span class="sourceLineNo">053</span>import org.slf4j.LoggerFactory;<a name="line.53"></a> +<span class="sourceLineNo">054</span><a name="line.54"></a> +<span class="sourceLineNo">055</span>import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;<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> * ZK based replication queue storage.<a name="line.58"></a> +<span class="sourceLineNo">059</span> * <p><a name="line.59"></a> +<span class="sourceLineNo">060</span> * The base znode for each regionserver is the regionserver name. For example:<a name="line.60"></a> +<span class="sourceLineNo">061</span> *<a name="line.61"></a> +<span class="sourceLineNo">062</span> * <pre><a name="line.62"></a> +<span class="sourceLineNo">063</span> * /hbase/replication/rs/hostname.example.org,6020,1234<a name="line.63"></a> +<span class="sourceLineNo">064</span> * </pre><a name="line.64"></a> +<span class="sourceLineNo">065</span> *<a name="line.65"></a> +<span class="sourceLineNo">066</span> * Within this znode, the region server maintains a set of WAL replication queues. These queues are<a name="line.66"></a> +<span class="sourceLineNo">067</span> * represented by child znodes named using there give queue id. For example:<a name="line.67"></a> +<span class="sourceLineNo">068</span> *<a name="line.68"></a> +<span class="sourceLineNo">069</span> * <pre><a name="line.69"></a> +<span class="sourceLineNo">070</span> * /hbase/replication/rs/hostname.example.org,6020,1234/1<a name="line.70"></a> +<span class="sourceLineNo">071</span> * /hbase/replication/rs/hostname.example.org,6020,1234/2<a name="line.71"></a> +<span class="sourceLineNo">072</span> * </pre><a name="line.72"></a> +<span class="sourceLineNo">073</span> *<a name="line.73"></a> +<span class="sourceLineNo">074</span> * Each queue has one child znode for every WAL that still needs to be replicated. The value of<a name="line.74"></a> +<span class="sourceLineNo">075</span> * these WAL child znodes is the latest position that has been replicated. This position is updated<a name="line.75"></a> +<span class="sourceLineNo">076</span> * every time a WAL entry is replicated. For example:<a name="line.76"></a> +<span class="sourceLineNo">077</span> *<a name="line.77"></a> +<span class="sourceLineNo">078</span> * <pre><a name="line.78"></a> +<span class="sourceLineNo">079</span> * /hbase/replication/rs/hostname.example.org,6020,1234/1/23522342.23422 [VALUE: 254]<a name="line.79"></a> +<span class="sourceLineNo">080</span> * </pre><a name="line.80"></a> +<span class="sourceLineNo">081</span> */<a name="line.81"></a> +<span class="sourceLineNo">082</span>@InterfaceAudience.Private<a name="line.82"></a> +<span class="sourceLineNo">083</span>class ZKReplicationQueueStorage extends ZKReplicationStorageBase<a name="line.83"></a> +<span class="sourceLineNo">084</span> implements ReplicationQueueStorage {<a name="line.84"></a> +<span class="sourceLineNo">085</span><a name="line.85"></a> +<span class="sourceLineNo">086</span> private static final Logger LOG = LoggerFactory.getLogger(ZKReplicationQueueStorage.class);<a name="line.86"></a> +<span class="sourceLineNo">087</span><a name="line.87"></a> +<span class="sourceLineNo">088</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY =<a name="line.88"></a> +<span class="sourceLineNo">089</span> "zookeeper.znode.replication.hfile.refs";<a name="line.89"></a> +<span class="sourceLineNo">090</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT = "hfile-refs";<a name="line.90"></a> +<span class="sourceLineNo">091</span><a name="line.91"></a> +<span class="sourceLineNo">092</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_REGIONS_KEY =<a name="line.92"></a> +<span class="sourceLineNo">093</span> "zookeeper.znode.replication.regions";<a name="line.93"></a> +<span class="sourceLineNo">094</span> public static final String ZOOKEEPER_ZNODE_REPLICATION_REGIONS_DEFAULT = "regions";<a name="line.94"></a> +<span class="sourceLineNo">095</span><a name="line.95"></a> +<span class="sourceLineNo">096</span> /**<a name="line.96"></a> +<span class="sourceLineNo">097</span> * The name of the znode that contains all replication queues<a name="line.97"></a> +<span class="sourceLineNo">098</span> */<a name="line.98"></a> +<span class="sourceLineNo">099</span> private final String queuesZNode;<a name="line.99"></a> +<span class="sourceLineNo">100</span><a name="line.100"></a> +<span class="sourceLineNo">101</span> /**<a name="line.101"></a> +<span class="sourceLineNo">102</span> * The name of the znode that contains queues of hfile references to be replicated<a name="line.102"></a> +<span class="sourceLineNo">103</span> */<a name="line.103"></a> +<span class="sourceLineNo">104</span> private final String hfileRefsZNode;<a name="line.104"></a> +<span class="sourceLineNo">105</span><a name="line.105"></a> +<span class="sourceLineNo">106</span> private final String regionsZNode;<a name="line.106"></a> +<span class="sourceLineNo">107</span><a name="line.107"></a> +<span class="sourceLineNo">108</span> public ZKReplicationQueueStorage(ZKWatcher zookeeper, Configuration conf) {<a name="line.108"></a> +<span class="sourceLineNo">109</span> super(zookeeper, conf);<a name="line.109"></a> +<span class="sourceLineNo">110</span><a name="line.110"></a> +<span class="sourceLineNo">111</span> String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");<a name="line.111"></a> +<span class="sourceLineNo">112</span> String hfileRefsZNodeName = conf.get(ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY,<a name="line.112"></a> +<span class="sourceLineNo">113</span> ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT);<a name="line.113"></a> +<span class="sourceLineNo">114</span> this.queuesZNode = ZNodePaths.joinZNode(replicationZNode, queuesZNodeName);<a name="line.114"></a> +<span class="sourceLineNo">115</span> this.hfileRefsZNode = ZNodePaths.joinZNode(replicationZNode, hfileRefsZNodeName);<a name="line.115"></a> +<span class="sourceLineNo">116</span> this.regionsZNode = ZNodePaths.joinZNode(replicationZNode, conf<a name="line.116"></a> +<span class="sourceLineNo">117</span> .get(ZOOKEEPER_ZNODE_REPLICATION_REGIONS_KEY, ZOOKEEPER_ZNODE_REPLICATION_REGIONS_DEFAULT));<a name="line.117"></a> +<span class="sourceLineNo">118</span> }<a name="line.118"></a> +<span class="sourceLineNo">119</span><a name="line.119"></a> +<span class="sourceLineNo">120</span> private String getRsNode(ServerName serverName) {<a name="line.120"></a> +<span class="sourceLineNo">121</span> return ZNodePaths.joinZNode(queuesZNode, serverName.getServerName());<a name="line.121"></a> +<span class="sourceLineNo">122</span> }<a name="line.122"></a> +<span class="sourceLineNo">123</span><a name="line.123"></a> +<span class="sourceLineNo">124</span> private String getQueueNode(ServerName serverName, String queueId) {<a name="line.124"></a> +<span class="sourceLineNo">125</span> return ZNodePaths.joinZNode(getRsNode(serverName), queueId);<a name="line.125"></a> +<span class="sourceLineNo">126</span> }<a name="line.126"></a> +<span class="sourceLineNo">127</span><a name="line.127"></a> +<span class="sourceLineNo">128</span> private String getFileNode(String queueNode, String fileName) {<a name="line.128"></a> +<span class="sourceLineNo">129</span> return ZNodePaths.joinZNode(queueNode, fileName);<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> private String getFileNode(ServerName serverName, String queueId, String fileName) {<a name="line.132"></a> +<span class="sourceLineNo">133</span> return getFileNode(getQueueNode(serverName, queueId), fileName);<a name="line.133"></a> +<span class="sourceLineNo">134</span> }<a name="line.134"></a> +<span class="sourceLineNo">135</span><a name="line.135"></a> +<span class="sourceLineNo">136</span> /**<a name="line.136"></a> +<span class="sourceLineNo">137</span> * Put all regions under /hbase/replication/regions znode will lead to too many children because<a name="line.137"></a> +<span class="sourceLineNo">138</span> * of the huge number of regions in real production environment. So here we use hash of encoded<a name="line.138"></a> +<span class="sourceLineNo">139</span> * region name to distribute the znode into multiple znodes. <br><a name="line.139"></a> +<span class="sourceLineNo">140</span> * So the final znode path will be format like this:<a name="line.140"></a> +<span class="sourceLineNo">141</span> *<a name="line.141"></a> +<span class="sourceLineNo">142</span> * <pre><a name="line.142"></a> +<span class="sourceLineNo">143</span> * /hbase/replication/regions/e1/ff/dd04e76a6966d4ffa908ed0586764767-100<a name="line.143"></a> +<span class="sourceLineNo">144</span> * </pre><a name="line.144"></a> +<span class="sourceLineNo">145</span> *<a name="line.145"></a> +<span class="sourceLineNo">146</span> * The e1 indicate the first level hash of encoded region name, and the ff indicate the second<a name="line.146"></a> +<span class="sourceLineNo">147</span> * level hash of encoded region name, the 100 indicate the peer id. <br><a name="line.147"></a> +<span class="sourceLineNo">148</span> * Note that here we use two-level hash because if only one-level hash (such as mod 65535), it<a name="line.148"></a> +<span class="sourceLineNo">149</span> * will still lead to too many children under the /hbase/replication/regions znode.<a name="line.149"></a> +<span class="sourceLineNo">150</span> * @param encodedRegionName the encoded region name.<a name="line.150"></a> +<span class="sourceLineNo">151</span> * @param peerId peer id for replication.<a name="line.151"></a> +<span class="sourceLineNo">152</span> * @return ZNode path to persist the max sequence id that we've pushed for the given region and<a name="line.152"></a> +<span class="sourceLineNo">153</span> * peer.<a name="line.153"></a> +<span class="sourceLineNo">154</span> */<a name="line.154"></a> +<span class="sourceLineNo">155</span> @VisibleForTesting<a name="line.155"></a> +<span class="sourceLineNo">156</span> public String getSerialReplicationRegionPeerNode(String encodedRegionName, String peerId) {<a name="line.156"></a> +<span class="sourceLineNo">157</span> if (encodedRegionName == null || encodedRegionName.length() != RegionInfo.MD5_HEX_LENGTH) {<a name="line.157"></a> +<span class="sourceLineNo">158</span> throw new IllegalArgumentException(<a name="line.158"></a> +<span class="sourceLineNo">159</span> "Invalid encoded region name: " + encodedRegionName + ", length should be 32.");<a name="line.159"></a> +<span class="sourceLineNo">160</span> }<a name="line.160"></a> +<span class="sourceLineNo">161</span> return new StringBuilder(regionsZNode).append(ZNodePaths.ZNODE_PATH_SEPARATOR)<a name="line.161"></a> +<span class="sourceLineNo">162</span> .append(encodedRegionName.substring(0, 2)).append(ZNodePaths.ZNODE_PATH_SEPARATOR)<a name="line.162"></a> +<span class="sourceLineNo">163</span> .append(encodedRegionName.substring(2, 4)).append(ZNodePaths.ZNODE_PATH_SEPARATOR)<a name="line.163"></a> +<span class="sourceLineNo">164</span> .append(encodedRegionName).append("-").append(peerId).toString();<a name="line.164"></a> <span class="sourceLineNo">165</span> }<a name="line.165"></a> <span class="sourceLineNo">166</span><a name="line.166"></a> <span class="sourceLineNo">167</span> @Override<a name="line.167"></a> -<span class="sourceLineNo">168</span> public void addWAL(ServerName serverName, String queueId, String fileName)<a name="line.168"></a> -<span class="sourceLineNo">169</span> throws ReplicationException {<a name="line.169"></a> -<span class="sourceLineNo">170</span> try {<a name="line.170"></a> -<span class="sourceLineNo">171</span> ZKUtil.createWithParents(zookeeper, getFileNode(serverName, queueId, fileName));<a name="line.171"></a> -<span class="sourceLineNo">172</span> } catch (KeeperException e) {<a name="line.172"></a> -<span class="sourceLineNo">173</span> throw new ReplicationException("Failed to add wal to queue (serverName=" + serverName<a name="line.173"></a> -<span class="sourceLineNo">174</span> + ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.174"></a> -<span class="sourceLineNo">175</span> }<a name="line.175"></a> -<span class="sourceLineNo">176</span> }<a name="line.176"></a> -<span class="sourceLineNo">177</span><a name="line.177"></a> -<span class="sourceLineNo">178</span> @Override<a name="line.178"></a> -<span class="sourceLineNo">179</span> public void removeWAL(ServerName serverName, String queueId, String fileName)<a name="line.179"></a> -<span class="sourceLineNo">180</span> throws ReplicationException {<a name="line.180"></a> -<span class="sourceLineNo">181</span> String fileNode = getFileNode(serverName, queueId, fileName);<a name="line.181"></a> -<span class="sourceLineNo">182</span> try {<a name="line.182"></a> -<span class="sourceLineNo">183</span> ZKUtil.deleteNode(zookeeper, fileNode);<a name="line.183"></a> -<span class="sourceLineNo">184</span> } catch (NoNodeException e) {<a name="line.184"></a> -<span class="sourceLineNo">185</span> LOG.warn("{} already deleted when removing log", fileNode);<a name="line.185"></a> -<span class="sourceLineNo">186</span> } catch (KeeperException e) {<a name="line.186"></a> -<span class="sourceLineNo">187</span> throw new ReplicationException("Failed to remove wal from queue (serverName=" + serverName +<a name="line.187"></a> -<span class="sourceLineNo">188</span> ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.188"></a> -<span class="sourceLineNo">189</span> }<a name="line.189"></a> -<span class="sourceLineNo">190</span> }<a name="line.190"></a> -<span class="sourceLineNo">191</span><a name="line.191"></a> -<span class="sourceLineNo">192</span> @Override<a name="line.192"></a> -<span class="sourceLineNo">193</span> public void setWALPosition(ServerName serverName, String queueId, String fileName, long position,<a name="line.193"></a> -<span class="sourceLineNo">194</span> Map<String, Long> lastSeqIds) throws ReplicationException {<a name="line.194"></a> -<span class="sourceLineNo">195</span> try {<a name="line.195"></a> -<span class="sourceLineNo">196</span> List<ZKUtilOp> listOfOps = new ArrayList<>();<a name="line.196"></a> -<span class="sourceLineNo">197</span> if (position > 0) {<a name="line.197"></a> -<span class="sourceLineNo">198</span> listOfOps.add(ZKUtilOp.setData(getFileNode(serverName, queueId, fileName),<a name="line.198"></a> -<span class="sourceLineNo">199</span> ZKUtil.positionToByteArray(position)));<a name="line.199"></a> -<span class="sourceLineNo">200</span> }<a name="line.200"></a> -<span class="sourceLineNo">201</span> // Persist the max sequence id(s) of regions for serial replication atomically.<a name="line.201"></a> -<span class="sourceLineNo">202</span> for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) {<a name="line.202"></a> -<span class="sourceLineNo">203</span> String peerId = new ReplicationQueueInfo(queueId).getPeerId();<a name="line.203"></a> -<span class="sourceLineNo">204</span> String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId);<a name="line.204"></a> -<span class="sourceLineNo">205</span> /*<a name="line.205"></a> -<span class="sourceLineNo">206</span> * Make sure the existence of path<a name="line.206"></a> -<span class="sourceLineNo">207</span> * /hbase/replication/regions/<hash>/<encoded-region-name>-<peer-id>. As the javadoc in<a name="line.207"></a> -<span class="sourceLineNo">208</span> * multiOrSequential() method said, if received a NodeExistsException, all operations will<a name="line.208"></a> -<span class="sourceLineNo">209</span> * fail. So create the path here, and in fact, no need to add this operation to listOfOps,<a name="line.209"></a> -<span class="sourceLineNo">210</span> * because only need to make sure that update file position and sequence id atomically.<a name="line.210"></a> -<span class="sourceLineNo">211</span> */<a name="line.211"></a> -<span class="sourceLineNo">212</span> ZKUtil.createWithParents(zookeeper, path);<a name="line.212"></a> -<span class="sourceLineNo">213</span> // Persist the max sequence id of region to zookeeper.<a name="line.213"></a> -<span class="sourceLineNo">214</span> listOfOps.add(ZKUtilOp.setData(path, ZKUtil.positionToByteArray(lastSeqEntry.getValue())));<a name="line.214"></a> -<span class="sourceLineNo">215</span> }<a name="line.215"></a> -<span class="sourceLineNo">216</span> if (!listOfOps.isEmpty()) {<a name="line.216"></a> -<span class="sourceLineNo">217</span> ZKUtil.multiOrSequential(zookeeper, listOfOps, false);<a name="line.217"></a> -<span class="sourceLineNo">218</span> }<a name="line.218"></a> -<span class="sourceLineNo">219</span> } catch (KeeperException e) {<a name="line.219"></a> -<span class="sourceLineNo">220</span> throw new ReplicationException("Failed to set log position (serverName=" + serverName<a name="line.220"></a> -<span class="sourceLineNo">221</span> + ", queueId=" + queueId + ", fileName=" + fileName + ", position=" + position + ")", e);<a name="line.221"></a> -<span class="sourceLineNo">222</span> }<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> @Override<a name="line.225"></a> -<span class="sourceLineNo">226</span> public long getLastSequenceId(String encodedRegionName, String peerId)<a name="line.226"></a> -<span class="sourceLineNo">227</span> throws ReplicationException {<a name="line.227"></a> -<span class="sourceLineNo">228</span> byte[] data;<a name="line.228"></a> -<span class="sourceLineNo">229</span> try {<a name="line.229"></a> -<span class="sourceLineNo">230</span> data =<a name="line.230"></a> -<span class="sourceLineNo">231</span> ZKUtil.getData(zookeeper, getSerialReplicationRegionPeerNode(encodedRegionName, peerId));<a name="line.231"></a> -<span class="sourceLineNo">232</span> } catch (KeeperException | InterruptedException e) {<a name="line.232"></a> -<span class="sourceLineNo">233</span> throw new ReplicationException("Failed to get the last sequence id(region="<a name="line.233"></a> -<span class="sourceLineNo">234</span> + encodedRegionName + ", peerId=" + peerId + ")");<a name="line.234"></a> -<span class="sourceLineNo">235</span> }<a name="line.235"></a> -<span class="sourceLineNo">236</span> try {<a name="line.236"></a> -<span class="sourceLineNo">237</span> return ZKUtil.parseWALPositionFrom(data);<a name="line.237"></a> -<span class="sourceLineNo">238</span> } catch (DeserializationException de) {<a name="line.238"></a> -<span class="sourceLineNo">239</span> LOG.warn("Failed to parse log position (region=" + encodedRegionName + ", peerId=" + peerId<a name="line.239"></a> -<span class="sourceLineNo">240</span> + "), data=" + Bytes.toStringBinary(data));<a name="line.240"></a> -<span class="sourceLineNo">241</span> }<a name="line.241"></a> -<span class="sourceLineNo">242</span> return HConstants.NO_SEQNUM;<a name="line.242"></a> -<span class="sourceLineNo">243</span> }<a name="line.243"></a> -<span class="sourceLineNo">244</span><a name="line.244"></a> -<span class="sourceLineNo">245</span> @Override<a name="line.245"></a> -<span class="sourceLineNo">246</span> public long getWALPosition(ServerName serverName, String queueId, String fileName)<a name="line.246"></a> -<span class="sourceLineNo">247</span> throws ReplicationException {<a name="line.247"></a> -<span class="sourceLineNo">248</span> byte[] bytes;<a name="line.248"></a> -<span class="sourceLineNo">249</span> try {<a name="line.249"></a> -<span class="sourceLineNo">250</span> bytes = ZKUtil.getData(zookeeper, getFileNode(serverName, queueId, fileName));<a name="line.250"></a> -<span class="sourceLineNo">251</span> } catch (KeeperException | InterruptedException e) {<a name="line.251"></a> -<span class="sourceLineNo">252</span> throw new ReplicationException("Failed to get log position (serverName=" + serverName +<a name="line.252"></a> -<span class="sourceLineNo">253</span> ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.253"></a> -<span class="sourceLineNo">254</span> }<a name="line.254"></a> -<span class="sourceLineNo">255</span> try {<a name="line.255"></a> -<span class="sourceLineNo">256</span> return ZKUtil.parseWALPositionFrom(bytes);<a name="line.256"></a> -<span class="sourceLineNo">257</span> } catch (DeserializationException de) {<a name="line.257"></a> -<span class="sourceLineNo">258</span> LOG.warn("Failed parse log position (serverName={}, queueId={}, fileName={})",<a name="line.258"></a> -<span class="sourceLineNo">259</span> serverName, queueId, fileName);<a name="line.259"></a> -<span class="sourceLineNo">260</span> }<a name="line.260"></a> -<span class="sourceLineNo">261</span> // if we can not parse the position, start at the beginning of the wal file again<a name="line.261"></a> -<span class="sourceLineNo">262</span> return 0;<a name="line.262"></a> -<span class="sourceLineNo">263</span> }<a name="line.263"></a> -<span class="sourceLineNo">264</span><a name="line.264"></a> -<span class="sourceLineNo">265</span> @Override<a name="line.265"></a> -<span class="sourceLineNo">266</span> public Pair<String, SortedSet<String>> claimQueue(ServerName sourceServerName, String queueId,<a name="line.266"></a> -<span class="sourceLineNo">267</span> ServerName destServerName) throws ReplicationException {<a name="line.267"></a> -<span class="sourceLineNo">268</span> LOG.info("Atomically moving {}/{}'s WALs to {}", sourceServerName, queueId, destServerName);<a name="line.268"></a> -<span class="sourceLineNo">269</span> try {<a name="line.269"></a> -<span class="sourceLineNo">270</span> ZKUtil.createWithParents(zookeeper, getRsNode(destServerName));<a name="line.270"></a> -<span class="sourceLineNo">271</span> } catch (KeeperException e) {<a name="line.271"></a> -<span class="sourceLineNo">272</span> throw new ReplicationException(<a name="line.272"></a> -<span class="sourceLineNo">273</span> "Claim queue queueId=" + queueId + " from " + sourceServerName + " to " + destServerName +<a name="line.273"></a> -<span class="sourceLineNo">274</span> " failed when creating the node for " + destServerName,<a name="line.274"></a> -<span class="sourceLineNo">275</span> e);<a name="line.275"></a> -<span class="sourceLineNo">276</span> }<a name="line.276"></a> -<span class="sourceLineNo">277</span> try {<a name="line.277"></a> -<span class="sourceLineNo">278</span> String oldQueueNode = getQueueNode(sourceServerName, queueId);<a name="line.278"></a> -<span class="sourceLineNo">279</span> List<String> wals = ZKUtil.listChildrenNoWatch(zookeeper, oldQueueNode);<a name="line.279"></a> -<span class="sourceLineNo">280</span> String newQueueId = queueId + "-" + sourceServerName;<a name="line.280"></a> -<span class="sourceLineNo">281</span> if (CollectionUtils.isEmpty(wals)) {<a name="line.281"></a> -<span class="sourceLineNo">282</span> ZKUtil.deleteNodeFailSilent(zookeeper, oldQueueNode);<a name="line.282"></a> -<span class="sourceLineNo">283</span> LOG.info("Removed empty {}/{}", sourceServerName, queueId);<a name="line.283"></a> -<span class="sourceLineNo">284</span> return new Pair<>(newQueueId, Collections.emptySortedSet());<a name="line.284"></a> -<span class="sourceLineNo">285</span> }<a name="line.285"></a> -<span class="sourceLineNo">286</span> String newQueueNode = getQueueNode(destServerName, newQueueId);<a name="line.286"></a> -<span class="sourceLineNo">287</span> List<ZKUtilOp> listOfOps = new ArrayList<>();<a name="line.287"></a> -<span class="sourceLineNo">288</span> SortedSet<String> logQueue = new TreeSet<>();<a name="line.288"></a> -<span class="sourceLineNo">289</span> // create the new cluster znode<a name="line.289"></a> -<span class="sourceLineNo">290</span> listOfOps.add(ZKUtilOp.createAndFailSilent(newQueueNode, HConstants.EMPTY_BYTE_ARRAY));<a name="line.290"></a> -<span class="sourceLineNo">291</span> // get the offset of the logs and set it to new znodes<a name="line.291"></a> -<span class="sourceLineNo">292</span> for (String wal : wals) {<a name="line.292"></a> -<span class="sourceLineNo">293</span> String oldWalNode = getFileNode(oldQueueNode, wal);<a name="line.293"></a> -<span class="sourceLineNo">294</span> byte[] logOffset = ZKUtil.getData(this.zookeeper, oldWalNode);<a name="line.294"></a> -<span class="sourceLineNo">295</span> LOG.debug("Creating {} with data {}", wal, Bytes.toStringBinary(logOffset));<a name="line.295"></a> -<span class="sourceLineNo">296</span> String newWalNode = getFileNode(newQueueNode, wal);<a name="line.296"></a> -<span class="sourceLineNo">297</span> listOfOps.add(ZKUtilOp.createAndFailSilent(newWalNode, logOffset));<a name="line.297"></a> -<span class="sourceLineNo">298</span> listOfOps.add(ZKUtilOp.deleteNodeFailSilent(oldWalNode));<a name="line.298"></a> -<span class="sourceLineNo">299</span> logQueue.add(wal);<a name="line.299"></a> -<span class="sourceLineNo">300</span> }<a name="line.300"></a> -<span class="sourceLineNo">301</span> // add delete op for peer<a name="line.301"></a> -<span class="sourceLineNo">302</span> listOfOps.add(ZKUtilOp.deleteNodeFailSilent(oldQueueNode));<a name="line.302"></a> -<span class="sourceLineNo">303</span><a name="line.303"></a> -<span class="sourceLineNo">304</span> LOG.trace("The multi list size is {}", listOfOps.size());<a name="line.304"></a> -<span class="sourceLineNo">305</span> ZKUtil.multiOrSequential(zookeeper, listOfOps, false);<a name="line.305"></a> -<span class="sourceLineNo">306</span><a name="line.306"></a> -<span class="sourceLineNo">307</span> LOG.info("Atomically moved {}/{}'s WALs to {}", sourceServerName, queueId, destServerName);<a name="line.307"></a> -<span class="sourceLineNo">308</span> return new Pair<>(newQueueId, logQueue);<a name="line.308"></a> -<span class="sourceLineNo">309</span> } catch (NoNodeException | NodeExistsException | NotEmptyException | BadVersionException e) {<a name="line.309"></a> -<span class="sourceLineNo">310</span> // Multi call failed; it looks like some other regionserver took away the logs.<a name="line.310"></a> -<span class="sourceLineNo">311</span> // These exceptions mean that zk tells us the request can not be execute so it is safe to just<a name="line.311"></a> -<span class="sourceLineNo">312</span> // return a null. For other types of exception should be thrown out to notify the upper layer.<a name="line.312"></a> -<span class="sourceLineNo">313</span> LOG.info("Claim queue queueId={} from {} to {} failed with {}, someone else took the log?",<a name="line.313"></a> -<span class="sourceLineNo">314</span> queueId,sourceServerName, destServerName, e.toString());<a name="line.314"></a> -<span class="sourceLineNo">315</span> return null;<a name="line.315"></a> -<span class="sourceLineNo">316</span> } catch (KeeperException | InterruptedException e) {<a name="line.316"></a> -<span class="sourceLineNo">317</span> throw new ReplicationException("Claim queue queueId=" + queueId + " from " +<a name="line.317"></a> -<span class="sourceLineNo">318</span> sourceServerName + " to " + destServerName + " failed", e);<a name="line.318"></a> -<span class="sourceLineNo">319</span> }<a name="line.319"></a> -<span class="sourceLineNo">320</span> }<a name="line.320"></a> -<span class="sourceLineNo">321</span><a name="line.321"></a> -<span class="sourceLineNo">322</span> @Override<a name="line.322"></a> -<span class="sourceLineNo">323</span> public void removeReplicatorIfQueueIsEmpty(ServerName serverName) throws ReplicationException {<a name="line.323"></a> -<span class="sourceLineNo">324</span> try {<a name="line.324"></a> -<span class="sourceLineNo">325</span> ZKUtil.deleteNodeFailSilent(zookeeper, getRsNode(serverName));<a name="line.325"></a> -<span class="sourceLineNo">326</span> } catch (NotEmptyException e) {<a name="line.326"></a> -<span class="sourceLineNo">327</span> // keep silence to avoid logging too much.<a name="line.327"></a> -<span class="sourceLineNo">328</span> } catch (KeeperException e) {<a name="line.328"></a> -<span class="sourceLineNo">329</span> throw new ReplicationException("Failed to remove replicator for " + serverName, e);<a name="line.329"></a> -<span class="sourceLineNo">330</span> }<a name="line.330"></a> -<span class="sourceLineNo">331</span> }<a name="line.331"></a> -<span class="sourceLineNo">332</span><a name="line.332"></a> -<span class="sourceLineNo">333</span> private List<ServerName> getListOfReplicators0() throws KeeperException {<a name="line.333"></a> -<span class="sourceLineNo">334</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, queuesZNode)).stream()<a name="line.334"></a> -<span class="sourceLineNo">335</span> .map(ServerName::parseServerName).collect(toList());<a name="line.335"></a> -<span class="sourceLineNo">336</span> }<a name="line.336"></a> -<span class="sourceLineNo">337</span><a name="line.337"></a> -<span class="sourceLineNo">338</span> @Override<a name="line.338"></a> -<span class="sourceLineNo">339</span> public List<ServerName> getListOfReplicators() throws ReplicationException {<a name="line.339"></a> -<span class="sourceLineNo">340</span> try {<a name="line.340"></a> -<span class="sourceLineNo">341</span> return getListOfReplicators0();<a name="line.341"></a> -<span class="sourceLineNo">342</span> } catch (KeeperException e) {<a name="line.342"></a> -<span class="sourceLineNo">343</span> throw new ReplicationException("Failed to get list of replicators", e);<a name="line.343"></a> -<span class="sourceLineNo">344</span> }<a name="line.344"></a> -<span class="sourceLineNo">345</span> }<a name="line.345"></a> -<span class="sourceLineNo">346</span><a name="line.346"></a> -<span class="sourceLineNo">347</span> private List<String> getWALsInQueue0(ServerName serverName, String queueId)<a name="line.347"></a> -<span class="sourceLineNo">348</span> throws KeeperException {<a name="line.348"></a> -<span class="sourceLineNo">349</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, getQueueNode(serverName, queueId)));<a name="line.349"></a> -<span class="sourceLineNo">350</span> }<a name="line.350"></a> -<span class="sourceLineNo">351</span><a name="line.351"></a> -<span class="sourceLineNo">352</span> @Override<a name="line.352"></a> -<span class="sourceLineNo">353</span> public List<String> getWALsInQueue(ServerName serverName, String queueId)<a name="line.353"></a> -<span class="sourceLineNo">354</span> throws ReplicationException {<a name="line.354"></a> -<span class="sourceLineNo">355</span> try {<a name="line.355"></a> -<span class="sourceLineNo">356</span> return getWALsInQueue0(serverName, queueId);<a name="line.356"></a> -<span class="sourceLineNo">357</span> } catch (KeeperException e) {<a name="line.357"></a> -<span class="sourceLineNo">358</span> throw new ReplicationException(<a name="line.358"></a> -<span class="sourceLineNo">359</span> "Failed to get wals in queue (serverName=" + serverName + ", queueId=" + queueId + ")",<a name="line.359"></a> -<span class="sourceLineNo">360</span> e);<a name="line.360"></a> -<span class="sourceLineNo">361</span> }<a name="line.361"></a> -<span class="sourceLineNo">362</span> }<a name="line.362"></a> -<span class="sourceLineNo">363</span><a name="line.363"></a> -<span class="sourceLineNo">364</span> private List<String> getAllQueues0(ServerName serverName) throws KeeperException {<a name="line.364"></a> -<span class="sourceLineNo">365</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, getRsNode(serverName)));<a name="line.365"></a> -<span class="sourceLineNo">366</span> }<a name="line.366"></a> -<span class="sourceLineNo">367</span><a name="line.367"></a> -<span class="sourceLineNo">368</span> @Override<a name="line.368"></a> -<span class="sourceLineNo">369</span> public List<String> getAllQueues(ServerName serverName) throws ReplicationException {<a name="line.369"></a> -<span class="sourceLineNo">370</span> try {<a name="line.370"></a> -<span class="sourceLineNo">371</span> return getAllQueues0(serverName);<a name="line.371"></a> -<span class="sourceLineNo">372</span> } catch (KeeperException e) {<a name="line.372"></a> -<span class="sourceLineNo">373</span> throw new ReplicationException("Failed to get all queues (serverName=" + serverName + ")", e);<a name="line.373"></a> -<span class="sourceLineNo">374</span> }<a name="line.374"></a> -<span class="sourceLineNo">375</span> }<a name="line.375"></a> -<span class="sourceLineNo">376</span><a name="line.376"></a> -<span class="sourceLineNo">377</span> // will be overridden in UTs<a name="line.377"></a> -<span class="sourceLineNo">378</span> @VisibleForTesting<a name="line.378"></a> -<span class="sourceLineNo">379</span> protected int getQueuesZNodeCversion() throws KeeperException {<a name="line.379"></a> -<span class="sourceLineNo">380</span> Stat stat = new Stat();<a name="line.380"></a> -<span class="sourceLineNo">381</span> ZKUtil.getDataNoWatch(this.zookeeper, this.queuesZNode, stat);<a name="line.381"></a> -<span class="sourceLineNo">382</span> return stat.getCversion();<a name="line.382"></a> -<span class="sourceLineNo">383</span> }<a name="line.383"></a> -<span class="sourceLineNo">384</span><a name="line.384"></a> -<span class="sourceLineNo">385</span> @Override<a name="line.385"></a> -<span class="sourceLineNo">386</span> public Set<String> getAllWALs() throws ReplicationException {<a name="line.386"></a> -<span class="sourceLineNo">387</span> try {<a name="line.387"></a> -<span class="sourceLineNo">388</span> for (int retry = 0;; retry++) {<a name="line.388"></a> -<span class="sourceLineNo">389</span> int v0 = getQueuesZNodeCversion();<a name="line.389"></a> -<span class="sourceLineNo">390</span> List<ServerName> rss = getListOfReplicators0();<a name="line.390"></a> -<span class="sourceLineNo">391</span> if (rss.isEmpty()) {<a name="line.391"></a> -<span class="sourceLineNo">392</span> LOG.debug("Didn't find a RegionServer that replicates, won't prevent deletions.");<a name="line.392"></a> -<span class="sourceLineNo">393</span> return Collections.emptySet();<a name="line.393"></a> -<span class="sourceLineNo">394</span> }<a name="line.394"></a> -<span class="sourceLineNo">395</span> Set<String> wals = new HashSet<>();<a name="line.395"></a> -<span class="sourceLineNo">396</span> for (ServerName rs : rss) {<a name="line.396"></a> -<span class="sourceLineNo">397</span> for (String queueId : getAllQueues0(rs)) {<a name="line.397"></a> -<span class="sourceLineNo">398</span> wals.addAll(getWALsInQueue0(rs, queueId));<a name="line.398"></a> -<span class="sourceLineNo">399</span> }<a name="line.399"></a> -<span class="sourceLineNo">400</span> }<a name="line.400"></a> -<span class="sourceLineNo">401</span> int v1 = getQueuesZNodeCversion();<a name="line.401"></a> -<span class="sourceLineNo">402</span> if (v0 == v1) {<a name="line.402"></a> -<span class="sourceLineNo">403</span> return wals;<a name="line.403"></a> +<span class="sourceLineNo">168</span> public void removeQueue(ServerName serverName, String queueId) throws ReplicationException {<a name="line.168"></a> +<span class="sourceLineNo">169</span> try {<a name="line.169"></a> +<span class="sourceLineNo">170</span> ZKUtil.deleteNodeRecursively(zookeeper, getQueueNode(serverName, queueId));<a name="line.170"></a> +<span class="sourceLineNo">171</span> } catch (KeeperException e) {<a name="line.171"></a> +<span class="sourceLineNo">172</span> throw new ReplicationException(<a name="line.172"></a> +<span class="sourceLineNo">173</span> "Failed to delete queue (serverName=" + serverName + ", queueId=" + queueId + ")", e);<a name="line.173"></a> +<span class="sourceLineNo">174</span> }<a name="line.174"></a> +<span class="sourceLineNo">175</span> }<a name="line.175"></a> +<span class="sourceLineNo">176</span><a name="line.176"></a> +<span class="sourceLineNo">177</span> @Override<a name="line.177"></a> +<span class="sourceLineNo">178</span> public void addWAL(ServerName serverName, String queueId, String fileName)<a name="line.178"></a> +<span class="sourceLineNo">179</span> throws ReplicationException {<a name="line.179"></a> +<span class="sourceLineNo">180</span> try {<a name="line.180"></a> +<span class="sourceLineNo">181</span> ZKUtil.createWithParents(zookeeper, getFileNode(serverName, queueId, fileName));<a name="line.181"></a> +<span class="sourceLineNo">182</span> } catch (KeeperException e) {<a name="line.182"></a> +<span class="sourceLineNo">183</span> throw new ReplicationException("Failed to add wal to queue (serverName=" + serverName<a name="line.183"></a> +<span class="sourceLineNo">184</span> + ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.184"></a> +<span class="sourceLineNo">185</span> }<a name="line.185"></a> +<span class="sourceLineNo">186</span> }<a name="line.186"></a> +<span class="sourceLineNo">187</span><a name="line.187"></a> +<span class="sourceLineNo">188</span> @Override<a name="line.188"></a> +<span class="sourceLineNo">189</span> public void removeWAL(ServerName serverName, String queueId, String fileName)<a name="line.189"></a> +<span class="sourceLineNo">190</span> throws ReplicationException {<a name="line.190"></a> +<span class="sourceLineNo">191</span> String fileNode = getFileNode(serverName, queueId, fileName);<a name="line.191"></a> +<span class="sourceLineNo">192</span> try {<a name="line.192"></a> +<span class="sourceLineNo">193</span> ZKUtil.deleteNode(zookeeper, fileNode);<a name="line.193"></a> +<span class="sourceLineNo">194</span> } catch (NoNodeException e) {<a name="line.194"></a> +<span class="sourceLineNo">195</span> LOG.warn("{} already deleted when removing log", fileNode);<a name="line.195"></a> +<span class="sourceLineNo">196</span> } catch (KeeperException e) {<a name="line.196"></a> +<span class="sourceLineNo">197</span> throw new ReplicationException("Failed to remove wal from queue (serverName=" + serverName +<a name="line.197"></a> +<span class="sourceLineNo">198</span> ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.198"></a> +<span class="sourceLineNo">199</span> }<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> @Override<a name="line.202"></a> +<span class="sourceLineNo">203</span> public void setWALPosition(ServerName serverName, String queueId, String fileName, long position,<a name="line.203"></a> +<span class="sourceLineNo">204</span> Map<String, Long> lastSeqIds) throws ReplicationException {<a name="line.204"></a> +<span class="sourceLineNo">205</span> try {<a name="line.205"></a> +<span class="sourceLineNo">206</span> List<ZKUtilOp> listOfOps = new ArrayList<>();<a name="line.206"></a> +<span class="sourceLineNo">207</span> if (position > 0) {<a name="line.207"></a> +<span class="sourceLineNo">208</span> listOfOps.add(ZKUtilOp.setData(getFileNode(serverName, queueId, fileName),<a name="line.208"></a> +<span class="sourceLineNo">209</span> ZKUtil.positionToByteArray(position)));<a name="line.209"></a> +<span class="sourceLineNo">210</span> }<a name="line.210"></a> +<span class="sourceLineNo">211</span> // Persist the max sequence id(s) of regions for serial replication atomically.<a name="line.211"></a> +<span class="sourceLineNo">212</span> for (Entry<String, Long> lastSeqEntry : lastSeqIds.entrySet()) {<a name="line.212"></a> +<span class="sourceLineNo">213</span> String peerId = new ReplicationQueueInfo(queueId).getPeerId();<a name="line.213"></a> +<span class="sourceLineNo">214</span> String path = getSerialReplicationRegionPeerNode(lastSeqEntry.getKey(), peerId);<a name="line.214"></a> +<span class="sourceLineNo">215</span> /*<a name="line.215"></a> +<span class="sourceLineNo">216</span> * Make sure the existence of path<a name="line.216"></a> +<span class="sourceLineNo">217</span> * /hbase/replication/regions/<hash>/<encoded-region-name>-<peer-id>. As the javadoc in<a name="line.217"></a> +<span class="sourceLineNo">218</span> * multiOrSequential() method said, if received a NodeExistsException, all operations will<a name="line.218"></a> +<span class="sourceLineNo">219</span> * fail. So create the path here, and in fact, no need to add this operation to listOfOps,<a name="line.219"></a> +<span class="sourceLineNo">220</span> * because only need to make sure that update file position and sequence id atomically.<a name="line.220"></a> +<span class="sourceLineNo">221</span> */<a name="line.221"></a> +<span class="sourceLineNo">222</span> ZKUtil.createWithParents(zookeeper, path);<a name="line.222"></a> +<span class="sourceLineNo">223</span> // Persist the max sequence id of region to zookeeper.<a name="line.223"></a> +<span class="sourceLineNo">224</span> listOfOps.add(ZKUtilOp.setData(path, ZKUtil.positionToByteArray(lastSeqEntry.getValue())));<a name="line.224"></a> +<span class="sourceLineNo">225</span> }<a name="line.225"></a> +<span class="sourceLineNo">226</span> if (!listOfOps.isEmpty()) {<a name="line.226"></a> +<span class="sourceLineNo">227</span> ZKUtil.multiOrSequential(zookeeper, listOfOps, false);<a name="line.227"></a> +<span class="sourceLineNo">228</span> }<a name="line.228"></a> +<span class="sourceLineNo">229</span> } catch (KeeperException e) {<a name="line.229"></a> +<span class="sourceLineNo">230</span> throw new ReplicationException("Failed to set log position (serverName=" + serverName<a name="line.230"></a> +<span class="sourceLineNo">231</span> + ", queueId=" + queueId + ", fileName=" + fileName + ", position=" + position + ")", e);<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><a name="line.234"></a> +<span class="sourceLineNo">235</span> @Override<a name="line.235"></a> +<span class="sourceLineNo">236</span> public long getLastSequenceId(String encodedRegionName, String peerId)<a name="line.236"></a> +<span class="sourceLineNo">237</span> throws ReplicationException {<a name="line.237"></a> +<span class="sourceLineNo">238</span> byte[] data;<a name="line.238"></a> +<span class="sourceLineNo">239</span> try {<a name="line.239"></a> +<span class="sourceLineNo">240</span> data =<a name="line.240"></a> +<span class="sourceLineNo">241</span> ZKUtil.getData(zookeeper, getSerialReplicationRegionPeerNode(encodedRegionName, peerId));<a name="line.241"></a> +<span class="sourceLineNo">242</span> } catch (KeeperException | InterruptedException e) {<a name="line.242"></a> +<span class="sourceLineNo">243</span> throw new ReplicationException("Failed to get the last sequence id(region="<a name="line.243"></a> +<span class="sourceLineNo">244</span> + encodedRegionName + ", peerId=" + peerId + ")");<a name="line.244"></a> +<span class="sourceLineNo">245</span> }<a name="line.245"></a> +<span class="sourceLineNo">246</span> try {<a name="line.246"></a> +<span class="sourceLineNo">247</span> return ZKUtil.parseWALPositionFrom(data);<a name="line.247"></a> +<span class="sourceLineNo">248</span> } catch (DeserializationException de) {<a name="line.248"></a> +<span class="sourceLineNo">249</span> LOG.warn("Failed to parse log position (region=" + encodedRegionName + ", peerId=" + peerId<a name="line.249"></a> +<span class="sourceLineNo">250</span> + "), data=" + Bytes.toStringBinary(data));<a name="line.250"></a> +<span class="sourceLineNo">251</span> }<a name="line.251"></a> +<span class="sourceLineNo">252</span> return HConstants.NO_SEQNUM;<a name="line.252"></a> +<span class="sourceLineNo">253</span> }<a name="line.253"></a> +<span class="sourceLineNo">254</span><a name="line.254"></a> +<span class="sourceLineNo">255</span> @Override<a name="line.255"></a> +<span class="sourceLineNo">256</span> public long getWALPosition(ServerName serverName, String queueId, String fileName)<a name="line.256"></a> +<span class="sourceLineNo">257</span> throws ReplicationException {<a name="line.257"></a> +<span class="sourceLineNo">258</span> byte[] bytes;<a name="line.258"></a> +<span class="sourceLineNo">259</span> try {<a name="line.259"></a> +<span class="sourceLineNo">260</span> bytes = ZKUtil.getData(zookeeper, getFileNode(serverName, queueId, fileName));<a name="line.260"></a> +<span class="sourceLineNo">261</span> } catch (KeeperException | InterruptedException e) {<a name="line.261"></a> +<span class="sourceLineNo">262</span> throw new ReplicationException("Failed to get log position (serverName=" + serverName +<a name="line.262"></a> +<span class="sourceLineNo">263</span> ", queueId=" + queueId + ", fileName=" + fileName + ")", e);<a name="line.263"></a> +<span class="sourceLineNo">264</span> }<a name="line.264"></a> +<span class="sourceLineNo">265</span> try {<a name="line.265"></a> +<span class="sourceLineNo">266</span> return ZKUtil.parseWALPositionFrom(bytes);<a name="line.266"></a> +<span class="sourceLineNo">267</span> } catch (DeserializationException de) {<a name="line.267"></a> +<span class="sourceLineNo">268</span> LOG.warn("Failed parse log position (serverName={}, queueId={}, fileName={})",<a name="line.268"></a> +<span class="sourceLineNo">269</span> serverName, queueId, fileName);<a name="line.269"></a> +<span class="sourceLineNo">270</span> }<a name="line.270"></a> +<span class="sourceLineNo">271</span> // if we can not parse the position, start at the beginning of the wal file again<a name="line.271"></a> +<span class="sourceLineNo">272</span> return 0;<a name="line.272"></a> +<span class="sourceLineNo">273</span> }<a name="line.273"></a> +<span class="sourceLineNo">274</span><a name="line.274"></a> +<span class="sourceLineNo">275</span> @Override<a name="line.275"></a> +<span class="sourceLineNo">276</span> public Pair<String, SortedSet<String>> claimQueue(ServerName sourceServerName, String queueId,<a name="line.276"></a> +<span class="sourceLineNo">277</span> ServerName destServerName) throws ReplicationException {<a name="line.277"></a> +<span class="sourceLineNo">278</span> LOG.info("Atomically moving {}/{}'s WALs to {}", sourceServerName, queueId, destServerName);<a name="line.278"></a> +<span class="sourceLineNo">279</span> try {<a name="line.279"></a> +<span class="sourceLineNo">280</span> ZKUtil.createWithParents(zookeeper, getRsNode(destServerName));<a name="line.280"></a> +<span class="sourceLineNo">281</span> } catch (KeeperException e) {<a name="line.281"></a> +<span class="sourceLineNo">282</span> throw new ReplicationException(<a name="line.282"></a> +<span class="sourceLineNo">283</span> "Claim queue queueId=" + queueId + " from " + sourceServerName + " to " + destServerName +<a name="line.283"></a> +<span class="sourceLineNo">284</span> " failed when creating the node for " + destServerName,<a name="line.284"></a> +<span class="sourceLineNo">285</span> e);<a name="line.285"></a> +<span class="sourceLineNo">286</span> }<a name="line.286"></a> +<span class="sourceLineNo">287</span> try {<a name="line.287"></a> +<span class="sourceLineNo">288</span> String oldQueueNode = getQueueNode(sourceServerName, queueId);<a name="line.288"></a> +<span class="sourceLineNo">289</span> List<String> wals = ZKUtil.listChildrenNoWatch(zookeeper, oldQueueNode);<a name="line.289"></a> +<span class="sourceLineNo">290</span> String newQueueId = queueId + "-" + sourceServerName;<a name="line.290"></a> +<span class="sourceLineNo">291</span> if (CollectionUtils.isEmpty(wals)) {<a name="line.291"></a> +<span class="sourceLineNo">292</span> ZKUtil.deleteNodeFailSilent(zookeeper, oldQueueNode);<a name="line.292"></a> +<span class="sourceLineNo">293</span> LOG.info("Removed empty {}/{}", sourceServerName, queueId);<a name="line.293"></a> +<span class="sourceLineNo">294</span> return new Pair<>(newQueueId, Collections.emptySortedSet());<a name="line.294"></a> +<span class="sourceLineNo">295</span> }<a name="line.295"></a> +<span class="sourceLineNo">296</span> String newQueueNode = getQueueNode(destServerName, newQueueId);<a name="line.296"></a> +<span class="sourceLineNo">297</span> List<ZKUtilOp> listOfOps = new ArrayList<>();<a name="line.297"></a> +<span class="sourceLineNo">298</span> SortedSet<String> logQueue = new TreeSet<>();<a name="line.298"></a> +<span class="sourceLineNo">299</span> // create the new cluster znode<a name="line.299"></a> +<span class="sourceLineNo">300</span> listOfOps.add(ZKUtilOp.createAndFailSilent(newQueueNode, HConstants.EMPTY_BYTE_ARRAY));<a name="line.300"></a> +<span class="sourceLineNo">301</span> // get the offset of the logs and set it to new znodes<a name="line.301"></a> +<span class="sourceLineNo">302</span> for (String wal : wals) {<a name="line.302"></a> +<span class="sourceLineNo">303</span> String oldWalNode = getFileNode(oldQueueNode, wal);<a name="line.303"></a> +<span class="sourceLineNo">304</span> byte[] logOffset = ZKUtil.getData(this.zookeeper, oldWalNode);<a name="line.304"></a> +<span class="sourceLineNo">305</span> LOG.debug("Creating {} with data {}", wal, Bytes.toStringBinary(logOffset));<a name="line.305"></a> +<span class="sourceLineNo">306</span> String newWalNode = getFileNode(newQueueNode, wal);<a name="line.306"></a> +<span class="sourceLineNo">307</span> listOfOps.add(ZKUtilOp.createAndFailSilent(newWalNode, logOffset));<a name="line.307"></a> +<span class="sourceLineNo">308</span> listOfOps.add(ZKUtilOp.deleteNodeFailSilent(oldWalNode));<a name="line.308"></a> +<span class="sourceLineNo">309</span> logQueue.add(wal);<a name="line.309"></a> +<span class="sourceLineNo">310</span> }<a name="line.310"></a> +<span class="sourceLineNo">311</span> // add delete op for peer<a name="line.311"></a> +<span class="sourceLineNo">312</span> listOfOps.add(ZKUtilOp.deleteNodeFailSilent(oldQueueNode));<a name="line.312"></a> +<span class="sourceLineNo">313</span><a name="line.313"></a> +<span class="sourceLineNo">314</span> LOG.trace("The multi list size is {}", listOfOps.size());<a name="line.314"></a> +<span class="sourceLineNo">315</span> ZKUtil.multiOrSequential(zookeeper, listOfOps, false);<a name="line.315"></a> +<span class="sourceLineNo">316</span><a name="line.316"></a> +<span class="sourceLineNo">317</span> LOG.info("Atomically moved {}/{}'s WALs to {}", sourceServerName, queueId, destServerName);<a name="line.317"></a> +<span class="sourceLineNo">318</span> return new Pair<>(newQueueId, logQueue);<a name="line.318"></a> +<span class="sourceLineNo">319</span> } catch (NoNodeException | NodeExistsException | NotEmptyException | BadVersionException e) {<a name="line.319"></a> +<span class="sourceLineNo">320</span> // Multi call failed; it looks like some other regionserver took away the logs.<a name="line.320"></a> +<span class="sourceLineNo">321</span> // These exceptions mean that zk tells us the request can not be execute so it is safe to just<a name="line.321"></a> +<span class="sourceLineNo">322</span> // return a null. For other types of exception should be thrown out to notify the upper layer.<a name="line.322"></a> +<span class="sourceLineNo">323</span> LOG.info("Claim queue queueId={} from {} to {} failed with {}, someone else took the log?",<a name="line.323"></a> +<span class="sourceLineNo">324</span> queueId,sourceServerName, destServerName, e.toString());<a name="line.324"></a> +<span class="sourceLineNo">325</span> return null;<a name="line.325"></a> +<span class="sourceLineNo">326</span> } catch (KeeperException | InterruptedException e) {<a name="line.326"></a> +<span class="sourceLineNo">327</span> throw new ReplicationException("Claim queue queueId=" + queueId + " from " +<a name="line.327"></a> +<span class="sourceLineNo">328</span> sourceServerName + " to " + destServerName + " failed", e);<a name="line.328"></a> +<span class="sourceLineNo">329</span> }<a name="line.329"></a> +<span class="sourceLineNo">330</span> }<a name="line.330"></a> +<span class="sourceLineNo">331</span><a name="line.331"></a> +<span class="sourceLineNo">332</span> @Override<a name="line.332"></a> +<span class="sourceLineNo">333</span> public void removeReplicatorIfQueueIsEmpty(ServerName serverName) throws ReplicationException {<a name="line.333"></a> +<span class="sourceLineNo">334</span> try {<a name="line.334"></a> +<span class="sourceLineNo">335</span> ZKUtil.deleteNodeFailSilent(zookeeper, getRsNode(serverName));<a name="line.335"></a> +<span class="sourceLineNo">336</span> } catch (NotEmptyException e) {<a name="line.336"></a> +<span class="sourceLineNo">337</span> // keep silence to avoid logging too much.<a name="line.337"></a> +<span class="sourceLineNo">338</span> } catch (KeeperException e) {<a name="line.338"></a> +<span class="sourceLineNo">339</span> throw new ReplicationException("Failed to remove replicator for " + serverName, e);<a name="line.339"></a> +<span class="sourceLineNo">340</span> }<a name="line.340"></a> +<span class="sourceLineNo">341</span> }<a name="line.341"></a> +<span class="sourceLineNo">342</span><a name="line.342"></a> +<span class="sourceLineNo">343</span> private List<ServerName> getListOfReplicators0() throws KeeperException {<a name="line.343"></a> +<span class="sourceLineNo">344</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, queuesZNode)).stream()<a name="line.344"></a> +<span class="sourceLineNo">345</span> .map(ServerName::parseServerName).collect(toList());<a name="line.345"></a> +<span class="sourceLineNo">346</span> }<a name="line.346"></a> +<span class="sourceLineNo">347</span><a name="line.347"></a> +<span class="sourceLineNo">348</span> @Override<a name="line.348"></a> +<span class="sourceLineNo">349</span> public List<ServerName> getListOfReplicators() throws ReplicationException {<a name="line.349"></a> +<span class="sourceLineNo">350</span> try {<a name="line.350"></a> +<span class="sourceLineNo">351</span> return getListOfReplicators0();<a name="line.351"></a> +<span class="sourceLineNo">352</span> } catch (KeeperException e) {<a name="line.352"></a> +<span class="sourceLineNo">353</span> throw new ReplicationException("Failed to get list of replicators", e);<a name="line.353"></a> +<span class="sourceLineNo">354</span> }<a name="line.354"></a> +<span class="sourceLineNo">355</span> }<a name="line.355"></a> +<span class="sourceLineNo">356</span><a name="line.356"></a> +<span class="sourceLineNo">357</span> private List<String> getWALsInQueue0(ServerName serverName, String queueId)<a name="line.357"></a> +<span class="sourceLineNo">358</span> throws KeeperException {<a name="line.358"></a> +<span class="sourceLineNo">359</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, getQueueNode(serverName, queueId)));<a name="line.359"></a> +<span class="sourceLineNo">360</span> }<a name="line.360"></a> +<span class="sourceLineNo">361</span><a name="line.361"></a> +<span class="sourceLineNo">362</span> @Override<a name="line.362"></a> +<span class="sourceLineNo">363</span> public List<String> getWALsInQueue(ServerName serverName, String queueId)<a name="line.363"></a> +<span class="sourceLineNo">364</span> throws ReplicationException {<a name="line.364"></a> +<span class="sourceLineNo">365</span> try {<a name="line.365"></a> +<span class="sourceLineNo">366</span> return getWALsInQueue0(serverName, queueId);<a name="line.366"></a> +<span class="sourceLineNo">367</span> } catch (KeeperException e) {<a name="line.367"></a> +<span class="sourceLineNo">368</span> throw new ReplicationException(<a name="line.368"></a> +<span class="sourceLineNo">369</span> "Failed to get wals in queue (serverName=" + serverName + ", queueId=" + queueId + ")",<a name="line.369"></a> +<span class="sourceLineNo">370</span> e);<a name="line.370"></a> +<span class="sourceLineNo">371</span> }<a name="line.371"></a> +<span class="sourceLineNo">372</span> }<a name="line.372"></a> +<span class="sourceLineNo">373</span><a name="line.373"></a> +<span class="sourceLineNo">374</span> private List<String> getAllQueues0(ServerName serverName) throws KeeperException {<a name="line.374"></a> +<span class="sourceLineNo">375</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, getRsNode(serverName)));<a name="line.375"></a> +<span class="sourceLineNo">376</span> }<a name="line.376"></a> +<span class="sourceLineNo">377</span><a name="line.377"></a> +<span class="sourceLineNo">378</span> @Override<a name="line.378"></a> +<span class="sourceLineNo">379</span> public List<String> getAllQueues(ServerName serverName) throws ReplicationException {<a name="line.379"></a> +<span class="sourceLineNo">380</span> try {<a name="line.380"></a> +<span class="sourceLineNo">381</span> return getAllQueues0(serverName);<a name="line.381"></a> +<span class="sourceLineNo">382</span> } catch (KeeperException e) {<a name="line.382"></a> +<span class="sourceLineNo">383</span> throw new ReplicationException("Failed to get all queues (serverName=" + serverName + ")", e);<a name="line.383"></a> +<span class="sourceLineNo">384</span> }<a name="line.384"></a> +<span class="sourceLineNo">385</span> }<a name="line.385"></a> +<span class="sourceLineNo">386</span><a name="line.386"></a> +<span class="sourceLineNo">387</span> // will be overridden in UTs<a name="line.387"></a> +<span class="sourceLineNo">388</span> @VisibleForTesting<a name="line.388"></a> +<span class="sourceLineNo">389</span> protected int getQueuesZNodeCversion() throws KeeperException {<a name="line.389"></a> +<span class="sourceLineNo">390</span> Stat stat = new Stat();<a name="line.390"></a> +<span class="sourceLineNo">391</span> ZKUtil.getDataNoWatch(this.zookeeper, this.queuesZNode, stat);<a name="line.391"></a> +<span class="sourceLineNo">392</span> return stat.getCversion();<a name="line.392"></a> +<span class="sourceLineNo">393</span> }<a name="line.393"></a> +<span class="sourceLineNo">394</span><a name="line.394"></a> +<span class="sourceLineNo">395</span> @Override<a name="line.395"></a> +<span class="sourceLineNo">396</span> public Set<String> getAllWALs() throws ReplicationException {<a name="line.396"></a> +<span class="sourceLineNo">397</span> try {<a name="line.397"></a> +<span class="sourceLineNo">398</span> for (int retry = 0;; retry++) {<a name="line.398"></a> +<span class="sourceLineNo">399</span> int v0 = getQueuesZNodeCversion();<a name="line.399"></a> +<span class="sourceLineNo">400</span> List<ServerName> rss = getListOfReplicators0();<a name="line.400"></a> +<span class="sourceLineNo">401</span> if (rss.isEmpty()) {<a name="line.401"></a> +<span class="sourceLineNo">402</span> LOG.debug("Didn't find a RegionServer that replicates, won't prevent deletions.");<a name="line.402"></a> +<span class="sourceLineNo">403</span> return Collections.emptySet();<a name="line.403"></a> <span class="sourceLineNo">404</span> }<a name="line.404"></a> -<span class="sourceLineNo">405</span> LOG.info("Replication queue node cversion changed from %d to %d, retry = %d",<a name="line.405"></a> -<span class="sourceLineNo">406</span> v0, v1, retry);<a name="line.406"></a> -<span class="sourceLineNo">407</span> }<a name="line.407"></a> -<span class="sourceLineNo">408</span> } catch (KeeperException e) {<a name="line.408"></a> -<span class="sourceLineNo">409</span> throw new ReplicationException("Failed to get all wals", e);<a name="line.409"></a> -<span class="sourceLineNo">410</span> }<a name="line.410"></a> -<span class="sourceLineNo">411</span> }<a name="line.411"></a> -<span class="sourceLineNo">412</span><a name="line.412"></a> -<span class="sourceLineNo">413</span> private String getHFileRefsPeerNode(String peerId) {<a name="line.413"></a> -<span class="sourceLineNo">414</span> return ZNodePaths.joinZNode(hfileRefsZNode, peerId);<a name="line.414"></a> -<span class="sourceLineNo">415</span> }<a name="line.415"></a> -<span class="sourceLineNo">416</span><a name="line.416"></a> -<span class="sourceLineNo">417</span> private String getHFileNode(String peerNode, String fileName) {<a name="line.417"></a> -<span class="sourceLineNo">418</span> return ZNodePaths.joinZNode(peerNode, fileName);<a name="line.418"></a> -<span class="sourceLineNo">419</span> }<a name="line.419"></a> -<span class="sourceLineNo">420</span><a name="line.420"></a> -<span class="sourceLineNo">421</span> @Override<a name="line.421"></a> -<span class="sourceLineNo">422</span> public void addPeerToHFileRefs(String peerId) throws ReplicationException {<a name="line.422"></a> -<span class="sourceLineNo">423</span> String peerNode = getHFileRefsPeerNode(peerId);<a name="line.423"></a> -<span class="sourceLineNo">424</span> try {<a name="line.424"></a> -<span class="sourceLineNo">425</span> if (ZKUtil.checkExists(zookeeper, peerNode) == -1) {<a name="line.425"></a> -<span class="sourceLineNo">426</span> LOG.info("Adding peer {} to hfile reference queue.", peerId);<a name="line.426"></a> -<span class="sourceLineNo">427</span> ZKUtil.createWithParents(zookeeper, peerNode);<a name="line.427"></a> -<span class="sourceLineNo">428</span> }<a name="line.428"></a> -<span class="sourceLineNo">429</span> } catch (KeeperException e) {<a name="line.429"></a> -<span class="sourceLineNo">430</span> throw new ReplicationException("Failed to add peer " + peerId + " to hfile reference queue.",<a name="line.430"></a> -<span class="sourceLineNo">431</span> e);<a name="line.431"></a> -<span class="sourceLineNo">432</span> }<a name="line.432"></a> -<span class="sourceLineNo">433</span> }<a name="line.433"></a> -<span class="sourceLineNo">434</span><a name="line.434"></a> -<span class="sourceLineNo">435</span> @Override<a name="line.435"></a> -<span class="sourceLineNo">436</span> public void removePeerFromHFileRefs(String peerId) throws ReplicationException {<a name="line.436"></a> -<span class="sourceLineNo">437</span> String peerNode = getHFileRefsPeerNode(peerId);<a name="line.437"></a> -<span class="sourceLineNo">438</span> try {<a name="line.438"></a> -<span class="sourceLineNo">439</span> if (ZKUtil.checkExists(zookeeper, peerNode) == -1) {<a name="line.439"></a> -<span class="sourceLineNo">440</span> LOG.debug("Peer {} not found in hfile reference queue.", peerNode);<a name="line.440"></a> -<span class="sourceLineNo">441</span> } else {<a name="line.441"></a> -<span class="sourceLineNo">442</span> LOG.info("Removing peer {} from hfile reference queue.", peerNode);<a name="line.442"></a> -<span class="sourceLineNo">443</span> ZKUtil.deleteNodeRecursively(zookeeper, peerNode);<a name="line.443"></a> -<span class="sourceLineNo">444</span> }<a name="line.444"></a> -<span class="sourceLineNo">445</span> } catch (KeeperException e) {<a name="line.445"></a> -<span class="sourceLineNo">446</span> throw new ReplicationException(<a name="line.446"></a> -<span class="sourceLineNo">447</span> "Failed to remove peer " + peerId + " from hfile reference queue.", e);<a name="line.447"></a> -<span class="sourceLineNo">448</span> }<a name="line.448"></a> -<span class="sourceLineNo">449</span> }<a name="line.449"></a> -<span class="sourceLineNo">450</span><a name="line.450"></a> -<span class="sourceLineNo">451</span> @Override<a name="line.451"></a> -<span class="sourceLineNo">452</span> public void addHFileRefs(String peerId, List<Pair<Path, Path>> pairs)<a name="line.452"></a> -<span class="sourceLineNo">453</span> throws ReplicationException {<a name="line.453"></a> -<span class="sourceLineNo">454</span> String peerNode = getHFileRefsPeerNode(peerId);<a name="line.454"></a> -<span class="sourceLineNo">455</span> LOG.debug("Adding hfile references {} in queue {}", pairs, peerNode);<a name="line.455"></a> -<span class="sourceLineNo">456</span> List<ZKUtilOp> listOfOps = pairs.stream().map(p -> p.getSecond().getName())<a name="line.456"></a> -<span class="sourceLineNo">457</span> .map(n -> getHFileNode(peerNode, n))<a name="line.457"></a> -<span class="sourceLineNo">458</span> .map(f -> ZKUtilOp.createAndFailSilent(f, HConstants.EMPTY_BYTE_ARRAY)).collect(toList());<a name="line.458"></a> -<span class="sourceLineNo">459</span> LOG.debug("The multi list size for adding hfile references in zk for node {} is {}",<a name="line.459"></a> -<span class="sourceLineNo">460</span> peerNode, listOfOps.size());<a name="line.460"></a> -<span class="sourceLineNo">461</span> try {<a name="line.461"></a> -<span class="sourceLineNo">462</span> ZKUtil.multiOrSequential(this.zookeeper, listOfOps, true);<a name="line.462"></a> -<span class="sourceLineNo">463</span> } catch (KeeperException e) {<a name="line.463"></a> -<span class="sourceLineNo">464</span> throw new ReplicationException("Failed to add hfile reference to peer " + peerId, e);<a name="line.464"></a> -<span class="sourceLineNo">465</span> }<a name="line.465"></a> -<span class="sourceLineNo">466</span> }<a name="line.466"></a> -<span class="sourceLineNo">467</span><a name="line.467"></a> -<span class="sourceLineNo">468</span> @Override<a name="line.468"></a> -<span class="sourceLineNo">469</span> public void removeHFileRefs(String peerId, List<String> files) throws ReplicationException {<a name="line.469"></a> -<span class="sourceLineNo">470</span> String peerNode = getHFileRefsPeerNode(peerId);<a name="line.470"></a> -<span class="sourceLineNo">471</span> LOG.debug("Removing hfile references {} from queue {}", files, peerNode);<a name="line.471"></a> -<span class="sourceLineNo">472</span><a name="line.472"></a> -<span class="sourceLineNo">473</span> List<ZKUtilOp> listOfOps = files.stream().map(n -> getHFileNode(peerNode, n))<a name="line.473"></a> -<span class="sourceLineNo">474</span> .map(ZKUtilOp::deleteNodeFailSilent).collect(toList());<a name="line.474"></a> -<span class="sourceLineNo">475</span> LOG.debug("The multi list size for removing hfile references in zk for node {} is {}",<a name="line.475"></a> -<span class="sourceLineNo">476</span> peerNode, listOfOps.size());<a name="line.476"></a> -<span class="sourceLineNo">477</span> try {<a name="line.477"></a> -<span class="sourceLineNo">478</span> ZKUtil.multiOrSequential(this.zookeeper, listOfOps, true);<a name="line.478"></a> -<span class="sourceLineNo">479</span> } catch (KeeperException e) {<a name="line.479"></a> -<span class="sourceLineNo">480</span> throw new ReplicationException("Failed to remove hfile reference from peer " + peerId, e);<a name="line.480"></a> -<span class="sourceLineNo">481</span> }<a name="line.481"></a> -<span class="sourceLineNo">482</span> }<a name="line.482"></a> -<span class="sourceLineNo">483</span><a name="line.483"></a> -<span class="sourceLineNo">484</span> private List<String> getAllPeersFromHFileRefsQueue0() throws KeeperException {<a name="line.484"></a> -<span class="sourceLineNo">485</span> return nullToEmpty(ZKUtil.listChildrenNoWatch(zookeeper, hfileRefsZNode));<a name="line.485"></a> -<span class="sourceLineNo">486</span> }<a name="line.486"></a> -<span class="sourceLineNo">487</span><a name="line.487"></a> -<span class="sourceLineNo">488</span> @Override<a name="line.488"></a> -<span class="sourceLineNo">489</span> public List<String> getAllPeersFromHFileRefsQueue() throws ReplicationException {<a name="line.489"></a> -<span class="sourceLineNo">490</span> try {<a name="line.490"></a> -<span class="sourceLineNo">491</span> return getAllPeersFromHFileRefsQueue0();<a name="line.491"></a> -<span class="sourceLineNo">492</span> } catch (KeeperException e) {<a name="line.492"></a> -<span class="sourceLineNo">493</span> throw new ReplicationException("Failed to get list of all peers in hfile references node.",<a name="line.493"></a> -<span class="sourceLineNo">494</span> e);<a name="line.494"></a> -<span class="sourceLineNo">495</span> }<a name="line.495"></a> +<span class="sourceLineNo">405</span> Set<String> wals = new HashSet<>();<a name="line.405"></a> +<span class="sourceLineNo">406</span> for (ServerName rs : rss) {<a name="line.406"></a> +<span class="sourceLineNo">407</span> for (String queueId : getAllQueues0(rs)) {<a name="line.407"></a> +<span class="sourceLineNo">408</span> wals.addAll(getWALsInQueue0(rs, queueId));<a name="line.408"></a> +<span class="sourceLineNo">409</span> }<a name="line.409"></a> +<span class="sourceLineNo">410</span> }<a name="line.410"></a> +<span class="sourceLineNo">411</span> int v1 = getQueuesZNodeCversion();<a name="line.411"></a> +<span class="sourceLineNo">412</span> if (v0 == v1) {<a name="line.412"></a> +<span class="sourceLineNo">413</span> return wals;<a name="line.413"></a> +<span class="sourceLineNo">414</span> }<a name="line.414"></a> +<span class="sourceLineNo">415</span> LOG.info("Replication queue node cversion changed from %d to %d, retry = %d",<a name="line.415"></a> +<span class="sourceLineNo">416</span> v0, v1, retry);<a name="line.416"></a> +<span class="sourceLineNo">417</span> }<a name="line.417"></a> +<span class="sourceLineNo">418</span> } catch (KeeperException e) {<a name="line.418"></a> +<span class="sourceLineNo">419</span> throw new ReplicationException("Failed to get all wals", e);<a name="line.419"></a> +<span class="sourceLineNo">420</span> }<a name="line.420"></a> +<span class="sourceLineNo">421</span> }<a name="line.421"></a> +<span class="sourceLineNo">422</span><a name="line.422"></a> +<span class="sourceLineNo">423</span> private String getHFileRefsPeerNode(String peerId) {<a name="line.423"></a> +<span class="sourceLineNo">424</span> return ZNodePaths.joinZNode(hfileRefsZNode, peerId);<a name="line.424"></a> +<span class="sourceLineNo">425</span> }<a name="line.425"></a> +<span class="sourceLineNo">426</span><a name="line.426"></a> +<span class="sourceLineNo">427</span> private String getHFileNode(String peerNode, String fileName) {<a name="line.427"></a> +<span class="sourceLineNo">428</span> return ZNodePaths.joinZNode(peerNode, fileName);<a name="line.428"></a> +<span class="sourceLineNo">429</span> }<a name="line.429"></a> +<span class="sourceLineNo">430</s
<TRUNCATED>