diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-28 16:56:25 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2017-09-28 16:56:25 +0200 |
| commit | 504598b320c689cca3d1bbf523a4dd82f69d7a61 (patch) | |
| tree | 5058464bf8b8f4068240020c639bb1de6721a029 /opendc-integration-jpa | |
| parent | d6d9d37abf17071ff050e45ea37c693e659a4e98 (diff) | |
Add thread pool for experiment platform
Diffstat (limited to 'opendc-integration-jpa')
6 files changed, 28 insertions, 11 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> |
