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

[scheduler] don't preempt task if worker queue is empty #976

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ abstract private class Worker(
val task = currentTask
val start = taskStartMs
val stalled = (task ne null) && start > 0 && start < nowMs - timeSliceMs
if (stalled) {
if (stalled && !queue.isEmpty()) {
task.doPreempt()
}
stalled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,47 +80,6 @@ class SchedulerTest extends AnyFreeSpec with NonImplicitAssertions {
}
}

"cycle" - {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not easy to test this at the scheduler level because, even if we submit more tasks, the concurrency regulator can introduce new workers. The behavior is covered in WorkerTest so I'm removing the tests here.

"cycles workers and preempts tasks" in withScheduler { scheduler =>
val cdl = new CountDownLatch(1)
val preemptLatch = new CountDownLatch(1)
val task = TestTask(
_preempt = () => preemptLatch.countDown(),
_run = () => {
cdl.await()
Task.Done
}
)
scheduler.schedule(task)
preemptLatch.await()
cdl.countDown()
eventually(assert(task.executions == 1))
}
"repeatedly cycles and preempts tasks" in withScheduler { scheduler =>
val cdl = new CountDownLatch(1)
val preemptions = new AtomicInteger(3)
val preempt = new AtomicBoolean
val task = TestTask(
_preempt = () =>
preempt.set(true),
_run = () => {
if (preemptions.getAndDecrement() > 0) {
while (preempt.compareAndSet(true, false)) {}
Task.Preempted
} else {
cdl.await()
Task.Done
}
}
)
scheduler.schedule(task)

eventually(assert(task.preemptions == 3))
cdl.countDown()
eventually(assert(task.executions == 4))
}
}

"asExecutor" - {
"returns an executor that schedules tasks" in withScheduler { scheduler =>
val executor = scheduler.asExecutor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class WorkerTest extends AnyFreeSpec with NonImplicitAssertions {
cdl.countDown()
}

"preempts long-running task" in withWorker { worker =>
"preempts long-running task if queue isn't empty" in withWorker { worker =>
var preempted = false
val longRunningTask = TestTask(
_run = () => {
Expand All @@ -467,11 +467,27 @@ class WorkerTest extends AnyFreeSpec with NonImplicitAssertions {
_preempt = () => preempted = true
)
worker.enqueue(longRunningTask)
worker.enqueue(longRunningTask)
eventually {
assert(!worker.checkAvailability(System.currentTimeMillis()))
assert(preempted)
}
}
"doesn't preempt long-running task if queue is empty" in withWorker { worker =>
var preempted = false
val longRunningTask = TestTask(
_run = () => {
while (!preempted) {}
Task.Done
},
_preempt = () => preempted = true
)
worker.enqueue(longRunningTask)
eventually {
assert(!worker.checkAvailability(System.currentTimeMillis()))
assert(!preempted)
}
}
"drains queue only once when transitioning to stalled state" in withWorker { worker =>
scheduled.set(0)
val cdl = new CountDownLatch(1)
Expand Down
Loading