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/azure/AzureResourceStateTableReader.kt | 73 +++++++++++++----- .../opendc/trace/azure/AzureResourceTableReader.kt | 87 +++++++++++++++------- .../org/opendc/trace/azure/AzureTraceFormat.kt | 21 +++--- 3 files changed, 124 insertions(+), 57 deletions(-) (limited to 'opendc-trace/opendc-trace-azure/src/main') diff --git a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt index 3132b1d9..e9017b35 100644 --- a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt +++ b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt @@ -29,7 +29,9 @@ import org.opendc.trace.* import org.opendc.trace.conv.RESOURCE_ID import org.opendc.trace.conv.RESOURCE_STATE_CPU_USAGE_PCT import org.opendc.trace.conv.RESOURCE_STATE_TIMESTAMP +import java.time.Duration import java.time.Instant +import java.util.* /** * A [TableReader] for the Azure v1 VM resource state table. @@ -63,20 +65,22 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta return true } - override fun resolve(column: TableColumn<*>): Int = columns[column] ?: -1 + private val COL_ID = 0 + private val COL_TIMESTAMP = 1 + private val COL_CPU_USAGE_PCT = 2 - override fun isNull(index: Int): Boolean { - require(index in 0..columns.size) { "Invalid column index" } - return false + override fun resolve(name: String): Int { + return when (name) { + RESOURCE_ID -> COL_ID + RESOURCE_STATE_TIMESTAMP -> COL_TIMESTAMP + RESOURCE_STATE_CPU_USAGE_PCT -> COL_CPU_USAGE_PCT + else -> -1 + } } - override fun get(index: Int): Any? { - return when (index) { - COL_ID -> id - COL_TIMESTAMP -> timestamp - COL_CPU_USAGE_PCT -> cpuUsagePct - else -> throw IllegalArgumentException("Invalid column index") - } + override fun isNull(index: Int): Boolean { + require(index in 0..COL_CPU_USAGE_PCT) { "Invalid column index" } + return false } override fun getBoolean(index: Int): Boolean { @@ -91,6 +95,10 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta throw IllegalArgumentException("Invalid column") } + override fun getFloat(index: Int): Float { + throw IllegalArgumentException("Invalid column") + } + override fun getDouble(index: Int): Double { return when (index) { COL_CPU_USAGE_PCT -> cpuUsagePct @@ -98,6 +106,40 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta } } + override fun getString(index: Int): String? { + return when (index) { + COL_ID -> id + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getUUID(index: Int): UUID? { + throw IllegalArgumentException("Invalid column") + } + + override fun getInstant(index: Int): Instant? { + return when (index) { + COL_TIMESTAMP -> timestamp + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getDuration(index: Int): Duration? { + throw IllegalArgumentException("Invalid column") + } + + override fun getList(index: Int, elementType: Class): List? { + throw IllegalArgumentException("Invalid column") + } + + override fun getMap(index: Int, keyType: Class, valueType: Class): Map? { + throw IllegalArgumentException("Invalid column") + } + + override fun getSet(index: Int, elementType: Class): Set? { + throw IllegalArgumentException("Invalid column") + } + override fun close() { parser.close() } @@ -131,15 +173,6 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta cpuUsagePct = Double.NaN } - private val COL_ID = 0 - private val COL_TIMESTAMP = 1 - private val COL_CPU_USAGE_PCT = 2 - private val columns = mapOf( - RESOURCE_ID to COL_ID, - RESOURCE_STATE_TIMESTAMP to COL_TIMESTAMP, - RESOURCE_STATE_CPU_USAGE_PCT to COL_CPU_USAGE_PCT - ) - companion object { /** * The [CsvSchema] that is used to parse the trace. diff --git a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt index 154a37e4..456a2536 100644 --- a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt +++ b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt @@ -27,7 +27,9 @@ import com.fasterxml.jackson.dataformat.csv.CsvParser import com.fasterxml.jackson.dataformat.csv.CsvSchema import org.opendc.trace.* import org.opendc.trace.conv.* +import java.time.Duration import java.time.Instant +import java.util.* /** * A [TableReader] for the Azure v1 VM resources table. @@ -63,22 +65,26 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe return true } - override fun resolve(column: TableColumn<*>): Int = columns[column] ?: -1 + private val COL_ID = 0 + private val COL_START_TIME = 1 + private val COL_STOP_TIME = 2 + private val COL_CPU_COUNT = 3 + private val COL_MEM_CAPACITY = 4 - override fun isNull(index: Int): Boolean { - require(index in 0..columns.size) { "Invalid column index" } - return false + override fun resolve(name: String): Int { + return when (name) { + RESOURCE_ID -> COL_ID + RESOURCE_START_TIME -> COL_START_TIME + RESOURCE_STOP_TIME -> COL_STOP_TIME + RESOURCE_CPU_COUNT -> COL_CPU_COUNT + RESOURCE_MEM_CAPACITY -> COL_MEM_CAPACITY + else -> -1 + } } - override fun get(index: Int): Any? { - return when (index) { - COL_ID -> id - COL_START_TIME -> startTime - COL_STOP_TIME -> stopTime - COL_CPU_COUNT -> getInt(index) - COL_MEM_CAPACITY -> getDouble(index) - else -> throw IllegalArgumentException("Invalid column") - } + override fun isNull(index: Int): Boolean { + require(index in 0..COL_MEM_CAPACITY) { "Invalid column index" } + return false } override fun getBoolean(index: Int): Boolean { @@ -93,6 +99,13 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getLong(index: Int): Long { + return when (index) { + COL_CPU_COUNT -> cpuCores.toLong() + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getFloat(index: Int): Float { throw IllegalArgumentException("Invalid column") } @@ -103,6 +116,41 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } } + override fun getString(index: Int): String? { + return when (index) { + COL_ID -> id + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getUUID(index: Int): UUID? { + throw IllegalArgumentException("Invalid column") + } + + override fun getInstant(index: Int): Instant? { + return when (index) { + COL_START_TIME -> startTime + COL_STOP_TIME -> stopTime + else -> throw IllegalArgumentException("Invalid column") + } + } + + override fun getDuration(index: Int): Duration? { + throw IllegalArgumentException("Invalid column") + } + + override fun getList(index: Int, elementType: Class): List? { + throw IllegalArgumentException("Invalid column") + } + + override fun getSet(index: Int, elementType: Class): Set? { + throw IllegalArgumentException("Invalid column") + } + + override fun getMap(index: Int, keyType: Class, valueType: Class): Map? { + throw IllegalArgumentException("Invalid column") + } + override fun close() { parser.close() } @@ -140,19 +188,6 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe memCapacity = Double.NaN } - private val COL_ID = 0 - private val COL_START_TIME = 1 - private val COL_STOP_TIME = 2 - private val COL_CPU_COUNT = 3 - private val COL_MEM_CAPACITY = 4 - private val columns = mapOf( - RESOURCE_ID to COL_ID, - RESOURCE_START_TIME to COL_START_TIME, - RESOURCE_STOP_TIME to COL_STOP_TIME, - RESOURCE_CPU_COUNT to COL_CPU_COUNT, - RESOURCE_MEM_CAPACITY to COL_MEM_CAPACITY - ) - companion object { /** * The [CsvSchema] that is used to parse the trace. diff --git a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureTraceFormat.kt b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureTraceFormat.kt index 73978990..2294e4a4 100644 --- a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureTraceFormat.kt +++ b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureTraceFormat.kt @@ -62,26 +62,25 @@ public class AzureTraceFormat : TraceFormat { return when (table) { TABLE_RESOURCES -> TableDetails( listOf( - RESOURCE_ID, - RESOURCE_START_TIME, - RESOURCE_STOP_TIME, - RESOURCE_CPU_COUNT, - RESOURCE_MEM_CAPACITY + TableColumn(RESOURCE_ID, TableColumnType.String), + TableColumn(RESOURCE_START_TIME, TableColumnType.Instant), + TableColumn(RESOURCE_STOP_TIME, TableColumnType.Instant), + TableColumn(RESOURCE_CPU_COUNT, TableColumnType.Int), + TableColumn(RESOURCE_MEM_CAPACITY, TableColumnType.Double), ) ) TABLE_RESOURCE_STATES -> TableDetails( listOf( - RESOURCE_ID, - RESOURCE_STATE_TIMESTAMP, - RESOURCE_STATE_CPU_USAGE_PCT - ), - listOf(RESOURCE_STATE_TIMESTAMP) + TableColumn(RESOURCE_ID, TableColumnType.String), + TableColumn(RESOURCE_STATE_TIMESTAMP, TableColumnType.Instant), + TableColumn(RESOURCE_STATE_CPU_USAGE_PCT, TableColumnType.Double), + ) ) 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_RESOURCES -> { val stream = GZIPInputStream(path.resolve("vmtable/vmtable.csv.gz").inputStream()) -- 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. --- .../trace/azure/AzureResourceStateTableReader.kt | 19 +++++++++++++++++++ .../opendc/trace/azure/AzureResourceTableReader.kt | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'opendc-trace/opendc-trace-azure/src/main') diff --git a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt index e9017b35..c0c67329 100644 --- a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt +++ b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceStateTableReader.kt @@ -37,11 +37,20 @@ import java.util.* * A [TableReader] for the Azure v1 VM resource state table. */ internal class AzureResourceStateTableReader(private val parser: CsvParser) : TableReader { + /** + * A flag to indicate whether a single row has been read already. + */ + private var isStarted = false + init { parser.schema = schema } override fun nextRow(): Boolean { + if (!isStarted) { + isStarted = true + } + reset() if (!nextStart()) { @@ -100,6 +109,7 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta } override fun getDouble(index: Int): Double { + checkActive() return when (index) { COL_CPU_USAGE_PCT -> cpuUsagePct else -> throw IllegalArgumentException("Invalid column") @@ -107,6 +117,7 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta } override fun getString(index: Int): String? { + checkActive() return when (index) { COL_ID -> id else -> throw IllegalArgumentException("Invalid column") @@ -118,6 +129,7 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta } override fun getInstant(index: Int): Instant? { + checkActive() return when (index) { COL_TIMESTAMP -> timestamp else -> throw IllegalArgumentException("Invalid column") @@ -144,6 +156,13 @@ internal class AzureResourceStateTableReader(private val parser: CsvParser) : Ta parser.close() } + /** + * Helper method to check if the reader is active. + */ + private fun checkActive() { + check(isStarted && !parser.isClosed) { "No active row. Did you call nextRow()?" } + } + /** * Advance the parser until the next object start. */ diff --git a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt index 456a2536..a8451301 100644 --- a/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt +++ b/opendc-trace/opendc-trace-azure/src/main/kotlin/org/opendc/trace/azure/AzureResourceTableReader.kt @@ -35,11 +35,20 @@ import java.util.* * A [TableReader] for the Azure v1 VM resources table. */ internal class AzureResourceTableReader(private val parser: CsvParser) : TableReader { + /** + * A flag to indicate whether a single row has been read already. + */ + private var isStarted = false + init { parser.schema = schema } override fun nextRow(): Boolean { + if (!isStarted) { + isStarted = true + } + reset() if (!nextStart()) { @@ -92,6 +101,7 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getInt(index: Int): Int { + checkActive() return when (index) { COL_CPU_COUNT -> cpuCores else -> throw IllegalArgumentException("Invalid column") @@ -99,6 +109,7 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getLong(index: Int): Long { + checkActive() return when (index) { COL_CPU_COUNT -> cpuCores.toLong() else -> throw IllegalArgumentException("Invalid column") @@ -110,6 +121,7 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getDouble(index: Int): Double { + checkActive() return when (index) { COL_MEM_CAPACITY -> memCapacity else -> throw IllegalArgumentException("Invalid column") @@ -117,6 +129,7 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getString(index: Int): String? { + checkActive() return when (index) { COL_ID -> id else -> throw IllegalArgumentException("Invalid column") @@ -128,6 +141,7 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe } override fun getInstant(index: Int): Instant? { + checkActive() return when (index) { COL_START_TIME -> startTime COL_STOP_TIME -> stopTime @@ -155,6 +169,13 @@ internal class AzureResourceTableReader(private val parser: CsvParser) : TableRe parser.close() } + /** + * Helper method to check if the reader is active. + */ + private fun checkActive() { + check(isStarted && !parser.isClosed) { "No active row. Did you call nextRow()?" } + } + /** * Advance the parser until the next object start. */ -- cgit v1.2.3