View Javadoc

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