1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.hbase.ipc; 20 21 import com.google.protobuf.Descriptors; 22 import com.google.protobuf.Message; 23 import com.google.protobuf.RpcCallback; 24 import com.google.protobuf.RpcController; 25 import com.google.protobuf.Service; 26 27 import org.apache.hadoop.hbase.classification.InterfaceAudience; 28 import org.apache.hadoop.util.StringUtils; 29 30 import java.io.IOException; 31 32 /** 33 * Used for server-side protobuf RPC service invocations. This handler allows 34 * invocation exceptions to easily be passed through to the RPC server from coprocessor 35 * {@link Service} implementations. 36 * 37 * <p> 38 * When implementing {@link Service} defined methods, coprocessor endpoints can use the following 39 * pattern to pass exceptions back to the RPC client: 40 * <code> 41 * public void myMethod(RpcController controller, MyRequest request, RpcCallback<MyResponse> done) { 42 * MyResponse response = null; 43 * try { 44 * // do processing 45 * response = MyResponse.getDefaultInstance(); // or use a new builder to populate the response 46 * } catch (IOException ioe) { 47 * // pass exception back up 48 * ResponseConverter.setControllerException(controller, ioe); 49 * } 50 * done.run(response); 51 * } 52 * </code> 53 * </p> 54 */ 55 @InterfaceAudience.Private 56 public class ServerRpcController implements RpcController { 57 /** 58 * The exception thrown within 59 * {@link Service#callMethod(Descriptors.MethodDescriptor, RpcController, Message, RpcCallback)}, 60 * if any. 61 */ 62 // TODO: it would be good widen this to just Throwable, but IOException is what we allow now 63 private IOException serviceException; 64 private String errorMessage; 65 66 @Override 67 public void reset() { 68 serviceException = null; 69 errorMessage = null; 70 } 71 72 @Override 73 public boolean failed() { 74 return (failedOnException() || errorMessage != null); 75 } 76 77 @Override 78 public String errorText() { 79 return errorMessage; 80 } 81 82 @Override 83 public void startCancel() { 84 // not implemented 85 } 86 87 @Override 88 public void setFailed(String message) { 89 errorMessage = message; 90 } 91 92 @Override 93 public boolean isCanceled() { 94 return false; 95 } 96 97 @Override 98 public void notifyOnCancel(RpcCallback<Object> objectRpcCallback) { 99 // not implemented 100 } 101 102 /** 103 * Sets an exception to be communicated back to the {@link Service} client. 104 * @param ioe the exception encountered during execution of the service method 105 */ 106 public void setFailedOn(IOException ioe) { 107 serviceException = ioe; 108 setFailed(StringUtils.stringifyException(ioe)); 109 } 110 111 /** 112 * Returns any exception thrown during service method invocation, or {@code null} if no exception 113 * was thrown. This can be used by clients to receive exceptions generated by RPC calls, even 114 * when {@link RpcCallback}s are used and no {@link com.google.protobuf.ServiceException} is 115 * declared. 116 */ 117 public IOException getFailedOn() { 118 return serviceException; 119 } 120 121 /** 122 * Returns whether or not a server exception was generated in the prior RPC invocation. 123 */ 124 public boolean failedOnException() { 125 return serviceException != null; 126 } 127 128 /** 129 * Throws an IOException back out if one is currently stored. 130 */ 131 public void checkFailed() throws IOException { 132 if (failedOnException()) { 133 throw getFailedOn(); 134 } 135 } 136 }