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

NIO.2 support #2515

Closed
chrisprobst opened this issue May 26, 2014 · 10 comments
Closed

NIO.2 support #2515

chrisprobst opened this issue May 26, 2014 · 10 comments

Comments

@chrisprobst
Copy link
Contributor

Hi,

I saw that Netty.4.Final dropped support for AIO. In the book "Netty in Action" I read that it was dropped mainly because it was not faster and that netty's threading model would make it hard to interoperate, which makes sense.

But maybe I could provide another view, since I'm heavily interested in windows support:

The NIO.1 Selector works using kqueue, epoll and select on pretty much all platforms. Now Windows does have IOCP which is way better than select (select on windows have scalability issues). But, of course, IOCP is the proactor model and does not fit in the reactor-selector-pattern.

That's why JDK7 finally brought us AIO, which implements the proactor pattern. The good thing about the proactor pattern is that it abstract's both select-based-techniques and IOCP very well.

What this all means is, that NIO.1 (Netty =( ) on windows simply does not scale as good as it could be.

I do understand the technical consequences of AIO for netty, though. AIO does have limitations, too.

  • First AIO expects a buffer before something was actually read, so if a lot of parallel clients are connected, the reading must be limited in some way.
  • AIO runs the completion handlers on the given executor. So if the executor has multiple threads the handlers may be invoked on different threads which would destroy netty's wonderful threading-model. But it's basically simple: Every event-loop in netty would have it's own AsynchronousChannelProvider running using single thread executor. This should work.

I know that this means some work and I am willing to contribute, but I would like to hear your point of view, maybe I do not have enough knowledge about this topic ;=)

Cheers,
Chris

@trustin
Copy link
Member

trustin commented May 28, 2014

Hi Chris,

We obviously did not consider Windows as a serious platform so far, and that's why we were neglecting NIO.2 AIO API which was implemented using IOCP on Windows. (On Linux, it wasn't any faster because it was using the same OS facility - epoll.)

There are two ways to implement your idea:

  1. Implement a new transport (Channel and EventLoop impl) using NIO.2 AIO API
  2. Implement a new transport using JNI

If you are proficient with Win32/C programming, I would recommend the second approach because there's not much point in having another layer of abstraction (NIO.2). As demonstrated by netty-transport-native-epoll, removing an abstraction layer between Netty and OS yields better performance.

It you are inclined to the first solution, you could check out some old pre-release Netty version which ships the AIO transport and start from there. In this case, I would be interested to compare the performance between the NIO transport and the AIO transport on Windows before merging it.

@trustin trustin added this to the 4.1.0.Final milestone May 28, 2014
@trustin
Copy link
Member

trustin commented May 28, 2014

Oh, and did I say you are more than welcome to contribute it to the project? :-)

@chrisprobst
Copy link
Contributor Author

Hi trustin!

Great, I will look into this issue the next weekend. Your point about native vs NIO-abstractions is definitely something I will consider. I will start with NIO.2 and check if it's worth it.

Some stuff I saw so far in the sun.nio.ch.WindowsSelectorImpl:

54 // Should be INIT_CAP times a power of 2
55 private final static int MAX_SELECTABLE_FDS = 1024;

69 // Number of helper threads needed for select. We need one thread per
70 // each additional set of MAX_SELECTABLE_FDS - 1 channels.
71 private int threadsCount = 0;

As you can see every 1024 sockets the select implementation will use a new thread, which is not that bad but because select is O(n) and especially bad on windows NIO.2 should bring a noticeable improvement. The bad thing about it is that I can not really benchmark it since I have only a dated windows laptop. I mainly work on OSX but windows is important for me nonetheless.

@trustin
Copy link
Member

trustin commented May 28, 2014

Sounds great. Let me stay tuned to this thread. Cheers!

@chrisprobst
Copy link
Contributor Author

I found some time today to investigate the AIO implementation of Netty.4.0.0.CR3.

