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