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.
- Loading branch information
Showing
7 changed files
with
148 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
86 changes: 86 additions & 0 deletions
86
driver-core/src/test/java/com/datastax/driver/core/HostResolutionReconnectionTest.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,86 @@ | ||
package com.datastax.driver.core; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import org.burningwave.tools.net.DefaultHostResolver; | ||
import org.burningwave.tools.net.HostResolutionRequestInterceptor; | ||
import org.burningwave.tools.net.MappedHostResolver; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testng.annotations.Test; | ||
|
||
public class HostResolutionReconnectionTest { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(HostResolutionReconnectionTest.class); | ||
|
||
@Test(groups = "isolated") | ||
public void should_reconnect_to_different_cluster() { | ||
// Configure host resolution | ||
Map<String, String> hostAliasesA = new LinkedHashMap<>(); | ||
hostAliasesA.put("control.reconnect.test", "127.1.1.1"); | ||
HostResolutionRequestInterceptor.INSTANCE.install( | ||
new MappedHostResolver(hostAliasesA), DefaultHostResolver.INSTANCE); | ||
|
||
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 | ||
Map<String, String> hostAliasesB = new LinkedHashMap<>(); | ||
hostAliasesB.put("control.reconnect.test", "127.2.2.1"); | ||
HostResolutionRequestInterceptor.INSTANCE.install( | ||
new MappedHostResolver(hostAliasesB), DefaultHostResolver.INSTANCE); | ||
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(); | ||
} | ||
} | ||
} |
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