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 */
018package org.apache.hadoop.hbase.ipc;
019
020import com.google.protobuf.RpcCallback;
021import com.google.protobuf.RpcController;
022import java.io.IOException;
023import org.apache.hadoop.util.StringUtils;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Used for server-side protobuf RPC service invocations. This handler allows invocation exceptions
028 * to easily be passed through to the RPC server from coprocessor
029 * {@link com.google.protobuf.Service} implementations.
030 * <p>
031 * When implementing {@link com.google.protobuf.Service} defined methods, coprocessor endpoints can
032 * use the following pattern to pass exceptions back to the RPC client: <code>
033 * public void myMethod(RpcController controller, MyRequest request,
034 *     RpcCallback&lt;MyResponse&gt; done) {
035 *   MyResponse response = null;
036 *   try {
037 *     // do processing
038 *     response = MyResponse.getDefaultInstance();  // or use a new builder to populate the response
039 *   } catch (IOException ioe) {
040 *     // pass exception back up
041 *     ResponseConverter.setControllerException(controller, ioe);
042 *   }
043 *   done.run(response);
044 * }
045 * </code>
046 * </p>
047 */
048@InterfaceAudience.Private
049public class ServerRpcController implements RpcController {
050  /**
051   * The exception thrown within
052   * {@link com.google.protobuf.Service#callMethod(com.google.protobuf.Descriptors.MethodDescriptor, RpcController, com.google.protobuf.Message, RpcCallback)}
053   * if any.
054   */
055  // TODO: it would be good widen this to just Throwable, but IOException is what we allow now
056  private IOException serviceException;
057  private String errorMessage;
058
059  @Override
060  public void reset() {
061    serviceException = null;
062    errorMessage = null;
063  }
064
065  @Override
066  public boolean failed() {
067    return (failedOnException() || errorMessage != null);
068  }
069
070  @Override
071  public String errorText() {
072    return errorMessage;
073  }
074
075  @Override
076  public void startCancel() {
077    // not implemented
078  }
079
080  @Override
081  public void setFailed(String message) {
082    errorMessage = message;
083  }
084
085  @Override
086  public boolean isCanceled() {
087    return false;
088  }
089
090  @Override
091  public void notifyOnCancel(RpcCallback<Object> objectRpcCallback) {
092    // not implemented
093  }
094
095  /**
096   * Sets an exception to be communicated back to the {@link com.google.protobuf.Service} client.
097   * @param ioe the exception encountered during execution of the service method
098   */
099  public void setFailedOn(IOException ioe) {
100    serviceException = ioe;
101    setFailed(StringUtils.stringifyException(ioe));
102  }
103
104  /**
105   * Returns any exception thrown during service method invocation, or {@code null} if no exception
106   * was thrown. This can be used by clients to receive exceptions generated by RPC calls, even when
107   * {@link RpcCallback}s are used and no {@link com.google.protobuf.ServiceException} is declared.
108   */
109  public IOException getFailedOn() {
110    return serviceException;
111  }
112
113  /**
114   * Returns whether or not a server exception was generated in the prior RPC invocation.
115   */
116  public boolean failedOnException() {
117    return serviceException != null;
118  }
119
120  /**
121   * Throws an IOException back out if one is currently stored.
122   */
123  public void checkFailed() throws IOException {
124    if (failedOnException()) {
125      throw getFailedOn();
126    }
127  }
128}