It's basically exactly what I was thinking of. The custom implementation of AioEventLoop should be fine, since every continuation is scheduled in one thread - the event loop thread. No useless context switches =).

The AioSocketChannel was a bit complicated but I think I got it now. After all I will test this implementation this evening or probably tomorrow on a windows network against the select version in terms of latency and CPU overhead (posix-select could eventually drive the cpu crazy on windows systems).

But my guess is that the AIO impl. will perform quite well. If this is the case I would kindly ask to take the AIO implementation back into master-branch =). I think it makes not much sense to write another AIO implementation if the existing one is already quite sophisticated ;=).

Or let's put it this way: What disadvantages would you see to support AIO ? I mean, of course, it adds maintenance costs which might or might not be worth it.

According to the book the main reasons were:

  • Not faster than NIO (epoll) on unix systems (which is true)
  • There is no daragram suppport
  • Unnecessary threading model (too much abstraction without usage)

I agree that AIO will not easily replace NIO, but it is useful for windows developers nonetheless.

These are my thoughts so far

@trustin
Copy link
Member

trustin commented May 29, 2014

Sounds good. Looking forward to your pull request. Please do not forget to modify SocketTestPermutation so that the testsuite module tests your transport thoroughly.

@normanmaurer
Copy link
Member

Sounds good... Thanks!

Am 29.05.2014 um 07:36 schrieb Trustin Lee notifications@github.com:

Sounds good. Looking forward to your pull request. Please do not forget to modify SocketTestPermutation so that the testsuite module tests your transport thoroughly.


Reply to this email directly or view it on GitHub.

@chrisprobst
Copy link
Contributor Author

Unfortunately I did not get access to the windows network, the admin was against it, as always 8-|
Because of this I spoke to a JVM expert, because I had to solve this issue theoretically. He assured me that the 1024-socket-per-selector-thread is a well proven and well tested implementation on windows and that IOCP is not going to improve a lot, besides less thread-context-switches.

I told him the fact that windows-select is crappy and he told me that this is not true anymore for Win7 & 8, which was really interesting for me. He said that it was actually very true for XP (probably even Vista), but since 7 the network stack got an update, which was new to me. IOCP still has some advantages but selector based networks are definitely capable of handling a lot of throughput since Win7, which is nice =).

I do not have any numbers but the project lead is happy with this now and so my priorities have changed, so for me this issue is kind if closed.

@normanmaurer
Copy link
Member

@chrisprobst @trustin let me close this ...

