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.ipc;
019
020import java.io.IOException;
021import java.net.InetSocketAddress;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.List;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.codec.Codec;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.testclassification.RPCTests;
030import org.apache.hadoop.hbase.util.JVM;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.experimental.categories.Category;
035import org.junit.runner.RunWith;
036import org.junit.runners.Parameterized;
037import org.junit.runners.Parameterized.Parameter;
038import org.junit.runners.Parameterized.Parameters;
039
040import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollEventLoopGroup;
041import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollSocketChannel;
042import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
043import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
044
045@RunWith(Parameterized.class)
046@Category({ RPCTests.class, MediumTests.class })
047public class TestNettyIPC extends AbstractTestIPC {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestNettyIPC.class);
052
053  @Parameters(name = "{index}: EventLoop={0}")
054  public static Collection<Object[]> parameters() {
055    List<Object[]> params = new ArrayList<>();
056    params.add(new Object[] { "nio" });
057    params.add(new Object[] { "perClientNio" });
058    if (JVM.isLinux() && JVM.isAmd64()) {
059      params.add(new Object[] { "epoll" });
060    }
061    return params;
062  }
063
064  @Parameter
065  public String eventLoopType;
066
067  private static NioEventLoopGroup NIO;
068
069  private static EpollEventLoopGroup EPOLL;
070
071  @BeforeClass
072  public static void setUpBeforeClass() {
073    NIO = new NioEventLoopGroup();
074    if (JVM.isLinux() && JVM.isAmd64()) {
075      EPOLL = new EpollEventLoopGroup();
076    }
077  }
078
079  @AfterClass
080  public static void tearDownAfterClass() {
081    if (NIO != null) {
082      NIO.shutdownGracefully();
083    }
084    if (EPOLL != null) {
085      EPOLL.shutdownGracefully();
086    }
087  }
088
089  private void setConf(Configuration conf) {
090    switch (eventLoopType) {
091      case "nio":
092        NettyRpcClientConfigHelper.setEventLoopConfig(conf, NIO, NioSocketChannel.class);
093        break;
094      case "epoll":
095        NettyRpcClientConfigHelper.setEventLoopConfig(conf, EPOLL, EpollSocketChannel.class);
096        break;
097      case "perClientNio":
098        NettyRpcClientConfigHelper.createEventLoopPerClient(conf);
099        break;
100      default:
101        break;
102    }
103  }
104
105  @Override
106  protected RpcServer createRpcServer(String name,
107    List<RpcServer.BlockingServiceAndInterface> services, InetSocketAddress bindAddress,
108    Configuration conf, RpcScheduler scheduler) throws IOException {
109    return new NettyRpcServer(null, name, services, bindAddress, conf, scheduler, true);
110  }
111
112  @Override
113  protected NettyRpcClient createRpcClientNoCodec(Configuration conf) {
114    setConf(conf);
115    return new NettyRpcClient(conf) {
116
117      @Override
118      protected Codec getCodec() {
119        return null;
120      }
121
122    };
123  }
124
125  @Override
126  protected NettyRpcClient createRpcClient(Configuration conf) {
127    setConf(conf);
128    return new NettyRpcClient(conf);
129  }
130
131  @Override
132  protected NettyRpcClient createRpcClientRTEDuringConnectionSetup(Configuration conf) {
133    setConf(conf);
134    return new NettyRpcClient(conf) {
135
136      @Override
137      protected boolean isTcpNoDelay() {
138        throw new RuntimeException("Injected fault");
139      }
140    };
141  }
142
143  @Override
144  protected RpcServer createTestFailingRpcServer(String name,
145    List<RpcServer.BlockingServiceAndInterface> services, InetSocketAddress bindAddress,
146    Configuration conf, RpcScheduler scheduler) throws IOException {
147    return new FailingNettyRpcServer(null, name, services, bindAddress, conf, scheduler);
148  }
149}