summaryrefslogtreecommitdiff
path: root/opendc-compute
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-01 10:52:46 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-27 20:49:39 +0000
commite0856b26c3e1961e7ff4bb3ca038adc4892bbc22 (patch)
tree03b5ebc1979392170b3032b54c4a2bc606595a65 /opendc-compute
parent6bbfbd7aeb99475308a140222316f3e9006aeec3 (diff)
refactor(compute/service): Expose state directly to clients
This change updates the implementation of the compute service to expose state to clients created by the compute service.
Diffstat (limited to 'opendc-compute')
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientFlavor.kt68
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientImage.kt61
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt118
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ComputeServiceImpl.kt27
-rw-r--r--opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt6
5 files changed, 19 insertions, 261 deletions
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientFlavor.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientFlavor.kt
deleted file mode 100644
index 45a3d472..00000000
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientFlavor.kt
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * 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.service.internal
-
-import org.opendc.compute.api.Flavor
-import java.util.UUID
-
-/**
- * A [Flavor] implementation that is passed to clients but delegates its implementation to another class.
- */
-internal class ClientFlavor(private val delegate: Flavor) : Flavor {
- override val uid: UUID = delegate.uid
-
- override var name: String = delegate.name
- private set
-
- override var cpuCount: Int = delegate.cpuCount
- private set
-
- override var memorySize: Long = delegate.memorySize
- private set
-
- override var labels: Map<String, String> = delegate.labels.toMap()
- private set
-
- override var meta: Map<String, Any> = delegate.meta.toMap()
- private set
-
- override fun delete() {
- delegate.delete()
- }
-
- override fun reload() {
- delegate.reload()
-
- name = delegate.name
- cpuCount = delegate.cpuCount
- memorySize = delegate.memorySize
- labels = delegate.labels
- meta = delegate.meta
- }
-
- override fun equals(other: Any?): Boolean = other is Flavor && other.uid == uid
-
- override fun hashCode(): Int = uid.hashCode()
-
- override fun toString(): String = "Flavor[uid=$uid,name=$name]"
-}
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientImage.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientImage.kt
deleted file mode 100644
index eb963f0e..00000000
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientImage.kt
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.service.internal
-
-import org.opendc.compute.api.Image
-import java.util.UUID
-
-/**
- * An [Image] implementation that is passed to clients but delegates its implementation to another class.
- */
-internal class ClientImage(private val delegate: Image) : Image {
- override val uid: UUID = delegate.uid
-
- override var name: String = delegate.name
- private set
-
- override var labels: Map<String, String> = delegate.labels.toMap()
- private set
-
- override var meta: Map<String, Any> = delegate.meta.toMap()
- private set
-
- override fun delete() {
- delegate.delete()
- reload()
- }
-
- override fun reload() {
- delegate.reload()
-
- name = delegate.name
- labels = delegate.labels
- meta = delegate.meta
- }
-
- override fun equals(other: Any?): Boolean = other is Image && other.uid == uid
-
- override fun hashCode(): Int = uid.hashCode()
-
- override fun toString(): String = "Image[uid=$uid,name=$name]"
-}
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt
deleted file mode 100644
index f2248466..00000000
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/internal/ClientServer.kt
+++ /dev/null
@@ -1,118 +0,0 @@
-/*
- * 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.service.internal
-
-import org.opendc.compute.api.Flavor
-import org.opendc.compute.api.Image
-import org.opendc.compute.api.Server
-import org.opendc.compute.api.ServerState
-import org.opendc.compute.api.ServerWatcher
-import java.time.Instant
-import java.util.UUID
-
-/**
- * A [Server] implementation that is passed to clients but delegates its implementation to another class.
- */
-internal class ClientServer(private val delegate: Server) : Server, ServerWatcher {
- private val watchers = mutableListOf<ServerWatcher>()
-
- override val uid: UUID = delegate.uid
-
- override var name: String = delegate.name
- private set
-
- override var flavor: Flavor = delegate.flavor
- private set
-
- override var image: Image = delegate.image
- private set
-
- override var labels: Map<String, String> = delegate.labels.toMap()
- private set
-
- override var meta: Map<String, Any> = delegate.meta.toMap()
- private set
-
- override var state: ServerState = delegate.state
- private set
-
- override var launchedAt: Instant? = null
- private set
-
- override fun start() {
- delegate.start()
- reload()
- }
-
- override fun stop() {
- delegate.stop()
- reload()
- }
-
- override fun delete() {
- delegate.delete()
- reload()
- }
-
- override fun watch(watcher: ServerWatcher) {
- if (watchers.isEmpty()) {
- delegate.watch(this)
- }
-
- watchers += watcher
- }
-
- override fun unwatch(watcher: ServerWatcher) {
- watchers += watcher
-
- if (watchers.isEmpty()) {
- delegate.unwatch(this)
- }
- }
-
- override fun reload() {
- delegate.reload()
-
- name = delegate.name
- flavor = delegate.flavor
- image = delegate.image
- labels = delegate.labels
- meta = delegate.meta
- state = delegate.state
- launchedAt = delegate.launchedAt
- }
-
- override fun onStateChanged(server: Server, newState: ServerState) {
- val watchers = watchers
-
- for (watcher in watchers) {
- watcher.onStateChanged(this, newState)
- }
- }
-
- override fun equals(other: Any?): Boolean = other is Server && other.uid == uid
-
- override fun hashCode(): Int = uid.hashCode()
-
- override fun toString(): String = "Server[uid=$uid,name=$name,state=$state]"
-}
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 ffebd5fa..ac66ff8b 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
@@ -90,11 +90,13 @@ internal class ComputeServiceImpl(
* The registered flavors for this compute service.
*/
internal val flavorById = mutableMapOf<UUID, InternalFlavor>()
+ private val flavors: MutableList<Flavor> = mutableListOf()
/**
* The registered images for this compute service.
*/
internal val imageById = mutableMapOf<UUID, InternalImage>()
+ private val images: MutableList<Image> = mutableListOf()
/**
* The registered servers for this compute service.
@@ -198,10 +200,12 @@ internal class ComputeServiceImpl(
internal fun delete(flavor: InternalFlavor) {
flavorById.remove(flavor.uid)
+ flavors.remove(flavor)
}
internal fun delete(image: InternalImage) {
imageById.remove(image.uid)
+ images.remove(image)
}
internal fun delete(server: InternalServer) {
@@ -359,13 +363,13 @@ internal class ComputeServiceImpl(
override fun queryFlavors(): List<Flavor> {
check(!isClosed) { "Client is already closed" }
- return service.flavorById.values.map { ClientFlavor(it) }
+ return service.flavors.toList()
}
override fun findFlavor(id: UUID): Flavor? {
check(!isClosed) { "Client is already closed" }
- return service.flavorById[id]?.let { ClientFlavor(it) }
+ return service.flavorById[id]
}
override fun newFlavor(
@@ -377,6 +381,7 @@ internal class ComputeServiceImpl(
): Flavor {
check(!isClosed) { "Client is already closed" }
+ val service = service
val uid = UUID(service.clock.millis(), service.random.nextLong())
val flavor = InternalFlavor(
service,
@@ -389,31 +394,34 @@ internal class ComputeServiceImpl(
)
service.flavorById[uid] = flavor
+ service.flavors.add(flavor)
- return ClientFlavor(flavor)
+ return flavor
}
override fun queryImages(): List<Image> {
check(!isClosed) { "Client is already closed" }
- return service.imageById.values.map { ClientImage(it) }
+ return service.images.toList()
}
override fun findImage(id: UUID): Image? {
check(!isClosed) { "Client is already closed" }
- return service.imageById[id]?.let { ClientImage(it) }
+ return service.imageById[id]
}
override fun newImage(name: String, labels: Map<String, String>, meta: Map<String, Any>): Image {
check(!isClosed) { "Client is already closed" }
+ val service = service
val uid = UUID(service.clock.millis(), service.random.nextLong())
val image = InternalImage(service, uid, name, labels, meta)
service.imageById[uid] = image
+ service.images.add(image)
- return ClientImage(image)
+ return image
}
override fun newServer(
@@ -426,6 +434,7 @@ internal class ComputeServiceImpl(
): Server {
check(!isClosed) { "Client is closed" }
+ val service = service
val uid = UUID(service.clock.millis(), service.random.nextLong())
val server = InternalServer(
service,
@@ -444,19 +453,19 @@ internal class ComputeServiceImpl(
server.start()
}
- return ClientServer(server)
+ return server
}
override fun findServer(id: UUID): Server? {
check(!isClosed) { "Client is already closed" }
- return service.serverById[id]?.let { ClientServer(it) }
+ return service.serverById[id]
}
override fun queryServers(): List<Server> {
check(!isClosed) { "Client is already closed" }
- return service.serverById.values.map { ClientServer(it) }
+ return service.servers.toList()
}
override fun close() {
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 13b926e8..a64c0885 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
@@ -302,6 +302,7 @@ internal class ComputeServiceTest {
@Test
fun testServerInvalidType() = scope.runSimulation {
val host = mockk<Host>(relaxUnitFun = true)
+ val server = mockk<Server>(relaxUnitFun = true)
val listeners = mutableListOf<HostListener>()
every { host.uid } returns UUID.randomUUID()
@@ -312,11 +313,6 @@ internal class ComputeServiceTest {
service.addHost(host)
- val client = service.newClient()
- val flavor = client.newFlavor("test", 1, 1024)
- val image = client.newImage("test")
- val server = client.newServer("test", image, flavor, start = false)
-
assertThrows<IllegalArgumentException> {
listeners.forEach { it.onStateChanged(host, server, ServerState.RUNNING) }
}