Skip to content
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

Add support for setting the serial consistency level for leader election #86

Open
wants to merge 2 commits into
base: master_v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Maven CI

on: push

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Run Maven tests
run: mvn --batch-mode test

4 changes: 2 additions & 2 deletions cassandra-migration-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<parent>
<groupId>org.cognitor.cassandra</groupId>
<artifactId>cassandra-migration-parent</artifactId>
<version>2.6.2_v4-SNAPSHOT</version>
<version>2.6.3_v4-SNAPSHOT</version>
</parent>

<artifactId>cassandra-migration-spring-boot-starter</artifactId>
<version>2.6.2_v4-SNAPSHOT</version>
<version>2.6.3_v4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cassandra Migration Spring Boot Integration</name>
<url>https://github.com/patka/cassandra-migration</url>
Expand Down
4 changes: 2 additions & 2 deletions cassandra-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<parent>
<groupId>org.cognitor.cassandra</groupId>
<artifactId>cassandra-migration-parent</artifactId>
<version>2.6.2_v4-SNAPSHOT</version>
<version>2.6.3_v4-SNAPSHOT</version>
</parent>

<artifactId>cassandra-migration</artifactId>
<version>2.6.2_v4-SNAPSHOT</version>
<version>2.6.3_v4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Cassandra Migration</name>
<url>https://github.com/patka/cassandra-migration</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public class Database implements Closeable {
private final String keyspaceName;
private final Keyspace keyspace;
private final CqlSession session;
private final ConsistencyLevel consistencyLevel = ConsistencyLevel.QUORUM;
private ConsistencyLevel consistencyLevel = ConsistencyLevel.SERIAL;
private final PreparedStatement logMigrationStatement;
private final PreparedStatement takeMigrationLeadStatement;
private final PreparedStatement releaseMigrationLeadStatement;
Expand Down Expand Up @@ -286,7 +286,8 @@ private void createSchemaTables() {

/**
* Attempts to acquire the lead on a migration through a LightWeight
* Transaction. For this statement the consistency level of <code>QUORUM</code> is used.
* Transaction. For this statement the consistency level of <code>SERIAL</code> is used by default,
* see {@link Database#setLeaderConsistencyLevel(ConsistencyLevel)} for configuring this.
*
* @param repositoryLatestVersion the latest version number in the migration repository
* @return if taking the lead succeeded.
Expand All @@ -296,8 +297,8 @@ boolean takeLeadOnMigrations(int repositoryLatestVersion) {
try {
LOGGER.debug("Trying to take lead on schema migrations");
BoundStatement boundStatement = takeMigrationLeadStatement.bind(getKeyspaceName(), this.instanceId,
this.instanceAddress);
ResultSet lwtResult = executeStatement(boundStatement, this.consistencyLevel);
this.instanceAddress).setSerialConsistencyLevel(this.consistencyLevel);
ResultSet lwtResult = executeStatement(boundStatement);

if (lwtResult.wasApplied()) {
LOGGER.debug("Took lead on schema migrations");
Expand Down Expand Up @@ -334,8 +335,8 @@ void removeLeadOnMigrations() {
if (tookLead) {
LOGGER.debug("Trying to release lead on schema migrations");

BoundStatement boundStatement = releaseMigrationLeadStatement.bind(getKeyspaceName(), this.instanceId);
ResultSet lwtResult = executeStatement(boundStatement, this.consistencyLevel);
BoundStatement boundStatement = releaseMigrationLeadStatement.bind(getKeyspaceName(), this.instanceId).setSerialConsistencyLevel(this.consistencyLevel);
ResultSet lwtResult = executeStatement(boundStatement);

if (lwtResult.wasApplied()) {
LOGGER.debug("Released lead on schema migrations");
Expand Down Expand Up @@ -389,13 +390,11 @@ private void executeMigrationStatement(String statement, DbMigration migration)
}

private ResultSet executeStatement(String statement) throws DriverException {
return executeStatement(SimpleStatement.newInstance(statement), this.migrationConsistencyLevel);
return executeStatement(SimpleStatement.newInstance(statement).setConsistencyLevel(this.migrationConsistencyLevel));
}

private ResultSet executeStatement(Statement<?> statement, ConsistencyLevel consistencyLevel) throws DriverException {
return session.execute(statement
.setExecutionProfileName(executionProfileName)
.setConsistencyLevel(consistencyLevel));
private ResultSet executeStatement(Statement<?> statement) throws DriverException {
return session.execute(statement.setExecutionProfileName(executionProfileName));
}

/**
Expand All @@ -406,8 +405,9 @@ private ResultSet executeStatement(Statement<?> statement, ConsistencyLevel cons
*/
private void logMigration(DbMigration migration, boolean wasSuccessful) {
BoundStatement boundStatement = logMigrationStatement.bind(wasSuccessful, migration.getVersion(),
migration.getScriptName(), migration.getMigrationScript(), Instant.now());
executeStatement(boundStatement, this.migrationConsistencyLevel);
migration.getScriptName(), migration.getMigrationScript(), Instant.now())
.setConsistencyLevel(this.migrationConsistencyLevel);
executeStatement(boundStatement);
}

/**
Expand All @@ -434,6 +434,18 @@ public Database setConsistencyLevel(ConsistencyLevel migrationConsistencyLevel)
return this;
}

/**
* Set the consistency level for the lightweight transaction used for taking the lead in migrations. Default is
* <code>ConsistencyLevel.SERIAL</code>.
*
* @param consistencyLevel The Cassandra serial consistency level to use for the lightweight transaction
* @return the current database instance
*/
public Database setLeaderConsistencyLevel(ConsistencyLevel consistencyLevel) {
this.consistencyLevel = notNull(consistencyLevel, "consistencyLevel");
return this;
}

/**
* Set the execution profile name to be used for schema upgrades. This profile has to match an existing
* profile defined in <code>application.conf</code> for the Cassandra driver. This should be set
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.cognitor.cassandra</groupId>
<artifactId>cassandra-migration-parent</artifactId>
<version>2.6.2_v4-SNAPSHOT</version>
<version>2.6.3_v4-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Cassandra Migration Parent</name>
<url>https://github.com/patka/cassandra-migration</url>
Expand Down