@trustin trustin modified the milestones: 4.1.0.Beta1, 4.1.0.Final Jul 3, 2014
NotAndOr added a commit to NotAndOr/bookmarks that referenced this issue Feb 27, 2018
- c10k
	- https://blogs.oracle.com/alanb/entry/epoll
	- https://www.kernel.org/doc/ols/2004/ols2004v1-pages-215-226.pdf
	- https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/
	- http://www.oschina.net/translate/how-to-use-epoll-a-complete-example-in-c
	- https://github.com/RajivKurian/epoll-example
	- https://github.com/hnakamur/luajit-examples
	- http://baus.net/on-tcp_cork/
	- http://stackoverflow.com/questions/22124098/is-there-any-significant-difference-between-tcp-cork-and-tcp-nodelay-in-this-use
	- http://stackoverflow.com/questions/22117205/is-there-an-equivalent-to-tcp-cork-in-winsock
	- http://stackoverflow.com/questions/3761276/when-should-i-use-tcp-nodelay-and-when-tcp-cork
	- http://ccr.sigcomm.org/archive/2001/jan01/ccr-200101-mogul.pdf
	- http://www.ulduzsoft.com/2014/01/select-poll-epoll-practical-difference-for-system-architects
	- http://daniel.haxx.se/docs/poll-vs-select.html
	- http://blog.codingnow.com/2006/04/iocp_kqueue_epoll.html
	- https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx
	- http://blog.csdn.net/laoyang360/article/details/8432753
	- http://blog.csdn.net/shallwake/article/details/5265287
	- http://www.artima.com/articles/io_design_patterns.html
	- http://www.cppblog.com/niewenlong/archive/2014/11/30/30224.html
	- http://stackoverflow.com/questions/23791024/java-nio-windows-implementation
	- http://www.mailinator.com/tymaPaulMultithreaded.pdf
	- http://www.thebuzzmedia.com/java-io-faster-than-nio-old-is-new-again/
	- netty/netty#2515
	- http://www.ibm.com/developerworks/library/j-nio2-1/
	- https://www.ibm.com/developerworks/java/library/j-nio2-2/
	- http://www.ibm.com/developerworks/cn/java/j-lo-iocp/
	- http://www.ibm.com/developerworks/cn/aix/library/1105_huangrg_kqueue/
	- https://news.ycombinator.com/item?id=1551776
	- http://blog.csdn.net/mlite/article/details/699340
	- https://eklitzke.org/blocking-io-nonblocking-io-and-epoll
	- http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch06lev1sec2.html
	- https://notes.shichao.io/unp/ch6/
	- http://www.cnblogs.com/Anker/p/3265058.html
	- http://www.baeldung.com/java-nio-selector
	- https://my.oschina.net/xianggao/blog/663655
	- https://www.zhihu.com/question/20122137
	- https://github.com/angrave/SystemProgramming/wiki/Networking,-Part-7:-Nonblocking-I-O,-select(),-and-epoll
		- https://idea.popcount.org/2016-11-01-a-brief-history-of-select2/
		- https://idea.popcount.org/2017-01-06-select-is-fundamentally-broken/
	- http://blog.csdn.net/wenbingoon/article/details/9004512
	- http://blog.csdn.net/wenbingoon/article/details/9880365
	- http://blog.csdn.net/mumumuwudi/article/details/47145801
 	- https://social.msdn.microsoft.com/Forums/exchange/en-US/d0849897-193c-4859-9a2d-00d102ca5d0b/wsaeventselect-and-reenabling-functions
	- http://www.voidcn.com/article/p-reypidzi-bkp.html
	- http://blog.csdn.net/ypoflyer/article/details/6119944

- Socket
	- https://www.jianshu.com/p/85af586fba54
	- https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/com.ibm.aix.networkcomm/protocols_transp_level.htm
	- http://www.cs.dartmouth.edu/~campbell/cs60/socketprogramming.html
	- http://beej.us/guide/bgnet/html/single/bgnet.html
	- http://alas.matf.bg.ac.rs/manuals/lspe/snode=40.html
	- https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states
	- https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/xnonblock.htm
	- http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch08lev1sec11.html
	- https://stackoverflow.com/questions/12763268/why-is-bind-used-in-tcp-why-is-it-used-only-on-server-side-and-not-in-client
	- https://idea.popcount.org/2014-04-03-bind-before-connect/
		- https://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
	- https://stackoverflow.com/questions/589928/socket-programming-how-do-i-handle-out-of-band-data
	- https://gist.github.com/todoshcenko/21f82bae5d5bbd89258d2949ee10812d
	- https://stackoverflow.com/questions/10104082/unix-socket-sock-seqpacket-vs-sock-dgram
		- http://urchin.earth.li/~twic/Sequenced_Packets_Over_Ordinary_TCP.html
	- https://stackoverflow.com/questions/5807246/event-driven-io-and-blocking-vs-nonblocking
	- https://stackoverflow.com/questions/6540346/java-solaris-nio-op-connect-problem
	- https://stackoverflow.com/questions/23465401/why-native-epoll-support-is-introduced-in-netty
	- https://stackoverflow.com/questions/14643252/how-to-use-client-socket-bind
	- https://stackoverflow.com/questions/3057029/do-i-have-to-bind-a-udp-socket-in-my-client-program-to-receive-data-i-always-g
	- https://stackoverflow.com/questions/8636717/is-it-always-required-to-bind-a-socket
	- http://itamarst.org/writings/win32sockets.html
	- https://www.defcon.org/images/defcon-13/dc13-presentations/DC_13-Levin.pdf
	- http://cybertiggyr.com/relia/relia.pdf
	- http://ra.ziti.uni-heidelberg.de/pages/student_work/seminar/ws0405/Christian_Leber/praesentation.pdf

