Skip to content

Commit

Permalink
refactor(alchemist-grid): remove simulation config factory
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-olaiya committed Sep 10, 2023
1 parent 0cc5520 commit 4c325f9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package it.unibo.alchemist.boundary.grid.cluster.management

import com.google.protobuf.ByteString
import com.google.protobuf.kotlin.toByteString
import it.unibo.alchemist.boundary.Exporter
import it.unibo.alchemist.boundary.DistributedExporter
import it.unibo.alchemist.boundary.Loader
import it.unibo.alchemist.boundary.exporters.GlobalExporter
import it.unibo.alchemist.boundary.grid.cluster.AlchemistClusterNode
Expand Down Expand Up @@ -83,6 +83,14 @@ class ClusterRegistry(
val jobIDToInitializers = mutableMapOf<UUID, SimulationInitializer>()
initializers.forEach {
val initializedEnvironment = loader.getWith<Any, _>(it.variables)
initializedEnvironment.exporters.forEach { exporter ->
require(exporter is DistributedExporter) {
"""
When using a distribution every exporter
must be an instance of ${DistributedExporter::class.simpleName}
""".trimIndent()
}
}
val serializedEnvironment = serializeObject(initializedEnvironment.environment).toByteString()
val serializedExporters = serializeObject(initializedEnvironment.exporters).toByteString()
val simulation = SimulationMessage.Simulation.newBuilder()
Expand Down Expand Up @@ -176,7 +184,11 @@ class ClusterRegistry(
val simulationConfig = SimulationMessage.SimulationConfiguration.parseFrom(config)
// save dependencies
val environment: Environment<T, P> = deserializeObject(job.environment) as Environment<T, P>
val exports = deserializeObject(job.exports) as List<Exporter<T, P>>
val exports = deserializeObject(job.exports) as List<DistributedExporter<T, P>>
exports.forEach {
it.bindRegistry(this)
it.bindJobId(jobID)
}
val engine = Engine(environment, simulationConfig.endStep, DoubleTime(simulationConfig.endTime))
engine.addOutputMonitor(GlobalExporter(exports))
return engine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import org.kaikikm.threadresloader.ResourceLoader
import java.nio.file.Files
import java.nio.file.Path

object SimulationConfigFactory {

fun newSimulationConfig(loader: Loader, endStep: Long, endTime: Time) = object : SimulationConfig {
override val loader = loader
override val endStep: Long = endStep
override val endTime = endTime
override val dependencies = loader.remoteDependencies.associateWith {
val dependencyURL = checkNotNull(ResourceLoader.getResource(it)) { "Could not find dependency file $it" }
Files.readAllBytes(Path.of(dependencyURL.toURI()))
}
class SimulationConfigImpl(
override val loader: Loader,
override val endStep: Long,
override val endTime: Time,
) : SimulationConfig {
override val dependencies: Map<String, ByteArray> = loader.remoteDependencies.associateWith {
val dependencyURL = checkNotNull(ResourceLoader.getResource(it)) { "Could not find dependency file $it" }
Files.readAllBytes(Path.of(dependencyURL.toURI()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import it.unibo.alchemist.boundary.grid.cluster.management.ClusterRegistry
import it.unibo.alchemist.boundary.grid.cluster.storage.EtcdKVStore
import it.unibo.alchemist.boundary.grid.simulation.ComplexityImpl
import it.unibo.alchemist.boundary.grid.simulation.SimulationBatchImpl
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfigFactory
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfigImpl
import it.unibo.alchemist.boundary.grid.simulation.SimulationInitializer
import it.unibo.alchemist.model.Time
import org.slf4j.LoggerFactory
Expand All @@ -32,7 +32,7 @@ class DistributedExecution(
override fun launch(loader: Loader) {
val endpoints = listOf("http://localhost:10001", "http://localhost:10002", "http://localhost:10003")
val cluster = ClusterImpl(ClusterRegistry(EtcdKVStore(endpoints)))
val configuration = SimulationConfigFactory.newSimulationConfig(loader, Long.MAX_VALUE, Time.INFINITY)
val configuration = SimulationConfigImpl(loader, Long.MAX_VALUE, Time.INFINITY)
val initializers = loader.variables.cartesianProductOf(variables).map(::SimulationInitializer)
val batch = SimulationBatchImpl(configuration, initializers)
val workerSet = cluster.workerSet(ComplexityImpl())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.kotest.core.test.TestResult
import it.unibo.alchemist.Alchemist
import it.unibo.alchemist.boundary.LoadAlchemist
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfig
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfigFactory
import it.unibo.alchemist.boundary.grid.simulation.SimulationConfigImpl
import it.unibo.alchemist.model.Time
import org.kaikikm.threadresloader.ResourceLoader
import java.io.File
Expand All @@ -31,7 +31,7 @@ object GridTestUtils {

fun getSimulationContext(yamlConfigurationPath: String): SimulationConfig {
val loader = getLoader(ResourceLoader.getResource(yamlConfigurationPath))
return SimulationConfigFactory.newSimulationConfig(loader, Long.MAX_VALUE, Time.INFINITY)
return SimulationConfigImpl(loader, Long.MAX_VALUE, Time.INFINITY)
}

fun getDockerExtension(composeFilePath: String) =
Expand Down
6 changes: 2 additions & 4 deletions alchemist-grid/src/test/resources/client-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ deployments:
- program: "{firing} --> +{token}"
#
export:
- type: CSVExporter
parameters:
fileNameRoot: "time_export"
interval: 1.5
- type: DistributedCSVExporter
parameters: ["time_export", 1.5]
data:
- time

Expand Down

0 comments on commit 4c325f9

Please sign in to comment.