-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
呈铭
committed
Mar 18, 2024
1 parent
df2dcae
commit d6b0a91
Showing
6 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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线程池 | ||
*/ | ||
|
53 changes: 53 additions & 0 deletions
53
...ting/remoting-http/src/test/java/com/alipay/sofa/rpc/transport/netty/NettyHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |