summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-28 16:56:25 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2017-09-28 16:56:25 +0200
commit504598b320c689cca3d1bbf523a4dd82f69d7a61 (patch)
tree5058464bf8b8f4068240020c639bb1de6721a029
parentd6d9d37abf17071ff050e45ea37c693e659a4e98 (diff)
Add thread pool for experiment platform
-rw-r--r--opendc-integration-jpa/build.gradle3
-rw-r--r--opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/integration/jpa/schema/Datacenter.kt2
-rw-r--r--opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperiment.kt4
-rw-r--r--opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperimentManager.kt13
-rw-r--r--opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaPlatformRunner.kt16
-rw-r--r--opendc-integration-jpa/src/main/resources/META-INF/persistence.xml1
-rw-r--r--opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt2
7 files changed, 29 insertions, 12 deletions
diff --git a/opendc-integration-jpa/build.gradle b/opendc-integration-jpa/build.gradle
index a3aa9dc8..4d0729b7 100644
--- a/opendc-integration-jpa/build.gradle
+++ b/opendc-integration-jpa/build.gradle
@@ -43,9 +43,12 @@ buildscript {
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'kotlin-jpa'
+apply plugin: 'application'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'org.junit.platform.gradle.plugin'
+mainClassName = "nl.atlarge.opendc.platform.JpaPlatformRunnerKt"
+
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
diff --git a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/integration/jpa/schema/Datacenter.kt b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/integration/jpa/schema/Datacenter.kt
index 26cf9706..1e6eaa2b 100644
--- a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/integration/jpa/schema/Datacenter.kt
+++ b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/integration/jpa/schema/Datacenter.kt
@@ -57,7 +57,7 @@ data class Datacenter(
* The interval at which task will be (re)scheduled.
* We set this to a fixed constant since the database provides no way of configuring this.
*/
- override val interval: Duration = 50
+ override val interval: Duration = 10
/**
* The initial state of the datacenter.
diff --git a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperiment.kt b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperiment.kt
index 4c284d1e..104011df 100644
--- a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperiment.kt
+++ b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperiment.kt
@@ -102,8 +102,6 @@ class JpaExperiment(val manager: EntityManager,
logger.info { "Starting simulation" }
while (trace.jobs.any { !it.finished }) {
- simulation.run(simulation.clock.now + 1)
-
// Collect data of simulation cycle
manager.transaction.begin()
machines.forEach { machine ->
@@ -134,6 +132,8 @@ class JpaExperiment(val manager: EntityManager,
}
manager.transaction.commit()
+ // Run next simulation cycle
+ simulation.run(simulation.clock.now + 1)
experiment.last = simulation.clock.now
}
diff --git a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperimentManager.kt b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperimentManager.kt
index faa1a771..ce69f84b 100644
--- a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperimentManager.kt
+++ b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaExperimentManager.kt
@@ -27,14 +27,21 @@ package nl.atlarge.opendc.platform
import nl.atlarge.opendc.integration.jpa.schema.Experiment as InternalExperiment
import nl.atlarge.opendc.integration.jpa.schema.ExperimentState
import javax.persistence.EntityManager
+import javax.persistence.EntityManagerFactory
/**
* A manager for [Experiment]s received from a JPA database.
*
- * @property manager The JPA entity manager to retrieve entities from the database from.
+ * @property factory The JPA entity manager factory to create [EntityManager]s to retrieve entities from the database
+ * from.
* @author Fabian Mastenbroek (f.s.mastenbroek@student.tudelft.nl)
*/
-class JpaExperimentManager(private val manager: EntityManager) {
+class JpaExperimentManager(private val factory: EntityManagerFactory) {
+ /**
+ * The entity manager for this experiment.
+ */
+ private val manager: EntityManager = factory.createEntityManager()
+
/**
* The amount of experiments in the queue. This property makes a call to the database and does therefore not
* run in O(1) time.
@@ -68,6 +75,6 @@ class JpaExperimentManager(private val manager: EntityManager) {
experiment!!.state = ExperimentState.CLAIMED
}
manager.transaction.commit()
- return experiment?.let { JpaExperiment(manager, it) }
+ return experiment?.let { JpaExperiment(factory.createEntityManager(), it) }
}
}
diff --git a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaPlatformRunner.kt b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaPlatformRunner.kt
index f95eb174..10e80260 100644
--- a/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaPlatformRunner.kt
+++ b/opendc-integration-jpa/src/main/kotlin/nl/atlarge/opendc/platform/JpaPlatformRunner.kt
@@ -26,6 +26,7 @@ package nl.atlarge.opendc.platform
import mu.KotlinLogging
import nl.atlarge.opendc.kernel.omega.OmegaKernel
+import java.util.concurrent.Executors
import javax.persistence.Persistence
val logger = KotlinLogging.logger {}
@@ -37,16 +38,21 @@ val logger = KotlinLogging.logger {}
* @param args The command line arguments of the program.
*/
fun main(args: Array<String>) {
+ val threads = 1
+ val executorService = Executors.newFixedThreadPool(threads)
val factory = Persistence.createEntityManagerFactory("opendc-frontend")
- val manager = factory.createEntityManager()
- val experiments = JpaExperimentManager(manager)
+ val experiments = JpaExperimentManager(factory)
logger.info { "Waiting for enqueued experiments..." }
while (true) {
val experiment = experiments.poll()
- experiment?.run {
- logger.info { "Found experiment. Running simulating now..." }
- run(OmegaKernel)
+ executorService.submit {
+ experiment?.run {
+ logger.info { "Found experiment. Running simulating now..." }
+ run(OmegaKernel)
+ }
}
+
+ Thread.sleep(500)
}
}
diff --git a/opendc-integration-jpa/src/main/resources/META-INF/persistence.xml b/opendc-integration-jpa/src/main/resources/META-INF/persistence.xml
index c997ab8d..f1fe4765 100644
--- a/opendc-integration-jpa/src/main/resources/META-INF/persistence.xml
+++ b/opendc-integration-jpa/src/main/resources/META-INF/persistence.xml
@@ -39,6 +39,7 @@
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
+ <property name="hibernate.jdbc.batch_size" value="50" />
</properties>
</persistence-unit>
</persistence>
diff --git a/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt b/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt
index 4338ae04..b1d881a1 100644
--- a/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt
+++ b/opendc-stdlib/src/main/kotlin/nl/atlarge/opendc/topology/machine/Machine.kt
@@ -79,7 +79,7 @@ open class Machine : Entity<Machine.State>, Process<Machine> {
val interval: Duration = 10
val cpus = outgoingEdges.destinations<Cpu>("cpu")
- val speed = cpus.fold(0, { acc, cpu -> acc + cpu.clockRate * cpu.cores }) / 10
+ val speed = cpus.fold(0, { acc, cpu -> acc + cpu.clockRate * cpu.cores })
var task: Task = receiveTask()
update(State(Status.RUNNING, task, load = 1.0, memory = state.memory + 50, temperature = 30.0))