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