From 21270e0b4250bd6927e85227fa825cf8ed59aaed Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Thu, 22 Sep 2022 10:55:13 +0200 Subject: refactor(compute): Add separate error host state This change adds a new HostState to indicate that the host is in an error state as opposed to being purposefully unavailable. --- .../org/opendc/compute/service/driver/HostState.kt | 11 ++++++++--- .../compute/service/internal/ComputeServiceImpl.kt | 2 +- .../kotlin/org/opendc/compute/simulator/SimHost.kt | 23 ++++++++-------------- 3 files changed, 17 insertions(+), 19 deletions(-) (limited to 'opendc-compute') diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/driver/HostState.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/driver/HostState.kt index 6d85ee2d..ca6c625c 100644 --- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/driver/HostState.kt +++ b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/driver/HostState.kt @@ -27,12 +27,17 @@ package org.opendc.compute.service.driver */ public enum class HostState { /** - * The host is up. + * The host is up and able to host guests. */ UP, /** - * The host is down. + * The host is in a (forced) down state and unable to host any guests. */ - DOWN + DOWN, + + /** + * The host is in an error state and unable to host any guests. + */ + ERROR, } 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 21aaa19e..52ee780b 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 @@ -411,7 +411,7 @@ internal class ComputeServiceImpl( // Re-schedule on the new machine requestSchedulingCycle() } - HostState.DOWN -> { + else -> { logger.debug { "[${clock.instant()}] Host ${host.uid} state changed: $newState" } val hv = hostToView[host] ?: return diff --git a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt index ece3f752..56b1c8d1 100644 --- a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt +++ b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt @@ -59,7 +59,7 @@ public class SimHost( override val name: String, model: MachineModel, override val meta: Map, - context: CoroutineContext, + private val context: CoroutineContext, engine: FlowEngine, hypervisorProvider: SimHypervisorProvider, random: SplittableRandom, @@ -69,11 +69,6 @@ public class SimHost( private val interferenceDomain: VmInterferenceDomain? = null, private val optimize: Boolean = false ) : Host, AutoCloseable { - /** - * The [CoroutineScope] of the host bounded by the lifecycle of the host. - */ - private val scope: CoroutineScope = CoroutineScope(context + Job()) - /** * The clock instance used by the host. */ @@ -148,7 +143,7 @@ public class SimHost( val interferenceKey = interferenceDomain?.getMember(key.name) val machine = hypervisor.newMachine(key.flavor.toMachineModel(), interferenceKey) val newGuest = Guest( - scope.coroutineContext, + context, clock, this, hypervisor, @@ -195,8 +190,7 @@ public class SimHost( } override fun close() { - reset() - scope.cancel() + reset(HostState.DOWN) machine.cancel() } @@ -271,7 +265,7 @@ public class SimHost( override fun toString(): String = "SimHost[uid=$uid,name=$name,model=$model]" public suspend fun fail() { - reset() + reset(HostState.ERROR) for (guest in _guests) { guest.fail() @@ -310,7 +304,7 @@ public class SimHost( _state = HostState.UP hypervisor.onStart(ctx) } catch (cause: Throwable) { - _state = HostState.DOWN + _state = HostState.ERROR _ctx = null throw cause } @@ -320,7 +314,6 @@ public class SimHost( try { hypervisor.onStop(ctx) } finally { - _state = HostState.DOWN _ctx = null } } @@ -330,12 +323,12 @@ public class SimHost( /** * Reset the machine. */ - private fun reset() { + private fun reset(state: HostState) { updateUptime() // Stop the hypervisor _ctx?.close() - _state = HostState.DOWN + _state = state } /** @@ -386,7 +379,7 @@ public class SimHost( if (_state == HostState.UP) { _uptime += duration - } else if (_state == HostState.DOWN && scope.isActive) { + } else if (_state == HostState.ERROR) { // Only increment downtime if the machine is in a failure state _downtime += duration } -- cgit v1.2.3