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.util;
019
020import org.apache.hbase.thirdparty.io.netty.channel.Channel;
021import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
022import org.apache.hbase.thirdparty.io.netty.channel.ServerChannel;
023import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollEventLoopGroup;
024import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollServerSocketChannel;
025import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollSocketChannel;
026import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
027import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioServerSocketChannel;
028import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
029import org.apache.hbase.thirdparty.io.netty.util.concurrent.DefaultThreadFactory;
030
031import java.util.concurrent.ThreadFactory;
032
033import org.apache.hadoop.conf.Configuration;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Event loop group related config.
038 */
039@InterfaceAudience.Private
040public class NettyEventLoopGroupConfig {
041
042  private final EventLoopGroup group;
043
044  private final Class<? extends ServerChannel> serverChannelClass;
045
046  private final Class<? extends Channel> clientChannelClass;
047
048  private static boolean useEpoll(Configuration conf) {
049    // Config to enable native transport.
050    boolean epollEnabled = conf.getBoolean("hbase.netty.nativetransport", true);
051    // Use the faster native epoll transport mechanism on linux if enabled
052    return epollEnabled && JVM.isLinux() && JVM.isAmd64();
053  }
054
055  public NettyEventLoopGroupConfig(Configuration conf, String threadPoolName) {
056    boolean useEpoll = useEpoll(conf);
057    int workerCount = conf.getInt("hbase.netty.worker.count", 0);
058    ThreadFactory eventLoopThreadFactory =
059        new DefaultThreadFactory(threadPoolName, true, Thread.MAX_PRIORITY);
060    if (useEpoll) {
061      group = new EpollEventLoopGroup(workerCount, eventLoopThreadFactory);
062      serverChannelClass = EpollServerSocketChannel.class;
063      clientChannelClass = EpollSocketChannel.class;
064    } else {
065      group = new NioEventLoopGroup(workerCount, eventLoopThreadFactory);
066      serverChannelClass = NioServerSocketChannel.class;
067      clientChannelClass = NioSocketChannel.class;
068    }
069  }
070
071  public EventLoopGroup group() {
072    return group;
073  }
074
075  public Class<? extends ServerChannel> serverChannelClass() {
076    return serverChannelClass;
077  }
078
079  public Class<? extends Channel> clientChannelClass() {
080    return clientChannelClass;
081  }
082}