Skip to content

Limit the task run per EventLoop tick via a timeout - #14557

Merged
normanmaurer merged 4 commits into
4.2from
task_timeout
Dec 21, 2024
Merged

normanmaurer merged 4 commits into
4.2from
task_timeout

Conversation

@normanmaurer

@normanmaurer normanmaurer commented Dec 12, 2024 •

Copy link
Copy Markdown
Member

Motivation:

We should better allow to limit the number of tasks that are run per EventLoop tick via a timeout. This timeout can be adjusted by -Dio.netty.eventLoop.maxTaskProcessingQuantumMs

Modifications:

Change property name and set default to 1 second.

Result:

Fixes #14165

Motivation:

We should better allow to limit the number of tasks that are run per EventLoop tick via a timeout. This timeout can be adjusted by -Dio.netty.eventLoop.maxTaskProcessingDurationPerRun

Modifications:

Change property name and set default to 1 second.

Result:

Fixes #14165
@normanmaurer

Copy link
Copy Markdown
Member Author

/cc @dreamlike-ocean

@normanmaurer

Copy link
Copy Markdown
Member Author

Replaces #14542

@dreamlike-ocean

Copy link
Copy Markdown
Member

I have another question.
Should we use the final to modify the maxTaskDurationPerRun field?

Perhaps we could provide a setMaxTaskDurationPerRun method to allow users to adjust the configuration of their ioEventLoop?

}
runAllTasks(maxTasksPerRun);
// Now run all tasks for the maximum configured amount of time before trying to run IO again.
runAllTasks(maxTaskDurationPerRun);

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.

this is a best effect (soft limit), not a hard limit too, I'm not sure the property name

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am open for better naming.

@normanmaurer

Copy link
Copy Markdown
Member Author

I have another question. Should we use the final to modify the maxTaskDurationPerRun field?

Perhaps we could provide a setMaxTaskDurationPerRun method to allow users to adjust the configuration of their ioEventLoop?

If we want we can add a constructor parameter for it... But I wanted to keep it simple for now.

// TODO: Is this a sensible default ?
protected static final int DEFAULT_MAX_TASKS_PER_RUN = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskPerRun", 1024 * 4));
protected static final long DEFAULT_MAX_TASKS_PER_RUN_NS = TimeUnit.MILLISECONDS.toNanos(Math.max(10,

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@franz1981 do you think this is enough ? I just wonder if this might let tasks grow.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@franz1981 Do you want to use 100 ms for the minimum quantum, or for the maximum? When I first saw your comment I thought you meant maximum quantum, so as to improve responsiveness in case we get more I/O work while working on the tasks, but on second reading I realize it's not fully clear which one you mean.

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.

Max :) Sorry i haven't explained properly

protected static final int DEFAULT_MAX_TASKS_PER_RUN = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskPerRun", 1024 * 4));
protected static final long DEFAULT_MAX_TASKS_PER_RUN_NS = TimeUnit.MILLISECONDS.toNanos(Math.max(10,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskProcessingDurationPerRun", 1000)));

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.

io.netty.eventLoop.maxTaskProcessingDurationPerRunSoftLimit what about

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In operating system lingo I think this is called a quantum, so it could also be io.netty.eventLoop.maxTaskProcessingQuantumSoftLimit, but He-Pin's suggestion is also fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we include the units in the name of the property to avoid confusion and incorrect units? io.netty.eventLoop.maxTaskProcessingQuantumNsSoftLimit

@bryce-anderson bryce-anderson Dec 20, 2024 •

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.

+1 to @Scottmitch's suggestion of including units in the name although the units look like their in ms and typically the units are at the end. I'd personally go with io.netty.eventLoop.maxTaskProcessingQuantumMs but don't have strong opinions other than including the units.

@normanmaurer normanmaurer added this to the 4.2.0.Final milestone Dec 13, 2024
protected static final int DEFAULT_MAX_TASKS_PER_RUN = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskPerRun", 1024 * 4));
protected static final long DEFAULT_MAX_TASKS_PER_RUN_NS = TimeUnit.MILLISECONDS.toNanos(Math.max(10,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskProcessingDurationPerRun", 1000)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we include the units in the name of the property to avoid confusion and incorrect units? io.netty.eventLoop.maxTaskProcessingQuantumNsSoftLimit

@chrisvest chrisvest left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are some good suggestions from others I'd also like to see, but I'll pre-approve here.

// TODO: Is this a sensible default ?
protected static final int DEFAULT_MAX_TASKS_PER_RUN = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskPerRun", 1024 * 4));
protected static final long DEFAULT_MAX_TASK_PROCESSING_QUANTUM_NS= TimeUnit.MILLISECONDS.toNanos(Math.max(100,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@franz1981 is this what you had in mind ?

// TODO: Is this a sensible default ?
protected static final int DEFAULT_MAX_TASKS_PER_RUN = Math.max(1,
SystemPropertyUtil.getInt("io.netty.eventLoop.maxTaskPerRun", 1024 * 4));
private static final long DEFAULT_MAX_TASK_PROCESSING_QUANTUM_NS = TimeUnit.MILLISECONDS.toNanos(Math.max(100,

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.

Yeah, in CFS this should be the target latency within a process should get its guaranteed fair schedule/chance to run IIRC.
And here, have a similarish meaning i.e. I/O will get its chance to run after this, although with a delay (our tasks are not preemptive).

TBH what CFS does is to update via a timer (the OS tick rate) the virtual time of the task running on CPU, guaranteed a min granularity time to run on the CPU or eventually perform a different decision if there are more.

@normanmaurer
normanmaurer merged commit fbe14e1 into 4.2 Dec 21, 2024
@normanmaurer
normanmaurer deleted the task_timeout branch December 21, 2024 19:03
@normanmaurer normanmaurer modified the milestones: 4.2.0.Final, 4.2.0.RC2 Jan 14, 2025
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.
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.

7 participants