-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support netty io_uring #1405
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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> | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
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线程池 | ||
*/ | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
There was a problem hiding this comment.
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 ofuseEpoll
overuseIoUring
and under what conditions each is used. Additionally, consider handling the case where bothuseEpoll
anduseIoUring
are false more explicitly to improve code clarity.