You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/01/31 03:34:03 UTC
[dubbo] branch master updated: support epoll on linux,
alternative impl of PR#4493 (#5680)
This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push:
new c6413a9 support epoll on linux, alternative impl of PR#4493 (#5680)
c6413a9 is described below
commit c6413a936bd623333af728b9fc229ca5c88097cb
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Fri Jan 31 11:33:14 2020 +0800
support epoll on linux, alternative impl of PR#4493 (#5680)
---
.../remoting/transport/netty4/NettyClient.java | 17 +++---
.../transport/netty4/NettyEventLoopFactory.java | 60 ++++++++++++++++++++++
.../remoting/transport/netty4/NettyServer.java | 18 +++----
3 files changed, 77 insertions(+), 18 deletions(-)
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
index f24e52a..697a79a 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyClient.java
@@ -34,16 +34,17 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
import io.netty.handler.proxy.Socks5ProxyHandler;
import io.netty.handler.timeout.IdleStateHandler;
-import io.netty.util.concurrent.DefaultThreadFactory;
import java.net.InetSocketAddress;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
+import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.eventLoopGroup;
+import static org.apache.dubbo.remoting.transport.netty4.NettyEventLoopFactory.socketChannelClass;
/**
* NettyClient.
@@ -54,7 +55,7 @@ public class NettyClient extends AbstractClient {
/**
* netty client bootstrap
*/
- private static final NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(Constants.DEFAULT_IO_THREADS, new DefaultThreadFactory("NettyClientWorker", true));
+ private static final EventLoopGroup eventLoopGroup = eventLoopGroup(Constants.DEFAULT_IO_THREADS, "NettyClientWorker");
private static final String SOCKS_PROXY_HOST = "socksProxyHost";
@@ -90,18 +91,18 @@ public class NettyClient extends AbstractClient {
protected void doOpen() throws Throwable {
final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this);
bootstrap = new Bootstrap();
- bootstrap.group(nioEventLoopGroup)
+ bootstrap.group(eventLoopGroup)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
//.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout())
- .channel(NioSocketChannel.class);
+ .channel(socketChannelClass());
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.max(3000, getConnectTimeout()));
- bootstrap.handler(new ChannelInitializer() {
+ bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
- protected void initChannel(Channel ch) throws Exception {
+ protected void initChannel(SocketChannel ch) throws Exception {
int heartbeatInterval = UrlUtils.getHeartbeat(getUrl());
if (getUrl().getParameter(SSL_ENABLED_KEY, false)) {
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java
new file mode 100644
index 0000000..f9c5680
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyEventLoopFactory.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.remoting.transport.netty4;
+
+import org.apache.dubbo.common.config.Configuration;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.Epoll;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.epoll.EpollSocketChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.ServerSocketChannel;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.util.concurrent.DefaultThreadFactory;
+
+import java.util.concurrent.ThreadFactory;
+
+public class NettyEventLoopFactory {
+ public static EventLoopGroup eventLoopGroup(int threads, String threadFactoryName) {
+ ThreadFactory threadFactory = new DefaultThreadFactory(threadFactoryName, true);
+ return shouldEpoll() ? new EpollEventLoopGroup(threads, threadFactory) :
+ new NioEventLoopGroup(threads, threadFactory);
+ }
+
+ public static Class<? extends SocketChannel> socketChannelClass() {
+ return shouldEpoll() ? EpollSocketChannel.class : NioSocketChannel.class;
+ }
+
+ public static Class<? extends ServerSocketChannel> serverSocketChannelClass() {
+ return shouldEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
+ }
+
+ private static boolean shouldEpoll() {
+ Configuration configuration = ApplicationModel.getEnvironment().getConfiguration();
+ if (configuration.getBoolean("netty.epoll.enable", false)) {
+ String osName = configuration.getString("os.name");
+ return osName.toLowerCase().contains("linux") && Epoll.isAvailable();
+ }
+
+ return false;
+ }
+}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
index 2b7fb7a..d4ca4b3 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java
@@ -16,6 +16,7 @@
*/
package org.apache.dubbo.remoting.transport.netty4;
+import io.netty.channel.socket.SocketChannel;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
@@ -36,11 +37,7 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
-import io.netty.util.concurrent.DefaultThreadFactory;
import java.net.InetSocketAddress;
import java.util.Collection;
@@ -89,21 +86,22 @@ public class NettyServer extends AbstractServer implements RemotingServer {
protected void doOpen() throws Throwable {
bootstrap = new ServerBootstrap();
- bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
- workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
- new DefaultThreadFactory("NettyServerWorker", true));
+ bossGroup = NettyEventLoopFactory.eventLoopGroup(1, "NettyServerBoss");
+ workerGroup = NettyEventLoopFactory.eventLoopGroup(
+ getUrl().getPositiveParameter(IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
+ "NettyServerWorker");
final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
channels = nettyServerHandler.getChannels();
bootstrap.group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class)
+ .channel(NettyEventLoopFactory.serverSocketChannelClass())
.option(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
- .childHandler(new ChannelInitializer<NioSocketChannel>() {
+ .childHandler(new ChannelInitializer<SocketChannel>() {
@Override
- protected void initChannel(NioSocketChannel ch) throws Exception {
+ protected void initChannel(SocketChannel ch) throws Exception {
// FIXME: should we use getTimeout()?
int idleTimeout = UrlUtils.getIdleTimeout(getUrl());
NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);