-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Acquire fewer locks in TaskRunner #8394
Open
swankjesse
wants to merge
3
commits into
master
Choose a base branch
from
jwilson.0428.fewer_locks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,31 +72,36 @@ class TaskRunner( | |
private val runnable: Runnable = | ||
object : Runnable { | ||
override fun run() { | ||
var incrementedRunCallCount = false | ||
while (true) { | ||
val task = | ||
[email protected] { | ||
if (!incrementedRunCallCount) { | ||
incrementedRunCallCount = true | ||
runCallCount++ | ||
var task: Task = | ||
lock.withLock { | ||
runCallCount++ | ||
awaitTaskToRun() | ||
} ?: return | ||
|
||
val currentThread = Thread.currentThread() | ||
val oldName = currentThread.name | ||
try { | ||
while (true) { | ||
currentThread.name = task.name | ||
val delayNanos = | ||
logger.logElapsed(task, task.queue!!) { | ||
task.runOnce() | ||
} | ||
|
||
// A task ran successfully. Update the execution state and take the next task. | ||
task = lock.withLock { | ||
afterRun(task, delayNanos, true) | ||
awaitTaskToRun() | ||
} ?: return | ||
|
||
logger.logElapsed(task, task.queue!!) { | ||
var completedNormally = false | ||
try { | ||
runTask(task) | ||
completedNormally = true | ||
} finally { | ||
// If the task is crashing start another thread to service the queues. | ||
if (!completedNormally) { | ||
lock.withLock { | ||
startAnotherThread() | ||
} | ||
} | ||
} | ||
} | ||
} catch (thrown: Throwable) { | ||
// A task failed. Update execution state and re-throw the exception. | ||
lock.withLock { | ||
afterRun(task, -1L, false) | ||
} | ||
throw thrown | ||
} finally { | ||
currentThread.name = oldName | ||
} | ||
} | ||
} | ||
|
@@ -130,25 +135,10 @@ class TaskRunner( | |
busyQueues.add(queue) | ||
} | ||
|
||
private fun runTask(task: Task) { | ||
val currentThread = Thread.currentThread() | ||
val oldName = currentThread.name | ||
currentThread.name = task.name | ||
|
||
var delayNanos = -1L | ||
try { | ||
delayNanos = task.runOnce() | ||
} finally { | ||
lock.withLock { | ||
afterRun(task, delayNanos) | ||
} | ||
currentThread.name = oldName | ||
} | ||
} | ||
|
||
private fun afterRun( | ||
task: Task, | ||
delayNanos: Long, | ||
completedNormally: Boolean, | ||
) { | ||
lock.assertHeld() | ||
|
||
|
@@ -166,6 +156,11 @@ class TaskRunner( | |
|
||
if (queue.futureTasks.isNotEmpty()) { | ||
readyQueues.add(queue) | ||
|
||
// If the task crashed, start another thread to run the next task. | ||
if (!completedNormally) { | ||
startAnotherThread() | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,11 +95,11 @@ class TaskRunnerTest { | |
assertThat(testLogHandler.takeAll()).containsExactly( | ||
"FINE: Q10000 scheduled after 100 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this new order better There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 more logical |
||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 run again after 150 µs: task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
"FINE: Q10000 run again after 150 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
) | ||
|
@@ -137,8 +137,8 @@ class TaskRunnerTest { | |
"FINE: Q10000 scheduled after 100 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 scheduled after 50 µs: task", | ||
"FINE: Q10000 already scheduled : task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
"FINE: Q10000 already scheduled : task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
) | ||
|
@@ -176,8 +176,8 @@ class TaskRunnerTest { | |
"FINE: Q10000 scheduled after 100 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 scheduled after 200 µs: task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
) | ||
|
@@ -306,8 +306,8 @@ class TaskRunnerTest { | |
assertThat(testLogHandler.takeAll()).containsExactly( | ||
"FINE: Q10000 scheduled after 100 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
"FINE: Q10000 run again after 50 µs: task", | ||
"FINE: Q10000 starting : task", | ||
"FINE: Q10000 finished run in 0 µs: task", | ||
) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also change the thread name fewer times!