summaryrefslogtreecommitdiff
path: root/opendc/opendc-compute/src/main
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-02-27 14:21:40 +0100
committerGeorgios Andreadis <info@gandreadis.com>2020-02-28 14:38:57 +0100
commit7cadeb711158609eacc86e4e4c60dd825c848b99 (patch)
tree2196c8dd893091b7f500cd8e50590e43ce65343d /opendc/opendc-compute/src/main
parent3a5eac673fb67a6cff7fc79f16312db78d706322 (diff)
Factor out allocation policy and add memory-aware policy
Diffstat (limited to 'opendc/opendc-compute/src/main')
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/VirtDriver.kt9
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/driver/hypervisor/HypervisorVirtDriver.kt4
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/SimpleVirtProvisioningService.kt6
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/VirtProvisioningService.kt3
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AllocationPolicy.kt13
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/AvailableMemoryAllocationPolicy.kt18
-rw-r--r--opendc/opendc-compute/src/main/kotlin/com/atlarge/opendc/compute/virt/service/allocation/NumHostsAllocationPolicy.kt15
7 files changed, 60 insertions, 8 deletions
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<VirtDriver>(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<Node>
+}
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<Node> = 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<Node> = Comparator { o1, o2 ->
+ runBlocking {
+ compareValuesBy(o1, o2) {
+ it.server!!.serviceRegistry[VirtDriver.Key].getNumberOfSpawnedImages()
+ }
+ }
+ }
+}