View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.ipc;
13  
14  import java.io.IOException;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.apache.hadoop.hbase.classification.InterfaceAudience;
19  import org.apache.hadoop.hbase.HConstants;
20  import org.apache.hadoop.hbase.ServerName;
21  import org.apache.hadoop.hbase.client.ClusterConnection;
22  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
23  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
24  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
25  import org.apache.hadoop.hbase.util.ByteStringer;
26  
27  
28  import com.google.protobuf.Descriptors;
29  import com.google.protobuf.Message;
30  import com.google.protobuf.RpcController;
31  
32  /**
33   * Provides clients with an RPC connection to call coprocessor endpoint
34   * {@link com.google.protobuf.Service}s against a given region server. An instance of this class may
35   * be obtained by calling {@link org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService(ServerName)},
36   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to
37   * call the endpoint methods.
38   * @see org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService(ServerName)
39   */
40  @InterfaceAudience.Private
41  public class RegionServerCoprocessorRpcChannel extends CoprocessorRpcChannel {
42    private static final Log LOG = LogFactory.getLog(RegionServerCoprocessorRpcChannel.class);
43    private final ClusterConnection connection;
44    private final ServerName serverName;
45  
46    public RegionServerCoprocessorRpcChannel(ClusterConnection conn, ServerName serverName) {
47      this.connection = conn;
48      this.serverName = serverName;
49    }
50  
51    @Override
52    protected Message callExecService(RpcController controller,
53        Descriptors.MethodDescriptor method, Message request, Message responsePrototype)
54            throws IOException {
55      if (LOG.isTraceEnabled()) {
56        LOG.trace("Call: " + method.getName() + ", " + request.toString());
57      }
58      final ClientProtos.CoprocessorServiceCall call =
59          ClientProtos.CoprocessorServiceCall.newBuilder()
60              .setRow(ByteStringer.wrap(HConstants.EMPTY_BYTE_ARRAY))
61              .setServiceName(method.getService().getFullName()).setMethodName(method.getName())
62              .setRequest(request.toByteString()).build();
63  
64      // TODO: Are we retrying here? Does not seem so. We should use RetryingRpcCaller
65      CoprocessorServiceResponse result =
66          ProtobufUtil.execRegionServerService(controller, connection.getClient(serverName), call);
67      Message response = null;
68      if (result.getValue().hasValue()) {
69        Message.Builder builder = responsePrototype.newBuilderForType();
70        ProtobufUtil.mergeFrom(builder, result.getValue().getValue());
71        response = builder.build();
72      } else {
73        response = responsePrototype.getDefaultInstanceForType();
74      }
75      if (LOG.isTraceEnabled()) {
76        LOG.trace("Result is value=" + response);
77      }
78      return response;
79    }
80  }