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