001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import java.io.IOException; 021import org.apache.hadoop.util.StringUtils; 022import org.apache.yetus.audience.InterfaceAudience; 023 024import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 025import org.apache.hbase.thirdparty.com.google.protobuf.RpcController; 026 027/** 028 * Used for server-side protobuf RPC service invocations. This handler allows invocation exceptions 029 * to easily be passed through to the RPC server from coprocessor 030 * {@link org.apache.hbase.thirdparty.com.google.protobuf.Service} implementations. 031 * <p/> 032 * When implementing {@link org.apache.hbase.thirdparty.com.google.protobuf.Service} defined 033 * methods, coprocessor endpoints can use the following pattern to pass exceptions back to the RPC 034 * client: <code> 035 * public void myMethod(RpcController controller, MyRequest request, 036 * RpcCallback<MyResponse> done) { 037 * MyResponse response = null; 038 * try { 039 * // do processing 040 * response = MyResponse.getDefaultInstance(); // or use a new builder to populate the response 041 * } catch (IOException ioe) { 042 * // pass exception back up 043 * ResponseConverter.setControllerException(controller, ioe); 044 * } 045 * done.run(response); 046 * } 047 * </code> 048 */ 049@InterfaceAudience.Private 050public class ServerRpcController implements RpcController { 051 /** 052 * The exception thrown within 053 * {@link com.google.protobuf.Service#callMethod(com.google.protobuf.Descriptors.MethodDescriptor, RpcController, com.google.protobuf.Message, RpcCallback)} 054 * if any. 055 */ 056 // TODO: it would be good widen this to just Throwable, but IOException is what we allow now 057 private IOException serviceException; 058 private String errorMessage; 059 060 @Override 061 public void reset() { 062 serviceException = null; 063 errorMessage = null; 064 } 065 066 @Override 067 public boolean failed() { 068 return (failedOnException() || errorMessage != null); 069 } 070 071 @Override 072 public String errorText() { 073 return errorMessage; 074 } 075 076 @Override 077 public void startCancel() { 078 // not implemented 079 } 080 081 @Override 082 public void setFailed(String message) { 083 errorMessage = message; 084 } 085 086 @Override 087 public boolean isCanceled() { 088 return false; 089 } 090 091 @Override 092 public void notifyOnCancel(RpcCallback<Object> objectRpcCallback) { 093 // not implemented 094 } 095 096 /** 097 * Sets an exception to be communicated back to the 098 * {@link org.apache.hbase.thirdparty.com.google.protobuf.Service} client. 099 * @param ioe the exception encountered during execution of the service method 100 */ 101 public void setFailedOn(IOException ioe) { 102 serviceException = ioe; 103 setFailed(StringUtils.stringifyException(ioe)); 104 } 105 106 /** 107 * Returns any exception thrown during service method invocation, or {@code null} if no exception 108 * was thrown. This can be used by clients to receive exceptions generated by RPC calls, even when 109 * {@link RpcCallback}s are used and no 110 * {@link org.apache.hbase.thirdparty.com.google.protobuf.ServiceException} is declared. 111 */ 112 public IOException getFailedOn() { 113 return serviceException; 114 } 115 116 /** 117 * Returns whether or not a server exception was generated in the prior RPC invocation. 118 */ 119 public boolean failedOnException() { 120 return serviceException != null; 121 } 122 123 /** 124 * Throws an IOException back out if one is currently stored. 125 */ 126 public void checkFailed() throws IOException { 127 if (failedOnException()) { 128 throw getFailedOn(); 129 } 130 } 131}