Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support netty io_uring #1405

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
<module.deploy.skip>true</module.deploy.skip>
<!-- Fabric8 for Kubernetes -->
<fabric8_kubernetes_version>6.9.2</fabric8_kubernetes_version>
<!-- io_uring -->
<netty-iouring.version>0.0.21.Final</netty-iouring.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -613,6 +615,25 @@
<version>0.16.0</version>
<scope>test</scope>
</dependency>

<!-- io_uring -->
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-classes-io_uring</artifactId>
<version>${netty-iouring.version}</version>
</dependency>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<version>${netty-iouring.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<version>${netty-iouring.version}</version>
<classifier>linux-aarch_64</classifier>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ public class RpcOptions {
* 默认开启epoll?
*/
public static final String TRANSPORT_USE_EPOLL = "transport.use.epoll";

/**
* 是否开始io_uring
*/
public static final String TRANSPORT_USE_IO_URING = "transport.use.ioUring";

/**
* 默认服务端 数据包限制
*/
Expand Down
16 changes: 16 additions & 0 deletions remoting/remoting-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
<artifactId>netty-tcnative-boringssl-static</artifactId>
<classifier>${os.detected.classifier}</classifier>
</dependency>

<!-- io_uring -->
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-classes-io_uring</artifactId>
</dependency>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<classifier>linux-aarch_64</classifier>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaderNames;
Expand Down Expand Up @@ -145,7 +143,7 @@ public void connect() {
int port = providerInfo.getPort();
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(transportConfig.isUseEpoll() ? EpollSocketChannel.class : NioSocketChannel.class);
b.channel(NettyHelper.socketChannel());
b.option(ChannelOption.SO_KEEPALIVE, true);
b.remoteAddress(host, port);
b.handler(initializer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.incubator.channel.uring.IOUring;
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
import io.netty.incubator.channel.uring.IOUringSocketChannel;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

Expand All @@ -42,6 +48,7 @@
import static com.alipay.sofa.rpc.common.RpcConfigs.getIntValue;
import static com.alipay.sofa.rpc.common.RpcOptions.TRANSPORT_CLIENT_IO_THREADS;
import static com.alipay.sofa.rpc.common.RpcOptions.TRANSPORT_USE_EPOLL;
import static com.alipay.sofa.rpc.common.RpcOptions.TRANSPORT_USE_IO_URING;

/**
* @author <a href="mailto:[email protected]">GengZhang</a>
Expand Down Expand Up @@ -188,16 +195,38 @@ public synchronized static EventLoopGroup getClientIOEventLoopGroup() {
clientIoThreads : // 用户配置
Math.max(4, SystemInfo.getCpuCores() + 1); // 默认cpu+1,至少4个
NamedThreadFactory threadName = new NamedThreadFactory("CLI-IO", true);
boolean useEpoll = getBooleanValue(TRANSPORT_USE_EPOLL);
clientIOEventLoopGroup = useEpoll ? new EpollEventLoopGroup(threads, threadName)
: new NioEventLoopGroup(threads, threadName);
clientIOEventLoopGroup = eventLoopGroup(threads, threadName);
refCounter.putIfAbsent(clientIOEventLoopGroup, new AtomicInteger(0));
// SelectStrategyFactory 未设置
}
refCounter.get(clientIOEventLoopGroup).incrementAndGet();
return clientIOEventLoopGroup;
}

public synchronized static EventLoopGroup eventLoopGroup(int threads, NamedThreadFactory threadName) {
boolean useEpoll = getBooleanValue(TRANSPORT_USE_EPOLL);
boolean useIoUring = getBooleanValue(TRANSPORT_USE_IO_URING);
if (useEpoll) {
return new EpollEventLoopGroup(threads, threadName);
} else if (useIoUring && SystemInfo.isLinux() && IOUring.isAvailable()) {
return new IOUringEventLoopGroup(threads, threadName);
} else {
return new NioEventLoopGroup(threads, threadName);
}
Comment on lines +206 to +215
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The eventLoopGroup method dynamically selects the event loop group based on configuration flags and system capabilities. It would be beneficial to add documentation explaining the precedence of useEpoll over useIoUring and under what conditions each is used. Additionally, consider handling the case where both useEpoll and useIoUring are false more explicitly to improve code clarity.

}

public synchronized static Class<? extends SocketChannel> socketChannel() {
boolean useEpoll = getBooleanValue(TRANSPORT_USE_EPOLL);
boolean useIoUring = getBooleanValue(TRANSPORT_USE_IO_URING);
if (useEpoll) {
return EpollSocketChannel.class;
} else if (useIoUring && SystemInfo.isLinux() && IOUring.isAvailable()) {
return IOUringSocketChannel.class;
} else {
return NioSocketChannel.class;
}
}

/**
* 关闭客户端IO线程池
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 com.alipay.sofa.rpc.transport.netty;

import com.alipay.sofa.rpc.common.struct.NamedThreadFactory;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.incubator.channel.uring.IOUring;
import io.netty.incubator.channel.uring.IOUringEventLoopGroup;
import io.netty.incubator.channel.uring.IOUringSocketChannel;
import org.junit.Assert;
import org.junit.Test;

import static com.alipay.sofa.rpc.common.RpcOptions.TRANSPORT_USE_IO_URING;

/**
* @author chengming
* @version NettyHelperTest.java, v 0.1 2024年03月18日 2:35 PM chengming
*/
public class NettyHelperTest {

@Test
public void testEventLoopGroup() {
System.setProperty("os.name", "linux111");
System.setProperty(TRANSPORT_USE_IO_URING, "true");

EventLoopGroup eventLoopGroup = NettyHelper.eventLoopGroup(1, new NamedThreadFactory("test", true));
Class<? extends SocketChannel> socketChannel = NettyHelper.socketChannel();
if (IOUring.isAvailable()) {
Assert.assertTrue(eventLoopGroup instanceof IOUringEventLoopGroup);
Assert.assertEquals(IOUringSocketChannel.class, socketChannel);
} else {
Assert.assertTrue(eventLoopGroup instanceof NioEventLoopGroup);
Assert.assertEquals(NioSocketChannel.class, socketChannel);
}
}
Comment on lines +38 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manipulating system properties directly within a test can lead to unintended side effects, especially when tests are run in parallel. Consider using a setup and teardown approach to ensure system properties are reset after the test. Additionally, it would be beneficial to include test scenarios that cover cases when io_uring is not available or when running on a non-Linux system to ensure comprehensive coverage.

}
Loading