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.client;
019
020import com.google.protobuf.Descriptors;
021import com.google.protobuf.Message;
022import com.google.protobuf.RpcCallback;
023import com.google.protobuf.RpcController;
024import com.google.protobuf.ServiceException;
025
026import java.io.IOException;
027
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031import org.apache.hadoop.hbase.ipc.CoprocessorRpcChannel;
032import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
033
034/**
035 * Base class which provides clients with an RPC connection to
036 * call coprocessor endpoint {@link com.google.protobuf.Service}s.
037 * Note that clients should not use this class directly, except through
038 * {@link org.apache.hadoop.hbase.client.Table#coprocessorService(byte[])}.
039 */
040@InterfaceAudience.Public
041abstract class SyncCoprocessorRpcChannel implements CoprocessorRpcChannel {
042  private static final Logger LOG = LoggerFactory.getLogger(SyncCoprocessorRpcChannel.class);
043
044  @Override
045  @InterfaceAudience.Private
046  public void callMethod(Descriptors.MethodDescriptor method,
047                         RpcController controller,
048                         Message request, Message responsePrototype,
049                         RpcCallback<Message> callback) {
050    Message response = null;
051    try {
052      response = callExecService(controller, method, request, responsePrototype);
053    } catch (IOException ioe) {
054      LOG.warn("Call failed on IOException", ioe);
055      CoprocessorRpcUtils.setControllerException(controller, ioe);
056    }
057    if (callback != null) {
058      callback.run(response);
059    }
060  }
061
062  @Override
063  @InterfaceAudience.Private
064  public Message callBlockingMethod(Descriptors.MethodDescriptor method,
065                                    RpcController controller,
066                                    Message request, Message responsePrototype)
067      throws ServiceException {
068    try {
069      return callExecService(controller, method, request, responsePrototype);
070    } catch (IOException ioe) {
071      throw new ServiceException("Error calling method "+method.getFullName(), ioe);
072    }
073  }
074
075  protected abstract Message callExecService(RpcController controller,
076      Descriptors.MethodDescriptor method, Message request, Message responsePrototype)
077          throws IOException;
078}