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