Skip to content

Introduce IoHandleEventLoopGroup / IoHandleEventLoop and its implemen… - #13991

Merged
normanmaurer merged 23 commits into
4.2from
eventloop_rework
Apr 29, 2024
Merged

normanmaurer merged 23 commits into
4.2from
eventloop_rework

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

…tations MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop

Motivation:

At the moment each transport has its own EventLoopGroup and EventLoop implementation. Most of these are final because they are tightly coubled with the specific Channel implementations. This is unfortunate for multiple reasons, as for example:

  • It's hard to instrument EventLoop* implementations as these are not extensible at all or even if they are the same logic needs to be added to multiple EventLoop* implementations.
  • Registration / Deregistration is limited to Channels which remove flexibility as for example some Operation Systems allowing to also handle other IO (for example file IO) with something like an EventLoop (one example would be io_uring).
  • A lot of code-duplication between different EventLoop* implementations exists as most share kind of the same logic of running non IO tasks.

Modifications:

  • Add the concept of IoHandleEventLoopGroup / IoHandleEventLoop and the corrosponding IoHandle, IoHandler and IoHandlerFactory. This allows to only have a custom implement for IO handling per transport while share the same code for execution of tasks, scheduled task and ultimatly IO.
  • Add the implementations MultiThreadIoHandleEventLoopGroup and SingleThreadIoHandleEventLoop that can be used by all (or most) transport implementations by having these just provide their own IoHandler / IoHandlerFactory implementations.

Result:

More code sharing and easier of customizing EventLoops.

@normanmaurer

normanmaurer commented Apr 21, 2024 •

Copy link
Copy Markdown
Member Author

I am opening this a draft mainly because I wanted to show and proof that we can back port some of the changes that we did in main without breaking too much things. I think even thought this PR introduce some API breaking it shouldn’t affect almost any user. To proof this I did not change how we create the different EventLoopGroups in our testsuite and everything still works.

So while the old way of constructing Eventloops still work the new way would be a bit different:

Old API:

new EpollEventLoop()

New API:

