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.TableName;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.client.ClusterConnection;
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.ByteStringer;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import com.google.protobuf.Descriptors;
37  import com.google.protobuf.Message;
38  import com.google.protobuf.RpcController;
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.Table#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.Table#coprocessorService(byte[])
47   */
48  @InterfaceAudience.Private
49  public class RegionCoprocessorRpcChannel extends CoprocessorRpcChannel{
50    private static final Log LOG = LogFactory.getLog(RegionCoprocessorRpcChannel.class);
51  
52    private final ClusterConnection connection;
53    private final TableName table;
54    private final byte[] row;
55    private byte[] lastRegion;
56    private int operationTimeout;
57  
58    private RpcRetryingCallerFactory rpcCallerFactory;
59    private RpcControllerFactory rpcControllerFactory;
60  
61    public RegionCoprocessorRpcChannel(ClusterConnection conn, TableName table, byte[] row) {
62      this.connection = conn;
63      this.table = table;
64      this.row = row;
65      this.rpcCallerFactory = conn.getRpcRetryingCallerFactory();
66      this.rpcControllerFactory = conn.getRpcControllerFactory();
67      this.operationTimeout = conn.getConnectionConfiguration().getOperationTimeout();
68    }
69  
70    @Override
71    protected Message callExecService(RpcController controller,
72        Descriptors.MethodDescriptor method, 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 RpcController rpcController = controller == null
83          ? rpcControllerFactory.newController() : controller;
84  
85      final ClientProtos.CoprocessorServiceCall call =
86          ClientProtos.CoprocessorServiceCall.newBuilder()
87              .setRow(ByteStringer.wrap(row))
88              .setServiceName(method.getService().getFullName())
89              .setMethodName(method.getName())
90              .setRequest(request.toByteString()).build();
91      RegionServerCallable<CoprocessorServiceResponse> callable =
92          new RegionServerCallable<CoprocessorServiceResponse>(connection, table, row) {
93        @Override
94        public CoprocessorServiceResponse call(int callTimeout) throws Exception {
95          if (rpcController instanceof PayloadCarryingRpcController) {
96            ((PayloadCarryingRpcController) rpcController).setPriority(tableName);
97          }
98          if (rpcController instanceof TimeLimitedRpcController) {
99            ((TimeLimitedRpcController) rpcController).setCallTimeout(callTimeout);
100         }
101         byte[] regionName = getLocation().getRegionInfo().getRegionName();
102         return ProtobufUtil.execService(rpcController, getStub(), call, regionName);
103       }
104     };
105     CoprocessorServiceResponse result = rpcCallerFactory.<CoprocessorServiceResponse> newCaller()
106         .callWithRetries(callable, operationTimeout);
107     Message response = null;
108     if (result.getValue().hasValue()) {
109       Message.Builder builder = responsePrototype.newBuilderForType();
110       ProtobufUtil.mergeFrom(builder, result.getValue().getValue());
111       response = builder.build();
112     } else {
113       response = responsePrototype.getDefaultInstanceForType();
114     }
115     lastRegion = result.getRegion().getValue().toByteArray();
116     if (LOG.isTraceEnabled()) {
117       LOG.trace("Result is region=" + Bytes.toStringBinary(lastRegion) + ", value=" + response);
118     }
119     return response;
120   }
121 
122   public byte[] getLastRegion() {
123     return lastRegion;
124   }
125 }