Repository navigation
Restore configurable ioRatio-based task scheduling in SingleThreadIoEventLoop - #17481
Conversation
…ventLoop 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.
There was a problem hiding this comment.
ObjectUtil.checkInRange is inclusive, so setIoRatio(0) is accepted and line 212 then does ioTime * 100 / 0 -> ArithmeticException out of run(), killing the loop and every channel on it. 4.1 rejected <= 0.
- this.ioRatio = ObjectUtil.checkInRange(ioRatio, 0, 100, "ioRatio");
+ this.ioRatio = ObjectUtil.checkInRange(ioRatio, 1, 100, "ioRatio");That's the red SingleThreadIoEventLoopTest.testSetIoRatio() on CI - the test is right.
Same issue on DEFAULT_IO_RATIO, which isn't validated at all: -Dio.netty.eventLoop.ioRatio=0 kills every loop in the JVM; >100 or negative yields a negative quantum, so runAllTasks falls into the deadline = 0 branch and silently caps at 64 tasks per iteration.
doh! you are right... fixed both. PTAL again. |
There was a problem hiding this comment.
Default 100 means the fix is off unless you already know the bug exists.
The symptom is mean-only with the median unmoved, so nobody goes looking for an event loop knob - the reported path was upgrade, mean RTT up, roll back to 4.1.
What 100 preserves also isn't a design we picked: before #14557 the budget was runAllTasks(4096), a task count in a nanos parameter, which always expired at the first 64-task check. Fixing the unit to 1s removed that implicit ceiling too. So 100 pins compatibility to an accident rather than to 4.1.
Why not default to 50?
|
I am not strong on it ... I just wanted to restore the ability to set a IO ratio again . What the default should be is another question |
|
Remaining issues after 1. 2. 3. 4. 5. Two validation policies on one field. The property clamps silently ( 6. Deprecated no-op I haven't run revapi on JDK 11, so no claim either way on whether the removed public methods trip the API check. |
f2a592d to
83d8765
Compare
|
@franz1981 I think 1,3 and 4 are valid... just pushed a fix for these. PTAL again |
There was a problem hiding this comment.
Measurement looks right now, thanks. Issues in the new version:
1. shouldReportActiveIoTime() should compare against 100, not DEFAULT_IO_RATIO. When the ratio comes from the property, ioRatio == DEFAULT_IO_RATIO, so that clause is false and the feature rests entirely on isSuspensionSupported(). That's true for nio/epoll/kqueue, but io_uring is !copy.singleIssuer() and singleIssuer defaults to true, and LocalIoHandler inherits the interface default false. On those, -Dio.netty.eventLoop.ioRatio=50 leaves activeIoTimeNanos == -1, so run() takes the full-quantum branch and the property silently does nothing - while setIoRatio(50) works.
2. activeIoTimeNanos is sticky. Nothing resets it before runIo(), and handlers can return without reporting: NioIoHandler:470-475 catches IOException, rebuilds the selector, sleeps 1s in handleLoopException and return 0s; and the report sits after processSelectedKeys() with no finally. Either way the next budget is computed from an older iteration. activeIoTimeNanos = -1 before runIo() fixes it.
3. LocalIoHandler reports 0 unconditionally, so with ioRatio < 100 its budget is permanently 0 - 64 tasks per iteration forever. Probably intended (matches 4.1's runAllTasks(0)), but the javadoc only describes a budget "proportional to the time just spent on IO" and never mentions the floor.
4. The override changes reportActiveIoTime semantics from accumulate (accumulatedActiveTimeNanos += nanos, "@PARAM nanos The active time in nanoseconds to add") to assignment. Latent - every in-tree handler reports once per run() - but the two overloads now disagree.
5. testDefaultIoRatio is tautological: it compares DEFAULT_IO_RATIO against a field initialised from DEFAULT_IO_RATIO, so it passes unconditionally and no longer checks that the default is 100.
Still open from before: the property clamps 0 -> 1 silently while setIoRatio(0) throws, and the deprecated no-op setIoRatio on the Nio/Epoll/KQueue loops and groups goes live on upgrade (BurstCostExecutorsBenchmark:209,216,223 is the in-tree case).
Minor: the commit message says "setter / getter" for MultiThreadIoEventLoopGroup, but only the setter is there.
|
@franz1981 thanks.... I fixed everything that is not by design. PTAL again |
|
Both fixes look right. One thing the reset introduced, though: when no measurement arrives, the fallback is now the maximum budget rather than a stale one. activeIoTimeNanos = -1;
runIo();
if (ioRatio == 100 || activeIoTimeNanos == -1) {
taskQuantumNs = maxTaskProcessingQuantumNs; // full quantum
}Both handlers have paths that return from
Before the reset those iterations reused a stale (usually small) value; now they get the full Same applies to any third-party Suggestion: keep - if (ioRatio == 100 || activeIoTimeNanos == -1) {
+ if (ioRatio == 100) {
taskQuantumNs = maxTaskProcessingQuantumNs;
+ } else if (activeIoTimeNanos == -1) {
+ taskQuantumNs = 0;
} else { |
I think it is fine as it is... these |
|
@dreamlike-ocean any concerns? you raised the point for the uring case for ioRatio IIRC? |
My earlier concern was that we couldn't distinguish time spent in blocking syscalls from time spent actively processing I/O. Now that we have |
|
@dreamlike-ocean PTAL as well |
| // Give tasks a budget proportional to the time just spent on IO, still bounded by the | ||
| // configured maximum so a burst of IO activity cannot starve the task queue indefinitely. | ||
| long quantum = Math.min(maxTaskProcessingQuantumNs, activeIoTimeNanos * (100 - ioRatio) / ioRatio); | ||
| if (quantum == 0) { |
There was a problem hiding this comment.
1.Tasks remain queued(the task budget was exhausted), so epoll polls without blocking.
2.No ready events → activeIoTime = 0.
3.The full-quantum fallback can delay newly arriving I/O.
This may be an edge case, but when it happens, netty will fall back to the same behavior this PR is trying to fix.
There was a problem hiding this comment.
Imho this is fine as it would be an edge case and only be true for a Limited time
There was a problem hiding this comment.
@dreamlike-ocean or any concerns with just ignoring this case ?
There was a problem hiding this comment.
OK, no further concerns from me.
✅ All tests passed ✅Test SummaryBuild PR / linux-x86_64-java17 build > Netty/Handler/Ssl/Ocsp
Build PR / linux-x86_64-java26 build > Netty/Handler/Ssl/Ocsp
Build PR / macos-aarch64-java11-boringssl build > Netty/Transport/Native/KQueue
🏷️ Commit: 375e9bf Test FailuresOcspServerCertificateValidatorTest (Netty/Handler/Ssl/Ocsp in Build PR / linux-x86_64-java17 build | Attempt 1/4)OcspServerCertificateValidatorTest (Netty/Handler/Ssl/Ocsp in Build PR / linux-x86_64-java26 build | Attempt 3/4)OcspServerCertificateValidatorTest (Netty/Handler/Ssl/Ocsp in Build PR / linux-x86_64-java26 build | Attempt 2/4)OcspServerCertificateValidatorTest (Netty/Handler/Ssl/Ocsp in Build PR / linux-x86_64-java26 build | Attempt 1/4)KQueueDatagramUnicastIPv6MappedTest > testSimpleSendWithoutBind(TestInfo) (Netty/Transport/Native/KQueue in Build PR / macos-aarch64-java11-boringssl build | Attempt 1/4)Learn more about TestLens at testlens.app/docs. |
| */ | ||
| public void setIoRatio(int ioRatio) { | ||
| ObjectUtil.checkInRange(ioRatio, 1, 100, "ioRatio"); | ||
| for (EventExecutor e: this) { |
There was a problem hiding this comment.
| for (EventExecutor e: this) { | |
| for (EventExecutor e : this) { |
| } else { | ||
| // Give tasks a budget proportional to the time just spent on IO, still bounded by the | ||
| // configured maximum so a burst of IO activity cannot starve the task queue indefinitely. | ||
| long quantum = Math.min(maxTaskProcessingQuantumNs, activeIoTimeNanos * (100 - ioRatio) / ioRatio); |
There was a problem hiding this comment.
what's special about 100 here? From this code it doesn't look like a percentage
There was a problem hiding this comment.
It's exactly the same how we did calculate it in 4.1 or I am missing something: https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java#L566 ?
yawkat
left a comment
There was a problem hiding this comment.
i think it'll work fine but the significance of 100 mystifies me. it just seems arbitrary
|
Could not create auto-port PR. |
|
Port for 5.0: #17603 |
…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.
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:
setIoRatio(int) on SingleThreadIoEventLoop, configurable via the new
io.netty.eventLoop.ioRatio system property (default 100, which
preserves the current fixed-quantum behaviour unchanged).
ioTime * (100 - ioRatio) / ioRatio, capped by
maxTaskProcessingQuantumNs, matching the pre-4.2 NioEventLoop formula
while keeping the existing quantum as a safety upper bound.
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.