1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.coprocessor;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.Coprocessor;
28 import org.apache.hadoop.hbase.CoprocessorEnvironment;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.testclassification.MediumTests;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos;
33 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyRequest;
34 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyResponse;
35 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyService;
36 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
37 import org.apache.hadoop.hbase.ipc.RemoteWithExtrasException;
38 import org.apache.hadoop.hbase.ipc.ServerRpcController;
39 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
40 import org.apache.hadoop.hbase.protobuf.ResponseConverter;
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45 import com.google.protobuf.RpcCallback;
46 import com.google.protobuf.RpcController;
47 import com.google.protobuf.Service;
48
49 @Category(MediumTests.class)
50 public class TestRegionServerCoprocessorEndpoint {
51 public static final FileNotFoundException WHAT_TO_THROW = new FileNotFoundException("/file.txt");
52 private static HBaseTestingUtility TEST_UTIL = null;
53 private static final String DUMMY_VALUE = "val";
54
55 @BeforeClass
56 public static void setupBeforeClass() throws Exception {
57 TEST_UTIL = new HBaseTestingUtility();
58 TEST_UTIL.getConfiguration().setStrings(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
59 DummyRegionServerEndpoint.class.getName());
60 TEST_UTIL.startMiniCluster();
61 }
62
63 @AfterClass
64 public static void tearDownAfterClass() throws Exception {
65 TEST_UTIL.shutdownMiniCluster();
66 }
67
68 @Test
69 public void testEndpoint() throws Exception {
70 final ServerName serverName = TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName();
71 final ServerRpcController controller = new ServerRpcController();
72 final BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse> rpcCallback =
73 new BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse>();
74 DummyRegionServerEndpointProtos.DummyService service =
75 ProtobufUtil.newServiceStub(DummyRegionServerEndpointProtos.DummyService.class,
76 TEST_UTIL.getHBaseAdmin().coprocessorService(serverName));
77 service.dummyCall(controller,
78 DummyRegionServerEndpointProtos.DummyRequest.getDefaultInstance(), rpcCallback);
79 assertEquals(DUMMY_VALUE, rpcCallback.get().getValue());
80 if (controller.failedOnException()) {
81 throw controller.getFailedOn();
82 }
83 }
84
85 @Test
86 public void testEndpointExceptions() throws Exception {
87 final ServerName serverName = TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName();
88 final ServerRpcController controller = new ServerRpcController();
89 final BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse> rpcCallback =
90 new BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse>();
91 DummyRegionServerEndpointProtos.DummyService service =
92 ProtobufUtil.newServiceStub(DummyRegionServerEndpointProtos.DummyService.class,
93 TEST_UTIL.getHBaseAdmin().coprocessorService(serverName));
94 service.dummyThrow(controller,
95 DummyRegionServerEndpointProtos.DummyRequest.getDefaultInstance(), rpcCallback);
96 assertEquals(null, rpcCallback.get());
97 assertTrue(controller.failedOnException());
98 assertEquals(WHAT_TO_THROW.getClass().getName().trim(),
99 ((RemoteWithExtrasException) controller.getFailedOn().getCause()).getClassName().trim());
100 }
101
102 static class DummyRegionServerEndpoint extends DummyService implements Coprocessor, SingletonCoprocessorService {
103
104 @Override
105 public Service getService() {
106 return this;
107 }
108
109 @Override
110 public void start(CoprocessorEnvironment env) throws IOException {
111
112 }
113
114 @Override
115 public void stop(CoprocessorEnvironment env) throws IOException {
116
117 }
118
119 @Override
120 public void dummyCall(RpcController controller, DummyRequest request,
121 RpcCallback<DummyResponse> callback) {
122 callback.run(DummyResponse.newBuilder().setValue(DUMMY_VALUE).build());
123 }
124
125 @Override
126 public void dummyThrow(RpcController controller,
127 DummyRequest request,
128 RpcCallback<DummyResponse> done) {
129 ResponseConverter.setControllerException(controller, WHAT_TO_THROW);
130
131 }
132 }
133 }