Skip to content

Commit

Permalink
Allow connecting with DcInferringLoadBalancingPolicy instead of local…
Browse files Browse the repository at this point in the history
…datacenter (#49)

* Allow connecting with DcInferringLoadBalancingPolicy instead of localdatacenter
* Restore imports
  • Loading branch information
kornilova203 committed Dec 15, 2023
1 parent af90f3a commit 96f7e64
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/ing/data/cassandra/jdbc/SessionHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private Session createSession(final Properties properties) throws SQLException {
final String username = properties.getProperty(TAG_USER, StringUtils.EMPTY);
final String password = properties.getProperty(TAG_PASSWORD, StringUtils.EMPTY);
final String loadBalancingPolicy = properties.getProperty(TAG_LOAD_BALANCING_POLICY, StringUtils.EMPTY);
final String localDatacenter = properties.getProperty(TAG_LOCAL_DATACENTER, StringUtils.EMPTY);
final String localDatacenter = properties.getProperty(TAG_LOCAL_DATACENTER, null);
final String retryPolicy = properties.getProperty(TAG_RETRY_POLICY, StringUtils.EMPTY);
final String reconnectPolicy = properties.getProperty(TAG_RECONNECT_POLICY, StringUtils.EMPTY);
final boolean debugMode = Boolean.TRUE.toString().equals(properties.getProperty(TAG_DEBUG,
Expand Down Expand Up @@ -270,7 +270,7 @@ private Session createSession(final Properties properties) throws SQLException {
// When a configuration file is used, we rely on the property 'basic.load-balancing-policy.local-datacenter'
// of the configuration file, so we must not call withLocalDatacenter() method because when both are specified,
// the programmatic value takes precedence.
if (configurationFile == null || !configurationFileExists) {
if ((configurationFile == null || !configurationFileExists) && localDatacenter != null) {
builder.withLocalDatacenter(localDatacenter);
}
if (!loadBalancingPolicy.isEmpty()) {
Expand Down
44 changes: 28 additions & 16 deletions src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.ing.data.cassandra.jdbc.utils.FakeReconnectionPolicy;
import com.ing.data.cassandra.jdbc.utils.FakeRetryPolicy;
import com.ing.data.cassandra.jdbc.utils.FakeSslEngineFactory;
import org.apache.commons.lang3.StringUtils;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -97,6 +96,18 @@ void givenInvalidConfigurationFile_whenGetConnection_createConnectionIgnoringCon
sqlConnection.close();
}

@Test
void givenNoLocalDataCenter_whenInferringLoadBalancingPolicySpecified_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "loadBalancing=DcInferringLoadBalancingPolicy");
assertNotNull(sqlConnection);
final Statement statement = sqlConnection.createStatement();
final ResultSet resultSet = statement.executeQuery("SELECT * FROM system.local");
assertNotNull(resultSet);
resultSet.close();
statement.close();
sqlConnection.close();
}

@Test
void givenValidConfigurationFile_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
final URL confTestUrl = this.getClass().getClassLoader().getResource("test_application.conf");
Expand Down Expand Up @@ -164,7 +175,7 @@ void givenValidConfigurationFile_whenGetConnection_createConnectionWithExpectedC

@Test
void givenRequestTimeout_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "requesttimeout=10000");
initConnection(KEYSPACE, "requesttimeout=10000", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -176,7 +187,7 @@ void givenRequestTimeout_whenGetConnection_createConnectionWithExpectedConfig()

@Test
void givenConnectTimeout_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "connecttimeout=8000");
initConnection(KEYSPACE, "connecttimeout=8000", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -193,7 +204,7 @@ void givenConnectTimeout_whenGetConnection_createConnectionWithExpectedConfig()

@Test
void givenNonDefaultSocketOptions_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "tcpnodelay=false", "keepalive=true");
initConnection(KEYSPACE, "tcpnodelay=false", "keepalive=true", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -207,7 +218,7 @@ void givenNonDefaultSocketOptions_whenGetConnection_createConnectionWithExpected

@Test
void givenNoLoadBalancingPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, StringUtils.EMPTY);
initConnection(KEYSPACE, "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand Down Expand Up @@ -240,7 +251,7 @@ void givenInvalidLoadBalancingPolicy_whenGetConnection_throwsException() {

@Test
void givenNoRetryPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, StringUtils.EMPTY);
initConnection(KEYSPACE, "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -253,7 +264,7 @@ void givenNoRetryPolicy_whenGetConnection_createConnectionWithExpectedConfig() t

@Test
void givenCustomRetryPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "retry=com.ing.data.cassandra.jdbc.utils.FakeRetryPolicy");
initConnection(KEYSPACE, "retry=com.ing.data.cassandra.jdbc.utils.FakeRetryPolicy", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -273,7 +284,7 @@ void givenInvalidRetryPolicy_whenGetConnection_throwsException() {

@Test
void givenConstantReconnectionPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "reconnection=ConstantReconnectionPolicy((long)10)");
initConnection(KEYSPACE, "reconnection=ConstantReconnectionPolicy((long)10)", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -286,7 +297,7 @@ void givenConstantReconnectionPolicy_whenGetConnection_createConnectionWithExpec

@Test
void givenExponentialReconnectionPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "reconnection=ExponentialReconnectionPolicy((long)10,(long)100)");
initConnection(KEYSPACE, "reconnection=ExponentialReconnectionPolicy((long)10,(long)100)", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -300,7 +311,8 @@ void givenExponentialReconnectionPolicy_whenGetConnection_createConnectionWithEx

@Test
void givenCustomReconnectionPolicy_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "reconnection=com.ing.data.cassandra.jdbc.utils.FakeReconnectionPolicy()");
initConnection(KEYSPACE, "reconnection=com.ing.data.cassandra.jdbc.utils.FakeReconnectionPolicy()",
"localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -312,7 +324,7 @@ void givenCustomReconnectionPolicy_whenGetConnection_createConnectionWithExpecte

@Test
void givenInvalidReconnectionPolicy_whenGetConnection_createConnectionWithDefaultPolicyConfig() throws Exception {
initConnection(KEYSPACE, "reconnection=ExponentialReconnectionPolicy((int)100)");
initConnection(KEYSPACE, "reconnection=ExponentialReconnectionPolicy((int)100)", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand All @@ -334,7 +346,7 @@ void givenInvalidReconnectionPolicy_whenGetConnectionInDebugMode_throwsException

@Test
void givenDisabledSsl_whenGetConnection_createConnectionWithExpectedConfig() throws Exception {
initConnection(KEYSPACE, "enablessl=false");
initConnection(KEYSPACE, "enablessl=false", "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getSession());
assertNotNull(sqlConnection.getSession().getContext());
Expand Down Expand Up @@ -415,7 +427,7 @@ void givenConfigurationFileWithSslEnabled_whenGetConnection_createConnectionWith
void givenSslEngineFactory_whenConfigureSsl_addGivenSslEngineFactoryToSessionBuilder() throws Exception {
final SessionHolder sessionHolder = new SessionHolder(Collections.singletonMap(URL_KEY,
buildJdbcUrl(cassandraContainer.getContactPoint().getHostName(),
cassandraContainer.getContactPoint().getPort(), KEYSPACE)), null);
cassandraContainer.getContactPoint().getPort(), KEYSPACE, "localdatacenter=datacenter1")), null);
final CqlSessionBuilder cqlSessionBuilder = spy(new CqlSessionBuilder());
sessionHolder.configureSslEngineFactory(cqlSessionBuilder,
"com.ing.data.cassandra.jdbc.utils.FakeSslEngineFactory");
Expand Down Expand Up @@ -456,7 +468,7 @@ void givenSessionToConnect_andLiquibaseCompliance() throws SQLException {

@Test
void givenConnection_whenGetMetaData_getExpectedResultSet() throws Exception {
initConnection(KEYSPACE);
initConnection(KEYSPACE, "localdatacenter=datacenter1");
assertNotNull(sqlConnection);
assertNotNull(sqlConnection.getMetaData());

Expand Down Expand Up @@ -484,13 +496,13 @@ void givenConnection_whenGetMetaData_getExpectedResultSet() throws Exception {

@Test
void givenCassandraConnection_whenUnwrap_returnUnwrappedConnection() throws Exception {
initConnection(KEYSPACE);
initConnection(KEYSPACE, "localdatacenter=datacenter1");
assertNotNull(sqlConnection.unwrap(Connection.class));
}

@Test
void givenCassandraConnection_whenUnwrapToInvalidInterface_throwException() throws Exception {
initConnection(KEYSPACE);
initConnection(KEYSPACE, "localdatacenter=datacenter1");
assertThrows(SQLException.class, () -> sqlConnection.unwrap(this.getClass()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DataSourceUnitTest extends UsingCassandraContainerTest {
@Test
void givenParameters_whenConstructDataSource_returnCassandraDataSource() throws Exception {
final CassandraDataSource cds = new CassandraDataSource(
Collections.singletonList(ContactPoint.of("localhost", 9042)), KEYSPACE, USER, PASSWORD, CONSISTENCY);
Collections.singletonList(ContactPoint.of("localhost", 9042)), KEYSPACE, USER, PASSWORD, CONSISTENCY, "datacenter1");
assertNotNull(cds.getContactPoints());
assertEquals(1, cds.getContactPoints().size());
final ContactPoint dsContactPoint = cds.getContactPoints().get(0);
Expand All @@ -49,7 +49,7 @@ void givenParameters_whenConstructDataSource_returnCassandraDataSource() throws

final DataSource ds = new CassandraDataSource(Collections.singletonList(ContactPoint.of(
cassandraContainer.getContactPoint().getHostName(), cassandraContainer.getContactPoint().getPort())),
KEYSPACE, USER, PASSWORD, CONSISTENCY);
KEYSPACE, USER, PASSWORD, CONSISTENCY, "datacenter1");
assertNotNull(ds);

// null username and password
Expand Down

0 comments on commit 96f7e64

Please sign in to comment.