From f5efde88ec95fc139e957303615c302d4aa2035d Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 9 Mar 2021 15:05:47 +0100 Subject: compute: Introduce labels and meta-data for resources This change adds the ability to define labels and meta-data for resources. This can be used in the future to identify servers and pass data between client and server. --- .../kotlin/org/opendc/compute/api/ComputeClient.kt | 4 ++ .../main/kotlin/org/opendc/compute/api/Resource.kt | 55 ++++++++++++++++++++++ .../main/kotlin/org/opendc/compute/api/Server.kt | 32 +++++-------- .../compute/service/internal/ClientServer.kt | 8 +++- .../compute/service/internal/ComputeServiceImpl.kt | 46 +++++++----------- .../org/opendc/compute/simulator/SimHostTest.kt | 5 +- 6 files changed, 99 insertions(+), 51 deletions(-) create mode 100644 simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Resource.kt (limited to 'simulator/opendc-compute') diff --git a/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/ComputeClient.kt b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/ComputeClient.kt index c7c507ed..8ae08284 100644 --- a/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/ComputeClient.kt +++ b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/ComputeClient.kt @@ -32,12 +32,16 @@ public interface ComputeClient : AutoCloseable { * @param name The name of the server to deploy. * @param image The image to be deployed. * @param flavor The flavor of the machine instance to run this [image] on. + * @param labels The identifying labels of the server. + * @param meta The non-identifying meta-data of the server. * @param start A flag to indicate that the server should be started immediately. */ public suspend fun newServer( name: String, image: Image, flavor: Flavor, + labels: Map = emptyMap(), + meta: Map = emptyMap(), start: Boolean = true ): Server diff --git a/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Resource.kt b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Resource.kt new file mode 100644 index 00000000..08120848 --- /dev/null +++ b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Resource.kt @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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 org.opendc.compute.api + +import java.util.UUID + +/** + * A generic resource provided by the OpenDC Compute service. + */ +public interface Resource { + /** + * The unique identifier of the resource. + */ + public val uid: UUID + + /** + * The name of the resource. + */ + public val name: String + + /** + * The identifying labels attached to the resource. + */ + public val labels: Map + + /** + * The non-identifying metadata attached to the resource. + */ + public val meta: Map + + /** + * Refresh the local state of the resource. + */ + public suspend fun refresh() +} diff --git a/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Server.kt b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Server.kt index a4f61c03..b508a9f8 100644 --- a/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Server.kt +++ b/simulator/opendc-compute/opendc-compute-api/src/main/kotlin/org/opendc/compute/api/Server.kt @@ -22,17 +22,10 @@ package org.opendc.compute.api -import org.opendc.core.resource.Resource - /** * A stateful object representing a server instance that is running on some physical or virtual machine. */ public interface Server : Resource { - /** - * The name of the server. - */ - public override val name: String - /** * The flavor of the server. */ @@ -43,28 +36,32 @@ public interface Server : Resource { */ public val image: Image - /** - * The tags assigned to the server. - */ - public override val tags: Map - /** * The last known state of the server. */ public val state: ServerState /** - * Start the server. + * Request the server to be started. + * + * This method is guaranteed to return after the request was acknowledged, but might return before the server was + * started. */ public suspend fun start() /** - * Stop the server. + * Request the server to be stopped. + * + * This method is guaranteed to return after the request was acknowledged, but might return before the server was + * stopped. */ public suspend fun stop() /** - * Delete the server instance from the service. + * Request the server to be deleted. + * + * This method is guaranteed to return after the request was acknowledged, but might return before the server was + * deleted. */ public suspend fun delete() @@ -81,9 +78,4 @@ public interface Server : Resource { * @param watcher The watcher to de-register from the server. */ public fun unwatch(watcher: ServerWatcher) - - /** - * Refresh the local state of the resource. - */ - public suspend fun refresh() } diff --git a/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt b/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt index e65c5f1c..bca1ad44 100644 --- a/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt +++ b/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt @@ -46,7 +46,10 @@ internal class ClientServer(private val delegate: Server) : Server, ServerWatche override var image: Image = delegate.image private set - override var tags: Map = delegate.tags.toMap() + override var labels: Map = delegate.labels.toMap() + private set + + override var meta: Map = delegate.meta.toMap() private set override var state: ServerState = delegate.state @@ -87,7 +90,8 @@ internal class ClientServer(private val delegate: Server) : Server, ServerWatche name = delegate.name flavor = delegate.flavor image = delegate.image - tags = delegate.tags + labels = delegate.labels + meta = delegate.meta state = delegate.state } diff --git a/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt b/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt index 453f5d65..0d4f379d 100644 --- a/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt +++ b/simulator/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt @@ -26,7 +26,6 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch -import kotlinx.coroutines.suspendCancellableCoroutine import mu.KotlinLogging import org.opendc.compute.api.* import org.opendc.compute.service.ComputeService @@ -41,9 +40,7 @@ import org.opendc.utils.TimerScheduler import org.opendc.utils.flow.EventFlow import java.time.Clock import java.util.* -import kotlin.coroutines.Continuation import kotlin.coroutines.CoroutineContext -import kotlin.coroutines.resume import kotlin.math.max /** @@ -130,6 +127,8 @@ public class ComputeServiceImpl( name: String, image: Image, flavor: Flavor, + labels: Map, + meta: Map, start: Boolean ): Server { check(!isClosed) { "Client is closed" } @@ -148,7 +147,14 @@ public class ComputeServiceImpl( ) ) - val server = createServer(name, image, flavor) + val server = ServerImpl( + uid = UUID(random.nextLong(), random.nextLong()), + name, + flavor, + image, + labels.toMutableMap(), + meta.toMutableMap() + ) if (start) { server.start() } @@ -189,19 +195,6 @@ public class ComputeServiceImpl( scope.cancel() } - private fun createServer( - name: String, - image: Image, - flavor: Flavor, - ): ServerImpl { - return ServerImpl( - uid = UUID(random.nextLong(), random.nextLong()), - name, - flavor, - image - ) - } - private fun requestCycle() { // Bail out in case we have already requested a new cycle. if (scheduler.isTimerActive(Unit)) { @@ -220,7 +213,7 @@ public class ComputeServiceImpl( private fun schedule() { while (queue.isNotEmpty()) { - val (server, cont) = queue.peekFirst() + val (server) = queue.peekFirst() val requiredMemory = server.flavor.memorySize val selectedHv = allocationLogic.select(availableHosts, server) @@ -265,7 +258,6 @@ public class ComputeServiceImpl( scope.launch { try { selectedHv.host.spawn(server) - cont.resume(Unit) activeServers[server] = selectedHv.host tracer.commit(VmScheduledEvent(server.name)) @@ -390,13 +382,15 @@ public class ComputeServiceImpl( } } - private data class LaunchRequest(val server: ServerImpl, val cont: Continuation) + private data class LaunchRequest(val server: ServerImpl) private inner class ServerImpl( override val uid: UUID, override val name: String, override val flavor: Flavor, - override val image: Image + override val image: Image, + override val labels: MutableMap, + override val meta: MutableMap ) : Server { val watchers = mutableListOf() @@ -417,11 +411,9 @@ public class ComputeServiceImpl( else -> { logger.info { "User requested to start server $uid" } state = ServerState.PROVISIONING - suspendCancellableCoroutine { cont -> - val request = LaunchRequest(this, cont) - queue += request - requestCycle() - } + val request = LaunchRequest(this) + queue += request + requestCycle() } } } @@ -463,8 +455,6 @@ public class ComputeServiceImpl( // No-op: this object is the source-of-truth } - override val tags: Map = emptyMap() - override var state: ServerState = ServerState.TERMINATED set(value) { if (value != field) { diff --git a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt index 931d8d68..b2f6bfc3 100644 --- a/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt +++ b/simulator/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/SimHostTest.kt @@ -147,7 +147,10 @@ internal class SimHostTest { override val flavor: Flavor, override val image: Image ) : Server { - override val tags: Map = emptyMap() + override val labels: Map = emptyMap() + + override val meta: Map = emptyMap() + override val state: ServerState = ServerState.TERMINATED override suspend fun start() {} -- cgit v1.2.3