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