forked from apache/cassandra-java-driver
-
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 option to consider initial contact points during reconnection
When control connection tries to reconnect usually it considers only nodes provided by load balancing policy. Usually those do not include what was initially passed to the driver but the recently seen alive nodes. In some setups the IPs can keep changing so it may be useful to have an option to try initial contact points as one of the options during reconnection. Mainly if the contact point is a hostname. This change adds the option to the `QueryOptions` to control that behaviour and adds necessary logic to `ControlConnection` class. It is disabled by default, meaning that default behaviour remains unchanged. Additionally adds org.burningwave tools dependency. This dependency has features that allow for easier host resolution mocking. Adds MappedHostResolverProvider class for testing as a single entry point for controlling hostname resolution. Adds an option to CcmBridge Builder to specify cluster name. Driver checks the cluster name when reconnecting so it will refuse to reconnect to a different CcmBridge auto-generated name.
- Loading branch information
Showing
8 changed files
with
201 additions
and
0 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
105 changes: 105 additions & 0 deletions
105
driver-core/src/test/java/com/datastax/driver/core/DnsEndpointTests.java
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,105 @@ | ||
package com.datastax.driver.core; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.List; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testng.annotations.Test; | ||
|
||
public class DnsEndpointTests { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DnsEndpointTests.class); | ||
|
||
@Test(groups = "long") | ||
public void replace_cluster_test() { | ||
// Configure host resolution | ||
MappedHostResolverProvider.addResolverEntry("control.reconnect.test", "127.1.1.1"); | ||
|
||
Cluster cluster = null; | ||
Session session = null; | ||
CCMBridge bridgeA = null; | ||
try { | ||
bridgeA = | ||
CCMBridge.builder() | ||
.withNodes(1) | ||
.withIpPrefix("127.1.1.") | ||
.withBinaryPort(9042) | ||
.withClusterName("same_name") | ||
.build(); | ||
bridgeA.start(); | ||
|
||
cluster = | ||
Cluster.builder() | ||
.addContactPointsWithPorts( | ||
InetSocketAddress.createUnresolved("control.reconnect.test", 9042)) | ||
.withPort(9042) | ||
.withoutAdvancedShardAwareness() | ||
.withQueryOptions(new QueryOptions().setAddOriginalContactsToReconnectionPlan(true)) | ||
.build(); | ||
session = cluster.connect(); | ||
|
||
ResultSet rs = session.execute("select * from system.local"); | ||
Row row = rs.one(); | ||
String address = row.getInet("broadcast_address").toString(); | ||
logger.info("Queried node has broadcast_address: {}}", address); | ||
System.out.flush(); | ||
} finally { | ||
assert bridgeA != null; | ||
bridgeA.close(); | ||
} | ||
|
||
CCMBridge bridgeB = null; | ||
// Overwrite host resolution | ||
MappedHostResolverProvider.removeResolverEntry("control.reconnect.test"); | ||
MappedHostResolverProvider.addResolverEntry("control.reconnect.test", "127.2.2.1"); | ||
try { | ||
bridgeB = | ||
CCMBridge.builder() | ||
.withNodes(1) | ||
.withIpPrefix("127.2.2.") | ||
.withBinaryPort(9042) | ||
.withClusterName("same_name") | ||
.build(); | ||
bridgeB.start(); | ||
Thread.sleep(1000 * 92); | ||
ResultSet rs = session.execute("select * from system.local"); | ||
Row row = rs.one(); | ||
String address = row.getInet("broadcast_address").toString(); | ||
logger.info("Queried node has broadcast_address: {}}", address); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
assert bridgeB != null; | ||
bridgeB.close(); | ||
} | ||
} | ||
|
||
@Test(groups = "long") | ||
public void should_connect_with_mocked_hostname() { | ||
MappedHostResolverProvider.addResolverEntry("mocked.hostname.test", "127.0.1.1"); | ||
try (CCMBridge ccmBridge = | ||
CCMBridge.builder().withNodes(1).withIpPrefix("127.0.1.").withBinaryPort(9042).build(); | ||
Cluster cluster = | ||
Cluster.builder() | ||
.addContactPointsWithPorts( | ||
InetSocketAddress.createUnresolved("mocked.hostname.test", 9042)) | ||
.withPort(9042) | ||
.withoutAdvancedShardAwareness() | ||
.build()) { | ||
ccmBridge.start(); | ||
Session session = cluster.connect(); | ||
ResultSet rs = session.execute("SELECT * FROM system.local"); | ||
List<Row> rows = rs.all(); | ||
assertThat(rows).hasSize(1); | ||
Row row = rows.get(0); | ||
assertThat(row.getInet("broadcast_address").toString()).contains("127.0.1.1"); | ||
assertTrue( | ||
session.getCluster().getMetadata().getAllHosts().stream() | ||
.map(Host::toString) | ||
.anyMatch(hostString -> hostString.contains("mocked.hostname.test"))); | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
driver-core/src/test/java/com/datastax/driver/core/MappedHostResolverProvider.java
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,34 @@ | ||
package com.datastax.driver.core; | ||
|
||
import org.burningwave.tools.net.DefaultHostResolver; | ||
import org.burningwave.tools.net.HostResolutionRequestInterceptor; | ||
import org.burningwave.tools.net.MappedHostResolver; | ||
|
||
public class MappedHostResolverProvider { | ||
private static volatile MappedHostResolver resolver = null; | ||
|
||
private MappedHostResolverProvider() {} | ||
|
||
public static synchronized boolean setResolver(MappedHostResolver newResolver) { | ||
if (resolver != null) { | ||
return false; | ||
} | ||
resolver = newResolver; | ||
HostResolutionRequestInterceptor.INSTANCE.install(resolver, DefaultHostResolver.INSTANCE); | ||
return true; | ||
} | ||
|
||
public static synchronized void addResolverEntry(String hostname, String address) { | ||
if (resolver == null) { | ||
setResolver(new MappedHostResolver()); | ||
} | ||
resolver.putHost(hostname, address); | ||
} | ||
|
||
public static synchronized void removeResolverEntry(String hostname) { | ||
if (resolver == null) { | ||
return; | ||
} | ||
resolver.removeHost(hostname); | ||
} | ||
} |
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,4 @@ | ||
managed-logger.repository=autodetect | ||
managed-logger.repository.enabled=false | ||
banner.hide=true | ||
priority-of-this-configuration=1000 |
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