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

Upgrade cassandra driver 4.10.0 #3952

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 6 additions & 2 deletions modules/cassandra/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
description = "TestContainers :: Cassandra"

dependencies {
compile project(":database-commons")
compile "com.datastax.cassandra:cassandra-driver-core:3.7.1"
implementation project(":database-commons")
implementation("com.datastax.oss:java-driver-core:4.10.0") {
// fasterxml 2.12 not compatible for com.github.docker-java
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-core'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.testcontainers.containers;

import com.datastax.driver.core.Cluster;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.github.dockerjava.api.command.InspectContainerResponse;
import org.apache.commons.io.IOUtils;
import org.testcontainers.containers.delegate.CassandraDatabaseDelegate;
Expand All @@ -12,6 +13,7 @@

import javax.script.ScriptException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
Expand All @@ -35,10 +37,11 @@ public class CassandraContainer<SELF extends CassandraContainer<SELF>> extends G
private static final String CONTAINER_CONFIG_LOCATION = "/etc/cassandra";
private static final String USERNAME = "cassandra";
private static final String PASSWORD = "cassandra";
protected static final String LOCAL_DC = "datacenter1";

private String configLocation;
private String initScriptPath;
private boolean enableJmxReporting;
private Object metricRegistry;

/**
* @deprecated use {@link #CassandraContainer(DockerImageName)} instead
Expand All @@ -59,7 +62,6 @@ public CassandraContainer(DockerImageName dockerImageName) {

addExposedPort(CQL_PORT);
setStartupAttempts(3);
this.enableJmxReporting = false;
}

@Override
Expand Down Expand Up @@ -136,10 +138,13 @@ public SELF withInitScript(String initScriptPath) {
}

/**
* Initialize Cassandra client with JMX reporting enabled or disabled
* Register an external Metric Registry object in the Cassandra driver,
* see https://docs.datastax.com/en/developer/java-driver/4.10/manual/core/metrics/#metric-registry
*
* @param metricRegistry
*/
public SELF withJmxReporting(boolean enableJmxReporting) {
this.enableJmxReporting = enableJmxReporting;
public SELF withMetricRegistry(Object metricRegistry) {
this.metricRegistry = metricRegistry;
return self();
}

Expand Down Expand Up @@ -167,27 +172,35 @@ public String getPassword() {
return PASSWORD;
}

public String getCqlHostAddress() {
return getHost() + ":" + getMappedPort(CassandraContainer.CQL_PORT);
}

public String getLocalDc() { return CassandraContainer.LOCAL_DC; }

/**
* Get configured Cluster
*
* Can be used to obtain connections to Cassandra in the container
*/
public Cluster getCluster() {
return getCluster(this, enableJmxReporting);
public CqlSession getCqlSession() {
return getCqlSession(this, this.metricRegistry);
}

public static Cluster getCluster(ContainerState containerState, boolean enableJmxReporting) {
final Cluster.Builder builder = Cluster.builder()
.addContactPoint(containerState.getHost())
.withPort(containerState.getMappedPort(CQL_PORT));
if (!enableJmxReporting) {
builder.withoutJMXReporting();
public static CqlSession getCqlSession(ContainerState containerState, Object meterRegistry) {
InetSocketAddress endpoint = new InetSocketAddress(containerState.getHost(), containerState.getMappedPort(CQL_PORT));
final CqlSessionBuilder builder = CqlSession.builder()
.addContactPoint(endpoint)
.withLocalDatacenter(LOCAL_DC);

if (meterRegistry != null) {
builder.withMetricRegistry(meterRegistry);
}
return builder.build();
}

public static Cluster getCluster(ContainerState containerState) {
return getCluster(containerState, false);
public CqlSession getCqlSession(ContainerState containerState) {
return getCqlSession(containerState, false);
}

private DatabaseDelegate getDatabaseDelegate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.testcontainers.containers.delegate;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.DriverException;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.CassandraContainer;
Expand All @@ -18,15 +19,14 @@
*/
@Slf4j
@RequiredArgsConstructor
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<Session> {
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<CqlSession> {

private final ContainerState container;

@Override
protected Session createNewConnection() {
protected CqlSession createNewConnection() {
try {
return CassandraContainer.getCluster(container)
.newSession();
return CassandraContainer.getCqlSession(container, false);
} catch (DriverException e) {
log.error("Could not obtain cassandra connection");
throw new ConnectionCreationException("Could not obtain cassandra connection", e);
Expand All @@ -48,9 +48,9 @@ public void execute(String statement, String scriptPath, int lineNumber, boolean
}

@Override
protected void closeConnectionQuietly(Session session) {
protected void closeConnectionQuietly(CqlSession cqlSession) {
try {
session.getCluster().close();
cqlSession.close();
} catch (Exception e) {
log.error("Could not close cassandra connection", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.testcontainers.containers;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.testcontainers.containers.wait.CassandraQueryWaitStrategy;
import org.testcontainers.utility.DockerImageName;

import java.net.InetSocketAddress;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -107,7 +109,7 @@ public void testCassandraQueryWaitStrategy() {
public void testCassandraGetCluster() {
try (CassandraContainer<?> cassandraContainer = new CassandraContainer<>()) {
cassandraContainer.start();
ResultSet resultSet = performQuery(cassandraContainer.getCluster(), "SELECT release_version FROM system.local");
ResultSet resultSet = performQuery(cassandraContainer, "SELECT release_version FROM system.local");
assertTrue("Query was not applied", resultSet.wasApplied());
assertNotNull("Result set has no release_version", resultSet.one().getString(0));
}
Expand All @@ -122,17 +124,13 @@ private void testInitScript(CassandraContainer<?> cassandraContainer) {
}

private ResultSet performQuery(CassandraContainer<?> cassandraContainer, String cql) {
Cluster explicitCluster = Cluster.builder()
.addContactPoint(cassandraContainer.getHost())
.withPort(cassandraContainer.getMappedPort(CassandraContainer.CQL_PORT))
InetSocketAddress endpoint = new InetSocketAddress(
cassandraContainer.getHost(),
cassandraContainer.getMappedPort(CassandraContainer.CQL_PORT));
CqlSession cqlSession = CqlSession.builder()
.addContactPoint(endpoint)
.withLocalDatacenter(CassandraContainer.LOCAL_DC)
.build();
return performQuery(explicitCluster, cql);
}

private ResultSet performQuery(Cluster cluster, String cql) {
try (Cluster closeableCluster = cluster) {
Session session = closeableCluster.newSession();
return session.execute(cql);
}
return cqlSession.execute(cql);
}
}