blob: 723f64408ca3e0f14bdda15edb419fc7d74b99b9 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en">
<head>
<!-- Generated by javadoc (17) -->
<title>Source code</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="source: package: org.apache.hadoop.hbase.ipc, class: CoprocessorRpcUtils">
<meta name="generator" content="javadoc/SourceToHTMLConverter">
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body class="source-page">
<main role="main">
<div class="source-container">
<pre><span class="source-line-no">001</span><span id="line-1">/*</span>
<span class="source-line-no">002</span><span id="line-2"> * Licensed to the Apache Software Foundation (ASF) under one</span>
<span class="source-line-no">003</span><span id="line-3"> * or more contributor license agreements. See the NOTICE file</span>
<span class="source-line-no">004</span><span id="line-4"> * distributed with this work for additional information</span>
<span class="source-line-no">005</span><span id="line-5"> * regarding copyright ownership. The ASF licenses this file</span>
<span class="source-line-no">006</span><span id="line-6"> * to you under the Apache License, Version 2.0 (the</span>
<span class="source-line-no">007</span><span id="line-7"> * "License"); you may not use this file except in compliance</span>
<span class="source-line-no">008</span><span id="line-8"> * with the License. You may obtain a copy of the License at</span>
<span class="source-line-no">009</span><span id="line-9"> *</span>
<span class="source-line-no">010</span><span id="line-10"> * http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="source-line-no">011</span><span id="line-11"> *</span>
<span class="source-line-no">012</span><span id="line-12"> * Unless required by applicable law or agreed to in writing, software</span>
<span class="source-line-no">013</span><span id="line-13"> * distributed under the License is distributed on an "AS IS" BASIS,</span>
<span class="source-line-no">014</span><span id="line-14"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="source-line-no">015</span><span id="line-15"> * See the License for the specific language governing permissions and</span>
<span class="source-line-no">016</span><span id="line-16"> * limitations under the License.</span>
<span class="source-line-no">017</span><span id="line-17"> */</span>
<span class="source-line-no">018</span><span id="line-18">package org.apache.hadoop.hbase.ipc;</span>
<span class="source-line-no">019</span><span id="line-19"></span>
<span class="source-line-no">020</span><span id="line-20">import static org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType.REGION_NAME;</span>
<span class="source-line-no">021</span><span id="line-21"></span>
<span class="source-line-no">022</span><span id="line-22">import edu.umd.cs.findbugs.annotations.Nullable;</span>
<span class="source-line-no">023</span><span id="line-23">import java.io.IOException;</span>
<span class="source-line-no">024</span><span id="line-24">import java.io.InterruptedIOException;</span>
<span class="source-line-no">025</span><span id="line-25">import org.apache.hadoop.hbase.DoNotRetryIOException;</span>
<span class="source-line-no">026</span><span id="line-26">import org.apache.hadoop.hbase.HConstants;</span>
<span class="source-line-no">027</span><span id="line-27">import org.apache.hadoop.hbase.exceptions.UnknownProtocolException;</span>
<span class="source-line-no">028</span><span id="line-28">import org.apache.hadoop.util.StringUtils;</span>
<span class="source-line-no">029</span><span id="line-29">import org.apache.yetus.audience.InterfaceAudience;</span>
<span class="source-line-no">030</span><span id="line-30">import org.slf4j.Logger;</span>
<span class="source-line-no">031</span><span id="line-31">import org.slf4j.LoggerFactory;</span>
<span class="source-line-no">032</span><span id="line-32"></span>
<span class="source-line-no">033</span><span id="line-33">import org.apache.hbase.thirdparty.com.google.protobuf.ByteString;</span>
<span class="source-line-no">034</span><span id="line-34">import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors;</span>
<span class="source-line-no">035</span><span id="line-35">import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.MethodDescriptor;</span>
<span class="source-line-no">036</span><span id="line-36">import org.apache.hbase.thirdparty.com.google.protobuf.Descriptors.ServiceDescriptor;</span>
<span class="source-line-no">037</span><span id="line-37">import org.apache.hbase.thirdparty.com.google.protobuf.Message;</span>
<span class="source-line-no">038</span><span id="line-38">import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;</span>
<span class="source-line-no">039</span><span id="line-39">import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;</span>
<span class="source-line-no">040</span><span id="line-40">import org.apache.hbase.thirdparty.com.google.protobuf.Service;</span>
<span class="source-line-no">041</span><span id="line-41"></span>
<span class="source-line-no">042</span><span id="line-42">import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;</span>
<span class="source-line-no">043</span><span id="line-43">import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter;</span>
<span class="source-line-no">044</span><span id="line-44">import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos;</span>
<span class="source-line-no">045</span><span id="line-45">import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceCall;</span>
<span class="source-line-no">046</span><span id="line-46">import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.CoprocessorServiceRequest;</span>
<span class="source-line-no">047</span><span id="line-47">import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;</span>
<span class="source-line-no">048</span><span id="line-48"></span>
<span class="source-line-no">049</span><span id="line-49">/**</span>
<span class="source-line-no">050</span><span id="line-50"> * Utilities for handling coprocessor rpc service calls.</span>
<span class="source-line-no">051</span><span id="line-51"> */</span>
<span class="source-line-no">052</span><span id="line-52">@InterfaceAudience.Private</span>
<span class="source-line-no">053</span><span id="line-53">public final class CoprocessorRpcUtils {</span>
<span class="source-line-no">054</span><span id="line-54"> private static final Logger LOG = LoggerFactory.getLogger(CoprocessorRpcUtils.class);</span>
<span class="source-line-no">055</span><span id="line-55"> /**</span>
<span class="source-line-no">056</span><span id="line-56"> * We assume that all HBase protobuf services share a common package name (defined in the .proto</span>
<span class="source-line-no">057</span><span id="line-57"> * files).</span>
<span class="source-line-no">058</span><span id="line-58"> */</span>
<span class="source-line-no">059</span><span id="line-59"> private static final String hbaseServicePackage;</span>
<span class="source-line-no">060</span><span id="line-60"> static {</span>
<span class="source-line-no">061</span><span id="line-61"> Descriptors.ServiceDescriptor clientService = ClientProtos.ClientService.getDescriptor();</span>
<span class="source-line-no">062</span><span id="line-62"> hbaseServicePackage = clientService.getFullName().substring(0,</span>
<span class="source-line-no">063</span><span id="line-63"> clientService.getFullName().lastIndexOf(clientService.getName()));</span>
<span class="source-line-no">064</span><span id="line-64"> }</span>
<span class="source-line-no">065</span><span id="line-65"></span>
<span class="source-line-no">066</span><span id="line-66"> private CoprocessorRpcUtils() {</span>
<span class="source-line-no">067</span><span id="line-67"> // private for utility class</span>
<span class="source-line-no">068</span><span id="line-68"> }</span>
<span class="source-line-no">069</span><span id="line-69"></span>
<span class="source-line-no">070</span><span id="line-70"> /**</span>
<span class="source-line-no">071</span><span id="line-71"> * Returns the name to use for coprocessor service calls. For core HBase services (in the hbase.pb</span>
<span class="source-line-no">072</span><span id="line-72"> * protobuf package), this returns the unqualified name in order to provide backward compatibility</span>
<span class="source-line-no">073</span><span id="line-73"> * across the package name change. For all other services, the fully-qualified service name is</span>
<span class="source-line-no">074</span><span id="line-74"> * used.</span>
<span class="source-line-no">075</span><span id="line-75"> */</span>
<span class="source-line-no">076</span><span id="line-76"> public static String getServiceName(Descriptors.ServiceDescriptor service) {</span>
<span class="source-line-no">077</span><span id="line-77"> if (service.getFullName().startsWith(hbaseServicePackage)) {</span>
<span class="source-line-no">078</span><span id="line-78"> return service.getName();</span>
<span class="source-line-no">079</span><span id="line-79"> }</span>
<span class="source-line-no">080</span><span id="line-80"> return service.getFullName();</span>
<span class="source-line-no">081</span><span id="line-81"> }</span>
<span class="source-line-no">082</span><span id="line-82"></span>
<span class="source-line-no">083</span><span id="line-83"> public static CoprocessorServiceRequest</span>
<span class="source-line-no">084</span><span id="line-84"> getCoprocessorServiceRequest(final Descriptors.MethodDescriptor method, final Message request) {</span>
<span class="source-line-no">085</span><span id="line-85"> return getCoprocessorServiceRequest(method, request, HConstants.EMPTY_BYTE_ARRAY,</span>
<span class="source-line-no">086</span><span id="line-86"> HConstants.EMPTY_BYTE_ARRAY);</span>
<span class="source-line-no">087</span><span id="line-87"> }</span>
<span class="source-line-no">088</span><span id="line-88"></span>
<span class="source-line-no">089</span><span id="line-89"> public static CoprocessorServiceRequest getCoprocessorServiceRequest(</span>
<span class="source-line-no">090</span><span id="line-90"> final Descriptors.MethodDescriptor method, final Message request, final byte[] row,</span>
<span class="source-line-no">091</span><span id="line-91"> final byte[] regionName) {</span>
<span class="source-line-no">092</span><span id="line-92"> return CoprocessorServiceRequest.newBuilder()</span>
<span class="source-line-no">093</span><span id="line-93"> .setCall(getCoprocessorServiceCall(method, request, row))</span>
<span class="source-line-no">094</span><span id="line-94"> .setRegion(RequestConverter.buildRegionSpecifier(REGION_NAME, regionName)).build();</span>
<span class="source-line-no">095</span><span id="line-95"> }</span>
<span class="source-line-no">096</span><span id="line-96"></span>
<span class="source-line-no">097</span><span id="line-97"> private static CoprocessorServiceCall getCoprocessorServiceCall(final MethodDescriptor method,</span>
<span class="source-line-no">098</span><span id="line-98"> final Message request, final byte[] row) {</span>
<span class="source-line-no">099</span><span id="line-99"> return CoprocessorServiceCall.newBuilder()</span>
<span class="source-line-no">100</span><span id="line-100"> .setRow(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations.unsafeWrap(row))</span>
<span class="source-line-no">101</span><span id="line-101"> .setServiceName(CoprocessorRpcUtils.getServiceName(method.getService()))</span>
<span class="source-line-no">102</span><span id="line-102"> .setMethodName(method.getName())</span>
<span class="source-line-no">103</span><span id="line-103"> // TODO!!!!! Come back here after!!!!! This is a double copy of the request if I read</span>
<span class="source-line-no">104</span><span id="line-104"> // it right copying from non-shaded to shaded version!!!!!! FIXXXXX!!!!!</span>
<span class="source-line-no">105</span><span id="line-105"> .setRequest(org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations</span>
<span class="source-line-no">106</span><span id="line-106"> .unsafeWrap(request.toByteArray()))</span>
<span class="source-line-no">107</span><span id="line-107"> .build();</span>
<span class="source-line-no">108</span><span id="line-108"> }</span>
<span class="source-line-no">109</span><span id="line-109"></span>
<span class="source-line-no">110</span><span id="line-110"> public static MethodDescriptor getMethodDescriptor(final String methodName,</span>
<span class="source-line-no">111</span><span id="line-111"> final ServiceDescriptor serviceDesc) throws UnknownProtocolException {</span>
<span class="source-line-no">112</span><span id="line-112"> MethodDescriptor methodDesc = serviceDesc.findMethodByName(methodName);</span>
<span class="source-line-no">113</span><span id="line-113"> if (methodDesc == null) {</span>
<span class="source-line-no">114</span><span id="line-114"> throw new UnknownProtocolException(</span>
<span class="source-line-no">115</span><span id="line-115"> "Unknown method " + methodName + " called on service " + serviceDesc.getFullName());</span>
<span class="source-line-no">116</span><span id="line-116"> }</span>
<span class="source-line-no">117</span><span id="line-117"> return methodDesc;</span>
<span class="source-line-no">118</span><span id="line-118"> }</span>
<span class="source-line-no">119</span><span id="line-119"></span>
<span class="source-line-no">120</span><span id="line-120"> public static Message getRequest(Service service, MethodDescriptor methodDesc,</span>
<span class="source-line-no">121</span><span id="line-121"> ByteString shadedRequest) throws IOException {</span>
<span class="source-line-no">122</span><span id="line-122"> Message.Builder builderForType = service.getRequestPrototype(methodDesc).newBuilderForType();</span>
<span class="source-line-no">123</span><span id="line-123"> ProtobufUtil.mergeFrom(builderForType,</span>
<span class="source-line-no">124</span><span id="line-124"> // TODO: COPY FROM SHADED TO NON_SHADED. DO I HAVE TOO?</span>
<span class="source-line-no">125</span><span id="line-125"> shadedRequest.toByteArray());</span>
<span class="source-line-no">126</span><span id="line-126"> return builderForType.build();</span>
<span class="source-line-no">127</span><span id="line-127"> }</span>
<span class="source-line-no">128</span><span id="line-128"></span>
<span class="source-line-no">129</span><span id="line-129"> public static Message getResponse(ClientProtos.CoprocessorServiceResponse result,</span>
<span class="source-line-no">130</span><span id="line-130"> Message responsePrototype) throws IOException {</span>
<span class="source-line-no">131</span><span id="line-131"> Message response;</span>
<span class="source-line-no">132</span><span id="line-132"> if (result.getValue().hasValue()) {</span>
<span class="source-line-no">133</span><span id="line-133"> Message.Builder builder = responsePrototype.newBuilderForType();</span>
<span class="source-line-no">134</span><span id="line-134"> builder.mergeFrom(result.getValue().getValue().newInput());</span>
<span class="source-line-no">135</span><span id="line-135"> response = builder.build();</span>
<span class="source-line-no">136</span><span id="line-136"> } else {</span>
<span class="source-line-no">137</span><span id="line-137"> response = responsePrototype.getDefaultInstanceForType();</span>
<span class="source-line-no">138</span><span id="line-138"> }</span>
<span class="source-line-no">139</span><span id="line-139"> if (LOG.isTraceEnabled()) {</span>
<span class="source-line-no">140</span><span id="line-140"> LOG.trace("Master Result is value=" + response);</span>
<span class="source-line-no">141</span><span id="line-141"> }</span>
<span class="source-line-no">142</span><span id="line-142"> return response;</span>
<span class="source-line-no">143</span><span id="line-143"> }</span>
<span class="source-line-no">144</span><span id="line-144"></span>
<span class="source-line-no">145</span><span id="line-145"> public static ClientProtos.CoprocessorServiceResponse getResponse(final Message result,</span>
<span class="source-line-no">146</span><span id="line-146"> final byte[] regionName) {</span>
<span class="source-line-no">147</span><span id="line-147"> ClientProtos.CoprocessorServiceResponse.Builder builder =</span>
<span class="source-line-no">148</span><span id="line-148"> ClientProtos.CoprocessorServiceResponse.newBuilder();</span>
<span class="source-line-no">149</span><span id="line-149"> builder.setRegion(</span>
<span class="source-line-no">150</span><span id="line-150"> RequestConverter.buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName));</span>
<span class="source-line-no">151</span><span id="line-151"> // TODO: UGLY COPY IN HERE!!!!</span>
<span class="source-line-no">152</span><span id="line-152"> builder.setValue(builder.getValueBuilder().setName(result.getClass().getName()).setValue(</span>
<span class="source-line-no">153</span><span id="line-153"> org.apache.hbase.thirdparty.com.google.protobuf.ByteString.copyFrom(result.toByteArray())));</span>
<span class="source-line-no">154</span><span id="line-154"> return builder.build();</span>
<span class="source-line-no">155</span><span id="line-155"> }</span>
<span class="source-line-no">156</span><span id="line-156"></span>
<span class="source-line-no">157</span><span id="line-157"> /**</span>
<span class="source-line-no">158</span><span id="line-158"> * Simple {@link RpcCallback} implementation providing a {@link java.util.concurrent.Future}-like</span>
<span class="source-line-no">159</span><span id="line-159"> * {@link BlockingRpcCallback#get()} method, which will block util the instance's</span>
<span class="source-line-no">160</span><span id="line-160"> * {@link BlockingRpcCallback#run(Object)} method has been called. {@code R} is the RPC response</span>
<span class="source-line-no">161</span><span id="line-161"> * type that will be passed to the {@link #run(Object)} method.</span>
<span class="source-line-no">162</span><span id="line-162"> */</span>
<span class="source-line-no">163</span><span id="line-163"> @InterfaceAudience.Private</span>
<span class="source-line-no">164</span><span id="line-164"> // Copy of BlockingRpcCallback but deriving from RpcCallback non-shaded.</span>
<span class="source-line-no">165</span><span id="line-165"> public static class BlockingRpcCallback&lt;R&gt; implements RpcCallback&lt;R&gt; {</span>
<span class="source-line-no">166</span><span id="line-166"> private R result;</span>
<span class="source-line-no">167</span><span id="line-167"> private boolean resultSet = false;</span>
<span class="source-line-no">168</span><span id="line-168"></span>
<span class="source-line-no">169</span><span id="line-169"> /**</span>
<span class="source-line-no">170</span><span id="line-170"> * Called on completion of the RPC call with the response object, or {@code null} in the case of</span>
<span class="source-line-no">171</span><span id="line-171"> * an error.</span>
<span class="source-line-no">172</span><span id="line-172"> * @param parameter the response object or {@code null} if an error occurred</span>
<span class="source-line-no">173</span><span id="line-173"> */</span>
<span class="source-line-no">174</span><span id="line-174"> @Override</span>
<span class="source-line-no">175</span><span id="line-175"> public void run(R parameter) {</span>
<span class="source-line-no">176</span><span id="line-176"> synchronized (this) {</span>
<span class="source-line-no">177</span><span id="line-177"> result = parameter;</span>
<span class="source-line-no">178</span><span id="line-178"> resultSet = true;</span>
<span class="source-line-no">179</span><span id="line-179"> this.notifyAll();</span>
<span class="source-line-no">180</span><span id="line-180"> }</span>
<span class="source-line-no">181</span><span id="line-181"> }</span>
<span class="source-line-no">182</span><span id="line-182"></span>
<span class="source-line-no">183</span><span id="line-183"> /**</span>
<span class="source-line-no">184</span><span id="line-184"> * Returns the parameter passed to {@link #run(Object)} or {@code null} if a null value was</span>
<span class="source-line-no">185</span><span id="line-185"> * passed. When used asynchronously, this method will block until the {@link #run(Object)}</span>
<span class="source-line-no">186</span><span id="line-186"> * method has been called.</span>
<span class="source-line-no">187</span><span id="line-187"> * @return the response object or {@code null} if no response was passed</span>
<span class="source-line-no">188</span><span id="line-188"> */</span>
<span class="source-line-no">189</span><span id="line-189"> public synchronized R get() throws IOException {</span>
<span class="source-line-no">190</span><span id="line-190"> while (!resultSet) {</span>
<span class="source-line-no">191</span><span id="line-191"> try {</span>
<span class="source-line-no">192</span><span id="line-192"> this.wait();</span>
<span class="source-line-no">193</span><span id="line-193"> } catch (InterruptedException ie) {</span>
<span class="source-line-no">194</span><span id="line-194"> InterruptedIOException exception = new InterruptedIOException(ie.getMessage());</span>
<span class="source-line-no">195</span><span id="line-195"> exception.initCause(ie);</span>
<span class="source-line-no">196</span><span id="line-196"> throw exception;</span>
<span class="source-line-no">197</span><span id="line-197"> }</span>
<span class="source-line-no">198</span><span id="line-198"> }</span>
<span class="source-line-no">199</span><span id="line-199"> return result;</span>
<span class="source-line-no">200</span><span id="line-200"> }</span>
<span class="source-line-no">201</span><span id="line-201"> }</span>
<span class="source-line-no">202</span><span id="line-202"></span>
<span class="source-line-no">203</span><span id="line-203"> /**</span>
<span class="source-line-no">204</span><span id="line-204"> * Stores an exception encountered during RPC invocation so it can be passed back through to the</span>
<span class="source-line-no">205</span><span id="line-205"> * client.</span>
<span class="source-line-no">206</span><span id="line-206"> * @param controller the controller instance provided by the client when calling the service</span>
<span class="source-line-no">207</span><span id="line-207"> * @param ioe the exception encountered</span>
<span class="source-line-no">208</span><span id="line-208"> */</span>
<span class="source-line-no">209</span><span id="line-209"> public static void setControllerException(RpcController controller, IOException ioe) {</span>
<span class="source-line-no">210</span><span id="line-210"> if (controller == null) {</span>
<span class="source-line-no">211</span><span id="line-211"> return;</span>
<span class="source-line-no">212</span><span id="line-212"> }</span>
<span class="source-line-no">213</span><span id="line-213"> if (controller instanceof org.apache.hadoop.hbase.ipc.ServerRpcController) {</span>
<span class="source-line-no">214</span><span id="line-214"> ((ServerRpcController) controller).setFailedOn(ioe);</span>
<span class="source-line-no">215</span><span id="line-215"> } else {</span>
<span class="source-line-no">216</span><span id="line-216"> controller.setFailed(StringUtils.stringifyException(ioe));</span>
<span class="source-line-no">217</span><span id="line-217"> }</span>
<span class="source-line-no">218</span><span id="line-218"> }</span>
<span class="source-line-no">219</span><span id="line-219"></span>
<span class="source-line-no">220</span><span id="line-220"> /**</span>
<span class="source-line-no">221</span><span id="line-221"> * Retreivies exception stored during RPC invocation.</span>
<span class="source-line-no">222</span><span id="line-222"> * @param controller the controller instance provided by the client when calling the service</span>
<span class="source-line-no">223</span><span id="line-223"> * @return exception if any, or null; Will return DoNotRetryIOException for string represented</span>
<span class="source-line-no">224</span><span id="line-224"> * failure causes in controller.</span>
<span class="source-line-no">225</span><span id="line-225"> */</span>
<span class="source-line-no">226</span><span id="line-226"> @Nullable</span>
<span class="source-line-no">227</span><span id="line-227"> public static IOException getControllerException(RpcController controller) throws IOException {</span>
<span class="source-line-no">228</span><span id="line-228"> if (controller == null || !controller.failed()) {</span>
<span class="source-line-no">229</span><span id="line-229"> return null;</span>
<span class="source-line-no">230</span><span id="line-230"> }</span>
<span class="source-line-no">231</span><span id="line-231"> if (controller instanceof ServerRpcController) {</span>
<span class="source-line-no">232</span><span id="line-232"> return ((ServerRpcController) controller).getFailedOn();</span>
<span class="source-line-no">233</span><span id="line-233"> }</span>
<span class="source-line-no">234</span><span id="line-234"> return new DoNotRetryIOException(controller.errorText());</span>
<span class="source-line-no">235</span><span id="line-235"> }</span>
<span class="source-line-no">236</span><span id="line-236">}</span>
</pre>
</div>
</main>
</body>
</html>