1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.ipc;
20
21 import java.io.IOException;
22
23 import com.google.protobuf.HBaseZeroCopyByteString;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.hbase.TableName;
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.Bytes;
35
36 import com.google.protobuf.Descriptors;
37 import com.google.protobuf.Message;
38
39
40
41
42
43
44
45
46
47 @InterfaceAudience.Private
48 public class RegionCoprocessorRpcChannel extends CoprocessorRpcChannel{
49 private static Log LOG = LogFactory.getLog(RegionCoprocessorRpcChannel.class);
50
51 private final HConnection connection;
52 private final TableName table;
53 private final byte[] row;
54 private byte[] lastRegion;
55
56 private RpcRetryingCallerFactory rpcFactory;
57
58 public RegionCoprocessorRpcChannel(HConnection conn, TableName table, byte[] row) {
59 this.connection = conn;
60 this.table = table;
61 this.row = row;
62 this.rpcFactory = RpcRetryingCallerFactory.instantiate(conn.getConfiguration());
63 }
64
65 @Override
66 protected Message callExecService(Descriptors.MethodDescriptor method,
67 Message request, Message responsePrototype)
68 throws IOException {
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Call: "+method.getName()+", "+request.toString());
71 }
72
73 if (row == null) {
74 throw new IllegalArgumentException("Missing row property for remote region location");
75 }
76
77 final ClientProtos.CoprocessorServiceCall call =
78 ClientProtos.CoprocessorServiceCall.newBuilder()
79 .setRow(HBaseZeroCopyByteString.wrap(row))
80 .setServiceName(method.getService().getFullName())
81 .setMethodName(method.getName())
82 .setRequest(request.toByteString()).build();
83 RegionServerCallable<CoprocessorServiceResponse> callable =
84 new RegionServerCallable<CoprocessorServiceResponse>(connection, table, row) {
85 public CoprocessorServiceResponse call() throws Exception {
86 byte[] regionName = getLocation().getRegionInfo().getRegionName();
87 return ProtobufUtil.execService(getStub(), call, regionName);
88 }
89 };
90 CoprocessorServiceResponse result = rpcFactory.<CoprocessorServiceResponse> newCaller()
91 .callWithRetries(callable);
92 Message response = null;
93 if (result.getValue().hasValue()) {
94 response = responsePrototype.newBuilderForType()
95 .mergeFrom(result.getValue().getValue()).build();
96 } else {
97 response = responsePrototype.getDefaultInstanceForType();
98 }
99 lastRegion = result.getRegion().getValue().toByteArray();
100 if (LOG.isTraceEnabled()) {
101 LOG.trace("Result is region=" + Bytes.toStringBinary(lastRegion) + ", value=" + response);
102 }
103 return response;
104 }
105
106 public byte[] getLastRegion() {
107 return lastRegion;
108 }
109 }