From 2358257c1080b7ce78270535f82f0b960d48261a Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Mon, 6 Jun 2022 16:21:21 +0200 Subject: refactor(trace/api): Introduce type system for trace API This change updates the trace API by introducing a limited type system for the table columns. Previously, the table columns could have any possible type representable by the JVM. With this change, we limit the available types to a small type system. --- .../trace/wfformat/WfFormatTaskTableReader.kt | 78 ++++++++++++++++------ .../opendc/trace/wfformat/WfFormatTraceFormat.kt | 17 +++-- 2 files changed, 66 insertions(+), 29 deletions(-) (limited to 'opendc-trace/opendc-trace-wfformat/src/main') diff --git a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt index d8eafa9c..0be9dec6 100644 --- a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt +++ b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt @@ -27,7 +27,10 @@ import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.core.JsonToken import org.opendc.trace.* import org.opendc.trace.conv.* +import org.opendc.trace.util.convertTo import java.time.Duration +import java.time.Instant +import java.util.* import kotlin.math.roundToInt /** @@ -95,41 +98,82 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe return hasJob } - override fun resolve(column: TableColumn<*>): Int = columns[column] ?: -1 + override fun resolve(name: String): Int { + return when (name) { + TASK_ID -> COL_ID + TASK_WORKFLOW_ID -> COL_WORKFLOW_ID + TASK_RUNTIME -> COL_RUNTIME + TASK_REQ_NCPUS -> COL_NPROC + TASK_PARENTS -> COL_PARENTS + TASK_CHILDREN -> COL_CHILDREN + else -> -1 + } + } override fun isNull(index: Int): Boolean { - check(index in 0..columns.size) { "Invalid column value" } + check(index in 0..COL_CHILDREN) { "Invalid column value" } return false } - override fun get(index: Int): Any? { + override fun getBoolean(index: Int): Boolean { + throw IllegalArgumentException("Invalid column") + } + + override fun getInt(index: Int): Int { + return when (index) { + COL_NPROC -> cores + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getLong(index: Int): Long { + throw IllegalArgumentException("Invalid column") + } + + override fun getFloat(index: Int): Float { + throw IllegalArgumentException("Invalid column") + } + + override fun getDouble(index: Int): Double { + throw IllegalArgumentException("Invalid column") + } + + override fun getString(index: Int): String? { return when (index) { COL_ID -> id COL_WORKFLOW_ID -> workflowId - COL_RUNTIME -> runtime - COL_PARENTS -> parents - COL_CHILDREN -> children - COL_NPROC -> getInt(index) else -> throw IllegalArgumentException("Invalid column") } } - override fun getBoolean(index: Int): Boolean { + override fun getUUID(index: Int): UUID? { throw IllegalArgumentException("Invalid column") } - override fun getInt(index: Int): Int { + override fun getInstant(index: Int): Instant? { + throw IllegalArgumentException("Invalid column") + } + + override fun getDuration(index: Int): Duration? { return when (index) { - COL_NPROC -> cores + COL_RUNTIME -> runtime else -> throw IllegalArgumentException("Invalid column") } } - override fun getLong(index: Int): Long { + override fun getList(index: Int, elementType: Class): List? { throw IllegalArgumentException("Invalid column") } - override fun getDouble(index: Int): Double { + override fun getSet(index: Int, elementType: Class): Set? { + return when (index) { + COL_PARENTS -> TYPE_PARENTS.convertTo(parents, elementType) + COL_CHILDREN -> TYPE_CHILDREN.convertTo(children, elementType) + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getMap(index: Int, keyType: Class, valueType: Class): Map? { throw IllegalArgumentException("Invalid column") } @@ -232,12 +276,6 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe private val COL_PARENTS = 5 private val COL_CHILDREN = 6 - private val columns = mapOf( - TASK_ID to COL_ID, - TASK_WORKFLOW_ID to COL_WORKFLOW_ID, - TASK_RUNTIME to COL_RUNTIME, - TASK_REQ_NCPUS to COL_NPROC, - TASK_PARENTS to COL_PARENTS, - TASK_CHILDREN to COL_CHILDREN, - ) + private val TYPE_PARENTS = TableColumnType.Set(TableColumnType.String) + private val TYPE_CHILDREN = TableColumnType.Set(TableColumnType.String) } diff --git a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTraceFormat.kt b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTraceFormat.kt index 8db4c169..154fa061 100644 --- a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTraceFormat.kt +++ b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTraceFormat.kt @@ -50,20 +50,19 @@ public class WfFormatTraceFormat : TraceFormat { return when (table) { TABLE_TASKS -> TableDetails( listOf( - TASK_ID, - TASK_WORKFLOW_ID, - TASK_RUNTIME, - TASK_REQ_NCPUS, - TASK_PARENTS, - TASK_CHILDREN - ), - emptyList() + TableColumn(TASK_ID, TableColumnType.String), + TableColumn(TASK_WORKFLOW_ID, TableColumnType.String), + TableColumn(TASK_RUNTIME, TableColumnType.Duration), + TableColumn(TASK_REQ_NCPUS, TableColumnType.Int), + TableColumn(TASK_PARENTS, TableColumnType.Set(TableColumnType.String)), + TableColumn(TASK_CHILDREN, TableColumnType.Set(TableColumnType.String)) + ) ) else -> throw IllegalArgumentException("Table $table not supported") } } - override fun newReader(path: Path, table: String, projection: List>?): TableReader { + override fun newReader(path: Path, table: String, projection: List?): TableReader { return when (table) { TABLE_TASKS -> WfFormatTaskTableReader(factory.createParser(path.toFile())) else -> throw IllegalArgumentException("Table $table not supported") -- cgit v1.2.3 From 9d759c9bc987965fae8b0c16c000772c546bf3a2 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 8 Jun 2022 15:06:14 +0200 Subject: test(trace): Add conformance suite for OpenDC trace API This change adds a re-usable test suite for the interface of the OpenDC trace API, so implementors can verify whether they match the specification of the interfaces. --- .../opendc/trace/wfformat/WfFormatTaskTableReader.kt | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'opendc-trace/opendc-trace-wfformat/src/main') diff --git a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt index 0be9dec6..ca1a29d0 100644 --- a/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt +++ b/opendc-trace/opendc-trace-wfformat/src/main/kotlin/org/opendc/trace/wfformat/WfFormatTaskTableReader.kt @@ -54,6 +54,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe // Check whether the document is not empty and starts with an object if (token == null) { + parser.close() break } else if (token != JsonToken.START_OBJECT) { throw JsonParseException(parser, "Expected object", parser.currentLocation) @@ -64,6 +65,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe ParserLevel.TRACE -> { // Seek for the workflow object in the file if (!seekWorkflow()) { + parser.close() break } else if (!parser.isExpectedStartObjectToken) { throw JsonParseException(parser, "Expected object", parser.currentLocation) @@ -111,7 +113,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe } override fun isNull(index: Int): Boolean { - check(index in 0..COL_CHILDREN) { "Invalid column value" } + require(index in 0..COL_CHILDREN) { "Invalid column value" } return false } @@ -120,6 +122,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe } override fun getInt(index: Int): Int { + checkActive() return when (index) { COL_NPROC -> cores else -> throw IllegalArgumentException("Invalid column") @@ -139,6 +142,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe } override fun getString(index: Int): String? { + checkActive() return when (index) { COL_ID -> id COL_WORKFLOW_ID -> workflowId @@ -155,6 +159,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe } override fun getDuration(index: Int): Duration? { + checkActive() return when (index) { COL_RUNTIME -> runtime else -> throw IllegalArgumentException("Invalid column") @@ -166,6 +171,7 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe } override fun getSet(index: Int, elementType: Class): Set? { + checkActive() return when (index) { COL_PARENTS -> TYPE_PARENTS.convertTo(parents, elementType) COL_CHILDREN -> TYPE_CHILDREN.convertTo(children, elementType) @@ -181,11 +187,18 @@ internal class WfFormatTaskTableReader(private val parser: JsonParser) : TableRe parser.close() } + /** + * Helper method to check if the reader is active. + */ + private fun checkActive() { + check(level != ParserLevel.TOP && !parser.isClosed) { "No active row. Did you call nextRow()?" } + } + /** * Parse the trace and seek until the workflow description. */ private fun seekWorkflow(): Boolean { - while (parser.nextValue() != JsonToken.END_OBJECT) { + while (parser.nextValue() != JsonToken.END_OBJECT && !parser.isClosed) { when (parser.currentName) { "name" -> workflowId = parser.text "workflow" -> return true -- cgit v1.2.3