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  package org.apache.hadoop.hbase.ipc;
19
20  import com.google.protobuf.Descriptors;
21  import com.google.protobuf.Message;
22  import com.google.protobuf.RpcController;
23
24  import java.io.IOException;
25
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.classification.InterfaceAudience;
30  import org.apache.hadoop.hbase.client.ClusterConnection;
31  import org.apache.hadoop.hbase.client.RegionServerCallable;
32  import org.apache.hadoop.hbase.client.RpcRetryingCallerFactory;
33  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
35  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.CoprocessorServiceResponse;
36  import org.apache.hadoop.hbase.util.Bytes;
37
38  /**
39   * Provides clients with an RPC connection to call coprocessor endpoint {@link com.google.protobuf.Service}s
40   * against a given table region.  An instance of this class may be obtained
41   * by calling {@link org.apache.hadoop.hbase.client.Table#coprocessorService(byte[])},
42   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to call the endpoint
43   * methods.
44   * @see org.apache.hadoop.hbase.client.Table#coprocessorService(byte[])
45   */
46  @InterfaceAudience.Private
47  public class RegionCoprocessorRpcChannel extends SyncCoprocessorRpcChannel {
48    private static final Log LOG = LogFactory.getLog(RegionCoprocessorRpcChannel.class);
49
50    private final ClusterConnection connection;
51    private final TableName table;
52    private final byte[] row;
53    private byte[] lastRegion;
54    private final int operationTimeout;
55
56    private final RpcRetryingCallerFactory rpcCallerFactory;
57    private final RpcControllerFactory rpcControllerFactory;
58
59    /**
60     * Constructor
61     * @param conn connection to use
62     * @param table to connect to
63     * @param row to locate region with
64     */
65    public RegionCoprocessorRpcChannel(ClusterConnection conn, TableName table, byte[] row) {
66      this.connection = conn;
67      this.table = table;
68      this.row = row;
69      this.rpcCallerFactory = conn.getRpcRetryingCallerFactory();
70      this.rpcControllerFactory = conn.getRpcControllerFactory();
71      this.operationTimeout = conn.getConnectionConfiguration().getOperationTimeout();
72    }
73
74    @Override
75    protected Message callExecService(RpcController controller,
76        Descriptors.MethodDescriptor method, Message request, Message responsePrototype)
77            throws IOException {
78      if (LOG.isTraceEnabled()) {
79        LOG.trace("Call: "+method.getName()+", "+request.toString());
80      }
81
82      if (row == null) {
83        throw new IllegalArgumentException("Missing row property for remote region location");
84      }
85
86      final RpcController rpcController = controller == null
87          ? rpcControllerFactory.newController() : controller;
88
89      final ClientProtos.CoprocessorServiceCall call =
90          CoprocessorRpcUtils.buildServiceCall(row, method, request);
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;
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   /**
123    * Get last region this RpcChannel communicated with
124    * @return region name as byte array
125    */
126   public byte[] getLastRegion() {
127     return lastRegion;
128   }
129 }