001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.coprocessor;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import com.google.protobuf.RpcCallback;
024import com.google.protobuf.RpcController;
025import com.google.protobuf.Service;
026import java.io.FileNotFoundException;
027import java.util.Collections;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos;
033import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyRequest;
034import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyResponse;
035import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyService;
036import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
037import org.apache.hadoop.hbase.ipc.RemoteWithExtrasException;
038import org.apache.hadoop.hbase.ipc.ServerRpcController;
039import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
040import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048@Category({CoprocessorTests.class, MediumTests.class})
049public class TestRegionServerCoprocessorEndpoint {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestRegionServerCoprocessorEndpoint.class);
054
055  public static final FileNotFoundException WHAT_TO_THROW = new FileNotFoundException("/file.txt");
056  private static HBaseTestingUtility TEST_UTIL = null;
057  private static Configuration CONF = null;
058  private static final String DUMMY_VALUE = "val";
059
060  @BeforeClass
061  public static void setupBeforeClass() throws Exception {
062    TEST_UTIL = new HBaseTestingUtility();
063    CONF = TEST_UTIL.getConfiguration();
064    CONF.setStrings(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
065      DummyRegionServerEndpoint.class.getName());
066    TEST_UTIL.startMiniCluster();
067  }
068
069  @AfterClass
070  public static void tearDownAfterClass() throws Exception {
071    TEST_UTIL.shutdownMiniCluster();
072  }
073
074  @Test
075  public void testEndpoint() throws Exception {
076    final ServerName serverName = TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName();
077    final ServerRpcController controller = new ServerRpcController();
078    final CoprocessorRpcUtils.BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse>
079        rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
080    DummyRegionServerEndpointProtos.DummyService service =
081        ProtobufUtil.newServiceStub(DummyRegionServerEndpointProtos.DummyService.class,
082          TEST_UTIL.getAdmin().coprocessorService(serverName));
083    service.dummyCall(controller,
084        DummyRegionServerEndpointProtos.DummyRequest.getDefaultInstance(), rpcCallback);
085    assertEquals(DUMMY_VALUE, rpcCallback.get().getValue());
086    if (controller.failedOnException()) {
087      throw controller.getFailedOn();
088    }
089  }
090
091  @Test
092  public void testEndpointExceptions() throws Exception {
093    final ServerName serverName = TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName();
094    final ServerRpcController controller = new ServerRpcController();
095    final CoprocessorRpcUtils.BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse>
096        rpcCallback = new CoprocessorRpcUtils.BlockingRpcCallback<>();
097    DummyRegionServerEndpointProtos.DummyService service =
098        ProtobufUtil.newServiceStub(DummyRegionServerEndpointProtos.DummyService.class,
099            TEST_UTIL.getAdmin().coprocessorService(serverName));
100    service.dummyThrow(controller,
101        DummyRegionServerEndpointProtos.DummyRequest.getDefaultInstance(), rpcCallback);
102    assertEquals(null, rpcCallback.get());
103    assertTrue(controller.failedOnException());
104    assertEquals(WHAT_TO_THROW.getClass().getName().trim(),
105        ((RemoteWithExtrasException) controller.getFailedOn().getCause()).getClassName().trim());
106  }
107
108  public static class DummyRegionServerEndpoint extends DummyService
109      implements RegionServerCoprocessor {
110
111    @Override
112    public Iterable<Service> getServices() {
113      return Collections.singleton(this);
114    }
115
116    @Override
117    public void dummyCall(RpcController controller, DummyRequest request,
118        RpcCallback<DummyResponse> callback) {
119      callback.run(DummyResponse.newBuilder().setValue(DUMMY_VALUE).build());
120    }
121
122    @Override
123    public void dummyThrow(RpcController controller,
124        DummyRequest request,
125        RpcCallback<DummyResponse> done) {
126      CoprocessorRpcUtils.setControllerException(controller, WHAT_TO_THROW);
127    }
128  }
129}