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.HConnection;
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  
31  /**
32   * Provides clients with an RPC connection to call coprocessor endpoint
33   * {@link com.google.protobuf.Service}s against a given region server. An instance of this class may
34   * be obtained by calling {@link org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService(ServerName)},
35   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to
36   * call the endpoint methods.
37   * @see org.apache.hadoop.hbase.client.HBaseAdmin#coprocessorService(ServerName)
38   */
39  @InterfaceAudience.Private
40  public class RegionServerCoprocessorRpcChannel extends CoprocessorRpcChannel {
41    private static Log LOG = LogFactory.getLog(RegionServerCoprocessorRpcChannel.class);
42    private final HConnection connection;
43    private final ServerName serverName;
44  
45    public RegionServerCoprocessorRpcChannel(HConnection conn, ServerName serverName) {
46      this.connection = conn;
47      this.serverName = serverName;
48    }
49  
50    @Override
51    protected Message callExecService(Descriptors.MethodDescriptor method, Message request,
52        Message responsePrototype) throws IOException {
53      if (LOG.isTraceEnabled()) {
54        LOG.trace("Call: " + method.getName() + ", " + request.toString());
55      }
56  
57      final ClientProtos.CoprocessorServiceCall call =
58          ClientProtos.CoprocessorServiceCall.newBuilder()
59              .setRow(ByteStringer.wrap(HConstants.EMPTY_BYTE_ARRAY))
60              .setServiceName(method.getService().getFullName()).setMethodName(method.getName())
61              .setRequest(request.toByteString()).build();
62      CoprocessorServiceResponse result =
63          ProtobufUtil.execRegionServerService(connection.getClient(serverName), call);
64      Message response = null;
65      if (result.getValue().hasValue()) {
66        response =
67            responsePrototype.newBuilderForType().mergeFrom(result.getValue().getValue()).build();
68      } else {
69        response = responsePrototype.getDefaultInstanceForType();
70      }
71      if (LOG.isTraceEnabled()) {
72        LOG.trace("Result is value=" + response);
73      }
74      return response;
75    }
76  }