-
Notifications
You must be signed in to change notification settings - Fork 13
Use TimingWheel on Scheduler when possible #671
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
Open
Intybyte
wants to merge
10
commits into
master
Choose a base branch
from
vaan/opt/scheduler
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
10 commits
Select commit
Hold shift + click to select a range
66a9711
Use TimingWheel when possible
Intybyte bc4005b
Refine Scheduler interface
Intybyte 3a94fa4
Make PriorityQueueScheduler thread safe
Intybyte 06d7d82
Relocate classes
Intybyte 1f285aa
Cleanup and small changes
Intybyte 72b3977
Use synchronizedQueue
Intybyte 93ba64f
Formatting goof
Seggan b4d59ef
Make TimingWheel internal
Intybyte d792a61
REEEEEEEEEEEEEBARRRRR
Intybyte 4d109fc
Whopsie
Intybyte 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
rebar/src/main/kotlin/io/github/pylonmc/rebar/async/RebarScheduledTask.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.github.pylonmc.rebar.async | ||
|
|
||
| data class RebarScheduledTask(val executeTick: Long, val runnable: Runnable) : Comparable<RebarScheduledTask> { | ||
| override fun compareTo(other: RebarScheduledTask): Int { | ||
| return executeTick.compareTo(other.executeTick) | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
rebar/src/main/kotlin/io/github/pylonmc/rebar/async/schedulers/PriorityQueueScheduler.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import com.google.common.collect.Queues | ||
| import io.github.pylonmc.rebar.async.RebarScheduledTask | ||
| import java.util.* | ||
|
|
||
| /** | ||
| * Scheduler using a [PriorityQueue] as a delegate | ||
| * | ||
| * O(log n) insertions and evictions | ||
| */ | ||
| class PriorityQueueScheduler : Scheduler { | ||
| private val taskQueue = Queues.synchronizedQueue(PriorityQueue<RebarScheduledTask>()) | ||
|
|
||
| override fun schedule(executeAt: Long, runnable: Runnable) { | ||
| taskQueue.add(RebarScheduledTask(executeAt, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(currentTick: Long): List<RebarScheduledTask> { | ||
| val list = mutableListOf<RebarScheduledTask>() | ||
| while (taskQueue.isNotEmpty() && taskQueue.peek().executeTick <= currentTick) { | ||
| val task = taskQueue.poll() | ||
| list.add(task) | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
rebar/src/main/kotlin/io/github/pylonmc/rebar/async/schedulers/Scheduler.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import io.github.pylonmc.rebar.async.RebarScheduledTask | ||
|
|
||
| interface Scheduler { | ||
|
|
||
| /** | ||
| * Adds a task to the scheduler | ||
| */ | ||
| fun schedule(executeAt: Long, runnable: Runnable) | ||
|
|
||
| /** | ||
| * Gets and evicts valid tasks | ||
| */ | ||
| fun getValid(currentTick: Long) : List<RebarScheduledTask> | ||
| } |
57 changes: 57 additions & 0 deletions
57
rebar/src/main/kotlin/io/github/pylonmc/rebar/async/schedulers/TimingWheel.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package io.github.pylonmc.rebar.async.schedulers | ||
|
|
||
| import io.github.pylonmc.rebar.async.RebarScheduledTask | ||
| import java.util.concurrent.ConcurrentLinkedQueue | ||
|
|
||
|
|
||
| /** | ||
| * This class schedules tasks in ticks and executes them efficiently using a circular array (the wheel). | ||
| * Each slot in the wheel represents a specific tick modulo the wheel size. | ||
| * Tasks are placed into slots based on their target execution tick. | ||
| * On each tick, the wheel checks the current slot and runs any tasks whose execute tick has been reached. | ||
| * | ||
| * O(1) task scheduling and retrieval within a single wheel rotation. | ||
| * We are using power of 2 for faster operations than modulo (even though I doubt there would be much improvement) | ||
| * | ||
| * @param exponent wheel size (wheelSize = 2 ^ exponent) | ||
| */ | ||
| internal class TimingWheel(exponent: Int) : Scheduler { | ||
| // note: if we are going to use a lot of tasks with a long delay, | ||
| // maybe add round support | ||
| private val wheelSize = 1 shl exponent | ||
| private val mask = wheelSize - 1L | ||
| private val wheel = Array(wheelSize) { ArrayDeque<RebarScheduledTask>() } | ||
| // use thread safe queue | ||
| private val incoming = ConcurrentLinkedQueue<RebarScheduledTask>() | ||
|
|
||
| override fun schedule(executeAt: Long, runnable: Runnable) { | ||
| incoming.add(RebarScheduledTask(executeAt, runnable)) | ||
| } | ||
|
|
||
| override fun getValid(currentTick: Long) : List<RebarScheduledTask> { | ||
| while (true) { | ||
| val task = incoming.poll() ?: break | ||
| val slot = (task.executeTick and mask).toInt() | ||
| wheel[slot].add(task) | ||
| } | ||
|
|
||
| val slot = (currentTick and mask).toInt() | ||
| val bucket = wheel[slot] | ||
| if (bucket.isEmpty()) { | ||
| return emptyList() | ||
| } | ||
|
|
||
| val iter = bucket.iterator() | ||
| val list = mutableListOf<RebarScheduledTask>() | ||
| while (iter.hasNext()) { | ||
| val task = iter.next() | ||
|
|
||
| if (task.executeTick <= currentTick) { | ||
| list.add(task) | ||
| iter.remove() | ||
| } | ||
| } | ||
|
|
||
| return list | ||
| } | ||
| } |
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.
Am I reading the docs right that you can only schedule tasks for tick 2^exponent? A little confused as to how this works, does it actually behave any differently from a priority queue in terms of when it runs task? The docs seem to imply so or maybe I am reading them wrong?
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.
No, view it as an hashmap based on the execution tick
It runs and check the tasks for a specific tick so the tasks are ran the same for tickSpeed = 1, otherwise it would make some buckets useless and it could break if tasks fall in said buckets
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.
for example if it does tickspeed 2 it would skip all the even position buckets, and if you run a delay and falls into said positions it would break said task