Skip to content

Commit 956cbd9

Browse files
committed
Review fixes
To be squashed into previous before merge.
1 parent 33a9063 commit 956cbd9

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

core/src/main/java/com/datastax/oss/driver/api/core/config/OptionsMap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ protected static void fillWithDriverDefaults(OptionsMap map) {
278278
map.put(TypedDriverOption.CONNECTION_WARN_INIT_ERROR, true);
279279
map.put(TypedDriverOption.CONNECTION_ADVANCED_SHARD_AWARENESS_ENABLED, true);
280280
map.put(TypedDriverOption.ADVANCED_SHARD_AWARENESS_PORT_LOW, 10000);
281-
map.put(TypedDriverOption.ADVANCED_SHARD_AWARENESS_PORT_HIGH, 60000);
281+
map.put(TypedDriverOption.ADVANCED_SHARD_AWARENESS_PORT_HIGH, 65535);
282282
map.put(TypedDriverOption.RECONNECT_ON_INIT, false);
283283
map.put(TypedDriverOption.RECONNECTION_POLICY_CLASS, "ExponentialReconnectionPolicy");
284284
map.put(TypedDriverOption.RECONNECTION_BASE_DELAY, Duration.ofSeconds(1));

core/src/main/java/com/datastax/oss/driver/internal/core/channel/ChannelFactory.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public static int getNextAvailablePort(int shardCount, int shardId, DriverContex
492492
scanStart = lowPort + (shardCount - lowPort % shardCount) + shardId;
493493

494494
for (int port = scanStart; port <= highPort; port += shardCount) {
495-
if (isTcpPortAvailable(port)) {
495+
if (isTcpPortAvailable(port, context)) {
496496
foundPort = port;
497497
break;
498498
}
@@ -508,11 +508,15 @@ public static int getNextAvailablePort(int shardCount, int shardId, DriverContex
508508
return foundPort;
509509
}
510510

511-
public static boolean isTcpPortAvailable(int port) {
511+
public static boolean isTcpPortAvailable(int port, DriverContext context) {
512512
try {
513513
ServerSocket serverSocket = new ServerSocket();
514514
try {
515-
serverSocket.setReuseAddress(false);
515+
serverSocket.setReuseAddress(
516+
context
517+
.getConfig()
518+
.getDefaultProfile()
519+
.getBoolean(DefaultDriverOption.SOCKET_REUSE_ADDRESS, false));
516520
serverSocket.bind(new InetSocketAddress(port), 1);
517521
return true;
518522
} finally {

core/src/main/resources/reference.conf

+14-3
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,24 @@ datastax-java-driver {
540540
advanced-shard-awareness {
541541
# Whether to use advanced shard awareness when trying to open new connections.
542542
#
543+
544+
# Requires passing shard-aware port as contact point (usually 19042 or 19142(ssl)).
543545
# Having this enabled makes sense only for ScyllaDB clusters.
544546
# Results in smaller connection storms in multi-client settings.
545-
# If set to false the driver will not attempt to use this feature.
547+
# In short it's a feature that allows targeting particular shard when connecting to a node by using specific
548+
# local port number.
549+
# For context see https://www.scylladb.com/2021/04/27/connect-faster-to-scylla-with-a-shard-aware-port/
550+
#
551+
# If set to false the driver will not attempt to use this feature. This means connection's local port
552+
# will be random according to system rules and driver will keep opening connections until it gets right shards.
553+
# In such case non-shard aware port is recommended (by default 9042 or 9142).
546554
# If set to true the driver will attempt to use it and will log warnings each time something
547555
# makes it not possible.
556+
#
548557
# If the node for some reason does not report it's sharding info the driver
549558
# will log a warning and create connection the same way as if this feature was disabled.
550559
# If the cluster ignores the request for specific shard warning will also be logged,
551-
# although the local port will be chosen according to advanced shard awareness rules.
560+
# although the local port will already be chosen according to advanced shard awareness rules.
552561
#
553562
# Required: yes
554563
# Modifiable at runtime: yes, the new value will be used for connections created after the
@@ -561,14 +570,16 @@ datastax-java-driver {
561570
# Required: yes
562571
# Modifiable at runtime: yes, the new value will be used for calls after the
563572
# change.
573+
# Overridable in a profile: no
564574
port-low = 10000
565575

566576
# Inclusive upper bound of port range to use in advanced shard awareness.
567577
# The driver will attempt to reserve ports for connection only within the range.
568578
# Required: yes
569579
# Modifiable at runtime: yes, the new value will be used for calls after the
570580
# change.
571-
port-high = 60000
581+
# Overridable in a profile: no
582+
port-high = 65535
572583
}
573584
}
574585

integration-tests/src/test/java/com/datastax/oss/driver/core/pool/AdvancedShardAwarenessIT.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.common.collect.ImmutableSet;
2222
import com.google.common.util.concurrent.Uninterruptibles;
23+
import com.tngtech.java.junit.dataprovider.DataProvider;
24+
import com.tngtech.java.junit.dataprovider.UseDataProvider;
2325
import java.net.InetSocketAddress;
2426
import java.time.Duration;
2527
import java.util.List;
@@ -54,6 +56,11 @@ public class AdvancedShardAwarenessIT {
5456
Pattern.compile(".*Scheduling next reconnection in.*");
5557
Set<Pattern> forbiddenOccurences = ImmutableSet.of(shardMismatchPattern, reconnectionPattern);
5658

59+
@DataProvider
60+
public static Object[][] reuseAddressOption() {
61+
return new Object[][] {{true}, {false}};
62+
}
63+
5764
@Before
5865
public void startCapturingLogs() {
5966
originalLevelChannelPool = channelPoolLogger.getLevel();
@@ -80,7 +87,8 @@ public void stopCapturingLogs() {
8087
}
8188

8289
@Test
83-
public void should_initialize_all_channels() {
90+
@UseDataProvider("reuseAddressOption")
91+
public void should_initialize_all_channels(boolean reuseAddress) {
8492
Map<Pattern, Integer> expectedOccurences =
8593
ImmutableMap.of(
8694
Pattern.compile(
@@ -96,6 +104,7 @@ public void should_initialize_all_channels() {
96104
Pattern.compile(".*127\\.0\\.0\\.2:19042\\] Trying to create 5 missing channels.*"), 1);
97105
DriverConfigLoader loader =
98106
SessionUtils.configLoaderBuilder()
107+
.withBoolean(DefaultDriverOption.SOCKET_REUSE_ADDRESS, reuseAddress)
99108
.withBoolean(DefaultDriverOption.CONNECTION_ADVANCED_SHARD_AWARENESS_ENABLED, true)
100109
.withInt(DefaultDriverOption.ADVANCED_SHARD_AWARENESS_PORT_LOW, 10000)
101110
.withInt(DefaultDriverOption.ADVANCED_SHARD_AWARENESS_PORT_HIGH, 60000)

0 commit comments

Comments
 (0)