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.BlockingRpcChannel;
22  import com.google.protobuf.Descriptors;
23  import com.google.protobuf.Message;
24  import com.google.protobuf.RpcCallback;
25  import com.google.protobuf.RpcChannel;
26  import com.google.protobuf.RpcController;
27  import com.google.protobuf.Service;
28  import com.google.protobuf.ServiceException;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.classification.InterfaceAudience;
32  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
33  
34  import java.io.IOException;
35  
36  /**
37   * Base class which provides clients with an RPC connection to
38   * call coprocessor endpoint {@link Service}s
39   */
40  @InterfaceAudience.Private
41  public abstract class CoprocessorRpcChannel implements RpcChannel, BlockingRpcChannel {
42    private static Log LOG = LogFactory.getLog(CoprocessorRpcChannel.class);
43  
44    @Override
45    public void callMethod(Descriptors.MethodDescriptor method,
46                           RpcController controller,
47                           Message request, Message responsePrototype,
48                           RpcCallback<Message> callback) {
49      Message response = null;
50      try {
51        response = callExecService(method, request, responsePrototype);
52      } catch (IOException ioe) {
53        LOG.warn("Call failed on IOException", ioe);
54        ResponseConverter.setControllerException(controller, ioe);
55      }
56      if (callback != null) {
57        callback.run(response);
58      }
59    }
60  
61    @Override
62    public Message callBlockingMethod(Descriptors.MethodDescriptor method,
63                                      RpcController controller,
64                                      Message request, Message responsePrototype)
65        throws ServiceException {
66      try {
67        return callExecService(method, request, responsePrototype);
68      } catch (IOException ioe) {
69        throw new ServiceException("Error calling method "+method.getFullName(), ioe);
70      }
71    }
72  
73    protected abstract Message callExecService(Descriptors.MethodDescriptor method,
74                                    Message request, Message responsePrototype) throws IOException;
75  }