- jdbc
add https://dzone.com/articles/spring-5-webflux-and-jdbc-to-block-or-not-to-block
add https://www.reddit.com/r/java/comments/54janw/nonblocking_jdbc_api_slides_from_talk_at_javaone/
add https://static.rainfocus.com/oracle/oow16/sess/1461693351182001EmRq/ppt/CONF1578%2020160916.pdf
add https://github.com/mheath/adbcj
add https://www.voxxed.com/2016/09/non-blocking-database-access/
yulewei referenced this issue Oct 20, 2019
The AIO transport was added in the past as we hoped it would have better latency as the NIO transport. But in reality this was never the case.
So there is no reason to use the AIO transport at all. It just put more burden on us as we need to also support it and fix bugs.
Because of this we dedicided to remove it for now. It will stay in the master_with_aio_transport branch so we can pick it up later again if it is ever needed.
NotAndOr added a commit to NotAndOr/bookmarks that referenced this issue Feb 19, 2023
- c10k
	- https://blogs.oracle.com/alanb/entry/epoll
	- https://www.kernel.org/doc/ols/2004/ols2004v1-pages-215-226.pdf
	- https://banu.com/blog/2/how-to-use-epoll-a-complete-example-in-c/
	- http://www.oschina.net/translate/how-to-use-epoll-a-complete-example-in-c
	- https://github.com/RajivKurian/epoll-example
	- https://github.com/hnakamur/luajit-examples
	- http://baus.net/on-tcp_cork/
	- http://stackoverflow.com/questions/22124098/is-there-any-significant-difference-between-tcp-cork-and-tcp-nodelay-in-this-use
	- http://stackoverflow.com/questions/22117205/is-there-an-equivalent-to-tcp-cork-in-winsock
	- http://stackoverflow.com/questions/3761276/when-should-i-use-tcp-nodelay-and-when-tcp-cork
	- http://ccr.sigcomm.org/archive/2001/jan01/ccr-200101-mogul.pdf
	- http://www.ulduzsoft.com/2014/01/select-poll-epoll-practical-difference-for-system-architects
	- http://daniel.haxx.se/docs/poll-vs-select.html
	- http://blog.codingnow.com/2006/04/iocp_kqueue_epoll.html
	- https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx
	- http://blog.csdn.net/laoyang360/article/details/8432753
	- http://blog.csdn.net/shallwake/article/details/5265287
	- http://www.artima.com/articles/io_design_patterns.html
	- http://www.cppblog.com/niewenlong/archive/2014/11/30/30224.html
	- http://stackoverflow.com/questions/23791024/java-nio-windows-implementation
	- http://www.mailinator.com/tymaPaulMultithreaded.pdf
	- http://www.thebuzzmedia.com/java-io-faster-than-nio-old-is-new-again/
	- netty/netty#2515
	- http://www.ibm.com/developerworks/library/j-nio2-1/
	- https://www.ibm.com/developerworks/java/library/j-nio2-2/
	- http://www.ibm.com/developerworks/cn/java/j-lo-iocp/
	- http://www.ibm.com/developerworks/cn/aix/library/1105_huangrg_kqueue/
	- https://news.ycombinator.com/item?id=1551776
	- http://blog.csdn.net/mlite/article/details/699340
	- https://eklitzke.org/blocking-io-nonblocking-io-and-epoll
	- http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch06lev1sec2.html
	- https://notes.shichao.io/unp/ch6/
	- http://www.cnblogs.com/Anker/p/3265058.html
	- http://www.baeldung.com/java-nio-selector
	- https://my.oschina.net/xianggao/blog/663655
	- https://www.zhihu.com/question/20122137
	- https://github.com/angrave/SystemProgramming/wiki/Networking,-Part-7:-Nonblocking-I-O,-select(),-and-epoll
		- https://idea.popcount.org/2016-11-01-a-brief-history-of-select2/
		- https://idea.popcount.org/2017-01-06-select-is-fundamentally-broken/
	- http://blog.csdn.net/wenbingoon/article/details/9004512
	- http://blog.csdn.net/wenbingoon/article/details/9880365
	- http://blog.csdn.net/mumumuwudi/article/details/47145801
 	- https://social.msdn.microsoft.com/Forums/exchange/en-US/d0849897-193c-4859-9a2d-00d102ca5d0b/wsaeventselect-and-reenabling-functions
	- http://www.voidcn.com/article/p-reypidzi-bkp.html
	- http://blog.csdn.net/ypoflyer/article/details/6119944

