summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-runner/src/main/kotlin/org/opendc/web/runner/OpenDCRunner.kt
blob: 83583eab43c143e03f79af566976713c5f034b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 * Copyright (c) 2022 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.web.runner

import mu.KotlinLogging
import org.opendc.compute.failure.prefab.FailurePrefab
import org.opendc.compute.failure.prefab.createFailureModelPrefab
import org.opendc.compute.simulator.provisioner.Provisioner
import org.opendc.compute.simulator.provisioner.registerComputeMonitor
import org.opendc.compute.simulator.provisioner.setupComputeService
import org.opendc.compute.simulator.provisioner.setupHosts
import org.opendc.compute.simulator.scheduler.createPrefabComputeScheduler
import org.opendc.compute.simulator.service.ComputeService
import org.opendc.compute.topology.specs.ClusterSpec
import org.opendc.compute.topology.specs.HostSpec
import org.opendc.compute.topology.specs.PowerSourceSpec
import org.opendc.compute.workload.ComputeWorkloadLoader
import org.opendc.experiments.base.runner.replay
import org.opendc.simulator.compute.models.CpuModel
import org.opendc.simulator.compute.models.MachineModel
import org.opendc.simulator.compute.models.MemoryUnit
import org.opendc.simulator.compute.power.PowerModels
import org.opendc.simulator.kotlin.runSimulation
import org.opendc.web.proto.runner.Job
import org.opendc.web.proto.runner.Scenario
import org.opendc.web.proto.runner.Topology
import org.opendc.web.runner.internal.WebComputeMonitor
import java.io.File
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Random
import java.util.concurrent.Executors
import java.util.concurrent.ForkJoinPool
import java.util.concurrent.ForkJoinPool.ForkJoinWorkerThreadFactory
import java.util.concurrent.ForkJoinWorkerThread
import java.util.concurrent.RecursiveAction
import java.util.concurrent.RecursiveTask
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

/**
 * Class to execute the pending jobs via the OpenDC web API.
 *
 * @param manager The underlying [JobManager] to manage the available jobs.
 * @param tracePath The directory where the traces are located.
 * @param jobTimeout The maximum duration of a simulation job.
 * @param pollInterval The interval to poll the API with.
 * @param heartbeatInterval The interval to send a heartbeat to the API server.
 */
