From c82b1725cc606769084155d6c4fba982cd320c41 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Fri, 18 Feb 2022 14:59:54 +0100 Subject: fix(compute): Disallow duplicate UIDs for SimHost This change fixes an issue with the ComputeServiceHelper where it allowed users to register multiple SimHost objects with the same UID. See this issue for more information: https://github.com/atlarge-research/opendc/issues/51 --- .../src/main/kotlin/org/opendc/compute/simulator/SimHost.kt | 6 ++++++ .../main/kotlin/org/opendc/compute/workload/ComputeServiceHelper.kt | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt index 95921e8b..43f33f27 100644 --- a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt +++ b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/SimHost.kt @@ -249,6 +249,12 @@ public class SimHost( machine.cancel() } + override fun hashCode(): Int = uid.hashCode() + + override fun equals(other: Any?): Boolean { + return other is SimHost && uid == other.uid + } + override fun toString(): String = "SimHost[uid=$uid,name=$name,model=$model]" public suspend fun fail() { diff --git a/opendc-compute/opendc-compute-workload/src/main/kotlin/org/opendc/compute/workload/ComputeServiceHelper.kt b/opendc-compute/opendc-compute-workload/src/main/kotlin/org/opendc/compute/workload/ComputeServiceHelper.kt index a1a65da3..4b0b343f 100644 --- a/opendc-compute/opendc-compute-workload/src/main/kotlin/org/opendc/compute/workload/ComputeServiceHelper.kt +++ b/opendc-compute/opendc-compute-workload/src/main/kotlin/org/opendc/compute/workload/ComputeServiceHelper.kt @@ -169,7 +169,7 @@ public class ComputeServiceHelper( optimize = optimize ) - _hosts.add(host) + require(_hosts.add(host)) { "Host with uid ${spec.uid} already exists" } service.addHost(host) return host -- cgit v1.2.3