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 java.io.IOException;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.util.StringUtils;
25
26 import com.google.protobuf.RpcCallback;
27 import com.google.protobuf.RpcController;
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 com.google.protobuf.Service} implementations.
36 *
37 * <p>
38 * When implementing {@link com.google.protobuf.Service} defined methods,
39 * coprocessor endpoints can use the following
40 * pattern to pass exceptions back to the RPC client:
41 * <code>
42 * public void myMethod(RpcController controller, MyRequest request, RpcCallback<MyResponse> done) {
43 * MyResponse response = null;
44 * try {
45 * // do processing
46 * response = MyResponse.getDefaultInstance(); // or use a new builder to populate the response
47 * } catch (IOException ioe) {
48 * // pass exception back up
49 * ResponseConverter.setControllerException(controller, ioe);
50 * }
51 * done.run(response);
52 * }
53 * </code>
54 * </p>
55 */
56 @InterfaceAudience.Private
57 public class ServerRpcController implements RpcController {
58 /**
59 * The exception thrown within
60 * {@link com.google.protobuf.Service#callMethod(
61 * Descriptors.MethodDescriptor, RpcController, Message, RpcCallback)},
62 * if any.
63 */
64 // TODO: it would be good widen this to just Throwable, but IOException is what we allow now
65 private IOException serviceException;
66 private String errorMessage;
67
68 @Override
69 public void reset() {
70 serviceException = null;
71 errorMessage = null;
72 }
73
74 @Override
75 public boolean failed() {
76 return (failedOnException() || errorMessage != null);
77 }
78
79 @Override
80 public String errorText() {
81 return errorMessage;
82 }
83
84 @Override
85 public void startCancel() {
86 // not implemented
87 }
88
89 @Override
90 public void setFailed(String message) {
91 errorMessage = message;
92 }
93
94 @Override
95 public boolean isCanceled() {
96 return false;
97 }
98
99 @Override
100 public void notifyOnCancel(RpcCallback<Object> objectRpcCallback) {
101 // not implemented
102 }
103
104 /**
105 * Sets an exception to be communicated back to the {@link com.google.protobuf.Service} client.
106 * @param ioe the exception encountered during execution of the service method
107 */
108 public void setFailedOn(IOException ioe) {
109 serviceException = ioe;
110 setFailed(StringUtils.stringifyException(ioe));
111 }
112
113 /**
114 * Returns any exception thrown during service method invocation, or {@code null} if no exception
115 * was thrown. This can be used by clients to receive exceptions generated by RPC calls, even
116 * when {@link RpcCallback}s are used and no {@link com.google.protobuf.ServiceException} is
117 * declared.
118 */
119 public IOException getFailedOn() {
120 return serviceException;
121 }
122
123 /**
124 * Returns whether or not a server exception was generated in the prior RPC invocation.
125 */
126 public boolean failedOnException() {
127 return serviceException != null;
128 }
129
130 /**
131 * Throws an IOException back out if one is currently stored.
132 */
133 public void checkFailed() throws IOException {
134 if (failedOnException()) {
135 throw getFailedOn();
136 }
137 }
138 }