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 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
39
40
41
42
43
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 }