From cfb3a87edad5093b6f75e0bbde0cfdb58fdc9487 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 14 Feb 2020 12:54:06 +0100 Subject: refactor: Rename FakeBareMetalDriver to SimpleBareMetalDriver --- .../compute/metal/driver/FakeBareMetalDriver.kt | 146 --------------------- .../compute/metal/driver/SimpleBareMetalDriver.kt | 145 ++++++++++++++++++++ .../metal/driver/FakeBareMetalDriverTest.kt | 70 ---------- .../metal/driver/SimpleBareMetalDriverTest.kt | 70 ++++++++++ .../metal/service/SimpleProvisioningServiceTest.kt | 4 +- .../environment/sc18/Sc18EnvironmentReader.kt | 4 +- 6 files changed, 219 insertions(+), 220 deletions(-) delete mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriver.kt create mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt delete mode 100644 opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriverTest.kt create mode 100644 opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriverTest.kt (limited to 'opendc') diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriver.kt deleted file mode 100644 index cc4cc46c..00000000 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriver.kt +++ /dev/null @@ -1,146 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.opendc.compute.metal.driver - -import com.atlarge.odcsim.processContext -import com.atlarge.opendc.compute.core.ServerFlavor -import com.atlarge.opendc.compute.core.Server -import com.atlarge.opendc.compute.core.ServerState -import com.atlarge.opendc.compute.core.execution.ServerManagementContext -import com.atlarge.opendc.compute.core.image.EmptyImage -import com.atlarge.opendc.compute.core.image.Image -import com.atlarge.opendc.compute.core.monitor.ServerMonitor -import com.atlarge.opendc.compute.metal.Node -import com.atlarge.opendc.compute.metal.PowerState -import java.util.UUID -import kotlin.math.max -import kotlinx.coroutines.delay - -/** - * A implementation of the [BareMetalDriver] that simulates an [Image] running on a bare-metal machine, but performs - * not actual computation. - * - * @param uid The unique identifier of the machine. - * @param name An optional name of the machine. - * @param flavor The hardware configuration of the machine. - */ -public class FakeBareMetalDriver( - uid: UUID, - name: String, - private val flavor: ServerFlavor -) : BareMetalDriver { - /** - * The monitor to use. - */ - private lateinit var monitor: ServerMonitor - - /** - * The machine state. - */ - private var node: Node = Node(uid, name, PowerState.POWER_OFF, EmptyImage, null) - - override suspend fun init(monitor: ServerMonitor): Node { - this.monitor = monitor - return node - } - - override suspend fun setPower(powerState: PowerState): Node { - val previousPowerState = node.powerState - val server = when (node.powerState to powerState) { - PowerState.POWER_OFF to PowerState.POWER_OFF -> null - PowerState.POWER_OFF to PowerState.POWER_ON -> Server(UUID.randomUUID(), node.name, flavor, node.image, ServerState.BUILD) - PowerState.POWER_ON to PowerState.POWER_OFF -> null // TODO Terminate existing image - PowerState.POWER_ON to PowerState.POWER_ON -> node.server - else -> throw IllegalStateException() - } - node = node.copy(powerState = powerState, server = server) - - if (powerState != previousPowerState && server != null) { - launch() - } - - return node - } - - override suspend fun setImage(image: Image): Node { - node = node.copy(image = image) - return node - } - - override suspend fun refresh(): Node = node - - /** - * Launch the server image on the machine. - */ - private suspend fun launch() { - val serverCtx = this.serverCtx - - processContext.spawn { - serverCtx.init() - try { - node.server!!.image(serverCtx) - serverCtx.exit() - } catch (cause: Throwable) { - serverCtx.exit(cause) - } - } - } - - private val serverCtx = object : ServerManagementContext { - private var initialized: Boolean = false - - override var server: Server - get() = node.server!! - set(value) { - node = node.copy(server = value) - } - - override suspend fun init() { - if (initialized) { - throw IllegalStateException() - } - - val previousState = server.state - server = server.copy(state = ServerState.ACTIVE) - monitor.onUpdate(server, previousState) - - initialized = true - } - - override suspend fun exit(cause: Throwable?) { - val previousState = server.state - val state = if (cause == null) ServerState.SHUTOFF else ServerState.ERROR - server = server.copy(state = state) - monitor.onUpdate(server, previousState) - initialized = false - } - - override suspend fun run(req: LongArray) { - // TODO Properly implement this for multiple CPUs - val time = max(0, req.max() ?: 0) * flavor.cpus[0].clockRate - delay(time.toLong()) - } - } -} diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt new file mode 100644 index 00000000..56fee524 --- /dev/null +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriver.kt @@ -0,0 +1,145 @@ +/* + * MIT License + * + * Copyright (c) 2020 atlarge-research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.atlarge.opendc.compute.metal.driver + +import com.atlarge.odcsim.processContext +import com.atlarge.opendc.compute.core.ServerFlavor +import com.atlarge.opendc.compute.core.Server +import com.atlarge.opendc.compute.core.ServerState +import com.atlarge.opendc.compute.core.execution.ServerManagementContext +import com.atlarge.opendc.compute.core.image.EmptyImage +import com.atlarge.opendc.compute.core.image.Image +import com.atlarge.opendc.compute.core.monitor.ServerMonitor +import com.atlarge.opendc.compute.metal.Node +import com.atlarge.opendc.compute.metal.PowerState +import java.util.UUID +import kotlin.math.max +import kotlinx.coroutines.delay + +/** + * A basic implementation of the [BareMetalDriver] that simulates an [Image] running on a bare-metal machine. + * + * @param uid The unique identifier of the machine. + * @param name An optional name of the machine. + * @param flavor The hardware configuration of the machine. + */ +public class SimpleBareMetalDriver( + uid: UUID, + name: String, + private val flavor: ServerFlavor +) : BareMetalDriver { + /** + * The monitor to use. + */ + private lateinit var monitor: ServerMonitor + + /** + * The machine state. + */ + private var node: Node = Node(uid, name, PowerState.POWER_OFF, EmptyImage, null) + + override suspend fun init(monitor: ServerMonitor): Node { + this.monitor = monitor + return node + } + + override suspend fun setPower(powerState: PowerState): Node { + val previousPowerState = node.powerState + val server = when (node.powerState to powerState) { + PowerState.POWER_OFF to PowerState.POWER_OFF -> null + PowerState.POWER_OFF to PowerState.POWER_ON -> Server(UUID.randomUUID(), node.name, flavor, node.image, ServerState.BUILD) + PowerState.POWER_ON to PowerState.POWER_OFF -> null // TODO Terminate existing image + PowerState.POWER_ON to PowerState.POWER_ON -> node.server + else -> throw IllegalStateException() + } + node = node.copy(powerState = powerState, server = server) + + if (powerState != previousPowerState && server != null) { + launch() + } + + return node + } + + override suspend fun setImage(image: Image): Node { + node = node.copy(image = image) + return node + } + + override suspend fun refresh(): Node = node + + /** + * Launch the server image on the machine. + */ + private suspend fun launch() { + val serverCtx = this.serverCtx + + processContext.spawn { + serverCtx.init() + try { + node.server!!.image(serverCtx) + serverCtx.exit() + } catch (cause: Throwable) { + serverCtx.exit(cause) + } + } + } + + private val serverCtx = object : ServerManagementContext { + private var initialized: Boolean = false + + override var server: Server + get() = node.server!! + set(value) { + node = node.copy(server = value) + } + + override suspend fun init() { + if (initialized) { + throw IllegalStateException() + } + + val previousState = server.state + server = server.copy(state = ServerState.ACTIVE) + monitor.onUpdate(server, previousState) + + initialized = true + } + + override suspend fun exit(cause: Throwable?) { + val previousState = server.state + val state = if (cause == null) ServerState.SHUTOFF else ServerState.ERROR + server = server.copy(state = state) + monitor.onUpdate(server, previousState) + initialized = false + } + + override suspend fun run(req: LongArray) { + // TODO Properly implement this for multiple CPUs + val time = max(0, req.max() ?: 0) * flavor.cpus[0].clockRate + delay(time.toLong()) + } + } +} diff --git a/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriverTest.kt b/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriverTest.kt deleted file mode 100644 index 39b170fb..00000000 --- a/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/FakeBareMetalDriverTest.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2020 atlarge-research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.atlarge.opendc.compute.metal.driver - -import com.atlarge.odcsim.SimulationEngineProvider -import com.atlarge.opendc.compute.core.ServerFlavor -import com.atlarge.opendc.compute.core.ProcessingUnit -import com.atlarge.opendc.compute.core.Server -import com.atlarge.opendc.compute.core.ServerState -import com.atlarge.opendc.compute.core.image.FlopsApplicationImage -import com.atlarge.opendc.compute.core.monitor.ServerMonitor -import com.atlarge.opendc.compute.metal.PowerState -import java.util.ServiceLoader -import java.util.UUID -import kotlinx.coroutines.delay -import kotlinx.coroutines.runBlocking -import org.junit.jupiter.api.Test - -internal class FakeBareMetalDriverTest { - /** - * A smoke test for the bare-metal driver. - */ - @Test - fun smoke() { - val provider = ServiceLoader.load(SimulationEngineProvider::class.java).first() - val system = provider({ ctx -> - val flavor = ServerFlavor(listOf(ProcessingUnit("Intel", "Xeon", "amd64", 2300.0, 4))) - val image = FlopsApplicationImage(1000, 2) - val monitor = object : ServerMonitor { - override suspend fun onUpdate(server: Server, previousState: ServerState) { - println(server) - } - } - val driver = FakeBareMetalDriver(UUID.randomUUID(), "test", flavor) - - driver.init(monitor) - driver.setImage(image) - driver.setPower(PowerState.POWER_ON) - delay(5) - println(driver.refresh()) - }, name = "sim") - - runBlocking { - system.run() - system.terminate() - } - } -} diff --git a/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriverTest.kt b/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriverTest.kt new file mode 100644 index 00000000..7835e305 --- /dev/null +++ b/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/driver/SimpleBareMetalDriverTest.kt @@ -0,0 +1,70 @@ +/* + * MIT License + * + * Copyright (c) 2020 atlarge-research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.atlarge.opendc.compute.metal.driver + +import com.atlarge.odcsim.SimulationEngineProvider +import com.atlarge.opendc.compute.core.ServerFlavor +import com.atlarge.opendc.compute.core.ProcessingUnit +import com.atlarge.opendc.compute.core.Server +import com.atlarge.opendc.compute.core.ServerState +import com.atlarge.opendc.compute.core.image.FlopsApplicationImage +import com.atlarge.opendc.compute.core.monitor.ServerMonitor +import com.atlarge.opendc.compute.metal.PowerState +import java.util.ServiceLoader +import java.util.UUID +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.junit.jupiter.api.Test + +internal class SimpleBareMetalDriverTest { + /** + * A smoke test for the bare-metal driver. + */ + @Test + fun smoke() { + val provider = ServiceLoader.load(SimulationEngineProvider::class.java).first() + val system = provider({ ctx -> + val flavor = ServerFlavor(listOf(ProcessingUnit("Intel", "Xeon", "amd64", 2300.0, 4))) + val image = FlopsApplicationImage(1000, 2) + val monitor = object : ServerMonitor { + override suspend fun onUpdate(server: Server, previousState: ServerState) { + println(server) + } + } + val driver = SimpleBareMetalDriver(UUID.randomUUID(), "test", flavor) + + driver.init(monitor) + driver.setImage(image) + driver.setPower(PowerState.POWER_ON) + delay(5) + println(driver.refresh()) + }, name = "sim") + + runBlocking { + system.run() + system.terminate() + } + } +} diff --git a/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/service/SimpleProvisioningServiceTest.kt b/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/service/SimpleProvisioningServiceTest.kt index f1abbaed..34981b0a 100644 --- a/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/service/SimpleProvisioningServiceTest.kt +++ b/opendc/opendc-compute/src/test/kotlin/com/atlarge/opendc/compute/metal/service/SimpleProvisioningServiceTest.kt @@ -31,7 +31,7 @@ import com.atlarge.opendc.compute.core.Server import com.atlarge.opendc.compute.core.ServerState import com.atlarge.opendc.compute.core.image.FlopsApplicationImage import com.atlarge.opendc.compute.core.monitor.ServerMonitor -import com.atlarge.opendc.compute.metal.driver.FakeBareMetalDriver +import com.atlarge.opendc.compute.metal.driver.SimpleBareMetalDriver import java.util.ServiceLoader import java.util.UUID import kotlinx.coroutines.delay @@ -56,7 +56,7 @@ internal class SimpleProvisioningServiceTest { println(server) } } - val driver = FakeBareMetalDriver(UUID.randomUUID(), "test", flavor) + val driver = SimpleBareMetalDriver(UUID.randomUUID(), "test", flavor) val provisioner = SimpleProvisioningService() provisioner.create(driver) diff --git a/opendc/opendc-format/src/main/kotlin/com/atlarge/opendc/format/environment/sc18/Sc18EnvironmentReader.kt b/opendc/opendc-format/src/main/kotlin/com/atlarge/opendc/format/environment/sc18/Sc18EnvironmentReader.kt index 4f7907c6..f1acef4f 100644 --- a/opendc/opendc-format/src/main/kotlin/com/atlarge/opendc/format/environment/sc18/Sc18EnvironmentReader.kt +++ b/opendc/opendc-format/src/main/kotlin/com/atlarge/opendc/format/environment/sc18/Sc18EnvironmentReader.kt @@ -26,7 +26,7 @@ package com.atlarge.opendc.format.environment.sc18 import com.atlarge.opendc.compute.core.ServerFlavor import com.atlarge.opendc.compute.core.ProcessingUnit -import com.atlarge.opendc.compute.metal.driver.FakeBareMetalDriver +import com.atlarge.opendc.compute.metal.driver.SimpleBareMetalDriver import com.atlarge.opendc.compute.metal.service.ProvisioningService import com.atlarge.opendc.compute.metal.service.SimpleProvisioningService import com.atlarge.opendc.core.Environment @@ -70,7 +70,7 @@ class Sc18EnvironmentReader(input: InputStream, mapper: ObjectMapper = jacksonOb } } val flavor = ServerFlavor(cores) - FakeBareMetalDriver(UUID.randomUUID(), "node-${counter++}", flavor) + SimpleBareMetalDriver(UUID.randomUUID(), "node-${counter++}", flavor) } } } -- cgit v1.2.3