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 */
018
019package org.apache.hadoop.hbase.ipc;
020
021import com.google.protobuf.RpcCallback;
022import com.google.protobuf.RpcController;
023
024import java.io.IOException;
025
026import org.apache.yetus.audience.InterfaceAudience;
027import org.apache.hadoop.util.StringUtils;
028
029/**
030 * Used for server-side protobuf RPC service invocations.  This handler allows
031 * invocation exceptions to easily be passed through to the RPC server from coprocessor
032 * {@link com.google.protobuf.Service} implementations.
033 *
034 * <p>
035 * When implementing {@link com.google.protobuf.Service} defined methods, 
036 * coprocessor endpoints can use the following pattern to pass exceptions back to the RPC client:
037 * <code>
038 * public void myMethod(RpcController controller, MyRequest request,
039 *     RpcCallback&lt;MyResponse&gt; done) {
040 *   MyResponse response = null;
041 *   try {
042 *     // do processing
043 *     response = MyResponse.getDefaultInstance();  // or use a new builder to populate the response
044 *   } catch (IOException ioe) {
045 *     // pass exception back up
046 *     ResponseConverter.setControllerException(controller, ioe);
047 *   }
048 *   done.run(response);
049 * }
050 * </code>
051 * </p>
052 */
053@InterfaceAudience.Private
054public class ServerRpcController implements RpcController {
055  /**
056   * The exception thrown within
057   * {@link com.google.protobuf.Service#callMethod(com.google.protobuf.Descriptors.MethodDescriptor, RpcController,
058   * com.google.protobuf.Message, RpcCallback)}
059   * if any.
060   */
061  // TODO: it would be good widen this to just Throwable, but IOException is what we allow now
062  private IOException serviceException;
063  private String errorMessage;
064
065  @Override
066  public void reset() {
067    serviceException = null;
068    errorMessage = null;
069  }
070
071  @Override
072  public boolean failed() {
073    return (failedOnException() || errorMessage != null);
074  }
075
076  @Override
077  public String errorText() {
078    return errorMessage;
079  }
080
081  @Override
082  public void startCancel() {
083    // not implemented
084  }
085
086  @Override
087  public void setFailed(String message) {
088    errorMessage = message;
089  }
090
091  @Override
092  public boolean isCanceled() {
093    return false;
094  }
095
096  @Override
097  public void notifyOnCancel(RpcCallback<Object> objectRpcCallback) {
098    // not implemented
099  }
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}