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
40   * {@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.Table#coprocessorService(byte[])},
43   * but should normally only be used in creating a new {@link com.google.protobuf.Service} stub to
44   * call the endpoint methods.
45   * @see org.apache.hadoop.hbase.client.Table#coprocessorService(byte[])
46   */
47  @InterfaceAudience.Private
48  public class RegionCoprocessorRpcChannel extends SyncCoprocessorRpcChannel {
49    private static final Log LOG = LogFactory.getLog(RegionCoprocessorRpcChannel.class);
50
51    private final ClusterConnection connection;
52    private final TableName table;
53    private final byte[] row;
54    private byte[] lastRegion;
55    private final int operationTimeout;
56
57    private final RpcRetryingCallerFactory rpcCallerFactory;
58    private final RpcControllerFactory rpcControllerFactory;
59
60    /**
61     * Constructor
62     * @param conn connection to use
63     * @param table to connect to
64     * @param row to locate region with
65     */
66    public RegionCoprocessorRpcChannel(ClusterConnection conn, TableName table, byte[] row) {
67      this.connection = conn;
68      this.table = table;
69      this.row = row;
70      this.rpcCallerFactory = conn.getRpcRetryingCallerFactory();
71      this.rpcControllerFactory = conn.getRpcControllerFactory();
72      this.operationTimeout = conn.getConnectionConfiguration().getOperationTimeout();
73    }
74
75    @Override
76    protected Message callExecService(RpcController controller,
77        Descriptors.MethodDescriptor method, Message request, Message responsePrototype)
78            throws IOException {
79      if (LOG.isTraceEnabled()) {
80        LOG.trace("Call: " + method.getName() + ", " + request.toString());
81      }
82      if (row == null) {
83        throw new IllegalArgumentException("Missing row property for remote region location");
84      }
85      final ClientProtos.CoprocessorServiceCall call =
86          CoprocessorRpcUtils.buildServiceCall(row, method, request);
87      RegionServerCallable<CoprocessorServiceResponse> callable =
88          new RegionServerCallable<CoprocessorServiceResponse>(connection,
89            controller == null? this.rpcControllerFactory.newController(): controller,
90            table, row) {
91        @Override
92        protected CoprocessorServiceResponse rpcCall() throws Exception {
93          byte[] regionName = getLocation().getRegionInfo().getRegionName();
94          return ProtobufUtil.execService(getRpcController(), getStub(), call, regionName);
95        }
96      };
97      CoprocessorServiceResponse result = rpcCallerFactory.<CoprocessorServiceResponse> newCaller()
98          .callWithRetries(callable, operationTimeout);
99      Message response;
100     if (result.getValue().hasValue()) {
101       Message.Builder builder = responsePrototype.newBuilderForType();
102       ProtobufUtil.mergeFrom(builder, result.getValue().getValue());
103       response = builder.build();
104     } else {
105       response = responsePrototype.getDefaultInstanceForType();
106     }
107     lastRegion = result.getRegion().getValue().toByteArray();
108     if (LOG.isTraceEnabled()) {
109       LOG.trace("Result is region=" + Bytes.toStringBinary(lastRegion) + ", value=" + response);
110     }
111     return response;
112   }
113
114   /**
115    * Get last region this RpcChannel communicated with
116    * @return region name as byte array
117    */
118   public byte[] getLastRegion() {
119     return lastRegion;
120   }
121 }