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.ipc;
19  
20  import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
21  import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.newBlockingStub;
22  import static org.mockito.Mockito.mock;
23  
24  import com.google.common.collect.Lists;
25  import com.google.protobuf.BlockingService;
26  
27  import java.io.IOException;
28  import java.net.InetSocketAddress;
29  
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.Abortable;
32  import org.apache.hadoop.hbase.HBaseConfiguration;
33  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoRequestProto;
34  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface;
35  import org.apache.hadoop.hbase.testclassification.RPCTests;
36  import org.apache.hadoop.hbase.testclassification.SmallTests;
37  import org.junit.Ignore;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category({ RPCTests.class, SmallTests.class })
42  public class TestRpcHandlerException {
43  
44    private final static Configuration CONF = HBaseConfiguration.create();
45  
46    /**
47     * Instance of server. We actually don't do anything speical in here so could just use
48     * HBaseRpcServer directly.
49     */
50    private static class TestRpcServer extends RpcServer {
51  
52      TestRpcServer(RpcScheduler scheduler) throws IOException {
53        super(null, "testRpcServer",
54            Lists.newArrayList(new BlockingServiceAndInterface((BlockingService) SERVICE, null)),
55            new InetSocketAddress("localhost", 0), CONF, scheduler);
56      }
57    }
58  
59    /**
60     * Tests that the rpc scheduler is called when requests arrive. When Rpc handler thread dies, the
61     * client will hang and the test will fail. The test is meant to be a unit test to test the
62     * behavior.
63     */
64    private class AbortServer implements Abortable {
65      private boolean aborted = false;
66  
67      @Override
68      public void abort(String why, Throwable e) {
69        aborted = true;
70      }
71  
72      @Override
73      public boolean isAborted() {
74        return aborted;
75      }
76    }
77  
78    /*
79     * This is a unit test to make sure to abort region server when the number of Rpc handler thread
80     * caught errors exceeds the threshold. Client will hang when RS aborts.
81     */
82    @Ignore
83    @Test
84    public void testRpcScheduler() throws IOException, InterruptedException {
85      PriorityFunction qosFunction = mock(PriorityFunction.class);
86      Abortable abortable = new AbortServer();
87      RpcScheduler scheduler = new SimpleRpcScheduler(CONF, 2, 0, 0, qosFunction, abortable, 0);
88      RpcServer rpcServer = new TestRpcServer(scheduler);
89      try (BlockingRpcClient client = new BlockingRpcClient(CONF)) {
90        rpcServer.start();
91        BlockingInterface stub = newBlockingStub(client, rpcServer.getListenerAddress());
92        stub.echo(null, EchoRequestProto.newBuilder().setMessage("hello").build());
93      } catch (Throwable e) {
94        assert (abortable.isAborted() == true);
95      } finally {
96        rpcServer.stop();
97      }
98    }
99  
100 }