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