hbase-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From git-site-r...@apache.org
Subject [09/51] [partial] hbase-site git commit: Published site at 82d554e3783372cc6b05489452c815b57c06f6cd.
Date Mon, 15 May 2017 14:59:00 GMT
http://git-wip-us.apache.org/repos/asf/hbase-site/blob/f55ebeaa/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleRpcServerResponder.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleRpcServerResponder.html b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleRpcServerResponder.html
new file mode 100644
index 0000000..e79bf23
--- /dev/null
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleRpcServerResponder.html
@@ -0,0 +1,388 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html lang="en">
+<head>
+<title>Source code</title>
+<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
+</head>
+<body>
+<div class="sourceContainer">
+<pre><span class="sourceLineNo">001</span>/**<a name="line.1"></a>
+<span class="sourceLineNo">002</span> * Licensed to the Apache Software Foundation (ASF) under one<a name="line.2"></a>
+<span class="sourceLineNo">003</span> * or more contributor license agreements.  See the NOTICE file<a name="line.3"></a>
+<span class="sourceLineNo">004</span> * distributed with this work for additional information<a name="line.4"></a>
+<span class="sourceLineNo">005</span> * regarding copyright ownership.  The ASF licenses this file<a name="line.5"></a>
+<span class="sourceLineNo">006</span> * to you under the Apache License, Version 2.0 (the<a name="line.6"></a>
+<span class="sourceLineNo">007</span> * "License"); you may not use this file except in compliance<a name="line.7"></a>
+<span class="sourceLineNo">008</span> * with the License.  You may obtain a copy of the License at<a name="line.8"></a>
+<span class="sourceLineNo">009</span> *<a name="line.9"></a>
+<span class="sourceLineNo">010</span> *     http://www.apache.org/licenses/LICENSE-2.0<a name="line.10"></a>
+<span class="sourceLineNo">011</span> *<a name="line.11"></a>
+<span class="sourceLineNo">012</span> * Unless required by applicable law or agreed to in writing, software<a name="line.12"></a>
+<span class="sourceLineNo">013</span> * distributed under the License is distributed on an "AS IS" BASIS,<a name="line.13"></a>
+<span class="sourceLineNo">014</span> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<a name="line.14"></a>
+<span class="sourceLineNo">015</span> * See the License for the specific language governing permissions and<a name="line.15"></a>
+<span class="sourceLineNo">016</span> * limitations under the License.<a name="line.16"></a>
+<span class="sourceLineNo">017</span> */<a name="line.17"></a>
+<span class="sourceLineNo">018</span>package org.apache.hadoop.hbase.ipc;<a name="line.18"></a>
+<span class="sourceLineNo">019</span><a name="line.19"></a>
+<span class="sourceLineNo">020</span>import java.io.IOException;<a name="line.20"></a>
+<span class="sourceLineNo">021</span>import java.nio.channels.CancelledKeyException;<a name="line.21"></a>
+<span class="sourceLineNo">022</span>import java.nio.channels.ClosedChannelException;<a name="line.22"></a>
+<span class="sourceLineNo">023</span>import java.nio.channels.SelectionKey;<a name="line.23"></a>
+<span class="sourceLineNo">024</span>import java.nio.channels.Selector;<a name="line.24"></a>
+<span class="sourceLineNo">025</span>import java.util.ArrayList;<a name="line.25"></a>
+<span class="sourceLineNo">026</span>import java.util.Collections;<a name="line.26"></a>
+<span class="sourceLineNo">027</span>import java.util.Iterator;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import java.util.Set;<a name="line.28"></a>
+<span class="sourceLineNo">029</span>import java.util.concurrent.ConcurrentHashMap;<a name="line.29"></a>
+<span class="sourceLineNo">030</span><a name="line.30"></a>
+<span class="sourceLineNo">031</span>import org.apache.hadoop.hbase.HBaseIOException;<a name="line.31"></a>
+<span class="sourceLineNo">032</span>import org.apache.hadoop.hbase.classification.InterfaceAudience;<a name="line.32"></a>
+<span class="sourceLineNo">033</span>import org.apache.hadoop.hbase.util.Threads;<a name="line.33"></a>
+<span class="sourceLineNo">034</span>import org.apache.hadoop.util.StringUtils;<a name="line.34"></a>
+<span class="sourceLineNo">035</span><a name="line.35"></a>
+<span class="sourceLineNo">036</span>/**<a name="line.36"></a>
+<span class="sourceLineNo">037</span> * Sends responses of RPC back to clients.<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>class SimpleRpcServerResponder extends Thread {<a name="line.40"></a>
+<span class="sourceLineNo">041</span>  /**  */<a name="line.41"></a>
+<span class="sourceLineNo">042</span>  private final SimpleRpcServer simpleRpcServer;<a name="line.42"></a>
+<span class="sourceLineNo">043</span>  private final Selector writeSelector;<a name="line.43"></a>
+<span class="sourceLineNo">044</span>  private final Set&lt;SimpleServerRpcConnection&gt; writingCons =<a name="line.44"></a>
+<span class="sourceLineNo">045</span>      Collections.newSetFromMap(new ConcurrentHashMap&lt;SimpleServerRpcConnection, Boolean&gt;());<a name="line.45"></a>
+<span class="sourceLineNo">046</span><a name="line.46"></a>
+<span class="sourceLineNo">047</span>  SimpleRpcServerResponder(SimpleRpcServer simpleRpcServer) throws IOException {<a name="line.47"></a>
+<span class="sourceLineNo">048</span>    this.simpleRpcServer = simpleRpcServer;<a name="line.48"></a>
+<span class="sourceLineNo">049</span>    this.setName("RpcServer.responder");<a name="line.49"></a>
+<span class="sourceLineNo">050</span>    this.setDaemon(true);<a name="line.50"></a>
+<span class="sourceLineNo">051</span>    this.setUncaughtExceptionHandler(Threads.LOGGING_EXCEPTION_HANDLER);<a name="line.51"></a>
+<span class="sourceLineNo">052</span>    writeSelector = Selector.open(); // create a selector<a name="line.52"></a>
+<span class="sourceLineNo">053</span>  }<a name="line.53"></a>
+<span class="sourceLineNo">054</span><a name="line.54"></a>
+<span class="sourceLineNo">055</span>  @Override<a name="line.55"></a>
+<span class="sourceLineNo">056</span>  public void run() {<a name="line.56"></a>
+<span class="sourceLineNo">057</span>    SimpleRpcServer.LOG.debug(getName() + ": starting");<a name="line.57"></a>
+<span class="sourceLineNo">058</span>    try {<a name="line.58"></a>
+<span class="sourceLineNo">059</span>      doRunLoop();<a name="line.59"></a>
+<span class="sourceLineNo">060</span>    } finally {<a name="line.60"></a>
+<span class="sourceLineNo">061</span>      SimpleRpcServer.LOG.info(getName() + ": stopping");<a name="line.61"></a>
+<span class="sourceLineNo">062</span>      try {<a name="line.62"></a>
+<span class="sourceLineNo">063</span>        writeSelector.close();<a name="line.63"></a>
+<span class="sourceLineNo">064</span>      } catch (IOException ioe) {<a name="line.64"></a>
+<span class="sourceLineNo">065</span>        SimpleRpcServer.LOG.error(getName() + ": couldn't close write selector", ioe);<a name="line.65"></a>
+<span class="sourceLineNo">066</span>      }<a name="line.66"></a>
+<span class="sourceLineNo">067</span>    }<a name="line.67"></a>
+<span class="sourceLineNo">068</span>  }<a name="line.68"></a>
+<span class="sourceLineNo">069</span><a name="line.69"></a>
+<span class="sourceLineNo">070</span>  /**<a name="line.70"></a>
+<span class="sourceLineNo">071</span>   * Take the list of the connections that want to write, and register them in the selector.<a name="line.71"></a>
+<span class="sourceLineNo">072</span>   */<a name="line.72"></a>
+<span class="sourceLineNo">073</span>  private void registerWrites() {<a name="line.73"></a>
+<span class="sourceLineNo">074</span>    Iterator&lt;SimpleServerRpcConnection&gt; it = writingCons.iterator();<a name="line.74"></a>
+<span class="sourceLineNo">075</span>    while (it.hasNext()) {<a name="line.75"></a>
+<span class="sourceLineNo">076</span>      SimpleServerRpcConnection c = it.next();<a name="line.76"></a>
+<span class="sourceLineNo">077</span>      it.remove();<a name="line.77"></a>
+<span class="sourceLineNo">078</span>      SelectionKey sk = c.channel.keyFor(writeSelector);<a name="line.78"></a>
+<span class="sourceLineNo">079</span>      try {<a name="line.79"></a>
+<span class="sourceLineNo">080</span>        if (sk == null) {<a name="line.80"></a>
+<span class="sourceLineNo">081</span>          try {<a name="line.81"></a>
+<span class="sourceLineNo">082</span>            c.channel.register(writeSelector, SelectionKey.OP_WRITE, c);<a name="line.82"></a>
+<span class="sourceLineNo">083</span>          } catch (ClosedChannelException e) {<a name="line.83"></a>
+<span class="sourceLineNo">084</span>            // ignore: the client went away.<a name="line.84"></a>
+<span class="sourceLineNo">085</span>            if (SimpleRpcServer.LOG.isTraceEnabled()) SimpleRpcServer.LOG.trace("ignored", e);<a name="line.85"></a>
+<span class="sourceLineNo">086</span>          }<a name="line.86"></a>
+<span class="sourceLineNo">087</span>        } else {<a name="line.87"></a>
+<span class="sourceLineNo">088</span>          sk.interestOps(SelectionKey.OP_WRITE);<a name="line.88"></a>
+<span class="sourceLineNo">089</span>        }<a name="line.89"></a>
+<span class="sourceLineNo">090</span>      } catch (CancelledKeyException e) {<a name="line.90"></a>
+<span class="sourceLineNo">091</span>        // ignore: the client went away.<a name="line.91"></a>
+<span class="sourceLineNo">092</span>        if (SimpleRpcServer.LOG.isTraceEnabled()) SimpleRpcServer.LOG.trace("ignored", e);<a name="line.92"></a>
+<span class="sourceLineNo">093</span>      }<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><a name="line.96"></a>
+<span class="sourceLineNo">097</span>  /**<a name="line.97"></a>
+<span class="sourceLineNo">098</span>   * Add a connection to the list that want to write,<a name="line.98"></a>
+<span class="sourceLineNo">099</span>   */<a name="line.99"></a>
+<span class="sourceLineNo">100</span>  public void registerForWrite(SimpleServerRpcConnection c) {<a name="line.100"></a>
+<span class="sourceLineNo">101</span>    if (writingCons.add(c)) {<a name="line.101"></a>
+<span class="sourceLineNo">102</span>      writeSelector.wakeup();<a name="line.102"></a>
+<span class="sourceLineNo">103</span>    }<a name="line.103"></a>
+<span class="sourceLineNo">104</span>  }<a name="line.104"></a>
+<span class="sourceLineNo">105</span><a name="line.105"></a>
+<span class="sourceLineNo">106</span>  private void doRunLoop() {<a name="line.106"></a>
+<span class="sourceLineNo">107</span>    long lastPurgeTime = 0; // last check for old calls.<a name="line.107"></a>
+<span class="sourceLineNo">108</span>    while (this.simpleRpcServer.running) {<a name="line.108"></a>
+<span class="sourceLineNo">109</span>      try {<a name="line.109"></a>
+<span class="sourceLineNo">110</span>        registerWrites();<a name="line.110"></a>
+<span class="sourceLineNo">111</span>        int keyCt = writeSelector.select(this.simpleRpcServer.purgeTimeout);<a name="line.111"></a>
+<span class="sourceLineNo">112</span>        if (keyCt == 0) {<a name="line.112"></a>
+<span class="sourceLineNo">113</span>          continue;<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>        Set&lt;SelectionKey&gt; keys = writeSelector.selectedKeys();<a name="line.116"></a>
+<span class="sourceLineNo">117</span>        Iterator&lt;SelectionKey&gt; iter = keys.iterator();<a name="line.117"></a>
+<span class="sourceLineNo">118</span>        while (iter.hasNext()) {<a name="line.118"></a>
+<span class="sourceLineNo">119</span>          SelectionKey key = iter.next();<a name="line.119"></a>
+<span class="sourceLineNo">120</span>          iter.remove();<a name="line.120"></a>
+<span class="sourceLineNo">121</span>          try {<a name="line.121"></a>
+<span class="sourceLineNo">122</span>            if (key.isValid() &amp;&amp; key.isWritable()) {<a name="line.122"></a>
+<span class="sourceLineNo">123</span>              doAsyncWrite(key);<a name="line.123"></a>
+<span class="sourceLineNo">124</span>            }<a name="line.124"></a>
+<span class="sourceLineNo">125</span>          } catch (IOException e) {<a name="line.125"></a>
+<span class="sourceLineNo">126</span>            SimpleRpcServer.LOG.debug(getName() + ": asyncWrite", e);<a name="line.126"></a>
+<span class="sourceLineNo">127</span>          }<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>        lastPurgeTime = purge(lastPurgeTime);<a name="line.130"></a>
+<span class="sourceLineNo">131</span><a name="line.131"></a>
+<span class="sourceLineNo">132</span>      } catch (OutOfMemoryError e) {<a name="line.132"></a>
+<span class="sourceLineNo">133</span>        if (this.simpleRpcServer.errorHandler != null) {<a name="line.133"></a>
+<span class="sourceLineNo">134</span>          if (this.simpleRpcServer.errorHandler.checkOOME(e)) {<a name="line.134"></a>
+<span class="sourceLineNo">135</span>            SimpleRpcServer.LOG.info(getName() + ": exiting on OutOfMemoryError");<a name="line.135"></a>
+<span class="sourceLineNo">136</span>            return;<a name="line.136"></a>
+<span class="sourceLineNo">137</span>          }<a name="line.137"></a>
+<span class="sourceLineNo">138</span>        } else {<a name="line.138"></a>
+<span class="sourceLineNo">139</span>          //<a name="line.139"></a>
+<span class="sourceLineNo">140</span>          // we can run out of memory if we have too many threads<a name="line.140"></a>
+<span class="sourceLineNo">141</span>          // log the event and sleep for a minute and give<a name="line.141"></a>
+<span class="sourceLineNo">142</span>          // some thread(s) a chance to finish<a name="line.142"></a>
+<span class="sourceLineNo">143</span>          //<a name="line.143"></a>
+<span class="sourceLineNo">144</span>          SimpleRpcServer.LOG.warn(getName() + ": OutOfMemoryError in server select", e);<a name="line.144"></a>
+<span class="sourceLineNo">145</span>          try {<a name="line.145"></a>
+<span class="sourceLineNo">146</span>            Thread.sleep(60000);<a name="line.146"></a>
+<span class="sourceLineNo">147</span>          } catch (InterruptedException ex) {<a name="line.147"></a>
+<span class="sourceLineNo">148</span>            SimpleRpcServer.LOG.debug("Interrupted while sleeping");<a name="line.148"></a>
+<span class="sourceLineNo">149</span>            return;<a name="line.149"></a>
+<span class="sourceLineNo">150</span>          }<a name="line.150"></a>
+<span class="sourceLineNo">151</span>        }<a name="line.151"></a>
+<span class="sourceLineNo">152</span>      } catch (Exception e) {<a name="line.152"></a>
+<span class="sourceLineNo">153</span>        SimpleRpcServer.LOG<a name="line.153"></a>
+<span class="sourceLineNo">154</span>            .warn(getName() + ": exception in Responder " + StringUtils.stringifyException(e), e);<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>    SimpleRpcServer.LOG.info(getName() + ": stopped");<a name="line.157"></a>
+<span class="sourceLineNo">158</span>  }<a name="line.158"></a>
+<span class="sourceLineNo">159</span><a name="line.159"></a>
+<span class="sourceLineNo">160</span>  /**<a name="line.160"></a>
+<span class="sourceLineNo">161</span>   * If there were some calls that have not been sent out for a long time, we close the connection.<a name="line.161"></a>
+<span class="sourceLineNo">162</span>   * @return the time of the purge.<a name="line.162"></a>
+<span class="sourceLineNo">163</span>   */<a name="line.163"></a>
+<span class="sourceLineNo">164</span>  private long purge(long lastPurgeTime) {<a name="line.164"></a>
+<span class="sourceLineNo">165</span>    long now = System.currentTimeMillis();<a name="line.165"></a>
+<span class="sourceLineNo">166</span>    if (now &lt; lastPurgeTime + this.simpleRpcServer.purgeTimeout) {<a name="line.166"></a>
+<span class="sourceLineNo">167</span>      return lastPurgeTime;<a name="line.167"></a>
+<span class="sourceLineNo">168</span>    }<a name="line.168"></a>
+<span class="sourceLineNo">169</span><a name="line.169"></a>
+<span class="sourceLineNo">170</span>    ArrayList&lt;SimpleServerRpcConnection&gt; conWithOldCalls = new ArrayList&lt;&gt;();<a name="line.170"></a>
+<span class="sourceLineNo">171</span>    // get the list of channels from list of keys.<a name="line.171"></a>
+<span class="sourceLineNo">172</span>    synchronized (writeSelector.keys()) {<a name="line.172"></a>
+<span class="sourceLineNo">173</span>      for (SelectionKey key : writeSelector.keys()) {<a name="line.173"></a>
+<span class="sourceLineNo">174</span>        SimpleServerRpcConnection connection = (SimpleServerRpcConnection) key.attachment();<a name="line.174"></a>
+<span class="sourceLineNo">175</span>        if (connection == null) {<a name="line.175"></a>
+<span class="sourceLineNo">176</span>          throw new IllegalStateException("Coding error: SelectionKey key without attachment.");<a name="line.176"></a>
+<span class="sourceLineNo">177</span>        }<a name="line.177"></a>
+<span class="sourceLineNo">178</span>        SimpleServerCall call = connection.responseQueue.peekFirst();<a name="line.178"></a>
+<span class="sourceLineNo">179</span>        if (call != null &amp;&amp; now &gt; call.lastSentTime + this.simpleRpcServer.purgeTimeout) {<a name="line.179"></a>
+<span class="sourceLineNo">180</span>          conWithOldCalls.add(call.getConnection());<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><a name="line.184"></a>
+<span class="sourceLineNo">185</span>    // Seems safer to close the connection outside of the synchronized loop...<a name="line.185"></a>
+<span class="sourceLineNo">186</span>    for (SimpleServerRpcConnection connection : conWithOldCalls) {<a name="line.186"></a>
+<span class="sourceLineNo">187</span>      this.simpleRpcServer.closeConnection(connection);<a name="line.187"></a>
+<span class="sourceLineNo">188</span>    }<a name="line.188"></a>
+<span class="sourceLineNo">189</span><a name="line.189"></a>
+<span class="sourceLineNo">190</span>    return now;<a name="line.190"></a>
+<span class="sourceLineNo">191</span>  }<a name="line.191"></a>
+<span class="sourceLineNo">192</span><a name="line.192"></a>
+<span class="sourceLineNo">193</span>  private void doAsyncWrite(SelectionKey key) throws IOException {<a name="line.193"></a>
+<span class="sourceLineNo">194</span>    SimpleServerRpcConnection connection = (SimpleServerRpcConnection) key.attachment();<a name="line.194"></a>
+<span class="sourceLineNo">195</span>    if (connection == null) {<a name="line.195"></a>
+<span class="sourceLineNo">196</span>      throw new IOException("doAsyncWrite: no connection");<a name="line.196"></a>
+<span class="sourceLineNo">197</span>    }<a name="line.197"></a>
+<span class="sourceLineNo">198</span>    if (key.channel() != connection.channel) {<a name="line.198"></a>
+<span class="sourceLineNo">199</span>      throw new IOException("doAsyncWrite: bad channel");<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 (processAllResponses(connection)) {<a name="line.202"></a>
+<span class="sourceLineNo">203</span>      try {<a name="line.203"></a>
+<span class="sourceLineNo">204</span>        // We wrote everything, so we don't need to be told when the socket is ready for<a name="line.204"></a>
+<span class="sourceLineNo">205</span>        // write anymore.<a name="line.205"></a>
+<span class="sourceLineNo">206</span>        key.interestOps(0);<a name="line.206"></a>
+<span class="sourceLineNo">207</span>      } catch (CancelledKeyException e) {<a name="line.207"></a>
+<span class="sourceLineNo">208</span>        /*<a name="line.208"></a>
+<span class="sourceLineNo">209</span>         * The Listener/reader might have closed the socket. We don't explicitly cancel the key, so<a name="line.209"></a>
+<span class="sourceLineNo">210</span>         * not sure if this will ever fire. This warning could be removed.<a name="line.210"></a>
+<span class="sourceLineNo">211</span>         */<a name="line.211"></a>
+<span class="sourceLineNo">212</span>        SimpleRpcServer.LOG.warn("Exception while changing ops : " + e);<a name="line.212"></a>
+<span class="sourceLineNo">213</span>      }<a name="line.213"></a>
+<span class="sourceLineNo">214</span>    }<a name="line.214"></a>
+<span class="sourceLineNo">215</span>  }<a name="line.215"></a>
+<span class="sourceLineNo">216</span><a name="line.216"></a>
+<span class="sourceLineNo">217</span>  /**<a name="line.217"></a>
+<span class="sourceLineNo">218</span>   * Process the response for this call. You need to have the lock on<a name="line.218"></a>
+<span class="sourceLineNo">219</span>   * {@link org.apache.hadoop.hbase.ipc.SimpleServerRpcConnection#responseWriteLock}<a name="line.219"></a>
+<span class="sourceLineNo">220</span>   * @param call the call<a name="line.220"></a>
+<span class="sourceLineNo">221</span>   * @return true if we proceed the call fully, false otherwise.<a name="line.221"></a>
+<span class="sourceLineNo">222</span>   * @throws IOException<a name="line.222"></a>
+<span class="sourceLineNo">223</span>   */<a name="line.223"></a>
+<span class="sourceLineNo">224</span>  boolean processResponse(final SimpleServerCall call) throws IOException {<a name="line.224"></a>
+<span class="sourceLineNo">225</span>    boolean error = true;<a name="line.225"></a>
+<span class="sourceLineNo">226</span>    try {<a name="line.226"></a>
+<span class="sourceLineNo">227</span>      // Send as much data as we can in the non-blocking fashion<a name="line.227"></a>
+<span class="sourceLineNo">228</span>      long numBytes =<a name="line.228"></a>
+<span class="sourceLineNo">229</span>          this.simpleRpcServer.channelWrite(call.getConnection().channel, call.response);<a name="line.229"></a>
+<span class="sourceLineNo">230</span>      if (numBytes &lt; 0) {<a name="line.230"></a>
+<span class="sourceLineNo">231</span>        throw new HBaseIOException(<a name="line.231"></a>
+<span class="sourceLineNo">232</span>            "Error writing on the socket " + "for the call:" + call.toShortString());<a name="line.232"></a>
+<span class="sourceLineNo">233</span>      }<a name="line.233"></a>
+<span class="sourceLineNo">234</span>      error = false;<a name="line.234"></a>
+<span class="sourceLineNo">235</span>    } finally {<a name="line.235"></a>
+<span class="sourceLineNo">236</span>      if (error) {<a name="line.236"></a>
+<span class="sourceLineNo">237</span>        SimpleRpcServer.LOG.debug(getName() + call.toShortString() + ": output error -- closing");<a name="line.237"></a>
+<span class="sourceLineNo">238</span>        // We will be closing this connection itself. Mark this call as done so that all the<a name="line.238"></a>
+<span class="sourceLineNo">239</span>        // buffer(s) it got from pool can get released<a name="line.239"></a>
+<span class="sourceLineNo">240</span>        call.done();<a name="line.240"></a>
+<span class="sourceLineNo">241</span>        this.simpleRpcServer.closeConnection(call.getConnection());<a name="line.241"></a>
+<span class="sourceLineNo">242</span>      }<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>    if (!call.response.hasRemaining()) {<a name="line.245"></a>
+<span class="sourceLineNo">246</span>      call.done();<a name="line.246"></a>
+<span class="sourceLineNo">247</span>      return true;<a name="line.247"></a>
+<span class="sourceLineNo">248</span>    } else {<a name="line.248"></a>
+<span class="sourceLineNo">249</span>      return false; // Socket can't take more, we will have to come back.<a name="line.249"></a>
+<span class="sourceLineNo">250</span>    }<a name="line.250"></a>
+<span class="sourceLineNo">251</span>  }<a name="line.251"></a>
+<span class="sourceLineNo">252</span><a name="line.252"></a>
+<span class="sourceLineNo">253</span>  /**<a name="line.253"></a>
+<span class="sourceLineNo">254</span>   * Process all the responses for this connection<a name="line.254"></a>
+<span class="sourceLineNo">255</span>   * @return true if all the calls were processed or that someone else is doing it. false if there *<a name="line.255"></a>
+<span class="sourceLineNo">256</span>   *         is still some work to do. In this case, we expect the caller to delay us.<a name="line.256"></a>
+<span class="sourceLineNo">257</span>   * @throws IOException<a name="line.257"></a>
+<span class="sourceLineNo">258</span>   */<a name="line.258"></a>
+<span class="sourceLineNo">259</span>  private boolean processAllResponses(final SimpleServerRpcConnection connection)<a name="line.259"></a>
+<span class="sourceLineNo">260</span>      throws IOException {<a name="line.260"></a>
+<span class="sourceLineNo">261</span>    // We want only one writer on the channel for a connection at a time.<a name="line.261"></a>
+<span class="sourceLineNo">262</span>    connection.responseWriteLock.lock();<a name="line.262"></a>
+<span class="sourceLineNo">263</span>    try {<a name="line.263"></a>
+<span class="sourceLineNo">264</span>      for (int i = 0; i &lt; 20; i++) {<a name="line.264"></a>
+<span class="sourceLineNo">265</span>        // protection if some handlers manage to need all the responder<a name="line.265"></a>
+<span class="sourceLineNo">266</span>        SimpleServerCall call = connection.responseQueue.pollFirst();<a name="line.266"></a>
+<span class="sourceLineNo">267</span>        if (call == null) {<a name="line.267"></a>
+<span class="sourceLineNo">268</span>          return true;<a name="line.268"></a>
+<span class="sourceLineNo">269</span>        }<a name="line.269"></a>
+<span class="sourceLineNo">270</span>        if (!processResponse(call)) {<a name="line.270"></a>
+<span class="sourceLineNo">271</span>          connection.responseQueue.addFirst(call);<a name="line.271"></a>
+<span class="sourceLineNo">272</span>          return false;<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>    } finally {<a name="line.275"></a>
+<span class="sourceLineNo">276</span>      connection.responseWriteLock.unlock();<a name="line.276"></a>
+<span class="sourceLineNo">277</span>    }<a name="line.277"></a>
+<span class="sourceLineNo">278</span><a name="line.278"></a>
+<span class="sourceLineNo">279</span>    return connection.responseQueue.isEmpty();<a name="line.279"></a>
+<span class="sourceLineNo">280</span>  }<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>  // Enqueue a response from the application.<a name="line.283"></a>
+<span class="sourceLineNo">284</span>  //<a name="line.284"></a>
+<span class="sourceLineNo">285</span>  void doRespond(SimpleServerCall call) throws IOException {<a name="line.285"></a>
+<span class="sourceLineNo">286</span>    boolean added = false;<a name="line.286"></a>
+<span class="sourceLineNo">287</span><a name="line.287"></a>
+<span class="sourceLineNo">288</span>    // If there is already a write in progress, we don't wait. This allows to free the handlers<a name="line.288"></a>
+<span class="sourceLineNo">289</span>    // immediately for other tasks.<a name="line.289"></a>
+<span class="sourceLineNo">290</span>    if (call.getConnection().responseQueue.isEmpty() &amp;&amp;<a name="line.290"></a>
+<span class="sourceLineNo">291</span>        call.getConnection().responseWriteLock.tryLock()) {<a name="line.291"></a>
+<span class="sourceLineNo">292</span>      try {<a name="line.292"></a>
+<span class="sourceLineNo">293</span>        if (call.getConnection().responseQueue.isEmpty()) {<a name="line.293"></a>
+<span class="sourceLineNo">294</span>          // If we're alone, we can try to do a direct call to the socket. It's<a name="line.294"></a>
+<span class="sourceLineNo">295</span>          // an optimisation to save on context switches and data transfer between cores..<a name="line.295"></a>
+<span class="sourceLineNo">296</span>          if (processResponse(call)) {<a name="line.296"></a>
+<span class="sourceLineNo">297</span>            return; // we're done.<a name="line.297"></a>
+<span class="sourceLineNo">298</span>          }<a name="line.298"></a>
+<span class="sourceLineNo">299</span>          // Too big to fit, putting ahead.<a name="line.299"></a>
+<span class="sourceLineNo">300</span>          call.getConnection().responseQueue.addFirst(call);<a name="line.300"></a>
+<span class="sourceLineNo">301</span>          added = true; // We will register to the selector later, outside of the lock.<a name="line.301"></a>
+<span class="sourceLineNo">302</span>        }<a name="line.302"></a>
+<span class="sourceLineNo">303</span>      } finally {<a name="line.303"></a>
+<span class="sourceLineNo">304</span>        call.getConnection().responseWriteLock.unlock();<a name="line.304"></a>
+<span class="sourceLineNo">305</span>      }<a name="line.305"></a>
+<span class="sourceLineNo">306</span>    }<a name="line.306"></a>
+<span class="sourceLineNo">307</span><a name="line.307"></a>
+<span class="sourceLineNo">308</span>    if (!added) {<a name="line.308"></a>
+<span class="sourceLineNo">309</span>      call.getConnection().responseQueue.addLast(call);<a name="line.309"></a>
+<span class="sourceLineNo">310</span>    }<a name="line.310"></a>
+<span class="sourceLineNo">311</span>    call.responder.registerForWrite(call.getConnection());<a name="line.311"></a>
+<span class="sourceLineNo">312</span><a name="line.312"></a>
+<span class="sourceLineNo">313</span>    // set the serve time when the response has to be sent later<a name="line.313"></a>
+<span class="sourceLineNo">314</span>    call.lastSentTime = System.currentTimeMillis();<a name="line.314"></a>
+<span class="sourceLineNo">315</span>  }<a name="line.315"></a>
+<span class="sourceLineNo">316</span>}<a name="line.316"></a>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+</pre>
+</div>
+</body>
+</html>

http://git-wip-us.apache.org/repos/asf/hbase-site/blob/f55ebeaa/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerCall.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerCall.html b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerCall.html
index 31df5ee..783d588 100644
--- a/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerCall.html
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerCall.html
@@ -32,59 +32,57 @@
 <span class="sourceLineNo">024</span>import org.apache.hadoop.hbase.classification.InterfaceAudience;<a name="line.24"></a>
 <span class="sourceLineNo">025</span>import org.apache.hadoop.hbase.io.ByteBufferPool;<a name="line.25"></a>
 <span class="sourceLineNo">026</span>import org.apache.hadoop.hbase.ipc.RpcServer.CallCleanup;<a name="line.26"></a>
-<span class="sourceLineNo">027</span>import org.apache.hadoop.hbase.ipc.SimpleRpcServer.Connection;<a name="line.27"></a>
-<span class="sourceLineNo">028</span>import org.apache.hadoop.hbase.ipc.SimpleRpcServer.Responder;<a name="line.28"></a>
-<span class="sourceLineNo">029</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.BlockingService;<a name="line.29"></a>
-<span class="sourceLineNo">030</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.MethodDescriptor;<a name="line.30"></a>
-<span class="sourceLineNo">031</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Message;<a name="line.31"></a>
-<span class="sourceLineNo">032</span>import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader;<a name="line.32"></a>
-<span class="sourceLineNo">033</span>import org.apache.htrace.TraceInfo;<a name="line.33"></a>
-<span class="sourceLineNo">034</span><a name="line.34"></a>
-<span class="sourceLineNo">035</span>/**<a name="line.35"></a>
-<span class="sourceLineNo">036</span> * Datastructure that holds all necessary to a method invocation and then afterward, carries the<a name="line.36"></a>
-<span class="sourceLineNo">037</span> * result.<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>class SimpleServerCall extends ServerCall {<a name="line.40"></a>
+<span class="sourceLineNo">027</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.BlockingService;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.MethodDescriptor;<a name="line.28"></a>
+<span class="sourceLineNo">029</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Message;<a name="line.29"></a>
+<span class="sourceLineNo">030</span>import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader;<a name="line.30"></a>
+<span class="sourceLineNo">031</span>import org.apache.htrace.TraceInfo;<a name="line.31"></a>
+<span class="sourceLineNo">032</span><a name="line.32"></a>
+<span class="sourceLineNo">033</span>/**<a name="line.33"></a>
+<span class="sourceLineNo">034</span> * Datastructure that holds all necessary to a method invocation and then afterward, carries the<a name="line.34"></a>
+<span class="sourceLineNo">035</span> * result.<a name="line.35"></a>
+<span class="sourceLineNo">036</span> */<a name="line.36"></a>
+<span class="sourceLineNo">037</span>@InterfaceAudience.Private<a name="line.37"></a>
+<span class="sourceLineNo">038</span>class SimpleServerCall extends ServerCall&lt;SimpleServerRpcConnection&gt; {<a name="line.38"></a>
+<span class="sourceLineNo">039</span><a name="line.39"></a>
+<span class="sourceLineNo">040</span>  long lastSentTime;<a name="line.40"></a>
 <span class="sourceLineNo">041</span><a name="line.41"></a>
-<span class="sourceLineNo">042</span>  long lastSentTime;<a name="line.42"></a>
+<span class="sourceLineNo">042</span>  final SimpleRpcServerResponder responder;<a name="line.42"></a>
 <span class="sourceLineNo">043</span><a name="line.43"></a>
-<span class="sourceLineNo">044</span>  final Responder responder;<a name="line.44"></a>
-<span class="sourceLineNo">045</span><a name="line.45"></a>
-<span class="sourceLineNo">046</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_ON_SOME_PATH",<a name="line.46"></a>
-<span class="sourceLineNo">047</span>      justification = "Can't figure why this complaint is happening... see below")<a name="line.47"></a>
-<span class="sourceLineNo">048</span>  SimpleServerCall(int id, final BlockingService service, final MethodDescriptor md,<a name="line.48"></a>
-<span class="sourceLineNo">049</span>      RequestHeader header, Message param, CellScanner cellScanner, RpcServer.Connection connection,<a name="line.49"></a>
-<span class="sourceLineNo">050</span>      long size, TraceInfo tinfo, final InetAddress remoteAddress, long receiveTime, int timeout,<a name="line.50"></a>
-<span class="sourceLineNo">051</span>      ByteBufferPool reservoir, CellBlockBuilder cellBlockBuilder, CallCleanup reqCleanup,<a name="line.51"></a>
-<span class="sourceLineNo">052</span>      Responder responder) {<a name="line.52"></a>
-<span class="sourceLineNo">053</span>    super(id, service, md, header, param, cellScanner, connection, size, tinfo, remoteAddress,<a name="line.53"></a>
-<span class="sourceLineNo">054</span>        receiveTime, timeout, reservoir, cellBlockBuilder, reqCleanup);<a name="line.54"></a>
-<span class="sourceLineNo">055</span>    this.responder = responder;<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>   * Call is done. Execution happened and we returned results to client. It is now safe to cleanup.<a name="line.59"></a>
-<span class="sourceLineNo">060</span>   */<a name="line.60"></a>
-<span class="sourceLineNo">061</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",<a name="line.61"></a>
-<span class="sourceLineNo">062</span>      justification = "Presume the lock on processing request held by caller is protection enough")<a name="line.62"></a>
-<span class="sourceLineNo">063</span>  @Override<a name="line.63"></a>
-<span class="sourceLineNo">064</span>  void done() {<a name="line.64"></a>
-<span class="sourceLineNo">065</span>    super.done();<a name="line.65"></a>
-<span class="sourceLineNo">066</span>    this.getConnection().decRpcCount(); // Say that we're done with this call.<a name="line.66"></a>
-<span class="sourceLineNo">067</span>  }<a name="line.67"></a>
-<span class="sourceLineNo">068</span><a name="line.68"></a>
-<span class="sourceLineNo">069</span>  @Override<a name="line.69"></a>
-<span class="sourceLineNo">070</span>  public synchronized void sendResponseIfReady() throws IOException {<a name="line.70"></a>
-<span class="sourceLineNo">071</span>    // set param null to reduce memory pressure<a name="line.71"></a>
-<span class="sourceLineNo">072</span>    this.param = null;<a name="line.72"></a>
-<span class="sourceLineNo">073</span>    this.responder.doRespond(this);<a name="line.73"></a>
-<span class="sourceLineNo">074</span>  }<a name="line.74"></a>
-<span class="sourceLineNo">075</span><a name="line.75"></a>
-<span class="sourceLineNo">076</span>  Connection getConnection() {<a name="line.76"></a>
-<span class="sourceLineNo">077</span>    return (Connection) this.connection;<a name="line.77"></a>
-<span class="sourceLineNo">078</span>  }<a name="line.78"></a>
-<span class="sourceLineNo">079</span>}<a name="line.79"></a>
+<span class="sourceLineNo">044</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP_NULL_ON_SOME_PATH",<a name="line.44"></a>
+<span class="sourceLineNo">045</span>      justification = "Can't figure why this complaint is happening... see below")<a name="line.45"></a>
+<span class="sourceLineNo">046</span>  SimpleServerCall(int id, final BlockingService service, final MethodDescriptor md,<a name="line.46"></a>
+<span class="sourceLineNo">047</span>      RequestHeader header, Message param, CellScanner cellScanner,<a name="line.47"></a>
+<span class="sourceLineNo">048</span>      SimpleServerRpcConnection connection, long size, TraceInfo tinfo,<a name="line.48"></a>
+<span class="sourceLineNo">049</span>      final InetAddress remoteAddress, long receiveTime, int timeout, ByteBufferPool reservoir,<a name="line.49"></a>
+<span class="sourceLineNo">050</span>      CellBlockBuilder cellBlockBuilder, CallCleanup reqCleanup, SimpleRpcServerResponder responder) {<a name="line.50"></a>
+<span class="sourceLineNo">051</span>    super(id, service, md, header, param, cellScanner, connection, size, tinfo, remoteAddress,<a name="line.51"></a>
+<span class="sourceLineNo">052</span>        receiveTime, timeout, reservoir, cellBlockBuilder, reqCleanup);<a name="line.52"></a>
+<span class="sourceLineNo">053</span>    this.responder = responder;<a name="line.53"></a>
+<span class="sourceLineNo">054</span>  }<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>   * Call is done. Execution happened and we returned results to client. It is now safe to cleanup.<a name="line.57"></a>
+<span class="sourceLineNo">058</span>   */<a name="line.58"></a>
+<span class="sourceLineNo">059</span>  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IS2_INCONSISTENT_SYNC",<a name="line.59"></a>
+<span class="sourceLineNo">060</span>      justification = "Presume the lock on processing request held by caller is protection enough")<a name="line.60"></a>
+<span class="sourceLineNo">061</span>  @Override<a name="line.61"></a>
+<span class="sourceLineNo">062</span>  void done() {<a name="line.62"></a>
+<span class="sourceLineNo">063</span>    super.done();<a name="line.63"></a>
+<span class="sourceLineNo">064</span>    this.getConnection().decRpcCount(); // Say that we're done with this call.<a name="line.64"></a>
+<span class="sourceLineNo">065</span>  }<a name="line.65"></a>
+<span class="sourceLineNo">066</span><a name="line.66"></a>
+<span class="sourceLineNo">067</span>  @Override<a name="line.67"></a>
+<span class="sourceLineNo">068</span>  public synchronized void sendResponseIfReady() throws IOException {<a name="line.68"></a>
+<span class="sourceLineNo">069</span>    // set param null to reduce memory pressure<a name="line.69"></a>
+<span class="sourceLineNo">070</span>    this.param = null;<a name="line.70"></a>
+<span class="sourceLineNo">071</span>    this.responder.doRespond(this);<a name="line.71"></a>
+<span class="sourceLineNo">072</span>  }<a name="line.72"></a>
+<span class="sourceLineNo">073</span><a name="line.73"></a>
+<span class="sourceLineNo">074</span>  SimpleServerRpcConnection getConnection() {<a name="line.74"></a>
+<span class="sourceLineNo">075</span>    return (SimpleServerRpcConnection) this.connection;<a name="line.75"></a>
+<span class="sourceLineNo">076</span>  }<a name="line.76"></a>
+<span class="sourceLineNo">077</span>}<a name="line.77"></a>
 
 
 

http://git-wip-us.apache.org/repos/asf/hbase-site/blob/f55ebeaa/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerRpcConnection.html
----------------------------------------------------------------------
diff --git a/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerRpcConnection.html b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerRpcConnection.html
new file mode 100644
index 0000000..ff541c9
--- /dev/null
+++ b/devapidocs/src-html/org/apache/hadoop/hbase/ipc/SimpleServerRpcConnection.html
@@ -0,0 +1,500 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html lang="en">
+<head>
+<title>Source code</title>
+<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
+</head>
+<body>
+<div class="sourceContainer">
+<pre><span class="sourceLineNo">001</span>/**<a name="line.1"></a>
+<span class="sourceLineNo">002</span> * Licensed to the Apache Software Foundation (ASF) under one<a name="line.2"></a>
+<span class="sourceLineNo">003</span> * or more contributor license agreements.  See the NOTICE file<a name="line.3"></a>
+<span class="sourceLineNo">004</span> * distributed with this work for additional information<a name="line.4"></a>
+<span class="sourceLineNo">005</span> * regarding copyright ownership.  The ASF licenses this file<a name="line.5"></a>
+<span class="sourceLineNo">006</span> * to you under the Apache License, Version 2.0 (the<a name="line.6"></a>
+<span class="sourceLineNo">007</span> * "License"); you may not use this file except in compliance<a name="line.7"></a>
+<span class="sourceLineNo">008</span> * with the License.  You may obtain a copy of the License at<a name="line.8"></a>
+<span class="sourceLineNo">009</span> *<a name="line.9"></a>
+<span class="sourceLineNo">010</span> *     http://www.apache.org/licenses/LICENSE-2.0<a name="line.10"></a>
+<span class="sourceLineNo">011</span> *<a name="line.11"></a>
+<span class="sourceLineNo">012</span> * Unless required by applicable law or agreed to in writing, software<a name="line.12"></a>
+<span class="sourceLineNo">013</span> * distributed under the License is distributed on an "AS IS" BASIS,<a name="line.13"></a>
+<span class="sourceLineNo">014</span> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.<a name="line.14"></a>
+<span class="sourceLineNo">015</span> * See the License for the specific language governing permissions and<a name="line.15"></a>
+<span class="sourceLineNo">016</span> * limitations under the License.<a name="line.16"></a>
+<span class="sourceLineNo">017</span> */<a name="line.17"></a>
+<span class="sourceLineNo">018</span>package org.apache.hadoop.hbase.ipc;<a name="line.18"></a>
+<span class="sourceLineNo">019</span><a name="line.19"></a>
+<span class="sourceLineNo">020</span>import java.io.IOException;<a name="line.20"></a>
+<span class="sourceLineNo">021</span>import java.io.InputStream;<a name="line.21"></a>
+<span class="sourceLineNo">022</span>import java.net.InetAddress;<a name="line.22"></a>
+<span class="sourceLineNo">023</span>import java.net.Socket;<a name="line.23"></a>
+<span class="sourceLineNo">024</span>import java.nio.ByteBuffer;<a name="line.24"></a>
+<span class="sourceLineNo">025</span>import java.nio.channels.ReadableByteChannel;<a name="line.25"></a>
+<span class="sourceLineNo">026</span>import java.nio.channels.SocketChannel;<a name="line.26"></a>
+<span class="sourceLineNo">027</span>import java.util.Arrays;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import java.util.concurrent.ConcurrentLinkedDeque;<a name="line.28"></a>
+<span class="sourceLineNo">029</span>import java.util.concurrent.atomic.LongAdder;<a name="line.29"></a>
+<span class="sourceLineNo">030</span>import java.util.concurrent.locks.Lock;<a name="line.30"></a>
+<span class="sourceLineNo">031</span>import java.util.concurrent.locks.ReentrantLock;<a name="line.31"></a>
+<span class="sourceLineNo">032</span><a name="line.32"></a>
+<span class="sourceLineNo">033</span>import org.apache.hadoop.hbase.CellScanner;<a name="line.33"></a>
+<span class="sourceLineNo">034</span>import org.apache.hadoop.hbase.DoNotRetryIOException;<a name="line.34"></a>
+<span class="sourceLineNo">035</span>import org.apache.hadoop.hbase.HConstants;<a name="line.35"></a>
+<span class="sourceLineNo">036</span>import org.apache.hadoop.hbase.classification.InterfaceAudience;<a name="line.36"></a>
+<span class="sourceLineNo">037</span>import org.apache.hadoop.hbase.client.VersionInfoUtil;<a name="line.37"></a>
+<span class="sourceLineNo">038</span>import org.apache.hadoop.hbase.exceptions.RequestTooBigException;<a name="line.38"></a>
+<span class="sourceLineNo">039</span>import org.apache.hadoop.hbase.ipc.RpcServer.CallCleanup;<a name="line.39"></a>
+<span class="sourceLineNo">040</span>import org.apache.hadoop.hbase.nio.ByteBuff;<a name="line.40"></a>
+<span class="sourceLineNo">041</span>import org.apache.hadoop.hbase.nio.SingleByteBuff;<a name="line.41"></a>
+<span class="sourceLineNo">042</span>import org.apache.hadoop.hbase.security.AccessDeniedException;<a name="line.42"></a>
+<span class="sourceLineNo">043</span>import org.apache.hadoop.hbase.security.AuthMethod;<a name="line.43"></a>
+<span class="sourceLineNo">044</span>import org.apache.hadoop.hbase.security.SaslStatus;<a name="line.44"></a>
+<span class="sourceLineNo">045</span>import org.apache.hadoop.hbase.security.SaslUtil;<a name="line.45"></a>
+<span class="sourceLineNo">046</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.BlockingService;<a name="line.46"></a>
+<span class="sourceLineNo">047</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.CodedInputStream;<a name="line.47"></a>
+<span class="sourceLineNo">048</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Message;<a name="line.48"></a>
+<span class="sourceLineNo">049</span>import org.apache.hadoop.hbase.shaded.com.google.protobuf.Descriptors.MethodDescriptor;<a name="line.49"></a>
+<span class="sourceLineNo">050</span>import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;<a name="line.50"></a>
+<span class="sourceLineNo">051</span>import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.RequestHeader;<a name="line.51"></a>
+<span class="sourceLineNo">052</span>import org.apache.hadoop.hbase.util.Bytes;<a name="line.52"></a>
+<span class="sourceLineNo">053</span>import org.apache.hadoop.hbase.util.Pair;<a name="line.53"></a>
+<span class="sourceLineNo">054</span>import org.apache.hadoop.io.IntWritable;<a name="line.54"></a>
+<span class="sourceLineNo">055</span>import org.apache.htrace.TraceInfo;<a name="line.55"></a>
+<span class="sourceLineNo">056</span><a name="line.56"></a>
+<span class="sourceLineNo">057</span>/** Reads calls from a connection and queues them for handling. */<a name="line.57"></a>
+<span class="sourceLineNo">058</span>@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "VO_VOLATILE_INCREMENT",<a name="line.58"></a>
+<span class="sourceLineNo">059</span>    justification = "False positive according to http://sourceforge.net/p/findbugs/bugs/1032/")<a name="line.59"></a>
+<span class="sourceLineNo">060</span>@InterfaceAudience.Private<a name="line.60"></a>
+<span class="sourceLineNo">061</span>class SimpleServerRpcConnection extends ServerRpcConnection {<a name="line.61"></a>
+<span class="sourceLineNo">062</span><a name="line.62"></a>
+<span class="sourceLineNo">063</span>  final SocketChannel channel;<a name="line.63"></a>
+<span class="sourceLineNo">064</span>  private ByteBuff data;<a name="line.64"></a>
+<span class="sourceLineNo">065</span>  private ByteBuffer dataLengthBuffer;<a name="line.65"></a>
+<span class="sourceLineNo">066</span>  protected final ConcurrentLinkedDeque&lt;SimpleServerCall&gt; responseQueue =<a name="line.66"></a>
+<span class="sourceLineNo">067</span>      new ConcurrentLinkedDeque&lt;&gt;();<a name="line.67"></a>
+<span class="sourceLineNo">068</span>  final Lock responseWriteLock = new ReentrantLock();<a name="line.68"></a>
+<span class="sourceLineNo">069</span>  private final LongAdder rpcCount = new LongAdder(); // number of outstanding rpcs<a name="line.69"></a>
+<span class="sourceLineNo">070</span>  private long lastContact;<a name="line.70"></a>
+<span class="sourceLineNo">071</span>  private final Socket socket;<a name="line.71"></a>
+<span class="sourceLineNo">072</span>  private final SimpleRpcServerResponder responder;<a name="line.72"></a>
+<span class="sourceLineNo">073</span><a name="line.73"></a>
+<span class="sourceLineNo">074</span>  public SimpleServerRpcConnection(SimpleRpcServer rpcServer, SocketChannel channel,<a name="line.74"></a>
+<span class="sourceLineNo">075</span>      long lastContact) {<a name="line.75"></a>
+<span class="sourceLineNo">076</span>    super(rpcServer);<a name="line.76"></a>
+<span class="sourceLineNo">077</span>    this.channel = channel;<a name="line.77"></a>
+<span class="sourceLineNo">078</span>    this.lastContact = lastContact;<a name="line.78"></a>
+<span class="sourceLineNo">079</span>    this.data = null;<a name="line.79"></a>
+<span class="sourceLineNo">080</span>    this.dataLengthBuffer = ByteBuffer.allocate(4);<a name="line.80"></a>
+<span class="sourceLineNo">081</span>    this.socket = channel.socket();<a name="line.81"></a>
+<span class="sourceLineNo">082</span>    this.addr = socket.getInetAddress();<a name="line.82"></a>
+<span class="sourceLineNo">083</span>    if (addr == null) {<a name="line.83"></a>
+<span class="sourceLineNo">084</span>      this.hostAddress = "*Unknown*";<a name="line.84"></a>
+<span class="sourceLineNo">085</span>    } else {<a name="line.85"></a>
+<span class="sourceLineNo">086</span>      this.hostAddress = addr.getHostAddress();<a name="line.86"></a>
+<span class="sourceLineNo">087</span>    }<a name="line.87"></a>
+<span class="sourceLineNo">088</span>    this.remotePort = socket.getPort();<a name="line.88"></a>
+<span class="sourceLineNo">089</span>    if (rpcServer.socketSendBufferSize != 0) {<a name="line.89"></a>
+<span class="sourceLineNo">090</span>      try {<a name="line.90"></a>
+<span class="sourceLineNo">091</span>        socket.setSendBufferSize(rpcServer.socketSendBufferSize);<a name="line.91"></a>
+<span class="sourceLineNo">092</span>      } catch (IOException e) {<a name="line.92"></a>
+<span class="sourceLineNo">093</span>        SimpleRpcServer.LOG.warn(<a name="line.93"></a>
+<span class="sourceLineNo">094</span>          "Connection: unable to set socket send buffer size to " + rpcServer.socketSendBufferSize);<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>    this.saslCall = new SimpleServerCall(SASL_CALLID, null, null, null, null, null, this, 0, null,<a name="line.97"></a>
+<span class="sourceLineNo">098</span>        null, System.currentTimeMillis(), 0, rpcServer.reservoir, rpcServer.cellBlockBuilder, null,<a name="line.98"></a>
+<span class="sourceLineNo">099</span>        rpcServer.responder);<a name="line.99"></a>
+<span class="sourceLineNo">100</span>    this.setConnectionHeaderResponseCall = new SimpleServerCall(CONNECTION_HEADER_RESPONSE_CALLID,<a name="line.100"></a>
+<span class="sourceLineNo">101</span>        null, null, null, null, null, this, 0, null, null, System.currentTimeMillis(), 0,<a name="line.101"></a>
+<span class="sourceLineNo">102</span>        rpcServer.reservoir, rpcServer.cellBlockBuilder, null, rpcServer.responder);<a name="line.102"></a>
+<span class="sourceLineNo">103</span>    this.authFailedCall = new SimpleServerCall(AUTHORIZATION_FAILED_CALLID, null, null, null, null,<a name="line.103"></a>
+<span class="sourceLineNo">104</span>        null, this, 0, null, null, System.currentTimeMillis(), 0, rpcServer.reservoir,<a name="line.104"></a>
+<span class="sourceLineNo">105</span>        rpcServer.cellBlockBuilder, null, rpcServer.responder);<a name="line.105"></a>
+<span class="sourceLineNo">106</span>    this.responder = rpcServer.responder;<a name="line.106"></a>
+<span class="sourceLineNo">107</span>  }<a name="line.107"></a>
+<span class="sourceLineNo">108</span><a name="line.108"></a>
+<span class="sourceLineNo">109</span>  public void setLastContact(long lastContact) {<a name="line.109"></a>
+<span class="sourceLineNo">110</span>    this.lastContact = lastContact;<a name="line.110"></a>
+<span class="sourceLineNo">111</span>  }<a name="line.111"></a>
+<span class="sourceLineNo">112</span><a name="line.112"></a>
+<span class="sourceLineNo">113</span>  public long getLastContact() {<a name="line.113"></a>
+<span class="sourceLineNo">114</span>    return lastContact;<a name="line.114"></a>
+<span class="sourceLineNo">115</span>  }<a name="line.115"></a>
+<span class="sourceLineNo">116</span><a name="line.116"></a>
+<span class="sourceLineNo">117</span>  /* Return true if the connection has no outstanding rpc */<a name="line.117"></a>
+<span class="sourceLineNo">118</span>  boolean isIdle() {<a name="line.118"></a>
+<span class="sourceLineNo">119</span>    return rpcCount.sum() == 0;<a name="line.119"></a>
+<span class="sourceLineNo">120</span>  }<a name="line.120"></a>
+<span class="sourceLineNo">121</span><a name="line.121"></a>
+<span class="sourceLineNo">122</span>  /* Decrement the outstanding RPC count */<a name="line.122"></a>
+<span class="sourceLineNo">123</span>  protected void decRpcCount() {<a name="line.123"></a>
+<span class="sourceLineNo">124</span>    rpcCount.decrement();<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>  /* Increment the outstanding RPC count */<a name="line.127"></a>
+<span class="sourceLineNo">128</span>  protected void incRpcCount() {<a name="line.128"></a>
+<span class="sourceLineNo">129</span>    rpcCount.increment();<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 int readPreamble() throws IOException {<a name="line.132"></a>
+<span class="sourceLineNo">133</span>    int count;<a name="line.133"></a>
+<span class="sourceLineNo">134</span>    // Check for 'HBas' magic.<a name="line.134"></a>
+<span class="sourceLineNo">135</span>    this.dataLengthBuffer.flip();<a name="line.135"></a>
+<span class="sourceLineNo">136</span>    if (!Arrays.equals(HConstants.RPC_HEADER, dataLengthBuffer.array())) {<a name="line.136"></a>
+<span class="sourceLineNo">137</span>      return doBadPreambleHandling(<a name="line.137"></a>
+<span class="sourceLineNo">138</span>        "Expected HEADER=" + Bytes.toStringBinary(HConstants.RPC_HEADER) + " but received HEADER=" +<a name="line.138"></a>
+<span class="sourceLineNo">139</span>            Bytes.toStringBinary(dataLengthBuffer.array()) + " from " + toString());<a name="line.139"></a>
+<span class="sourceLineNo">140</span>    }<a name="line.140"></a>
+<span class="sourceLineNo">141</span>    // Now read the next two bytes, the version and the auth to use.<a name="line.141"></a>
+<span class="sourceLineNo">142</span>    ByteBuffer versionAndAuthBytes = ByteBuffer.allocate(2);<a name="line.142"></a>
+<span class="sourceLineNo">143</span>    count = this.rpcServer.channelRead(channel, versionAndAuthBytes);<a name="line.143"></a>
+<span class="sourceLineNo">144</span>    if (count &lt; 0 || versionAndAuthBytes.remaining() &gt; 0) {<a name="line.144"></a>
+<span class="sourceLineNo">145</span>      return count;<a name="line.145"></a>
+<span class="sourceLineNo">146</span>    }<a name="line.146"></a>
+<span class="sourceLineNo">147</span>    int version = versionAndAuthBytes.get(0);<a name="line.147"></a>
+<span class="sourceLineNo">148</span>    byte authbyte = versionAndAuthBytes.get(1);<a name="line.148"></a>
+<span class="sourceLineNo">149</span>    this.authMethod = AuthMethod.valueOf(authbyte);<a name="line.149"></a>
+<span class="sourceLineNo">150</span>    if (version != SimpleRpcServer.CURRENT_VERSION) {<a name="line.150"></a>
+<span class="sourceLineNo">151</span>      String msg = getFatalConnectionString(version, authbyte);<a name="line.151"></a>
+<span class="sourceLineNo">152</span>      return doBadPreambleHandling(msg, new WrongVersionException(msg));<a name="line.152"></a>
+<span class="sourceLineNo">153</span>    }<a name="line.153"></a>
+<span class="sourceLineNo">154</span>    if (authMethod == null) {<a name="line.154"></a>
+<span class="sourceLineNo">155</span>      String msg = getFatalConnectionString(version, authbyte);<a name="line.155"></a>
+<span class="sourceLineNo">156</span>      return doBadPreambleHandling(msg, new BadAuthException(msg));<a name="line.156"></a>
+<span class="sourceLineNo">157</span>    }<a name="line.157"></a>
+<span class="sourceLineNo">158</span>    if (this.rpcServer.isSecurityEnabled &amp;&amp; authMethod == AuthMethod.SIMPLE) {<a name="line.158"></a>
+<span class="sourceLineNo">159</span>      if (this.rpcServer.allowFallbackToSimpleAuth) {<a name="line.159"></a>
+<span class="sourceLineNo">160</span>        this.rpcServer.metrics.authenticationFallback();<a name="line.160"></a>
+<span class="sourceLineNo">161</span>        authenticatedWithFallback = true;<a name="line.161"></a>
+<span class="sourceLineNo">162</span>      } else {<a name="line.162"></a>
+<span class="sourceLineNo">163</span>        AccessDeniedException ae = new AccessDeniedException("Authentication is required");<a name="line.163"></a>
+<span class="sourceLineNo">164</span>        this.rpcServer.setupResponse(authFailedResponse, authFailedCall, ae, ae.getMessage());<a name="line.164"></a>
+<span class="sourceLineNo">165</span>        authFailedCall.sendResponseIfReady();<a name="line.165"></a>
+<span class="sourceLineNo">166</span>        throw ae;<a name="line.166"></a>
+<span class="sourceLineNo">167</span>      }<a name="line.167"></a>
+<span class="sourceLineNo">168</span>    }<a name="line.168"></a>
+<span class="sourceLineNo">169</span>    if (!this.rpcServer.isSecurityEnabled &amp;&amp; authMethod != AuthMethod.SIMPLE) {<a name="line.169"></a>
+<span class="sourceLineNo">170</span>      doRawSaslReply(SaslStatus.SUCCESS, new IntWritable(SaslUtil.SWITCH_TO_SIMPLE_AUTH), null,<a name="line.170"></a>
+<span class="sourceLineNo">171</span>        null);<a name="line.171"></a>
+<span class="sourceLineNo">172</span>      authMethod = AuthMethod.SIMPLE;<a name="line.172"></a>
+<span class="sourceLineNo">173</span>      // client has already sent the initial Sasl message and we<a name="line.173"></a>
+<span class="sourceLineNo">174</span>      // should ignore it. Both client and server should fall back<a name="line.174"></a>
+<span class="sourceLineNo">175</span>      // to simple auth from now on.<a name="line.175"></a>
+<span class="sourceLineNo">176</span>      skipInitialSaslHandshake = true;<a name="line.176"></a>
+<span class="sourceLineNo">177</span>    }<a name="line.177"></a>
+<span class="sourceLineNo">178</span>    if (authMethod != AuthMethod.SIMPLE) {<a name="line.178"></a>
+<span class="sourceLineNo">179</span>      useSasl = true;<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>    dataLengthBuffer.clear();<a name="line.182"></a>
+<span class="sourceLineNo">183</span>    connectionPreambleRead = true;<a name="line.183"></a>
+<span class="sourceLineNo">184</span>    return count;<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>  private int read4Bytes() throws IOException {<a name="line.187"></a>
+<span class="sourceLineNo">188</span>    if (this.dataLengthBuffer.remaining() &gt; 0) {<a name="line.188"></a>
+<span class="sourceLineNo">189</span>      return this.rpcServer.channelRead(channel, this.dataLengthBuffer);<a name="line.189"></a>
+<span class="sourceLineNo">190</span>    } else {<a name="line.190"></a>
+<span class="sourceLineNo">191</span>      return 0;<a name="line.191"></a>
+<span class="sourceLineNo">192</span>    }<a name="line.192"></a>
+<span class="sourceLineNo">193</span>  }<a name="line.193"></a>
+<span class="sourceLineNo">194</span><a name="line.194"></a>
+<span class="sourceLineNo">195</span>  /**<a name="line.195"></a>
+<span class="sourceLineNo">196</span>   * Read off the wire. If there is not enough data to read, update the connection state with what<a name="line.196"></a>
+<span class="sourceLineNo">197</span>   * we have and returns.<a name="line.197"></a>
+<span class="sourceLineNo">198</span>   * @return Returns -1 if failure (and caller will close connection), else zero or more.<a name="line.198"></a>
+<span class="sourceLineNo">199</span>   * @throws IOException<a name="line.199"></a>
+<span class="sourceLineNo">200</span>   * @throws InterruptedException<a name="line.200"></a>
+<span class="sourceLineNo">201</span>   */<a name="line.201"></a>
+<span class="sourceLineNo">202</span>  public int readAndProcess() throws IOException, InterruptedException {<a name="line.202"></a>
+<span class="sourceLineNo">203</span>    // Try and read in an int. If new connection, the int will hold the 'HBas' HEADER. If it<a name="line.203"></a>
+<span class="sourceLineNo">204</span>    // does, read in the rest of the connection preamble, the version and the auth method.<a name="line.204"></a>
+<span class="sourceLineNo">205</span>    // Else it will be length of the data to read (or -1 if a ping). We catch the integer<a name="line.205"></a>
+<span class="sourceLineNo">206</span>    // length into the 4-byte this.dataLengthBuffer.<a name="line.206"></a>
+<span class="sourceLineNo">207</span>    int count = read4Bytes();<a name="line.207"></a>
+<span class="sourceLineNo">208</span>    if (count &lt; 0 || dataLengthBuffer.remaining() &gt; 0) {<a name="line.208"></a>
+<span class="sourceLineNo">209</span>      return count;<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>    // If we have not read the connection setup preamble, look to see if that is on the wire.<a name="line.212"></a>
+<span class="sourceLineNo">213</span>    if (!connectionPreambleRead) {<a name="line.213"></a>
+<span class="sourceLineNo">214</span>      count = readPreamble();<a name="line.214"></a>
+<span class="sourceLineNo">215</span>      if (!connectionPreambleRead) {<a name="line.215"></a>
+<span class="sourceLineNo">216</span>        return count;<a name="line.216"></a>
+<span class="sourceLineNo">217</span>      }<a name="line.217"></a>
+<span class="sourceLineNo">218</span><a name="line.218"></a>
+<span class="sourceLineNo">219</span>      count = read4Bytes();<a name="line.219"></a>
+<span class="sourceLineNo">220</span>      if (count &lt; 0 || dataLengthBuffer.remaining() &gt; 0) {<a name="line.220"></a>
+<span class="sourceLineNo">221</span>        return count;<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>    // We have read a length and we have read the preamble. It is either the connection header<a name="line.225"></a>
+<span class="sourceLineNo">226</span>    // or it is a request.<a name="line.226"></a>
+<span class="sourceLineNo">227</span>    if (data == null) {<a name="line.227"></a>
+<span class="sourceLineNo">228</span>      dataLengthBuffer.flip();<a name="line.228"></a>
+<span class="sourceLineNo">229</span>      int dataLength = dataLengthBuffer.getInt();<a name="line.229"></a>
+<span class="sourceLineNo">230</span>      if (dataLength == RpcClient.PING_CALL_ID) {<a name="line.230"></a>
+<span class="sourceLineNo">231</span>        if (!useWrap) { // covers the !useSasl too<a name="line.231"></a>
+<span class="sourceLineNo">232</span>          dataLengthBuffer.clear();<a name="line.232"></a>
+<span class="sourceLineNo">233</span>          return 0; // ping message<a name="line.233"></a>
+<span class="sourceLineNo">234</span>        }<a name="line.234"></a>
+<span class="sourceLineNo">235</span>      }<a name="line.235"></a>
+<span class="sourceLineNo">236</span>      if (dataLength &lt; 0) { // A data length of zero is legal.<a name="line.236"></a>
+<span class="sourceLineNo">237</span>        throw new DoNotRetryIOException(<a name="line.237"></a>
+<span class="sourceLineNo">238</span>            "Unexpected data length " + dataLength + "!! from " + getHostAddress());<a name="line.238"></a>
+<span class="sourceLineNo">239</span>      }<a name="line.239"></a>
+<span class="sourceLineNo">240</span><a name="line.240"></a>
+<span class="sourceLineNo">241</span>      if (dataLength &gt; this.rpcServer.maxRequestSize) {<a name="line.241"></a>
+<span class="sourceLineNo">242</span>        String msg = "RPC data length of " + dataLength + " received from " + getHostAddress() +<a name="line.242"></a>
+<span class="sourceLineNo">243</span>            " is greater than max allowed " + this.rpcServer.maxRequestSize + ". Set \"" +<a name="line.243"></a>
+<span class="sourceLineNo">244</span>            SimpleRpcServer.MAX_REQUEST_SIZE +<a name="line.244"></a>
+<span class="sourceLineNo">245</span>            "\" on server to override this limit (not recommended)";<a name="line.245"></a>
+<span class="sourceLineNo">246</span>        SimpleRpcServer.LOG.warn(msg);<a name="line.246"></a>
+<span class="sourceLineNo">247</span><a name="line.247"></a>
+<span class="sourceLineNo">248</span>        if (connectionHeaderRead &amp;&amp; connectionPreambleRead) {<a name="line.248"></a>
+<span class="sourceLineNo">249</span>          incRpcCount();<a name="line.249"></a>
+<span class="sourceLineNo">250</span>          // Construct InputStream for the non-blocking SocketChannel<a name="line.250"></a>
+<span class="sourceLineNo">251</span>          // We need the InputStream because we want to read only the request header<a name="line.251"></a>
+<span class="sourceLineNo">252</span>          // instead of the whole rpc.<a name="line.252"></a>
+<span class="sourceLineNo">253</span>          ByteBuffer buf = ByteBuffer.allocate(1);<a name="line.253"></a>
+<span class="sourceLineNo">254</span>          InputStream is = new InputStream() {<a name="line.254"></a>
+<span class="sourceLineNo">255</span>            @Override<a name="line.255"></a>
+<span class="sourceLineNo">256</span>            public int read() throws IOException {<a name="line.256"></a>
+<span class="sourceLineNo">257</span>              SimpleServerRpcConnection.this.rpcServer.channelRead(channel, buf);<a name="line.257"></a>
+<span class="sourceLineNo">258</span>              buf.flip();<a name="line.258"></a>
+<span class="sourceLineNo">259</span>              int x = buf.get();<a name="line.259"></a>
+<span class="sourceLineNo">260</span>              buf.flip();<a name="line.260"></a>
+<span class="sourceLineNo">261</span>              return x;<a name="line.261"></a>
+<span class="sourceLineNo">262</span>            }<a name="line.262"></a>
+<span class="sourceLineNo">263</span>          };<a name="line.263"></a>
+<span class="sourceLineNo">264</span>          CodedInputStream cis = CodedInputStream.newInstance(is);<a name="line.264"></a>
+<span class="sourceLineNo">265</span>          int headerSize = cis.readRawVarint32();<a name="line.265"></a>
+<span class="sourceLineNo">266</span>          Message.Builder builder = RequestHeader.newBuilder();<a name="line.266"></a>
+<span class="sourceLineNo">267</span>          ProtobufUtil.mergeFrom(builder, cis, headerSize);<a name="line.267"></a>
+<span class="sourceLineNo">268</span>          RequestHeader header = (RequestHeader) builder.build();<a name="line.268"></a>
+<span class="sourceLineNo">269</span><a name="line.269"></a>
+<span class="sourceLineNo">270</span>          // Notify the client about the offending request<a name="line.270"></a>
+<span class="sourceLineNo">271</span>          SimpleServerCall reqTooBig = new SimpleServerCall(header.getCallId(), this.service, null,<a name="line.271"></a>
+<span class="sourceLineNo">272</span>              null, null, null, this, 0, null, this.addr, System.currentTimeMillis(), 0,<a name="line.272"></a>
+<span class="sourceLineNo">273</span>              this.rpcServer.reservoir, this.rpcServer.cellBlockBuilder, null, responder);<a name="line.273"></a>
+<span class="sourceLineNo">274</span>          this.rpcServer.metrics.exception(SimpleRpcServer.REQUEST_TOO_BIG_EXCEPTION);<a name="line.274"></a>
+<span class="sourceLineNo">275</span>          // Make sure the client recognizes the underlying exception<a name="line.275"></a>
+<span class="sourceLineNo">276</span>          // Otherwise, throw a DoNotRetryIOException.<a name="line.276"></a>
+<span class="sourceLineNo">277</span>          if (VersionInfoUtil.hasMinimumVersion(connectionHeader.getVersionInfo(),<a name="line.277"></a>
+<span class="sourceLineNo">278</span>            RequestTooBigException.MAJOR_VERSION, RequestTooBigException.MINOR_VERSION)) {<a name="line.278"></a>
+<span class="sourceLineNo">279</span>            this.rpcServer.setupResponse(null, reqTooBig, SimpleRpcServer.REQUEST_TOO_BIG_EXCEPTION,<a name="line.279"></a>
+<span class="sourceLineNo">280</span>              msg);<a name="line.280"></a>
+<span class="sourceLineNo">281</span>          } else {<a name="line.281"></a>
+<span class="sourceLineNo">282</span>            this.rpcServer.setupResponse(null, reqTooBig, new DoNotRetryIOException(), msg);<a name="line.282"></a>
+<span class="sourceLineNo">283</span>          }<a name="line.283"></a>
+<span class="sourceLineNo">284</span>          // We are going to close the connection, make sure we process the response<a name="line.284"></a>
+<span class="sourceLineNo">285</span>          // before that. In rare case when this fails, we still close the connection.<a name="line.285"></a>
+<span class="sourceLineNo">286</span>          responseWriteLock.lock();<a name="line.286"></a>
+<span class="sourceLineNo">287</span>          try {<a name="line.287"></a>
+<span class="sourceLineNo">288</span>            this.responder.processResponse(reqTooBig);<a name="line.288"></a>
+<span class="sourceLineNo">289</span>          } finally {<a name="line.289"></a>
+<span class="sourceLineNo">290</span>            responseWriteLock.unlock();<a name="line.290"></a>
+<span class="sourceLineNo">291</span>          }<a name="line.291"></a>
+<span class="sourceLineNo">292</span>        }<a name="line.292"></a>
+<span class="sourceLineNo">293</span>        // Close the connection<a name="line.293"></a>
+<span class="sourceLineNo">294</span>        return -1;<a name="line.294"></a>
+<span class="sourceLineNo">295</span>      }<a name="line.295"></a>
+<span class="sourceLineNo">296</span><a name="line.296"></a>
+<span class="sourceLineNo">297</span>      // Initialize this.data with a ByteBuff.<a name="line.297"></a>
+<span class="sourceLineNo">298</span>      // This call will allocate a ByteBuff to read request into and assign to this.data<a name="line.298"></a>
+<span class="sourceLineNo">299</span>      // Also when we use some buffer(s) from pool, it will create a CallCleanup instance also and<a name="line.299"></a>
+<span class="sourceLineNo">300</span>      // assign to this.callCleanup<a name="line.300"></a>
+<span class="sourceLineNo">301</span>      initByteBuffToReadInto(dataLength);<a name="line.301"></a>
+<span class="sourceLineNo">302</span><a name="line.302"></a>
+<span class="sourceLineNo">303</span>      // Increment the rpc count. This counter will be decreased when we write<a name="line.303"></a>
+<span class="sourceLineNo">304</span>      // the response. If we want the connection to be detected as idle properly, we<a name="line.304"></a>
+<span class="sourceLineNo">305</span>      // need to keep the inc / dec correct.<a name="line.305"></a>
+<span class="sourceLineNo">306</span>      incRpcCount();<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>    count = channelDataRead(channel, data);<a name="line.309"></a>
+<span class="sourceLineNo">310</span><a name="line.310"></a>
+<span class="sourceLineNo">311</span>    if (count &gt;= 0 &amp;&amp; data.remaining() == 0) { // count==0 if dataLength == 0<a name="line.311"></a>
+<span class="sourceLineNo">312</span>      process();<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>    return count;<a name="line.315"></a>
+<span class="sourceLineNo">316</span>  }<a name="line.316"></a>
+<span class="sourceLineNo">317</span><a name="line.317"></a>
+<span class="sourceLineNo">318</span>  // It creates the ByteBuff and CallCleanup and assign to Connection instance.<a name="line.318"></a>
+<span class="sourceLineNo">319</span>  private void initByteBuffToReadInto(int length) {<a name="line.319"></a>
+<span class="sourceLineNo">320</span>    // We create random on heap buffers are read into those when<a name="line.320"></a>
+<span class="sourceLineNo">321</span>    // 1. ByteBufferPool is not there.<a name="line.321"></a>
+<span class="sourceLineNo">322</span>    // 2. When the size of the req is very small. Using a large sized (64 KB) buffer from pool is<a name="line.322"></a>
+<span class="sourceLineNo">323</span>    // waste then. Also if all the reqs are of this size, we will be creating larger sized<a name="line.323"></a>
+<span class="sourceLineNo">324</span>    // buffers and pool them permanently. This include Scan/Get request and DDL kind of reqs like<a name="line.324"></a>
+<span class="sourceLineNo">325</span>    // RegionOpen.<a name="line.325"></a>
+<span class="sourceLineNo">326</span>    // 3. If it is an initial handshake signal or initial connection request. Any way then<a name="line.326"></a>
+<span class="sourceLineNo">327</span>    // condition 2 itself will match<a name="line.327"></a>
+<span class="sourceLineNo">328</span>    // 4. When SASL use is ON.<a name="line.328"></a>
+<span class="sourceLineNo">329</span>    if (this.rpcServer.reservoir == null || skipInitialSaslHandshake || !connectionHeaderRead ||<a name="line.329"></a>
+<span class="sourceLineNo">330</span>        useSasl || length &lt; this.rpcServer.minSizeForReservoirUse) {<a name="line.330"></a>
+<span class="sourceLineNo">331</span>      this.data = new SingleByteBuff(ByteBuffer.allocate(length));<a name="line.331"></a>
+<span class="sourceLineNo">332</span>    } else {<a name="line.332"></a>
+<span class="sourceLineNo">333</span>      Pair&lt;ByteBuff, CallCleanup&gt; pair = RpcServer.allocateByteBuffToReadInto(<a name="line.333"></a>
+<span class="sourceLineNo">334</span>        this.rpcServer.reservoir, this.rpcServer.minSizeForReservoirUse, length);<a name="line.334"></a>
+<span class="sourceLineNo">335</span>      this.data = pair.getFirst();<a name="line.335"></a>
+<span class="sourceLineNo">336</span>      this.callCleanup = pair.getSecond();<a name="line.336"></a>
+<span class="sourceLineNo">337</span>    }<a name="line.337"></a>
+<span class="sourceLineNo">338</span>  }<a name="line.338"></a>
+<span class="sourceLineNo">339</span><a name="line.339"></a>
+<span class="sourceLineNo">340</span>  protected int channelDataRead(ReadableByteChannel channel, ByteBuff buf) throws IOException {<a name="line.340"></a>
+<span class="sourceLineNo">341</span>    int count = buf.read(channel);<a name="line.341"></a>
+<span class="sourceLineNo">342</span>    if (count &gt; 0) {<a name="line.342"></a>
+<span class="sourceLineNo">343</span>      this.rpcServer.metrics.receivedBytes(count);<a name="line.343"></a>
+<span class="sourceLineNo">344</span>    }<a name="line.344"></a>
+<span class="sourceLineNo">345</span>    return count;<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>  /**<a name="line.348"></a>
+<span class="sourceLineNo">349</span>   * Process the data buffer and clean the connection state for the next call.<a name="line.349"></a>
+<span class="sourceLineNo">350</span>   */<a name="line.350"></a>
+<span class="sourceLineNo">351</span>  private void process() throws IOException, InterruptedException {<a name="line.351"></a>
+<span class="sourceLineNo">352</span>    data.rewind();<a name="line.352"></a>
+<span class="sourceLineNo">353</span>    try {<a name="line.353"></a>
+<span class="sourceLineNo">354</span>      if (skipInitialSaslHandshake) {<a name="line.354"></a>
+<span class="sourceLineNo">355</span>        skipInitialSaslHandshake = false;<a name="line.355"></a>
+<span class="sourceLineNo">356</span>        return;<a name="line.356"></a>
+<span class="sourceLineNo">357</span>      }<a name="line.357"></a>
+<span class="sourceLineNo">358</span><a name="line.358"></a>
+<span class="sourceLineNo">359</span>      if (useSasl) {<a name="line.359"></a>
+<span class="sourceLineNo">360</span>        saslReadAndProcess(data);<a name="line.360"></a>
+<span class="sourceLineNo">361</span>      } else {<a name="line.361"></a>
+<span class="sourceLineNo">362</span>        processOneRpc(data);<a name="line.362"></a>
+<span class="sourceLineNo">363</span>      }<a name="line.363"></a>
+<span class="sourceLineNo">364</span><a name="line.364"></a>
+<span class="sourceLineNo">365</span>    } finally {<a name="line.365"></a>
+<span class="sourceLineNo">366</span>      dataLengthBuffer.clear(); // Clean for the next call<a name="line.366"></a>
+<span class="sourceLineNo">367</span>      data = null; // For the GC<a name="line.367"></a>
+<span class="sourceLineNo">368</span>      this.callCleanup = null;<a name="line.368"></a>
+<span class="sourceLineNo">369</span>    }<a name="line.369"></a>
+<span class="sourceLineNo">370</span>  }<a name="line.370"></a>
+<span class="sourceLineNo">371</span><a name="line.371"></a>
+<span class="sourceLineNo">372</span>  private int doBadPreambleHandling(final String msg) throws IOException {<a name="line.372"></a>
+<span class="sourceLineNo">373</span>    return doBadPreambleHandling(msg, new FatalConnectionException(msg));<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>  private int doBadPreambleHandling(final String msg, final Exception e) throws IOException {<a name="line.376"></a>
+<span class="sourceLineNo">377</span>    SimpleRpcServer.LOG.warn(msg);<a name="line.377"></a>
+<span class="sourceLineNo">378</span>    SimpleServerCall fakeCall = new SimpleServerCall(-1, null, null, null, null, null, this, -1,<a name="line.378"></a>
+<span class="sourceLineNo">379</span>        null, null, System.currentTimeMillis(), 0, this.rpcServer.reservoir,<a name="line.379"></a>
+<span class="sourceLineNo">380</span>        this.rpcServer.cellBlockBuilder, null, responder);<a name="line.380"></a>
+<span class="sourceLineNo">381</span>    this.rpcServer.setupResponse(null, fakeCall, e, msg);<a name="line.381"></a>
+<span class="sourceLineNo">382</span>    this.responder.doRespond(fakeCall);<a name="line.382"></a>
+<span class="sourceLineNo">383</span>    // Returning -1 closes out the connection.<a name="line.383"></a>
+<span class="sourceLineNo">384</span>    return -1;<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>  @Override<a name="line.387"></a>
+<span class="sourceLineNo">388</span>  public synchronized void close() {<a name="line.388"></a>
+<span class="sourceLineNo">389</span>    disposeSasl();<a name="line.389"></a>
+<span class="sourceLineNo">390</span>    data = null;<a name="line.390"></a>
+<span class="sourceLineNo">391</span>    callCleanup = null;<a name="line.391"></a>
+<span class="sourceLineNo">392</span>    if (!channel.isOpen()) return;<a name="line.392"></a>
+<span class="sourceLineNo">393</span>    try {<a name="line.393"></a>
+<span class="sourceLineNo">394</span>      socket.shutdownOutput();<a name="line.394"></a>
+<span class="sourceLineNo">395</span>    } catch (Exception ignored) {<a name="line.395"></a>
+<span class="sourceLineNo">396</span>      if (SimpleRpcServer.LOG.isTraceEnabled()) {<a name="line.396"></a>
+<span class="sourceLineNo">397</span>        SimpleRpcServer.LOG.trace("Ignored exception", ignored);<a name="line.397"></a>
+<span class="sourceLineNo">398</span>      }<a name="line.398"></a>
+<span class="sourceLineNo">399</span>    }<a name="line.399"></a>
+<span class="sourceLineNo">400</span>    if (channel.isOpen()) {<a name="line.400"></a>
+<span class="sourceLineNo">401</span>      try {<a name="line.401"></a>
+<span class="sourceLineNo">402</span>        channel.close();<a name="line.402"></a>
+<span class="sourceLineNo">403</span>      } catch (Exception ignored) {<a name="line.403"></a>
+<span class="sourceLineNo">404</span>      }<a name="line.404"></a>
+<span class="sourceLineNo">405</span>    }<a name="line.405"></a>
+<span class="sourceLineNo">406</span>    try {<a name="line.406"></a>
+<span class="sourceLineNo">407</span>      socket.close();<a name="line.407"></a>
+<span class="sourceLineNo">408</span>    } catch (Exception ignored) {<a name="line.408"></a>
+<span class="sourceLineNo">409</span>      if (SimpleRpcServer.LOG.isTraceEnabled()) {<a name="line.409"></a>
+<span class="sourceLineNo">410</span>        SimpleRpcServer.LOG.trace("Ignored exception", ignored);<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>  }<a name="line.413"></a>
+<span class="sourceLineNo">414</span><a name="line.414"></a>
+<span class="sourceLineNo">415</span>  @Override<a name="line.415"></a>
+<span class="sourceLineNo">416</span>  public boolean isConnectionOpen() {<a name="line.416"></a>
+<span class="sourceLineNo">417</span>    return channel.isOpen();<a name="line.417"></a>
+<span class="sourceLineNo">418</span>  }<a name="line.418"></a>
+<span class="sourceLineNo">419</span><a name="line.419"></a>
+<span class="sourceLineNo">420</span>  @Override<a name="line.420"></a>
+<span class="sourceLineNo">421</span>  public SimpleServerCall createCall(int id, BlockingService service, MethodDescriptor md,<a name="line.421"></a>
+<span class="sourceLineNo">422</span>      RequestHeader header, Message param, CellScanner cellScanner, long size, TraceInfo tinfo,<a name="line.422"></a>
+<span class="sourceLineNo">423</span>      InetAddress remoteAddress, int timeout, CallCleanup reqCleanup) {<a name="line.423"></a>
+<span class="sourceLineNo">424</span>    return new SimpleServerCall(id, service, md, header, param, cellScanner, this, size, tinfo,<a name="line.424"></a>
+<span class="sourceLineNo">425</span>        remoteAddress, System.currentTimeMillis(), timeout, this.rpcServer.reservoir,<a name="line.425"></a>
+<span class="sourceLineNo">426</span>        this.rpcServer.cellBlockBuilder, reqCleanup, this.responder);<a name="line.426"></a>
+<span class="sourceLineNo">427</span>  }<a name="line.427"></a>
+<span class="sourceLineNo">428</span>}<a name="line.428"></a>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+</pre>
+</div>
+</body>
+</html>


Mime
View raw message