diff options
12 files changed, 57 insertions, 244 deletions
diff --git a/build.gradle.kts b/build.gradle.kts index f8a20895..aa35d70a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,3 +1,4 @@ + /* * Copyright (c) 2017 AtLarge Research * diff --git a/opendc-common/build.gradle.kts b/opendc-common/build.gradle.kts index 0c87303c..9fe4710c 100644 --- a/opendc-common/build.gradle.kts +++ b/opendc-common/build.gradle.kts @@ -31,6 +31,7 @@ plugins { } repositories { + mavenCentral() maven(url = "https://packages.confluent.io/maven/") } @@ -38,7 +39,6 @@ repositories { val serializationVersion = "1.6.0" dependencies { - //@Mateusz: for the postgresql database implementation("org.postgresql:postgresql:42.7.10") @@ -65,6 +65,9 @@ dependencies { // Source: https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-toml implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.21.0") + + implementation("io.javalin:javalin:6.7.0") + } diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt deleted file mode 100644 index 8261f6f0..00000000 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/ConfigParser.kt +++ /dev/null @@ -1,106 +0,0 @@ -package org.opendc.common.utils - -import kotlinx.serialization.ExperimentalSerializationApi -import kotlinx.serialization.Serializable -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream - -import java.io.File -import java.io.IOException -import java.io.InputStream -import java.io.OutputStream -import java.net.Socket -import java.sql.Connection - -/** - * @property name - * @property backlog the amount of connections to accept - * @property address IPv4 address - * @property port - * @property postgresql Postgresql port - * @property username Postgresql user - * @property password Postgresql password - * @property database Postgresql database - * @property topic Kafka topic and database table name - * @property kafka Kafka port - * @author Mateusz - */ -/* - - Use `by lazy` here. - Use design patterns - singleton. - */ -@Serializable -public data class Config( - val name: String = "", - var backlog: Int = 0, - val address: String = "", - val port: Int = 0, - val postgresql: Int = 0, - val username : String = "", - val password : String = "", - val database: String = "", - val topic : String = "", - val kafka: Int = 0, -){ - - public companion object{ - public var input: InputStream? = null - public var output: OutputStream? = null - public var connection : Connection? = null - public var kafka : Kafka? = null - public var database : PostgresqlDB? = null - - public var socket: Socket? = null - - public fun setConfigSocket(socket: Socket?){ - this.socket = socket - // no try catch if the exception is not from Java - // do not use raw sockets, use a service for the communication - // use redis instead of HTTP GET (consider it, but not bound in stone) - // make an API KTor - try { - input = socket?.getInputStream() - output = socket?.getOutputStream() - } catch (e: IOException){ - print("${e.message}") - } - } - - public fun getConfigReader() : InputStream? { - return input - } - - public fun getConfigWriter() : OutputStream? { - return output - } - - public fun setKafkaInstance(kafka : Kafka) { - this.kafka = kafka - } - - public fun getKafkaInstance() : Kafka? { - return this.kafka - } - - public fun setDB(db : PostgresqlDB){ - this.database = db - } - - public fun getDB() : PostgresqlDB?{ - return this.database - } - } -} -/** - * @author Mateusz - * Reads `config.json` into Config data class. - */ -public class ConfigReader { - private val jsonReader = Json - public fun read(file: File): Config = read(file.inputStream()) - @OptIn(ExperimentalSerializationApi::class) - public fun read(input: InputStream): Config { - return jsonReader.decodeFromStream<Config>(input) - } -} diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt new file mode 100644 index 00000000..9db7bfaf --- /dev/null +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/JavalinRunner.kt @@ -0,0 +1,25 @@ +package org.opendc.common.utils + +import io.javalin.Javalin +import io.javalin.http.Context +import io.javalin.http.Handler + +public class JavalinRunner { + + private val handleHello: Handler = Handler { ctx -> + ctx.status(200) + ctx.contentType("application/x-protobuf") + ctx.result("Hello world") + } + + init { + val app = Javalin.create().start() + + app.get("/hello", handleHello) + + app.exception<Exception?>(Exception::class.java, { e: Exception?, ctx: Context? -> + e!!.printStackTrace() + ctx!!.status(500) + }) + } +}
\ No newline at end of file diff --git a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt index 361925ee..03fd902c 100644 --- a/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt +++ b/opendc-common/src/main/kotlin/org/opendc/common/utils/PostgresqlDB.kt @@ -24,7 +24,7 @@ public class PostgresqlDB { properties = TomlMapper().readerFor(Properties().javaClass) .readValue(PostgresqlDB::class.java.getResource("/database.toml")) connection = DriverManager.getConnection( - properties.getProperty("address").asJdbc(properties.getProperty("table")), + properties.getProperty("address").asJdbc(properties.getProperty("database")), properties.getProperty("user"), properties.getProperty("password")) clear() @@ -45,8 +45,9 @@ public class PostgresqlDB { } } - private fun String.asJdbc(table : String) : String { - return "jdbc:postgresql://$this/$table" + private fun String.asJdbc(database : String) : String { + return "jdbc:postgresql://$this/$database" + } }
\ No newline at end of file diff --git a/opendc-demo/build.gradle.kts b/opendc-demo/build.gradle.kts index d5a84120..7512211a 100644 --- a/opendc-demo/build.gradle.kts +++ b/opendc-demo/build.gradle.kts @@ -25,8 +25,10 @@ description = "Demo for the OpenDC digital twin" plugins { `kotlin-library-conventions` + kotlin("plugin.serialization") version "1.9.22" } -dependencies { - implementation(project(mapOf("path" to "::opendc-compute:opendc-compute-simulator"))) +repositories { + mavenCentral() + maven(url = "https://packages.confluent.io/maven/") } diff --git a/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentCli.kt b/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentCli.kt index 78ce6158..f3e634e6 100644 --- a/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentCli.kt +++ b/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentCli.kt @@ -28,21 +28,15 @@ import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.options.defaultLazy import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.file -import org.opendc.common.utils.Config -import org.opendc.common.utils.ConfigReader -import org.opendc.common.utils.PostgresqlDB import org.opendc.experiments.base.experiment.getExperiment import java.io.File import java.io.IOException -import java.net.InetAddress -import java.net.ServerSocket -import java.net.Socket /** * Main entrypoint of the application. */ public fun main(args: Array<String>) { - if(args.size == 4) ExperimentCommand().main(args) + if(args.size == 2) ExperimentCommand().main(args) else ExperimentListener().main(args) } @@ -51,55 +45,33 @@ public fun main(args: Array<String>) { * @author Mateusz */ internal class ExperimentCommand : CliktCommand(name = "experiment") { - private val configPath by option("--config-path", help = "path to config file") - .file(canBeDir = false, canBeFile = true) - .defaultLazy { File("resources/config.json") } - private val experimentPath by option("--experiment-path", help = "path to experiment file") .file(canBeDir = false, canBeFile = true) .defaultLazy { File("resources/experiment.json") } override fun run() { - val configReader = ConfigReader() - val config = configReader.read(configPath) - var clientSocket : Socket? = null - try { - Config.setConfigSocket(clientSocket) val experiment = getExperiment(experimentPath) runExperiment(experiment) } catch (e: IOException) { println("${e.message}") - } finally { - clientSocket?.close() } } } /** - * Creates a server socket and database connection from `config`. - * @author Mateusz + * Parses CLI arguments. + * + * @author Mateusz Kwiatkowski */ internal class ExperimentListener: CliktCommand(name = "listener") { - private val configPath by option("--config-path", help = "path to config file") - .file(canBeDir = false, canBeFile = true) - .defaultLazy { File("resources/config.json") } - override fun run() { - val configReader = ConfigReader() - var serverSocket: ServerSocket? = null - val config = configReader.read(configPath) try { - - val inetAddress = InetAddress.getByName(config.address) - serverSocket = ServerSocket(config.port, config.backlog, inetAddress) - runListener(serverSocket) + runListener() } catch (e: IOException) { println("${e.message}") - } finally { - serverSocket?.close() } } }
\ No newline at end of file diff --git a/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentRunner.kt b/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentRunner.kt index 3867a9f0..a777262f 100644 --- a/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentRunner.kt +++ b/opendc-experiments/opendc-experiments-base/src/main/kotlin/org/opendc/experiments/base/runner/ExperimentRunner.kt @@ -24,12 +24,9 @@ package org.opendc.experiments.base.runner import me.tongfei.progressbar.ProgressBarBuilder import me.tongfei.progressbar.ProgressBarStyle -import org.opendc.common.utils.Config +import org.opendc.common.utils.JavalinRunner import org.opendc.common.utils.PostgresqlDB import org.opendc.experiments.base.experiment.Scenario -import java.io.IOException -import java.net.ServerSocket -import java.net.Socket /** * Run scenario when no pool is available for parallel execution @@ -65,29 +62,13 @@ public fun runExperiment(experiment: List<Scenario>) { } /** - * @author Mateusz - * Accepts a (single) connection and listens for requests. - * @param socket The socket to listen to. + * Accepts a HTTP server and listens for requests. + * + * @author Mateusz Kwiatkowski + * + * @see <a href=https://ktor.io/docs>https://ktor.io/docs</a> */ -public fun runListener(socket: ServerSocket) { - var client : Socket? = null - val db = PostgresqlDB() - try { - client = socket.accept() - Config.setConfigSocket(client) - // val request = ByteArray(1024) - - while(true){ -/* - val ret : Int? = Config.getConfigReader()?.read(request) - if(ret == -1) break - if(ret != null && ret > 0) runRequest(String(request, 0, ret)) - - */ } - - } catch (e: IOException) { - println("${e.message}") - } finally { - client?.close() - } +public fun runListener() { + PostgresqlDB() + JavalinRunner() }
\ No newline at end of file diff --git a/resources/experiments/config.json b/resources/experiments/config.json deleted file mode 100644 index ae383f45..00000000 --- a/resources/experiments/config.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "configuration file", - "port": "8080", - "backlog": 20, - "address": "127.0.0.1", - "postgresql" : "5432", - "username" : "matt", - "password" : "admin", - "database" : "opendc", - "topic" : "postgres_topic", - "kafka" : "9092" -} - diff --git a/resources/experiments/config.toml b/resources/experiments/config.toml deleted file mode 100644 index b9ff2b38..00000000 --- a/resources/experiments/config.toml +++ /dev/null @@ -1,11 +0,0 @@ -// use a log4j to log your results in a form of a log - - -// Use check instead of require -// use type-aliases -// use higher-order -// annotation classes -// operator objects - Factory Design Pattern -// if it can be modeled as an instance then you do not need functions -// make your kotlin program write to standalone -// latex files that create figures themselves instead of using python diff --git a/resources/experiments/sink-jdbc.properties b/resources/experiments/sink-jdbc.properties deleted file mode 100644 index a36dad31..00000000 --- a/resources/experiments/sink-jdbc.properties +++ /dev/null @@ -1,47 +0,0 @@ -# -# Copyright 2018 Confluent Inc. -# -# Licensed under the Confluent Community License (the "License"); you may not use -# this file except in compliance with the License. You may obtain a copy of the -# License at -# -# http://www.confluent.io/confluent-community-license -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OF ANY KIND, either express or implied. See the License for the -# specific language governing permissions and limitations under the License. -# - -# A simple example that copies from a topic to a Postgres database. -# The first few settings are required for all connectors: -# a name, the connector class to run, and the maximum number of tasks to create: -name=postgresql-sink -connector.class=io.confluent.connect.jdbc.JdbcSinkConnector -tasks.max=1 - -key.converter=org.apache.kafka.connect.storage.StringConverter -value.converter=io.confluent.connect.protobuf.ProtobufConverter -value.converter.schema.registry.url=http://localhost:8081 - -# The topics to consume from - required for sink connectors like this one -topics=postgres_topic - -# Configuration specific to the JDBC sink connector. -# We want to connect to a Postgres database stored in the file test.db and auto-create tables. -connection.url=jdbc:postgresql://127.0.0.1:5432/opendc - -connection.user=matt -connection.password=admin -auto.create=true -auto.evolve=true - -# Define when identifiers should be quoted in DDL and DML statements. -# The default is 'always' to maintain backward compatibility with prior versions. -# Set this to 'never' to avoid quoting fully-qualified or simple table and column names. -#quote.sql.identifiers=always - -# Here are some values that enable JSON formatted files to be ingested by Postgresql - -insert.mode=insert -table.name.format=postgres_topic
\ No newline at end of file @@ -0,0 +1,5 @@ +#!/usr/bin/zsh +kafka-storage format -t 2vi2WtHxQAOPyXb1Bj1Jvw -c $CONFLUENT_HOME/etc/kafka/server.properties --standalone +kafka-server-start $CONFLUENT_HOME/etc/kafka/server.properties +schema-registry-start $CONFLUENT_HOME/etc/schema-registry/schema-registry.properties +connect-standalone $CONFLUENT_HOME/etc/kafka/connect-standalone.properties $CONFLUENT_HOME/share/confluent-common/connectors/sink-jdbc.properties |
