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

Add an option to allow developers to enable thread affinity of Netty if net.openhft.affinity is on classpath #1489

Closed
JamesChenX opened this issue Feb 3, 2021 · 5 comments
Labels
for/stackoverflow Questions are best asked on SO or Gitter

Comments

@JamesChenX
Copy link
Contributor

JamesChenX commented Feb 3, 2021

Motivation

  1. Make full use of CPUs cache
  2. Reduce context switching

Desired solution

Add an option to allow developers to enable thread affinity of Netty if net.openhft.affinity is on classpath

TcpServer.create()
	.host("localhost") 
	.port(8080)
	// .threadAffinity() // Something like this
	// .threadAffinity(false)
	.bindNow();

Reference: https://github.com/netty/netty/wiki/Thread-Affinity

Considered alternatives

No.

Additional context

No.

@JamesChenX JamesChenX added status/need-triage A new issue that still need to be evaluated as a whole type/enhancement A general enhancement labels Feb 3, 2021
@JamesChenX JamesChenX changed the title Add an option to allow developers to support thread affinity if net.openhft » affinity is on classpath Add an option to allow developers to enable thread affinity of Netty if net.openhft.affinity is on classpath Feb 3, 2021
@violetagg
Copy link
Member

@JamesChenX You should be able to use the current API

ThreadFactory threadFactory = new AffinityThreadFactory("atf_wrk", AffinityStrategies.DIFFERENT_CORE);
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(10, threadFactory);

TcpServer.create()
	.host("localhost") 
	.port(8080)
	.runOn(eventLoopGroup)
	.bindNow();

https://projectreactor.io/docs/netty/release/api/reactor/netty/transport/Transport.html#runOn-io.netty.channel.EventLoopGroup-

@violetagg violetagg added for/user-attention This issue needs user attention (feedback, rework, etc...) and removed status/need-triage A new issue that still need to be evaluated as a whole labels Feb 3, 2021
@JamesChenX
Copy link
Contributor Author

JamesChenX commented Feb 3, 2021

OK, it's fine for us to use the APIs, and I just wanted to confirm whether you will do it, so we don't reinvent the wheel. Thanks.

@violetagg violetagg added for/stackoverflow Questions are best asked on SO or Gitter and removed for/user-attention This issue needs user attention (feedback, rework, etc...) type/enhancement A general enhancement labels Feb 3, 2021
@JamesChenX
Copy link
Contributor Author

JamesChenX commented Feb 3, 2021

@violetagg . One more enhancement point, we would like to specify the number of select (acceptor) threads and the number of workers while using thread affinity. But there is no way for that now, could you provide a method like .runOn(acceptorEventLoopGroup, workerEventLoopGroup)? So that we can use custom acceptor/worker EventLoopGroup with thread affinity enabled.

@JamesChenX JamesChenX reopened this Feb 3, 2021
@violetagg
Copy link
Member

@JamesChenX You can do it like this:

final int acceptorThreads = 1;
final int workerThreads = 10;
EventLoopGroup acceptorGroup = new NioEventLoopGroup(acceptorThreads);
ThreadFactory threadFactory = new AffinityThreadFactory("atf_wrk", AffinityStrategies.DIFFERENT_CORE);
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads, threadFactory);

TcpServer.create()
	.host("localhost") 
	.port(8080)
        .runOn(new LoopResources() {
	         @Override
	         public EventLoopGroup onServer(boolean useNative) {
		         return workerGroup;
	         }

	         @Override
	         public EventLoopGroup onServerSelect(boolean useNative) {
		         return acceptorGroup;
	         }
         })
	.bindNow();

@JamesChenX
Copy link
Contributor Author

@violetagg it can work but the code isn't that elegant compared to runOn(acceptorEventLoopGroup, workerEventLoopGroup). Anyway, thanks for your continuous support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for/stackoverflow Questions are best asked on SO or Gitter
Projects
None yet
Development

No branches or pull requests

2 participants