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 java.util.concurrent.ThreadFactory;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.ipc.NettyRpcClientConfigHelper;
023import org.apache.hadoop.hbase.wal.NettyAsyncFSWALConfigHelper;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.io.netty.channel.Channel;
027import org.apache.hbase.thirdparty.io.netty.channel.EventLoopGroup;
028import org.apache.hbase.thirdparty.io.netty.channel.ServerChannel;
029import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollEventLoopGroup;
030import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollServerSocketChannel;
031import org.apache.hbase.thirdparty.io.netty.channel.epoll.EpollSocketChannel;
032import org.apache.hbase.thirdparty.io.netty.channel.nio.NioEventLoopGroup;
033import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioServerSocketChannel;
034import org.apache.hbase.thirdparty.io.netty.channel.socket.nio.NioSocketChannel;
035import org.apache.hbase.thirdparty.io.netty.util.concurrent.DefaultThreadFactory;
036
037/**
038 * Event loop group related config.
039 */
040@InterfaceAudience.Private
041public class NettyEventLoopGroupConfig {
042
043  public static final String NETTY_WORKER_COUNT_KEY = "hbase.netty.worker.count";
044  public static final int DEFAULT_NETTY_WORKER_COUNT = 0;
045
046  public static final String NETTY_NATIVETRANSPORT_KEY = "hbase.netty.nativetransport";
047  public static final boolean DEFAULT_NETTY_NATIVETRANSPORT = true;
048
049  private final EventLoopGroup group;
050
051  private final Class<? extends ServerChannel> serverChannelClass;
052
053  private final Class<? extends Channel> clientChannelClass;
054
055  private static boolean useEpoll(Configuration conf) {
056    // Config to enable native transport.
057    final boolean epollEnabled =
058      conf.getBoolean(NETTY_NATIVETRANSPORT_KEY, DEFAULT_NETTY_NATIVETRANSPORT);
059    // Use the faster native epoll transport mechanism on linux if enabled and the
060    // hardware architecture is either amd64 or aarch64. Netty is known to have native
061    // epoll support for these combinations.
062    return epollEnabled && JVM.isLinux() && (JVM.isAmd64() || JVM.isAarch64());
063  }
064
065  public NettyEventLoopGroupConfig(Configuration conf, String threadPoolName) {
066    final boolean useEpoll = useEpoll(conf);
067    final int workerCount = conf.getInt(NETTY_WORKER_COUNT_KEY,
068      // For backwards compatibility we also need to consider
069      // "hbase.netty.eventloop.rpcserver.thread.count"
070      // if it is defined in site configuration instead.
071      conf.getInt("hbase.netty.eventloop.rpcserver.thread.count", DEFAULT_NETTY_WORKER_COUNT));
072    ThreadFactory eventLoopThreadFactory =
073      new DefaultThreadFactory(threadPoolName, true, Thread.MAX_PRIORITY);
074    if (useEpoll) {
075      group = new EpollEventLoopGroup(workerCount, eventLoopThreadFactory);
076      serverChannelClass = EpollServerSocketChannel.class;
077      clientChannelClass = EpollSocketChannel.class;
078    } else {
079      group = new NioEventLoopGroup(workerCount, eventLoopThreadFactory);
080      serverChannelClass = NioServerSocketChannel.class;
081      clientChannelClass = NioSocketChannel.class;
082    }
083  }
084
085  public EventLoopGroup group() {
086    return group;
087  }
088
089  public Class<? extends ServerChannel> serverChannelClass() {
090    return serverChannelClass;
091  }
092
093  public Class<? extends Channel> clientChannelClass() {
094    return clientChannelClass;
095  }
096
097  public static NettyEventLoopGroupConfig setup(Configuration conf, String threadPoolName) {
098    // Initialize netty event loop group at start as we may use it for rpc server, rpc client & WAL.
099    NettyEventLoopGroupConfig nelgc = new NettyEventLoopGroupConfig(conf, threadPoolName);
100    NettyRpcClientConfigHelper.setEventLoopConfig(conf, nelgc.group(), nelgc.clientChannelClass());
101    NettyAsyncFSWALConfigHelper.setEventLoopConfig(conf, nelgc.group(), nelgc.clientChannelClass());
102    return nelgc;
103  }
104}