-
Notifications
You must be signed in to change notification settings - Fork 117
Cassandra Actions #1729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gonzalotguerrero
wants to merge
11
commits into
master
Choose a base branch
from
feature/cassandra-actions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Cassandra Actions #1729
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fffbc7f
Merge branch 'master' into feature/cassandra-actions
gonzalotguerrero 82ad0a5
Merge branch 'feature/cassandra-handler' into feature/cassandra-actions
gonzalotguerrero af2e36c
Merge branch 'feature/cassandra-dsl' into feature/cassandra-actions
gonzalotguerrero 191ede5
Add Cassandra Actions
gonzalotguerrero b55b6dd
Add tests for Cassandra Actions
gonzalotguerrero 25e99b4
Add CassandraWriter
gonzalotguerrero 0f98cee
Add Cassandra evaluated action
gonzalotguerrero 6a751c7
Merge branch 'feature/cassandra-dsl' into feature/cassandra-actions
gonzalotguerrero a1d5f7e
Merge branch 'feature/cassandra-dsl' into feature/cassandra-actions
gonzalotguerrero 6c189c1
Improve building of insertions
gonzalotguerrero 6240011
Add CqlDurationGene
gonzalotguerrero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraColumn.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| /** | ||
| * A single column of a Cassandra table, as recovered from the schema description string carried by | ||
| * a failed CQL query reported by the SUT driver. | ||
| */ | ||
| data class CassandraColumn( | ||
|
|
||
| val name: String, | ||
|
|
||
| /** | ||
| * The CQL type of the column, as named in the CQL schema, eg "text", "int", "map<text, int>". | ||
| */ | ||
| val cqlType: String, | ||
|
|
||
| /** | ||
| * Whether this column is part of the table's partition key. | ||
| */ | ||
| val isPartitionKey: Boolean = false, | ||
|
|
||
| /** | ||
| * Whether this column is one of the table's clustering columns. | ||
| */ | ||
| val isClusteringColumn: Boolean = false | ||
| ) |
74 changes: 74 additions & 0 deletions
74
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraColumnGeneBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| import org.evomaster.core.search.gene.BooleanGene | ||
| import org.evomaster.core.search.gene.Gene | ||
| import org.evomaster.core.search.gene.UUIDGene | ||
| import org.evomaster.core.search.gene.cassandra.CqlDurationGene | ||
| import org.evomaster.core.search.gene.datetime.DateGene | ||
| import org.evomaster.core.search.gene.datetime.DateTimeGene | ||
| import org.evomaster.core.search.gene.datetime.TimeGene | ||
| import org.evomaster.core.search.gene.numeric.* | ||
| import org.evomaster.core.search.gene.string.StringGene | ||
|
|
||
| /** | ||
| * Builds the gene used to generate the value of a Cassandra column, based on its CQL type. | ||
| * | ||
| * Two different reasons keep a CQL type out of the ones handled here: | ||
| * - the value of a column of that type cannot be generated at all, ie a counter, which is only | ||
| * writable with an UPDATE, and a timeuuid, which requires a version 1 UUID, whereas [UUIDGene] | ||
| * generates a random one; | ||
| * - no gene generating a value of that type has been written yet, ie blob, inet, the collections | ||
| * and the user defined types. | ||
| */ | ||
| object CassandraColumnGeneBuilder { | ||
|
|
||
| /** | ||
| * How the gene generating the value of a column is built, for each of the CQL types handled | ||
| * here, keyed by the normalized name of the type. Being the single place where such types are | ||
| * enumerated, it is also what [isSupported] answers from, so that the two cannot disagree. | ||
| */ | ||
| private val GENE_BUILDERS: Map<String, (String) -> Gene> = mapOf( | ||
| "ascii" to { name -> StringGene(name) }, | ||
| "text" to { name -> StringGene(name) }, | ||
| "varchar" to { name -> StringGene(name) }, | ||
| "tinyint" to { name -> IntegerGene(name, min = Byte.MIN_VALUE.toInt(), max = Byte.MAX_VALUE.toInt()) }, | ||
| "smallint" to { name -> IntegerGene(name, min = Short.MIN_VALUE.toInt(), max = Short.MAX_VALUE.toInt()) }, | ||
| "int" to { name -> IntegerGene(name) }, | ||
| "bigint" to { name -> LongGene(name) }, | ||
| "varint" to { name -> BigIntegerGene(name) }, | ||
| "decimal" to { name -> BigDecimalGene(name) }, | ||
| "float" to { name -> FloatGene(name) }, | ||
| "double" to { name -> DoubleGene(name) }, | ||
| "boolean" to { name -> BooleanGene(name) }, | ||
| "uuid" to { name -> UUIDGene(name) }, | ||
| /* | ||
| Only valid values are generated, as these genes are used to set up the state of the | ||
| database, and Cassandra would just reject an insertion carrying an invalid one. | ||
| */ | ||
| "timestamp" to { name -> DateTimeGene(name, onlyValid = true) }, | ||
| "date" to { name -> DateGene(name, onlyValidDates = true) }, | ||
| "time" to { name -> TimeGene(name, onlyValidTimes = true) }, | ||
| "duration" to { name -> CqlDurationGene(name) } | ||
| ) | ||
|
|
||
| /** | ||
| * @return whether a gene can be built for [column], ie whether its CQL type is one of the | ||
| * scalar types handled here | ||
| */ | ||
| fun isSupported(column: CassandraColumn) = normalize(column.cqlType) in GENE_BUILDERS | ||
|
|
||
| /** | ||
| * @throws IllegalArgumentException if the CQL type of [column] is not handled, as verifiable | ||
| * beforehand with [isSupported] | ||
| */ | ||
| fun buildGene(column: CassandraColumn): Gene { | ||
|
|
||
| val builder = GENE_BUILDERS[normalize(column.cqlType)] | ||
| ?: throw IllegalArgumentException("Cannot handle the CQL type of column $column") | ||
|
|
||
| return builder(column.name) | ||
| } | ||
|
|
||
| private fun normalize(cqlType: String) = cqlType.trim().lowercase() | ||
|
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraDbAction.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| import org.evomaster.core.search.action.Action | ||
| import org.evomaster.core.search.action.EnvironmentAction | ||
| import org.evomaster.core.search.gene.Gene | ||
|
|
||
| /** | ||
| * An action inserting a single row into a Cassandra table, used to set up the state of the database | ||
| * before the main actions of a test are executed. | ||
| */ | ||
| class CassandraDbAction( | ||
| /** | ||
| * The keyspace containing the table to insert the row into | ||
| */ | ||
| val keyspace: String, | ||
| /** | ||
| * The table to insert the row into | ||
| */ | ||
| val table: String, | ||
| /** | ||
| * The columns the row is composed of, ie the ones a value is generated for. | ||
| * There is exactly one gene per column, in the same order. | ||
| */ | ||
| val columns: List<CassandraColumn>, | ||
| /** | ||
| * The genes generating the value of each of the [columns], in the same order. | ||
| * Only meant to be given when copying an existing action, so that its genes are carried over | ||
| * instead of being built anew: when not given, one gene is built per column. | ||
| */ | ||
| computedGenes: List<Gene>? = null | ||
| ) : EnvironmentAction(listOf()) { | ||
|
|
||
| private val genes: List<Gene> = (computedGenes ?: computeGenes()).also { addChildren(it) } | ||
|
|
||
| init { | ||
| if (genes.size != columns.size) { | ||
| throw IllegalArgumentException("Mismatch between the ${columns.size} columns and the ${genes.size} genes") | ||
| } | ||
| } | ||
|
|
||
| private fun computeGenes(): List<Gene> { | ||
| return columns.map { CassandraColumnGeneBuilder.buildGene(it) } | ||
| } | ||
|
|
||
| override fun getName(): String { | ||
| return "CASSANDRA_Insert_${keyspace}_${table}" | ||
| } | ||
|
|
||
| override fun seeTopGenes(): List<Gene> { | ||
| return genes | ||
| } | ||
|
|
||
| override fun copyContent(): Action { | ||
| return CassandraDbAction(keyspace, table, columns, genes.map(Gene::copy)) | ||
| } | ||
| } |
36 changes: 36 additions & 0 deletions
36
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraDbActionResult.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| import org.evomaster.core.search.action.Action | ||
| import org.evomaster.core.search.action.ActionResult | ||
|
|
||
| /** | ||
| * Cassandra insert action execution result | ||
| */ | ||
| class CassandraDbActionResult : ActionResult { | ||
|
|
||
| constructor(sourceLocalId: String, stopping: Boolean = false) : super(sourceLocalId, stopping) | ||
| constructor(other: CassandraDbActionResult) : super(other) | ||
|
|
||
| companion object { | ||
| const val INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY = "INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY" | ||
| } | ||
|
|
||
| override fun copy(): CassandraDbActionResult { | ||
| return CassandraDbActionResult(this) | ||
| } | ||
|
|
||
| /** | ||
| * @param success specifies whether the INSERT CASSANDRA executed successfully | ||
| */ | ||
| fun setInsertExecutionResult(success: Boolean) = | ||
| addResultValue(INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY, success.toString()) | ||
|
|
||
| /** | ||
| * @return whether the Cassandra action executed successfully | ||
| */ | ||
| fun getInsertExecutionResult() = getResultValue(INSERT_CASSANDRA_EXECUTE_SUCCESSFULLY)?.toBoolean() ?: false | ||
|
|
||
| override fun matchedType(action: Action): Boolean { | ||
| return action is CassandraDbAction | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraDbActionTransformer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| import org.evomaster.client.java.controller.api.dto.database.operations.CassandraDatabaseCommandDto | ||
| import org.evomaster.client.java.controller.api.dto.database.operations.CassandraInsertionDto | ||
| import org.evomaster.client.java.controller.api.dto.database.operations.CassandraInsertionEntryDto | ||
|
|
||
| /** | ||
| * Transforms the Cassandra insert actions of an individual into the commands to be executed on the | ||
| * SUT side. | ||
| */ | ||
| object CassandraDbActionTransformer { | ||
|
|
||
| fun transform(actions: List<CassandraDbAction>): CassandraDatabaseCommandDto { | ||
|
|
||
| val insertionDtos = mutableListOf<CassandraInsertionDto>() | ||
|
|
||
| for (action in actions) { | ||
|
|
||
| val insertionDto = CassandraInsertionDto().apply { | ||
| keyspaceName = action.keyspace | ||
| tableName = action.table | ||
| } | ||
|
|
||
| action.seeTopGenes() | ||
| .filter { it.isPrintable() } | ||
| .forEach { gene -> | ||
| val entry = CassandraInsertionEntryDto().apply { | ||
| columnName = gene.name | ||
| printableValue = CassandraLiteralRenderer.toCqlLiteral(gene) | ||
| } | ||
| insertionDto.data.add(entry) | ||
| } | ||
|
|
||
| insertionDtos.add(insertionDto) | ||
| } | ||
|
|
||
| return CassandraDatabaseCommandDto().apply { this.insertions = insertionDtos } | ||
| } | ||
| } |
83 changes: 83 additions & 0 deletions
83
core/src/main/kotlin/org/evomaster/core/database/cassandra/CassandraInsertBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package org.evomaster.core.database.cassandra | ||
|
|
||
| import org.evomaster.core.logging.LoggingUtil | ||
| import org.slf4j.Logger | ||
| import org.slf4j.LoggerFactory | ||
|
|
||
| /** | ||
| * Builds the action inserting a row into a Cassandra table, based on the description of the | ||
| * columns of that table reported by the SUT driver. | ||
| */ | ||
| class CassandraInsertBuilder { | ||
|
|
||
| companion object { | ||
| private val log: Logger = LoggerFactory.getLogger(CassandraInsertBuilder::class.java) | ||
| } | ||
|
|
||
| /** | ||
| * @param tableSchema the description of the columns of a table, as reported by the SUT driver | ||
| * @return whether an insertion that could be executed can be built for such a table, ie whether | ||
| * a value can be generated for at least one of its columns and for all of the ones composing | ||
| * its primary key | ||
| */ | ||
| fun canBuildInsertionFor(tableSchema: String): Boolean { | ||
|
|
||
| val (supported, unsupported) = partitionBySupport(CassandraTableSchemaParser.parse(tableSchema)) | ||
|
|
||
| return supported.isNotEmpty() && unsupported.none { isPartOfPrimaryKey(it) } | ||
| } | ||
|
|
||
| /** | ||
| * The columns whose CQL type is not handled are left out of the insertion, as no value can be | ||
| * generated for them. The resulting insertion is still worth executing, since the remaining | ||
| * columns might be all that is needed, and a rejected insertion is already recorded as a failed | ||
| * one instead of stopping the search. | ||
| * | ||
| * That argument does not hold when no value can be generated for any column, nor when one of | ||
| * the skipped columns is part of the primary key, as Cassandra requires a full primary key in | ||
| * an INSERT: in both cases the insertion could only be rejected, so none is built. | ||
| * | ||
| * Note that the genes of the returned action are not initialized yet, which is left to the | ||
| * caller, as it is done for the other types of database action. | ||
| * | ||
| * @throws IllegalArgumentException if no insertion that could be executed can be built for the | ||
| * table, as verifiable beforehand with [canBuildInsertionFor] | ||
| */ | ||
| fun createCassandraInsertionAction(keyspace: String, table: String, tableSchema: String): CassandraDbAction { | ||
|
|
||
| val (supported, unsupported) = partitionBySupport(CassandraTableSchemaParser.parse(tableSchema)) | ||
|
|
||
| val qualifiedTableName = "$keyspace.$table" | ||
|
|
||
| if (supported.isEmpty()) { | ||
| throw IllegalArgumentException("No value can be generated for any column of" + | ||
| " $qualifiedTableName: ${describe(unsupported)}") | ||
| } | ||
|
|
||
| val unsupportedKeyColumns = unsupported.filter { isPartOfPrimaryKey(it) } | ||
| if (unsupportedKeyColumns.isNotEmpty()) { | ||
| throw IllegalArgumentException("No value can be generated for some of the columns composing" + | ||
| " the primary key of $qualifiedTableName: ${describe(unsupportedKeyColumns)}") | ||
| } | ||
|
|
||
| if (unsupported.isNotEmpty()) { | ||
| LoggingUtil.uniqueWarn( | ||
| log, | ||
| "Cannot generate data for some columns of a Cassandra table, as their CQL type is not handled: {}", | ||
| "$qualifiedTableName: ${describe(unsupported)}" | ||
| ) | ||
| } | ||
|
|
||
| return CassandraDbAction(keyspace, table, supported).apply { forceNewTaints() } | ||
| } | ||
|
|
||
| /** | ||
| * @return the columns a value can be generated for (first), and the ones it cannot (second) | ||
| */ | ||
| private fun partitionBySupport(columns: List<CassandraColumn>) = | ||
| columns.partition { CassandraColumnGeneBuilder.isSupported(it) } | ||
|
|
||
| private fun isPartOfPrimaryKey(column: CassandraColumn) = column.isPartitionKey || column.isClusteringColumn | ||
|
|
||
| private fun describe(columns: List<CassandraColumn>) = columns.joinToString(", ") { "${it.name} ${it.cqlType}" } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace these strings with constants