-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic migration test from Cassandra
- Loading branch information
Showing
9 changed files
with
256 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,3 @@ | ||
cassandra/ | ||
scylla/ | ||
spark-master/ |
43 changes: 43 additions & 0 deletions
43
tests/src/test/configurations/cassandra-to-scylla-basic.yaml
This file contains 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,43 @@ | ||
source: | ||
type: cassandra | ||
host: cassandra | ||
port: 9042 | ||
localDC: datacenter1 | ||
credentials: | ||
username: dummy | ||
password: dummy | ||
keyspace: test | ||
table: basictest | ||
consistencyLevel: LOCAL_QUORUM | ||
preserveTimestamps: true | ||
splitCount: 8 | ||
connections: 8 | ||
fetchSize: 1000 | ||
|
||
target: | ||
type: scylla | ||
host: scylla | ||
port: 9042 | ||
localDC: datacenter1 | ||
credentials: | ||
username: dummy | ||
password: dummy | ||
keyspace: test | ||
table: basictest | ||
consistencyLevel: LOCAL_QUORUM | ||
connections: 16 | ||
stripTrailingZerosForDecimals: false | ||
|
||
renames: [] | ||
|
||
savepoints: | ||
path: /app/savepoints | ||
intervalSeconds: 300 | ||
skipTokenRanges: [] | ||
validation: | ||
compareTimestamps: true | ||
ttlToleranceMillis: 60000 | ||
writetimeToleranceMillis: 1000 | ||
failuresToFetch: 100 | ||
floatingPointTolerance: 0.001 | ||
timestampMsTolerance: 0 |
This file contains 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,5 @@ | ||
datastax-java-driver { | ||
basic.request { | ||
timeout = 10 seconds | ||
} | ||
} |
This file contains 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
43 changes: 43 additions & 0 deletions
43
tests/src/test/scala/com/scylladb/migrator/scylla/BasicMigrationTest.scala
This file contains 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,43 @@ | ||
package com.scylladb.migrator.scylla | ||
|
||
import com.datastax.oss.driver.api.querybuilder.QueryBuilder | ||
import com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal | ||
import com.datastax.oss.driver.api.querybuilder.term.Term | ||
|
||
import scala.jdk.CollectionConverters._ | ||
import scala.util.chaining._ | ||
|
||
class BasicMigrationTest extends MigratorSuite { | ||
|
||
withTable("BasicTest").test("Read from source and write to target") { tableName => | ||
val insertStatement = | ||
QueryBuilder | ||
.insertInto(keyspace, tableName) | ||
.values(Map[String, Term]( | ||
"id" -> literal("12345"), | ||
"foo" -> literal("bar") | ||
).asJava) | ||
.build() | ||
|
||
// Insert some items | ||
sourceCassandra.execute(insertStatement) | ||
|
||
// Perform the migration | ||
submitSparkJob("cassandra-to-scylla-basic.yaml") | ||
|
||
// Check that the item has been migrated to the target table | ||
val selectAllStatement = QueryBuilder | ||
.selectFrom(keyspace, tableName) | ||
.all() | ||
.build() | ||
targetScylla.execute(selectAllStatement).tap { resultSet => | ||
val rows = resultSet.all().asScala | ||
assertEquals(rows.size, 1) | ||
val row = rows.head | ||
assertEquals(row.getColumnDefinitions.size(), 2) | ||
assertEquals(row.getString("id"), "12345") | ||
assertEquals(row.getString("foo"), "bar") | ||
} | ||
} | ||
|
||
} |
136 changes: 136 additions & 0 deletions
136
tests/src/test/scala/com/scylladb/migrator/scylla/MigratorSuite.scala
This file contains 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,136 @@ | ||
package com.scylladb.migrator.scylla | ||
|
||
import com.datastax.oss.driver.api.core.CqlSession | ||
import com.datastax.oss.driver.api.core.`type`.DataTypes | ||
import com.datastax.oss.driver.api.querybuilder.SchemaBuilder | ||
|
||
import java.net.InetSocketAddress | ||
import scala.sys.process.Process | ||
import scala.jdk.CollectionConverters._ | ||
import scala.util.chaining._ | ||
|
||
/** | ||
* Base class for implementing end-to-end tests. | ||
* | ||
* It expects external services (Cassandra, Scylla, Spark, etc.) to be running. | ||
* See the files `CONTRIBUTING.md` and `docker-compose-tests.yml` for more information. | ||
*/ | ||
trait MigratorSuite extends munit.FunSuite { | ||
|
||
val keyspace = "test" | ||
|
||
/** Client of a source Cassandra instance */ | ||
val sourceCassandra: CqlSession = CqlSession | ||
.builder() | ||
.addContactPoint(new InetSocketAddress("localhost", 9043)) | ||
.withLocalDatacenter("datacenter1") | ||
.withAuthCredentials("dummy", "dummy") | ||
.build() | ||
|
||
/** Client of a target ScyllaDB instance */ | ||
val targetScylla: CqlSession = CqlSession | ||
.builder() | ||
.addContactPoint(new InetSocketAddress("localhost", 9042)) | ||
.withLocalDatacenter("datacenter1") | ||
.withAuthCredentials("dummy", "dummy") | ||
.build() | ||
|
||
/** | ||
* Fixture automating the house-keeping work when migrating a table. | ||
* | ||
* It deletes the table from both the source and target databases in case it was already | ||
* existing, and then recreates it in the source database. | ||
* | ||
* After the test is executed, it deletes the table from both the source and target | ||
* databases. | ||
* | ||
* @param name Name of the table | ||
*/ | ||
def withTable(name: String): FunFixture[String] = FunFixture( | ||
setup = { _ => | ||
def dropAndRecreateTable(database: CqlSession): Unit = | ||
try { | ||
val dropTableStatement = | ||
SchemaBuilder | ||
.dropTable(keyspace, name) | ||
.ifExists() | ||
.build() | ||
database | ||
.execute(dropTableStatement) | ||
.ensuring(_.wasApplied()) | ||
val createTableStatement = | ||
SchemaBuilder | ||
.createTable(keyspace, name) | ||
.withPartitionKey("id", DataTypes.TEXT) | ||
.withColumn("foo", DataTypes.TEXT) | ||
.build() | ||
database | ||
.execute(createTableStatement) | ||
.ensuring(_.wasApplied()) | ||
} catch { | ||
case any: Throwable => | ||
fail(s"Something did not work as expected", any) | ||
} | ||
// Make sure the source and target databases do not contain the table already | ||
dropAndRecreateTable(sourceCassandra) | ||
dropAndRecreateTable(targetScylla) | ||
name | ||
}, | ||
teardown = { _ => | ||
// Clean-up both the source and target databases | ||
val dropTableQuery = SchemaBuilder.dropTable(keyspace, name).build() | ||
targetScylla.execute(dropTableQuery) | ||
sourceCassandra.execute(dropTableQuery) | ||
() | ||
} | ||
) | ||
|
||
/** | ||
* Run a migration by submitting a Spark job to the Spark cluster. | ||
* @param migratorConfigFile Configuration file to use. Write your | ||
* configuration files in the directory | ||
* `src/test/configurations`, which is | ||
* automatically mounted to the Spark | ||
* cluster by Docker Compose. | ||
*/ | ||
def submitSparkJob(migratorConfigFile: String): Unit = { | ||
Process( | ||
Seq( | ||
"docker", | ||
"compose", | ||
"-f", "docker-compose-tests.yml", | ||
"exec", | ||
"spark-master", | ||
"/spark/bin/spark-submit", | ||
"--class", "com.scylladb.migrator.Migrator", | ||
"--master", "spark://spark-master:7077", | ||
"--conf", "spark.driver.host=spark-master", | ||
"--conf", s"spark.scylla.config=/app/configurations/${migratorConfigFile}", | ||
// Uncomment one of the following lines to plug a remote debugger on the Spark master or worker. | ||
// "--conf", "spark.driver.extraJavaOptions=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005", | ||
// "--conf", "spark.executor.extraJavaOptions=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5006", | ||
"/jars/scylla-migrator-assembly-0.0.1.jar" | ||
) | ||
).run().exitValue().tap { statusCode => | ||
assertEquals(statusCode, 0, "Spark job failed") | ||
} | ||
() | ||
} | ||
|
||
override def beforeAll(): Unit = { | ||
val keyspaceStatement = | ||
SchemaBuilder | ||
.createKeyspace(keyspace) | ||
.ifNotExists() | ||
.withReplicationOptions(Map[String, AnyRef]("class" -> "SimpleStrategy", "replication_factor" -> new Integer(1)).asJava) | ||
.build() | ||
sourceCassandra.execute(keyspaceStatement) | ||
targetScylla.execute(keyspaceStatement) | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
sourceCassandra.close() | ||
targetScylla.close() | ||
} | ||
|
||
} |