summaryrefslogtreecommitdiff
path: root/opendc-compute
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-02-18 12:35:11 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-02-18 14:46:16 +0100
commit028960fbf584c903156c713447194df56ec5059e (patch)
tree5526a7d82e800263df4d6176e985f4634475c379 /opendc-compute
parentd7c173f0f7b4cb2584a498155519c287abedeae9 (diff)
feat(utils): Add Pacer to pace scheduling cycles
This change adds a new Pacer class that can pace the incoming scheduling requests into scheduling cycles by allowing the user to specify a scheduling quantum.
Diffstat (limited to 'opendc-compute')
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt26
1 files changed, 8 insertions, 18 deletions
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt
index 27a6ecae..6df3c4f9 100644
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt
+++ b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt
@@ -35,7 +35,7 @@ import org.opendc.compute.service.driver.Host
import org.opendc.compute.service.driver.HostListener
import org.opendc.compute.service.driver.HostState
import org.opendc.compute.service.scheduler.ComputeScheduler
-import org.opendc.utils.TimerScheduler
+import org.opendc.utils.Pacer
import java.time.Clock
import java.time.Duration
import java.util.*
@@ -56,7 +56,7 @@ internal class ComputeServiceImpl(
private val clock: Clock,
meterProvider: MeterProvider,
private val scheduler: ComputeScheduler,
- private val schedulingQuantum: Duration
+ schedulingQuantum: Duration
) : ComputeService, HostListener {
/**
* The [CoroutineScope] of the service bounded by the lifecycle of the service.
@@ -147,9 +147,9 @@ internal class ComputeServiceImpl(
private val _serversActiveAttr = Attributes.of(AttributeKey.stringKey("state"), "active")
/**
- * The [TimerScheduler] to use for scheduling the scheduler cycles.
+ * The [Pacer] to use for scheduling the scheduler cycles.
*/
- private var timerScheduler: TimerScheduler<Unit> = TimerScheduler(scope.coroutineContext, clock)
+ private val pacer = Pacer(scope.coroutineContext, clock, schedulingQuantum.toMillis(), ::doSchedule)
override val hosts: Set<Host>
get() = hostToView.keys
@@ -354,28 +354,18 @@ internal class ComputeServiceImpl(
* Indicate that a new scheduling cycle is needed due to a change to the service's state.
*/
private fun requestSchedulingCycle() {
- // Bail out in case we have already requested a new cycle or the queue is empty.
- if (timerScheduler.isTimerActive(Unit) || queue.isEmpty()) {
+ // Bail out in case the queue is empty.
+ if (queue.isEmpty()) {
return
}
- val quantum = schedulingQuantum.toMillis()
-
- // We assume that the provisioner runs at a fixed slot every time quantum (e.g t=0, t=60, t=120).
- // This is important because the slices of the VMs need to be aligned.
- // We calculate here the delay until the next scheduling slot.
- val delay = quantum - (clock.millis() % quantum)
-
- timerScheduler.startSingleTimer(Unit, delay) {
- doSchedule()
- }
+ pacer.enqueue()
}
/**
* Run a single scheduling iteration.
*/
- private fun doSchedule() {
- val now = clock.millis()
+ private fun doSchedule(now: Long) {
while (queue.isNotEmpty()) {
val request = queue.peek()