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