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