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