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