- Socket
	- https://www.jianshu.com/p/85af586fba54
	- https://www.ibm.com/support/knowledgecenter/en/ssw_aix_71/com.ibm.aix.networkcomm/protocols_transp_level.htm
	- http://www.cs.dartmouth.edu/~campbell/cs60/socketprogramming.html
	- http://beej.us/guide/bgnet/html/single/bgnet.html
	- http://alas.matf.bg.ac.rs/manuals/lspe/snode=40.html
	- https://superuser.com/questions/173535/what-are-close-wait-and-time-wait-states
	- https://www.ibm.com/support/knowledgecenter/en/ssw_i5_54/rzab6/xnonblock.htm
	- http://www.masterraghu.com/subjects/np/introduction/unix_network_programming_v1.3/ch08lev1sec11.html
	- https://stackoverflow.com/questions/12763268/why-is-bind-used-in-tcp-why-is-it-used-only-on-server-side-and-not-in-client
	- https://idea.popcount.org/2014-04-03-bind-before-connect/
		- https://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
	- https://stackoverflow.com/questions/589928/socket-programming-how-do-i-handle-out-of-band-data
	- https://gist.github.com/todoshcenko/21f82bae5d5bbd89258d2949ee10812d
	- https://stackoverflow.com/questions/10104082/unix-socket-sock-seqpacket-vs-sock-dgram
		- http://urchin.earth.li/~twic/Sequenced_Packets_Over_Ordinary_TCP.html
	- https://stackoverflow.com/questions/5807246/event-driven-io-and-blocking-vs-nonblocking
	- https://stackoverflow.com/questions/6540346/java-solaris-nio-op-connect-problem
	- https://stackoverflow.com/questions/23465401/why-native-epoll-support-is-introduced-in-netty
	- https://stackoverflow.com/questions/14643252/how-to-use-client-socket-bind
	- https://stackoverflow.com/questions/3057029/do-i-have-to-bind-a-udp-socket-in-my-client-program-to-receive-data-i-always-g
	- https://stackoverflow.com/questions/8636717/is-it-always-required-to-bind-a-socket
	- http://itamarst.org/writings/win32sockets.html
	- https://www.defcon.org/images/defcon-13/dc13-presentations/DC_13-Levin.pdf
	- http://cybertiggyr.com/relia/relia.pdf
	- http://ra.ziti.uni-heidelberg.de/pages/student_work/seminar/ws0405/Christian_Leber/praesentation.pdf

- jdbc
add https://dzone.com/articles/spring-5-webflux-and-jdbc-to-block-or-not-to-block
add https://www.reddit.com/r/java/comments/54janw/nonblocking_jdbc_api_slides_from_talk_at_javaone/
add https://static.rainfocus.com/oracle/oow16/sess/1461693351182001EmRq/ppt/CONF1578%2020160916.pdf
add https://github.com/mheath/adbcj
add https://www.voxxed.com/2016/09/non-blocking-database-access/
@lizhup
Copy link

lizhup commented Mar 21, 2024

@chrisprobst 然后呢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants