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.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.HConnection;
28  import org.apache.hadoop.hbase.client.RegionServerCallable;
29  import org.apache.hadoop.hbase.client.RpcRetryingCallerFactory;
30  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
31  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
32  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  import com.google.protobuf.ByteString;
36  import com.google.protobuf.Descriptors;
37  import com.google.protobuf.Message;
38  
39  /**
40   * Provides clients with an RPC connection to call coprocessor endpoint {@link com.google.protobuf.Service}s
41   * against a given table region.  An instance of this class may be obtained
42   * by calling {@link org.apache.hadoop.hbase.client.HTable#coprocessorService(byte[])},
43   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to call the endpoint
44   * methods.
45   * @see org.apache.hadoop.hbase.client.HTable#coprocessorService(byte[])
46   */
47  @InterfaceAudience.Private
48  public class RegionCoprocessorRpcChannel extends CoprocessorRpcChannel{
49    private static Log LOG = LogFactory.getLog(RegionCoprocessorRpcChannel.class);
50  
51    private final HConnection connection;
52    private final TableName table;
53    private final byte[] row;
54    private byte[] lastRegion;
55  
56    private RpcRetryingCallerFactory rpcFactory;
57  
58    public RegionCoprocessorRpcChannel(HConnection conn, TableName table, byte[] row) {
59      this.connection = conn;
60      this.table = table;
61      this.row = row;
62      this.rpcFactory = RpcRetryingCallerFactory.instantiate(conn.getConfiguration());
63    }
64  
65    @Override
66    protected Message callExecService(Descriptors.MethodDescriptor method,
67                                    Message request, Message responsePrototype)
68        throws IOException {
69      if (LOG.isTraceEnabled()) {
70        LOG.trace("Call: "+method.getName()+", "+request.toString());
71      }
72  
73      if (row == null) {
74        throw new IllegalArgumentException("Missing row property for remote region location");
75      }
76  
77      final ClientProtos.CoprocessorServiceCall call =
78          ClientProtos.CoprocessorServiceCall.newBuilder()
79              .setRow(ByteString.copyFrom(row))
80              .setServiceName(method.getService().getFullName())
81              .setMethodName(method.getName())
82              .setRequest(request.toByteString()).build();
83      RegionServerCallable<CoprocessorServiceResponse> callable =
84          new RegionServerCallable<CoprocessorServiceResponse>(connection, table, row) {
85            public CoprocessorServiceResponse call() throws Exception {
86              byte[] regionName = getLocation().getRegionInfo().getRegionName();
87              return ProtobufUtil.execService(getStub(), call, regionName);
88            }
89          };
90      CoprocessorServiceResponse result = rpcFactory.<CoprocessorServiceResponse> newCaller()
91          .callWithRetries(callable);
92      Message response = null;
93      if (result.getValue().hasValue()) {
94        response = responsePrototype.newBuilderForType()
95            .mergeFrom(result.getValue().getValue()).build();
96      } else {
97        response = responsePrototype.getDefaultInstanceForType();
98      }
99      lastRegion = result.getRegion().getValue().toByteArray();
100     if (LOG.isTraceEnabled()) {
101       LOG.trace("Result is region=" + Bytes.toStringBinary(lastRegion) + ", value=" + response);
102     }
103     return response;
104   }
105 
106   public byte[] getLastRegion() {
107     return lastRegion;
108   }
109 }