public class OpenDCRunner(
    private val manager: JobManager,
    private val tracePath: File,
    parallelism: Int = Runtime.getRuntime().availableProcessors(),
    private val jobTimeout: Duration = Duration.ofMinutes(10),
    private val pollInterval: Duration = Duration.ofSeconds(30),
    private val heartbeatInterval: Duration = Duration.ofMinutes(1),
) : Runnable {
    /**
     * Logging instance for this runner.
     */
    private val logger = KotlinLogging.logger {}

    /**
     * Helper class to load the workloads.
     */
    private val workloadLoader = ComputeWorkloadLoader(tracePath)

    /**
     * The [ForkJoinPool] that is used to execute the simulation jobs.
     */
    private val pool =
        ForkJoinPool(parallelism, RunnerThreadFactory(Thread.currentThread().contextClassLoader), null, false)

    /**
     * A [ScheduledExecutorService] to manage the heartbeat of simulation jobs as well as tracking the deadline of
     * individual simulations.
     */
    private val scheduler = Executors.newSingleThreadScheduledExecutor()

    /**
     * Start the runner process.
     *
     * This method will block until interrupted and poll the OpenDC API for new jobs to execute.
     */
    override fun run() {
        try {
            while (true) {
                val job = manager.findNext()
                if (job == null) {
                    Thread.sleep(pollInterval.toMillis())
                    continue
                }

                val id = job.id

                logger.info { "Found queued job $id: attempting to claim" }

                if (!manager.claim(id)) {
                    logger.info { "Failed to claim scenario" }
                    continue
                }

                pool.submit(JobAction(job))
            }
        } catch (_: InterruptedException) {
            // Gracefully exit when the thread is interrupted
        } finally {
            workloadLoader.reset()

            pool.shutdown()
            scheduler.shutdown()

            pool.awaitTermination(5, TimeUnit.SECONDS)
        }
    }

    /**
     * A [RecursiveAction] that runs a simulation job (consisting of possible multiple simulations).
     *
     * @param job The job to simulate.
     */
    private inner class JobAction(private val job: Job) : RecursiveAction() {
        override fun compute() {
            val id = job.id
            val scenario = job.scenario
            val startTime = Instant.now()
            val currentThread = Thread.currentThread()

            val heartbeat =
                scheduler.scheduleWithFixedDelay(
                    {
                        if (!manager.heartbeat(id, startTime.secondsSince())) {
                            currentThread.interrupt()
                        }
                    },
                    0,
                    heartbeatInterval.toMillis(),
                    TimeUnit.MILLISECONDS,
                )

            try {
                val topology = convertTopology(scenario.topology)
                val jobs =
                    (0 until scenario.portfolio.targets.repeats).map { repeat ->
                        SimulationTask(
                            scenario,
                            repeat,
                            topology,
                        )
                    }
                val results = invokeAll(jobs).map { it.rawResult }

                heartbeat.cancel(true)

                val duration = startTime.secondsSince()
                logger.info { "Finished simulation for job $id (in $duration seconds)" }

                manager.finish(
                    id,
                    duration,
                    mapOf(
                        "total_requested_burst" to results.map { it.totalActiveTime + it.totalIdleTime },
                        "total_granted_burst" to results.map { it.totalActiveTime },
                        "total_overcommitted_burst" to results.map { it.totalStealTime },
                        "total_interfered_burst" to results.map { it.totalLostTime },
                        "mean_cpu_usage" to results.map { it.meanCpuUsage },
                        "mean_cpu_demand" to results.map { it.meanCpuDemand },
                        "mean_num_deployed_images" to results.map { it.meanNumDeployedImages },
                        "max_num_deployed_images" to results.map { it.maxNumDeployedImages },
                        "total_power_draw" to results.map { it.totalPowerDraw },
                        "total_failure_slices" to results.map { it.totalFailureSlices },
                        "total_failure_vm_slices" to results.map { it.totalFailureVmSlices },
                        "total_vms_submitted" to results.map { it.totalVmsSubmitted },
                        "total_vms_queued" to results.map { it.totalVmsQueued },
                        "total_vms_finished" to results.map { it.totalVmsFinished },
                        "total_vms_failed" to results.map { it.totalVmsFailed },
                    ),
                )
            } catch (e: Exception) {
                // Check whether the job failed due to exceeding its time budget
                if (Thread.interrupted()) {
                    logger.info { "Simulation job $id exceeded time limit (${startTime.secondsSince()} seconds)" }
                } else {
                    logger.info(e) { "Simulation job $id failed" }
                }

                try {
                    heartbeat.cancel(true)
                    manager.fail(id, startTime.secondsSince())
                } catch (e: Throwable) {
                    logger.error(e) { "Failed to update job" }
                }
            }
        }

        /**
         * Calculate the seconds since the specified instant.
         */
        private fun Instant.secondsSince(): Int {
            return ChronoUnit.SECONDS.between(this, Instant.now()).toInt()
        }
    }

    /**
     * A [RecursiveTask] that simulates a single scenario.
     *
     * @param scenario The scenario to simulate.
     * @param repeat The repeat number used to seed the simulation.
     * @param topologyHosts The topology to simulate.
     */
    private inner class SimulationTask(
        private val scenario: Scenario,
        private val repeat: Int,
        private val topologyHosts: List<HostSpec>,
    ) : RecursiveTask<WebComputeMonitor.Results>() {
        override fun compute(): WebComputeMonitor.Results {
            val monitor = WebComputeMonitor()

            // Schedule task that interrupts the simulation if it runs for too long.
            val currentThread = Thread.currentThread()
            val interruptTask =
                scheduler.schedule({ currentThread.interrupt() }, jobTimeout.toMillis(), TimeUnit.MILLISECONDS)

            try {
                runSimulation(monitor)
            } finally {
                interruptTask.cancel(false)
            }

            return monitor.collectResults()
        }

        /**
         * Run a single simulation of the scenario.
         */
        private fun runSimulation(monitor: WebComputeMonitor) =
            runSimulation {
                val serviceDomain = "compute.opendc.org"
                val seed = repeat.toLong()

                val scenario = scenario

                val powerSourceSpec =
                    PowerSourceSpec(
                        totalPower = Long.MAX_VALUE,
                    )
                val topology = listOf(ClusterSpec("cluster", topologyHosts, powerSourceSpec))

                Provisioner(dispatcher, seed).use { provisioner ->

//                    val workload =
//                        trace(scenario.workload.trace.id).sampleByLoad(scenario.workload.samplingFraction)
//                    val vms = workload.resolve(workloadLoader, Random(seed))

                    val vms = workloadLoader.sampleByLoad(scenario.workload.samplingFraction)
                    val startTime = vms.minOf { it.submittedAt }

                    provisioner.runSteps(
                        setupComputeService(
                            serviceDomain,
                            { createPrefabComputeScheduler(scenario.schedulerName, Random(it.seeder.nextLong()), timeSource) },
                        ),
                        registerComputeMonitor(serviceDomain, monitor),
                        setupHosts(serviceDomain, topology, startTime),
                    )

                    val service = provisioner.registry.resolve(serviceDomain, ComputeService::class.java)!!

                    val phenomena = scenario.phenomena
                    val failureModel =
                        if (phenomena.failures) {
                            createFailureModelPrefab(coroutineContext, timeSource, service, Random(seed), FailurePrefab.G5k06Exp)
                        } else {
                            null
                        }

                    // Run workload trace
                    service.replay(timeSource, vms, seed = seed)

                    val serviceMetrics = service.getSchedulerStats()
                    logger.debug {
                        "Scheduler " +
                            "Success=${serviceMetrics.attemptsSuccess} " +
                            "Failure=${serviceMetrics.attemptsFailure} " +
                            "Pending=${serviceMetrics.tasksPending} " +
                            "Active=${serviceMetrics.tasksActive}"
                    }
                }
            }
    }

    /**
     * Convert the specified [topology] into an [Topology] understood by OpenDC.
     */
    private fun convertTopology(topology: Topology): List<HostSpec> {
        val res = mutableListOf<HostSpec>()
        val random = Random(0)

        val machines =
            topology.rooms.asSequence()
                .flatMap { room ->
                    room.tiles.flatMap { tile ->
                        val rack = tile.rack
                        rack?.machines?.map { machine -> rack to machine } ?: emptyList()
                    }
                }

        for ((rack, machine) in machines) {
            val clusterId = rack.id
            val position = machine.position

            val processors =
                machine.cpus.map { cpu ->
                    CpuModel(
                        0,
                        cpu.numberOfCores,
                        cpu.clockRateMhz,
                        "Intel",
                        "amd64",
                        cpu.name,
                    )
                }

            val memoryUnits =
                machine.memory.map { memory ->
                    MemoryUnit(
                        "Samsung",
                        memory.name,
                        memory.speedMbPerS,
                        memory.sizeMb.toLong(),
                    )
                }

            val energyConsumptionW = machine.cpus.sumOf { it.energyConsumptionW }
            val cpuPowerModel = PowerModels.linear(2 * energyConsumptionW, energyConsumptionW * 0.5)

            val spec =
                HostSpec(
                    "node-$clusterId-$position",
                    "node-$clusterId",
                    clusterId,
                    MachineModel(processors, memoryUnits[0]),
                    cpuPowerModel,
                    null,
                )

            res += spec
        }

        return res
    }

    /**
     * A custom [ForkJoinWorkerThreadFactory] that uses the [ClassLoader] of specified by the runner.
     */
    private class RunnerThreadFactory(private val classLoader: ClassLoader) : ForkJoinWorkerThreadFactory {
        override fun newThread(pool: ForkJoinPool): ForkJoinWorkerThread =
            object : ForkJoinWorkerThread(pool) {
                init {
                    contextClassLoader = classLoader
                }
            }
    }
}