summaryrefslogtreecommitdiff
path: root/opendc-compute
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-04 17:14:46 +0100
committerFabian Mastenbroek <mail.fabianm@gmail.com>2022-11-04 17:21:58 +0100
commit7143584da87e248277ab95a4848a57eccd62db69 (patch)
treef889f310de1ec33ab59d3fe04e204f9cc7c11b4d /opendc-compute
parentacb45a1dea61dd844fba839cc31c79a7aca4bbe4 (diff)
refactor: Use RandomGenerator as randomness source
This change updates the modules of OpenDC to always accept the `RandomGenerator` interface as source of randomness. This interface is implemented by the slower `java.util.Random` class, but also by the faster `java.util.SplittableRandom` class
Diffstat (limited to 'opendc-compute')
-rw-r--r--opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/scheduler/FilterScheduler.kt7
-rw-r--r--opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/failure/StochasticVictimSelector.kt26
-rw-r--r--opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/failure/HostFaultInjectorTest.kt2
3 files changed, 28 insertions, 7 deletions
diff --git a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/scheduler/FilterScheduler.kt b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/scheduler/FilterScheduler.kt
index 233f5ef6..0840ba7e 100644
--- a/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/scheduler/FilterScheduler.kt
+++ b/opendc-compute/opendc-compute-service/src/main/kotlin/org/opendc/compute/service/scheduler/FilterScheduler.kt
@@ -26,7 +26,8 @@ import org.opendc.compute.api.Server
import org.opendc.compute.service.internal.HostView
import org.opendc.compute.service.scheduler.filters.HostFilter
import org.opendc.compute.service.scheduler.weights.HostWeigher
-import java.util.Random
+import java.util.SplittableRandom
+import java.util.random.RandomGenerator
import kotlin.math.min
/**
@@ -39,13 +40,13 @@ import kotlin.math.min
* @param filters The list of filters to apply when searching for an appropriate host.
* @param weighers The list of weighers to apply when searching for an appropriate host.
* @param subsetSize The size of the subset of best hosts from which a target is randomly chosen.
- * @param random A [Random] instance for selecting
+ * @param random A [RandomGenerator] instance for selecting
*/
public class FilterScheduler(
private val filters: List<HostFilter>,
private val weighers: List<HostWeigher>,
private val subsetSize: Int = 1,
- private val random: Random = Random(0)
+ private val random: RandomGenerator = SplittableRandom(0)
) : ComputeScheduler {
/**
* The pool of hosts available to the scheduler.
diff --git a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/failure/StochasticVictimSelector.kt b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/failure/StochasticVictimSelector.kt
index b6d466bd..4aba0e91 100644
--- a/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/failure/StochasticVictimSelector.kt
+++ b/opendc-compute/opendc-compute-simulator/src/main/kotlin/org/opendc/compute/simulator/failure/StochasticVictimSelector.kt
@@ -24,7 +24,9 @@ package org.opendc.compute.simulator.failure
import org.apache.commons.math3.distribution.RealDistribution
import org.opendc.compute.simulator.SimHost
-import java.util.Random
+import java.util.ArrayList
+import java.util.SplittableRandom
+import java.util.random.RandomGenerator
import kotlin.math.roundToInt
/**
@@ -32,12 +34,30 @@ import kotlin.math.roundToInt
*/
public class StochasticVictimSelector(
private val size: RealDistribution,
- private val random: Random = Random(0)
+ private val random: RandomGenerator = SplittableRandom(0)
) : VictimSelector {
override fun select(hosts: Set<SimHost>): List<SimHost> {
val n = size.sample().roundToInt()
- return hosts.shuffled(random).take(n)
+ val result = ArrayList<SimHost>(n)
+
+ val random = random
+ var samplesNeeded = n
+ var remainingHosts = hosts.size
+ val iterator = hosts.iterator()
+
+ while (iterator.hasNext() && samplesNeeded > 0) {
+ val host = iterator.next()
+
+ if (random.nextInt(remainingHosts) < samplesNeeded) {
+ result.add(host)
+ samplesNeeded--
+ }
+
+ remainingHosts--
+ }
+
+ return result
}
override fun toString(): String = "StochasticVictimSelector[$size]"
diff --git a/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/failure/HostFaultInjectorTest.kt b/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/failure/HostFaultInjectorTest.kt
index 90f534e6..a65c37cf 100644
--- a/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/failure/HostFaultInjectorTest.kt
+++ b/opendc-compute/opendc-compute-simulator/src/test/kotlin/org/opendc/compute/simulator/failure/HostFaultInjectorTest.kt
@@ -38,7 +38,7 @@ import kotlin.math.ln
/**
* Test suite for [HostFaultInjector] class.
*/
-internal class HostFaultInjectorTest {
+class HostFaultInjectorTest {
/**
* Simple test case to test that nothing happens when the injector is not started.
*/