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