From 7cadeb711158609eacc86e4e4c60dd825c848b99 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 27 Feb 2020 14:21:40 +0100 Subject: Factor out allocation policy and add memory-aware policy --- .../atlarge/opendc/compute/virt/driver/VirtDriver.kt | 9 ++++++++- .../virt/driver/hypervisor/HypervisorVirtDriver.kt | 4 ---- .../virt/service/SimpleVirtProvisioningService.kt | 6 +++--- .../compute/virt/service/VirtProvisioningService.kt | 3 +++ .../virt/service/allocation/AllocationPolicy.kt | 13 +++++++++++++ .../allocation/AvailableMemoryAllocationPolicy.kt | 18 ++++++++++++++++++ .../service/allocation/NumHostsAllocationPolicy.kt | 15 +++++++++++++++ 7 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AllocationPolicy.kt create mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AvailableMemoryAllocationPolicy.kt create mode 100644 opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/NumHostsAllocationPolicy.kt (limited to 'opendc/opendc-compute/src') diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/VirtDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/VirtDriver.kt index 68b8e541..e68dd488 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/VirtDriver.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/VirtDriver.kt @@ -24,8 +24,8 @@ package com.atlarge.opendc.compute.virt.driver -import com.atlarge.opendc.compute.core.Server import com.atlarge.opendc.compute.core.Flavor +import com.atlarge.opendc.compute.core.Server import com.atlarge.opendc.compute.core.image.Image import com.atlarge.opendc.compute.core.monitor.ServerMonitor import com.atlarge.opendc.core.services.AbstractServiceKey @@ -53,5 +53,12 @@ public interface VirtDriver { */ public suspend fun getNumberOfSpawnedImages(): Int + /** + * Returns the available memory on the server managed by this driver. + * + * @return The available memory, in MB. + */ + public suspend fun getAvailableMemory(): Long + companion object Key : AbstractServiceKey(UUID.randomUUID(), "virtual-driver") } diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/hypervisor/HypervisorVirtDriver.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/hypervisor/HypervisorVirtDriver.kt index 9745b56c..4b23159f 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/hypervisor/HypervisorVirtDriver.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/hypervisor/HypervisorVirtDriver.kt @@ -69,10 +69,6 @@ class HypervisorVirtDriver( return server } - override suspend fun getNumberOfSpawnedImages(): Int { - return vms.size - } - internal inner class VmServerContext( override var server: Server, val monitor: ServerMonitor, diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/SimpleVirtProvisioningService.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/SimpleVirtProvisioningService.kt index 888364e2..309cfa83 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/SimpleVirtProvisioningService.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/SimpleVirtProvisioningService.kt @@ -12,9 +12,11 @@ import com.atlarge.opendc.compute.virt.driver.VirtDriver import com.atlarge.opendc.compute.virt.driver.hypervisor.HypervisorImage import com.atlarge.opendc.compute.virt.driver.hypervisor.InsufficientMemoryOnServerException import com.atlarge.opendc.compute.virt.monitor.HypervisorMonitor +import com.atlarge.opendc.compute.virt.service.allocation.AllocationPolicy import kotlinx.coroutines.launch class SimpleVirtProvisioningService( + public override val allocationPolicy: AllocationPolicy, private val ctx: SimulationContext, private val provisioningService: ProvisioningService, private val hypervisorMonitor: HypervisorMonitor @@ -83,9 +85,7 @@ class SimpleVirtProvisioningService( for (imageInstance in imagesToBeScheduled) { println("Spawning $imageInstance") - val selectedNode = availableNodes.minBy { - it.server!!.serviceRegistry[VirtDriver.Key].getNumberOfSpawnedImages() - } + val selectedNode = availableNodes.minWith(allocationPolicy().thenBy { it.uid }) try { imageInstance.server = selectedNode?.server!!.serviceRegistry[VirtDriver.Key].spawn( diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/VirtProvisioningService.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/VirtProvisioningService.kt index fb087f9d..7770ec50 100644 --- a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/VirtProvisioningService.kt +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/VirtProvisioningService.kt @@ -3,11 +3,14 @@ package com.atlarge.opendc.compute.virt.service import com.atlarge.opendc.compute.core.Flavor import com.atlarge.opendc.compute.core.image.Image import com.atlarge.opendc.compute.core.monitor.ServerMonitor +import com.atlarge.opendc.compute.virt.service.allocation.AllocationPolicy /** * A service for VM provisioning on a cloud. */ interface VirtProvisioningService { + val allocationPolicy: AllocationPolicy + /** * Submit the specified [Image] to the provisioning service. * diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AllocationPolicy.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AllocationPolicy.kt new file mode 100644 index 00000000..fc08aa87 --- /dev/null +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AllocationPolicy.kt @@ -0,0 +1,13 @@ +package com.atlarge.opendc.compute.virt.service.allocation + +import com.atlarge.opendc.compute.metal.Node + +/** + * A policy for selecting the [Node] an image should be deployed to, + */ +interface AllocationPolicy { + /** + * Builds the logic of the policy. + */ + suspend operator fun invoke(): Comparator +} diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AvailableMemoryAllocationPolicy.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AvailableMemoryAllocationPolicy.kt new file mode 100644 index 00000000..e02e2cef --- /dev/null +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AvailableMemoryAllocationPolicy.kt @@ -0,0 +1,18 @@ +package com.atlarge.opendc.compute.virt.service.allocation + +import com.atlarge.opendc.compute.metal.Node +import com.atlarge.opendc.compute.virt.driver.VirtDriver +import kotlinx.coroutines.runBlocking + +/** + * Allocation policy that selects the node with the most available memory. + */ +class AvailableMemoryAllocationPolicy : AllocationPolicy { + override suspend fun invoke(): Comparator = Comparator { o1, o2 -> + runBlocking { + compareValuesBy(o1, o2) { + -it.server!!.serviceRegistry[VirtDriver.Key].getAvailableMemory() + } + } + } +} diff --git a/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/NumHostsAllocationPolicy.kt b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/NumHostsAllocationPolicy.kt new file mode 100644 index 00000000..c5ee29c5 --- /dev/null +++ b/opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/NumHostsAllocationPolicy.kt @@ -0,0 +1,15 @@ +package com.atlarge.opendc.compute.virt.service.allocation + +import com.atlarge.opendc.compute.metal.Node +import com.atlarge.opendc.compute.virt.driver.VirtDriver +import kotlinx.coroutines.runBlocking + +class NumHostsAllocationPolicy : AllocationPolicy { + override suspend fun invoke(): Comparator = Comparator { o1, o2 -> + runBlocking { + compareValuesBy(o1, o2) { + it.server!!.serviceRegistry[VirtDriver.Key].getNumberOfSpawnedImages() + } + } + } +} -- cgit v1.2.3