new MultiThreadIoHandleEventLoopGroup(EpollHandler.newFactory();

The benefit of the new way is that it is quite easy to instrument / doctorate an EventLoop/EventLoopGroup now. Basically you can just extend MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop and add all the customization once while be able to reuse it with any transport implementation.
This includes things like:

  • able to get a count of registered channels / handles
  • understand how long the io processing vs task processing took.
  • how many channels / handles were processed per loop run
  • customize / decorate promises
  • …

Beside this IoHandleEventLoop can support not only Channel but also other subtypes of IoHandle. This would for example allow us to add file io support with io_uring and still use the same eventloop as for sockets etc. I am sure there are more things we could do by adding this kind of flexibility.

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might want to aim for releasing 4.2.0 with the following change:

  • This PR
  • Change the minimum java version to 8
  • Maybe a few more low risk changes.

I would love to hear what other project members and the community think about this ?

@normanmaurer

Copy link
Copy Markdown
Member Author

@normanmaurer

Copy link
Copy Markdown
Member Author

If interested I can also change all the creation code of EventLoopGroups in the testsuite and examples. I just wanted to not do this for now to show how small the impact for most people should be

@normanmaurer
normanmaurer requested a review from chrisvest April 21, 2024 17:27
…tations MultiThreadIoHandleEventLoopGroup / SingleThreadIoHandleEventLoop

Motivation:

At the moment each transport has its own EventLoopGroup and EventLoop implementation. Most of these are final because they are tightly coubled with the specific Channel implementations.
This is unfortunate for multiple reasons, as for example:

- It's hard to instrument EventLoop* implementations as these are not extensible at all or even if they are the same logic needs to be added to multiple EventLoop* implementations.
- Registration / Deregistration is limited to Channels which remove flexibility as for example some Operation Systems allowing to also handle other IO (for example file IO) with
  something like an EventLoop (one example would be io_uring).
- A lot of code-duplication between different EventLoop* implementations exists as most share kind of the same logic of running non IO tasks.

Modifications:

- Add the concept of IoHandleEventLoopGroup / IoHandleEventLoop and the corrosponding IoHandle, IoHandler and IoHandlerFactory. This allows to only have a custom implement for IO handling per transport while share the same code for execution of tasks, scheduled task and ultimatly IO.
- Add the implementations MultiThreadIoHandleEventLoopGroup and SingleThreadIoHandleEventLoop that can be used by all (or most) transport implementations by having these just provide their own IoHandler / IoHandlerFactory implementations.

Result:

More code sharing and easier of customizing EventLoops.
@He-Pin

He-Pin commented Apr 22, 2024

Copy link
Copy Markdown
Contributor

Do you plan to align with the design in Netty 5?

for (EventExecutor e: this) {
((EpollEventLoop) e).setIoRatio(ioRatio);
}
// NOOP

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here, need to document that this does nothing now

@normanmaurer

Copy link
Copy Markdown
Member Author

Will look into the build timeout later today

@normanmaurer

Copy link
Copy Markdown
Member Author

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

@He-Pin

He-Pin commented Apr 22, 2024

Copy link
Copy Markdown
Contributor

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

I mean split the ioHandler with the event loop, I will take some time to look into this.

@normanmaurer

Copy link
Copy Markdown
Member Author

Do you plan to align with the design in Netty 5?

What design you talk about ? The ideas was to introduce the same concept with minimal real world breakage

I mean split the ioHandler with the event loop, I will take some time to look into this.

That's exactly what this PR does ;)

@normanmaurer

Copy link
Copy Markdown
Member Author

Also I think if we would switch to java8 I could maybe do something more flexible to support different IoHandle in the future.

@violetagg

violetagg commented Apr 22, 2024 •

Copy link
Copy Markdown
Member

@normanmaurer I don't know whether we should discuss this here in this PR but

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might
want to aim for releasing 4.2.0 with the following change:

This PR
Change the minimum java version to 8
Maybe a few more low risk changes.

Have you considered moving out of incubation some of the projects? (io_uring, QUIC, HTTP/3)?

@normanmaurer

Copy link
Copy Markdown
Member Author

@normanmaurer I don't know whether we should discuss this here in this PR but

With all this said I still think the change is too dangerous for a new 4.1.x release. With this in mind I wondered if we might
want to aim for releasing 4.2.0 with the following change:

This PR
Change the minimum java version to 8
Maybe a few more low risk changes.

Have you considered moving out of incubation some of the projects? (io_uring, QUIC, HTTP/3)?

Yes but let's focus on this one first :)

@He-Pin

He-Pin commented Apr 22, 2024 •

Copy link
Copy Markdown
Contributor

I would like to +1 for moving to Java 8 on Netty 4.2.x, as Netty 4.1.x has a very long history, With Java 8's default method, lambda completableFuture, etc., we can achieve more than it is now.

We can submit a PR and wait to see if people complain about moving to Java 8 on Netty 4.2.x

Comment thread pom.xml Resolved
@normanmaurer

Copy link
Copy Markdown
Member Author

I would like to +1 for moving to Java 8 on Netty 4.2.x, as Netty 4.1.x has a very long history, With Java 8's default method, lambda completableFuture, etc., we can achieve more than it is now.

We can submit a PR and wait to see if people complain about moving to Java 8 on Netty 4.2.x

I think we should definitely try or even use 11. Let's discuss about this as a followup.

@normanmaurer
normanmaurer merged commit 596a1f0 into 4.2 Apr 29, 2024
@normanmaurer
normanmaurer deleted the eventloop_rework branch April 29, 2024 18:11
@normanmaurer

Copy link
Copy Markdown
Member Author

Thanks a lot for all the reviews.... This is now merged :)

@normanmaurer normanmaurer added this to the 4.2.0.Final milestone Apr 29, 2024
normanmaurer added a commit that referenced this pull request Apr 30, 2024
…ucting it

Motiviation:

#13991 introduced a new way of how EventLoopGroups should be constructed. We should update our code to use the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
normanmaurer added a commit that referenced this pull request Apr 30, 2024
…ucting it

Motiviation:

