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.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.codec.Codec;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.RPCTests;
028import org.apache.hadoop.hbase.util.JVM;
029import org.junit.AfterClass;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.experimental.categories.Category;
033import org.junit.runner.RunWith;
034import org.junit.runners.Parameterized;
035import org.junit.runners.Parameterized.Parameter;
036import org.junit.runners.Parameterized.Parameters;
037
038import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollEventLoopGroup;
039import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollSocketChannel;
040import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
041import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
042
043@RunWith(Parameterized.class)
044@Category({ RPCTests.class, MediumTests.class })
045public class TestNettyIPC extends AbstractTestIPC {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestNettyIPC.class);
050
051  private static List<String> getEventLoopTypes() {
052    List<String> types = new ArrayList<>();
053    types.add("nio");
054    types.add("perClientNio");
055    if (JVM.isLinux() && JVM.isAmd64()) {
056      types.add("epoll");
057    }
058    return types;
059  }
060
061  @Parameters(name = "{index}: rpcServerImpl={0}, EventLoop={1}")
062  public static List<Object[]> parameters() {
063    List<Object[]> params = new ArrayList<>();
064    for (String eventLoopType : getEventLoopTypes()) {
065      params.add(new Object[] { SimpleRpcServer.class, eventLoopType });
066      params.add(new Object[] { NettyRpcServer.class, eventLoopType });
067    }
068    return params;
069  }
070
071  @Parameter(1)
072  public String eventLoopType;
073
074  private static NioEventLoopGroup NIO;
075
076  private static EpollEventLoopGroup EPOLL;
077
078  @BeforeClass
079  public static void setUpBeforeClass() {
080    NIO = new NioEventLoopGroup();
081    if (JVM.isLinux() && JVM.isAmd64()) {
082      EPOLL = new EpollEventLoopGroup();
083    }
084  }
085
086  @AfterClass
087  public static void tearDownAfterClass() {
088    if (NIO != null) {
089      NIO.shutdownGracefully();
090    }
091    if (EPOLL != null) {
092      EPOLL.shutdownGracefully();
093    }
094  }
095
096  private void setConf(Configuration conf) {
097    switch (eventLoopType) {
098      case "nio":
099        NettyRpcClientConfigHelper.setEventLoopConfig(conf, NIO, NioSocketChannel.class);
100        break;
101      case "epoll":
102        NettyRpcClientConfigHelper.setEventLoopConfig(conf, EPOLL, EpollSocketChannel.class);
103        break;
104      case "perClientNio":
105        NettyRpcClientConfigHelper.createEventLoopPerClient(conf);
106        break;
107      default:
108        break;
109    }
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 AbstractRpcClient<?> createBadAuthRpcClient(Configuration conf) {
145    return new NettyRpcClient(conf) {
146
147      @Override
148      protected NettyRpcConnection createConnection(ConnectionId remoteId) throws IOException {
149        return new BadAuthNettyRpcConnection(this, remoteId);
150      }
151    };
152  }
153}