summaryrefslogtreecommitdiff
path: root/opendc-compute/opendc-compute-service
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-09 21:59:07 +0000
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-13 17:42:01 +0000
commitfb2672afb2d8236d5291cd028196c99d8e4d47f1 (patch)
tree508bbec117239b3d8490cd1bde8d12b6a8ab2155 /opendc-compute/opendc-compute-service
parent00ac59e8e9d6a41c2eac55aa25420dce8fa9c6e0 (diff)
refactor: Replace use of CoroutineContext by Dispatcher
This change replaces the use of `CoroutineContext` for passing the `SimulationDispatcher` across the different modules of OpenDC by the lightweight `Dispatcher` interface of the OpenDC common module.
Diffstat (limited to 'opendc-compute/opendc-compute-service')
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/ComputeService.kt11
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt12
-rw-r--r--opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt3
3 files changed, 10 insertions, 16 deletions
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/ComputeService.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/ComputeService.kt
index 4ced9569..9d7dcba6 100644
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/ComputeService.kt
+++ b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/ComputeService.kt
@@ -22,6 +22,7 @@
package org.opendc.compute.service
+import org.opendc.common.Dispatcher
import org.opendc.compute.api.ComputeClient
import org.opendc.compute.api.Server
import org.opendc.compute.service.driver.Host
@@ -29,8 +30,6 @@ import org.opendc.compute.service.internal.ComputeServiceImpl
import org.opendc.compute.service.scheduler.ComputeScheduler
import org.opendc.compute.service.telemetry.SchedulerStats
import java.time.Duration
-import java.time.InstantSource
-import kotlin.coroutines.CoroutineContext
/**
* The [ComputeService] hosts the API implementation of the OpenDC Compute service.
@@ -80,18 +79,16 @@ public interface ComputeService : AutoCloseable {
/**
* Construct a new [ComputeService] implementation.
*
- * @param context The [CoroutineContext] to use in the service.
- * @param clock The clock instance to use.
+ * @param dispatcher The [Dispatcher] for scheduling future events.
* @param scheduler The scheduler implementation to use.
* @param schedulingQuantum The interval between scheduling cycles.
*/
public operator fun invoke(
- context: CoroutineContext,
- clock: InstantSource,
+ dispatcher: Dispatcher,
scheduler: ComputeScheduler,
schedulingQuantum: Duration = Duration.ofMinutes(5)
): ComputeService {
- return ComputeServiceImpl(context, clock, scheduler, schedulingQuantum)
+ return ComputeServiceImpl(dispatcher, scheduler, schedulingQuantum)
}
}
}
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 2b755988..77932545 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
@@ -23,6 +23,7 @@
package org.opendc.compute.service.internal
import mu.KotlinLogging
+import org.opendc.common.Dispatcher
import org.opendc.common.util.Pacer
import org.opendc.compute.api.ComputeClient
import org.opendc.compute.api.Flavor
@@ -37,25 +38,21 @@ import org.opendc.compute.service.scheduler.ComputeScheduler
import org.opendc.compute.service.telemetry.SchedulerStats
import java.time.Duration
import java.time.Instant
-import java.time.InstantSource
import java.util.ArrayDeque
import java.util.Deque
import java.util.Random
import java.util.UUID
-import kotlin.coroutines.CoroutineContext
import kotlin.math.max
/**
* Internal implementation of the OpenDC Compute service.
*
- * @param coroutineContext The [CoroutineContext] to use in the service.
- * @param clock The clock instance to use.
+ * @param dispatcher The [Dispatcher] for scheduling future events.
* @param scheduler The scheduler implementation to use.
* @param schedulingQuantum The interval between scheduling cycles.
*/
internal class ComputeServiceImpl(
- coroutineContext: CoroutineContext,
- private val clock: InstantSource,
+ private val dispatcher: Dispatcher,
private val scheduler: ComputeScheduler,
schedulingQuantum: Duration
) : ComputeService, HostListener {
@@ -108,6 +105,7 @@ internal class ComputeServiceImpl(
override val hosts: Set<Host>
get() = hostToView.keys
+ private val clock = dispatcher.timeSource
private var maxCores = 0
private var maxMemory = 0L
private var _attemptsSuccess = 0L
@@ -120,7 +118,7 @@ internal class ComputeServiceImpl(
/**
* The [Pacer] to use for scheduling the scheduler cycles.
*/
- private val pacer = Pacer(coroutineContext, clock, schedulingQuantum.toMillis()) { doSchedule() }
+ private val pacer = Pacer(dispatcher, schedulingQuantum.toMillis()) { doSchedule() }
override fun newClient(): ComputeClient {
check(!isClosed) { "Service is already closed" }
diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt
index b790d36f..b5685aba 100644
--- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt
+++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt
@@ -62,12 +62,11 @@ internal class ComputeServiceTest {
@BeforeEach
fun setUp() {
scope = SimulationCoroutineScope()
- val clock = scope.timeSource
val computeScheduler = FilterScheduler(
filters = listOf(ComputeFilter(), VCpuFilter(allocationRatio = 1.0), RamFilter(allocationRatio = 1.0)),
weighers = listOf(RamWeigher())
)
- service = ComputeService(scope.coroutineContext, clock, computeScheduler)
+ service = ComputeService(scope.dispatcher, computeScheduler)
}
@Test