#13991 introduced a new way of how EventLoopGroups should be constructed. We should update our code to use the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
normanmaurer added a commit that referenced this pull request May 2, 2024
…ructing it (#14020)

Motiviation:

#13991 introduced a new way of how
EventLoopGroups should be constructed. We should update our code to use
the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
AlohaWorld pushed a commit to AlohaWorld/Netty.Official.Examples that referenced this pull request Mar 1, 2026
…ructing it (#14020)

Motiviation:

netty/netty#13991 introduced a new way of how
EventLoopGroups should be constructed. We should update our code to use
the new way in examples and tests

Modifications:

Rewrite all tests / example to use new way of constructing

Result:
Cleanup
normanmaurer added a commit that referenced this pull request Sep 22, 2026
…ventLoop (#17481)

Motivation:

The unification done in #13991/#14557 replaced the per-transport ioRatio
scheduling with a fixed maxTaskProcessingQuantumMs cap on task
processing
before returning to IO. Under sustained load the task queue is never
empty, so the event loop can spend up to the full quantum on tasks every
iteration regardless of how little time was just spent on IO, delaying
IO servicing (and so response completion) for a minority of requests.
This was observed as an elevated mean (but not median) round-trip
latency after upgrading from 4.1 to 4.2, which went away on rollback.

Modifications:

- Add back a volatile ioRatio field and functional getIoRatio()/
  setIoRatio(int) on SingleThreadIoEventLoop, configurable via the new
  io.netty.eventLoop.ioRatio system property (default 100, which
  preserves the current fixed-quantum behaviour unchanged).
- When ioRatio < 100, size each iteration's task budget as
  ioTime * (100 - ioRatio) / ioRatio, capped by
  maxTaskProcessingQuantumNs, matching the pre-4.2 NioEventLoop formula
  while keeping the existing quantum as a safety upper bound.
- Add setter / getter to MultiThreadIoEventLoopGroup so it is supported
globally

Result:

Users hitting the 4.2 scheduling regression can restore proportional
IO/task balancing with a single setIoRatio(50) call (or the new system
property), with no change in behaviour for anyone who doesn't opt in.
normanmaurer added a commit that referenced this pull request Sep 25, 2026
…eadIoEventLoop (#17481) (#17603)

Motivation:

The unification done in #13991/#14557 replaced the per-transport ioRatio
scheduling with a fixed maxTaskProcessingQuantumMs cap on task
processing
before returning to IO. Under sustained load the task queue is never
empty, so the event loop can spend up to the full quantum on tasks every
iteration regardless of how little time was just spent on IO, delaying
IO servicing (and so response completion) for a minority of requests.
This was observed as an elevated mean (but not median) round-trip
latency after upgrading from 4.1 to 4.2, which went away on rollback.

Modifications:

- Add back a volatile ioRatio field and functional getIoRatio()/
  setIoRatio(int) on SingleThreadIoEventLoop, configurable via the new
  io.netty.eventLoop.ioRatio system property (default 100, which
  preserves the current fixed-quantum behaviour unchanged).
- When ioRatio < 100, size each iteration's task budget as
  ioTime * (100 - ioRatio) / ioRatio, capped by
  maxTaskProcessingQuantumNs, matching the pre-4.2 NioEventLoop formula
  while keeping the existing quantum as a safety upper bound.
- Add setter / getter to MultiThreadIoEventLoopGroup so it is supported
globally

Result:

Users hitting the 4.2 scheduling regression can restore proportional
IO/task balancing with a single setIoRatio(50) call (or the new system
property), with no change in behaviour for anyone who doesn't opt in.
DavideD added a commit to DavideD/netty that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
franz1981 pushed a commit to DavideD/netty that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
franz1981 pushed a commit that referenced this pull request Oct 28, 2026
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 28, 2026
…#18137) (#18141)

Auto-port of #18138 to 5.0
Cherry-picked commit: 06518dc

---
Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s used
about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since #9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and
are not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>

Co-authored-by: Davide D'Alto <[email protected]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…tty/netty#18137)

Motivation:

NioIoHandler.millisBeforeDeadline(...) adds 500_000 ns before dividing
by 1_000_000, which rounds to the nearest millisecond, while the comment
says it rounds up. If the next scheduled task is less than 0.5 ms away
the timeout is 0, so select(...) calls selectNow() and returns. The task
is not due yet, so the event loop runs another iteration and does the
same again until the deadline is reached. An event loop that has I/O and
a pending timer therefore polls the selector in a loop: an application
with a timer that fires every ms, 4 event loops and 4000 requests/s
used about 3 times the CPU of 4.1, with 8 times as many epoll_wait calls.

NioEventLoop in 4.1 rounds up (deadline + 995000) since netty/netty#9799, so the
timeout is only 0 if the deadline is within 5 microseconds. The 4.2
NioIoHandler was introduced (netty/netty#13991) with the older rounding.

Modification:

Add 995_000 instead of 500_000 and adjust the overflow guard to the new
bias. Make the method package private so it can be tested.

Result:

The event loop sleeps in Selector.select(...) until the deadline instead
of spinning. Timers can fire up to 1 ms later than before, like in 4.1.
The epoll, kqueue and io_uring transports use nanosecond timeouts and are
not affected.

Assisted-by: Claude Opus 5.5 <[email protected]>
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

Successfully merging this pull request may close these issues.

6 participants