Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-11 Thread via GitHub


absurdfarce commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2961383755

   No worries @jahstreet ... I'm just happy we figured it out and have a clear 
path now to get this in!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-10 Thread via GitHub


jahstreet commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2961365667

   Sorry to make you spend much time time on it. Rebase is obviously a good 
thing to have always. On it.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-10 Thread via GitHub


absurdfarce commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2960787743

   Oh, almost forgot... the reason the build now fails with that change in 
place is because you're using the normal Guava packages 
[here](https://github.com/jahstreet/cassandra-java-driver/blob/add-subnet-address-translator/core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java#L21)
 (and potentially other places in your PR... I admit I haven't checked yet).  
This should be changed to use the shaded packages for Guava along the lines of 
the [VisibleForTesting 
import](https://github.com/jahstreet/cassandra-java-driver/blob/add-subnet-address-translator/core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java#L20)
 just above your addition.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-10 Thread via GitHub


absurdfarce commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2960783203

   Bah, Jenkins failed with complaints like the following:
   
   ```
   [2025-06-10T22:25:35.767Z] [INFO] --- maven-compiler-plugin:3.8.1:compile 
(default-compile) @ java-driver-core ---
   [2025-06-10T22:25:35.767Z] [INFO] Compiling 796 source files to 
/home/jenkins/workspace/drivers_java_oss_PR-2013/core/target/classes
   [2025-06-10T22:25:38.559Z] [INFO] 
-
   [2025-06-10T22:25:38.559Z] [ERROR] COMPILATION ERROR : 
   [2025-06-10T22:25:38.559Z] [INFO] 
-
   [2025-06-10T22:25:38.559Z] [ERROR] 
/home/jenkins/workspace/drivers_java_oss_PR-2013/core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java:[21,30]
 package com.google.common.base does not exist
   [2025-06-10T22:25:38.559Z] [INFO] 1 error
   [2025-06-10T22:25:38.559Z] [INFO] 
-
   ```
   
   Weird thing was that local builds were just fine.  This made absolutely no 
sense to me; I spent the afternoon trying various combinations of Maven 
versions and POM settings to see what might account for the difference.
   
   Naturally the answer was pretty much right in front of me the whole time.  
This PR was branched off from a point in 4.x before [this 
commit](https://github.com/jahstreet/cassandra-java-driver/commit/d7e829775c4956d10c888c86b653f7ac2d10fa4b)
 went in.  And that commit fixes precisely this behaviour.  Once I included 
this change in my local checkout of the PR branch I was able to easily 
reproduce the failure above in my local build.
   
   @jahstreet can you merge 4.x into your PR branch so that we can get this fix 
on your branch as well?  I think such a merge + the other work you've already 
done should enable us to run a full build on our Jenkins server.
   
   Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-10 Thread via GitHub


absurdfarce commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2960430702

   Confirmed that the build looks good locally with the recent changes from 
@jahstreet .  Kicking off a DataStax Jenkins run now to confirm that we haven't 
had any unexpected regressions.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-10 Thread via GitHub


jahstreet commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2960385965

   Suggested commit message:
   
   "Add SubnetAddressTranslator to translate Cassandra node IPs from private 
network based on its subnet mask"


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-07 Thread via GitHub


jahstreet commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2952368535

   Deal, thx for the feedback.
   Will address it asap.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127542294


##
core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java:
##
@@ -994,7 +994,48 @@ public enum DefaultDriverOption implements DriverOption {
*
* Value-type: boolean
*/
-  
SSL_ALLOW_DNS_REVERSE_LOOKUP_SAN("advanced.ssl-engine-factory.allow-dns-reverse-lookup-san");
+  
SSL_ALLOW_DNS_REVERSE_LOOKUP_SAN("advanced.ssl-engine-factory.allow-dns-reverse-lookup-san"),
+  /**
+   * An address to always translate all node addresses to that same proxy 
hostname no matter what IP
+   * address a node has, but still using its native transport port.
+   *
+   * Value-Type: {@link String}
+   */
+  
ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME("advanced.address-translator.advertised-hostname"),
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+   * }
+   * 
+   *
+   * Note: subnets must be represented as prefix blocks, see {@link
+   * inet.ipaddr.Address#isPrefixBlock()}.
+   *
+   * Value type: {@link java.util.Map Map}<{@link String},{@link 
String}>
+   */
+  
ADDRESS_TRANSLATOR_SUBNET_ADDRESSES("advanced.address-translator.subnet-addresses"),
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets.
+   *
+   * Value-Type: {@link String}
+   */
+  
ADDRESS_TRANSLATOR_DEFAULT_ADDRESS("advanced.address-translator.default-address"),
+  /**
+   * Whether to resolve the addresses on initialization (if true) or on each 
node (re-)connection
+   * (if false). Defaults to false.
+   *
+   * Value-Type: boolean
+   */
+  
ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES("advanced.address-translator.resolve-addresses");

Review Comment:
   TypedDriverOptionTest [will 
fail](https://github.com/apache/cassandra-java-driver/blob/4.19.0/core/src/test/java/com/datastax/oss/driver/api/core/config/TypedDriverOptionTest.java#L35)
 if this enum contains values which aren't also in TypedDriverOption.  The 
following diff should get you past this issue:
   
   ```diff
   diff --git 
a/core/src/main/java/com/datastax/oss/driver/api/core/config/TypedDriverOption.java
 
b/core/src/main/java/com/datastax/oss/driver/api/core/config/TypedDriverOption.java
   index 93e2b4684..aa4e4af12 100644
   --- 
a/core/src/main/java/com/datastax/oss/driver/api/core/config/TypedDriverOption.java
   +++ 
b/core/src/main/java/com/datastax/oss/driver/api/core/config/TypedDriverOption.java
   @@ -896,6 +896,20 @@ public class TypedDriverOption {
  
DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS,
  GenericType.BOOLEAN);

   +  public static final TypedDriverOption 
ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME =
   +  new TypedDriverOption<>(
   +  DefaultDriverOption.ADDRESS_TRANSLATOR_ADVERTISED_HOSTNAME, 
GenericType.STRING);
   +  public static final TypedDriverOption> 
ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
   +  new TypedDriverOption<>(
   +  DefaultDriverOption.ADDRESS_TRANSLATOR_SUBNET_ADDRESSES,
   +  GenericType.mapOf(GenericType.STRING, GenericType.STRING));
   +  public static final TypedDriverOption 
ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
   +  new TypedDriverOption<>(
   +  DefaultDriverOption.ADDRESS_TRANSLATOR_DEFAULT_ADDRESS, 
GenericType.STRING);
   +  public static final TypedDriverOption 
ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES =
   +  new TypedDriverOption<>(
   +  DefaultDriverOption.ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES, 
GenericType.BOOLEAN);
   +
  /**
   * Ordered preference list of remote dcs optionally supplied for 
automatic failover and included
   * in query plan. This feature is enabled only when 
max-nodes-per-remote-dc is greater than 0.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127402217


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java:
##
@@ -0,0 +1,157 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import 
com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+
+class Subnet {
+  private final byte[] subnet;
+  private final byte[] networkMask;
+  private final byte[] upper;
+  private final byte[] lower;
+
+  private Subnet(byte[] subnet, byte[] networkMask) {
+this.subnet = subnet;
+this.networkMask = networkMask;
+
+byte[] upper = new byte[subnet.length];
+byte[] lower = new byte[subnet.length];
+for (int i = 0; i < subnet.length; i++) {
+  upper[i] = (byte) (subnet[i] | ~networkMask[i]);
+  lower[i] = (byte) (subnet[i] & networkMask[i]);
+}
+this.upper = upper;
+this.lower = lower;
+  }
+
+  static Subnet parse(String subnetCIDR) throws UnknownHostException {
+String[] parts = subnetCIDR.split("/");

Review Comment:
   ErrorProne complains about this line when building due to [known issues with 
String.split()](https://errorprone.info/bugpattern/StringSplitter).  
Recommendation is to replace this with:
   
   ```java
   List parts = Splitter.on('/').splitToList(subnetCIDR);
   ```
   
   I used something slightly different to preserve the `String[]` type 
throughout the rest of the code and it seemed to work fine:
   
   ```java
   String[] parts = Iterables.toArray(Splitter.on('/').split(subnetCIDR), 
String.class);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127402217


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java:
##
@@ -0,0 +1,157 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import 
com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+
+class Subnet {
+  private final byte[] subnet;
+  private final byte[] networkMask;
+  private final byte[] upper;
+  private final byte[] lower;
+
+  private Subnet(byte[] subnet, byte[] networkMask) {
+this.subnet = subnet;
+this.networkMask = networkMask;
+
+byte[] upper = new byte[subnet.length];
+byte[] lower = new byte[subnet.length];
+for (int i = 0; i < subnet.length; i++) {
+  upper[i] = (byte) (subnet[i] | ~networkMask[i]);
+  lower[i] = (byte) (subnet[i] & networkMask[i]);
+}
+this.upper = upper;
+this.lower = lower;
+  }
+
+  static Subnet parse(String subnetCIDR) throws UnknownHostException {
+String[] parts = subnetCIDR.split("/");

Review Comment:
   ErrorProne complains about this line when building due to [known issues with 
String.split()](https://errorprone.info/bugpattern/StringSplitter).  
Recommendation is to replace this with:
   
   ```java
   List parts = Splitter.on('/').splitToList(subnetCIDR);
   ```
   
   I used something slightly different to preserve the `String[]` type 
throughout the rest of the code and it seemed to work fine:
   
   ```java
   String[] parts = Iterables.toArray(Splitter.on('/').split(subnetCIDR), 
String.class);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127402217


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java:
##
@@ -0,0 +1,157 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import 
com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Arrays;
+
+class Subnet {
+  private final byte[] subnet;
+  private final byte[] networkMask;
+  private final byte[] upper;
+  private final byte[] lower;
+
+  private Subnet(byte[] subnet, byte[] networkMask) {
+this.subnet = subnet;
+this.networkMask = networkMask;
+
+byte[] upper = new byte[subnet.length];
+byte[] lower = new byte[subnet.length];
+for (int i = 0; i < subnet.length; i++) {
+  upper[i] = (byte) (subnet[i] | ~networkMask[i]);
+  lower[i] = (byte) (subnet[i] & networkMask[i]);
+}
+this.upper = upper;
+this.lower = lower;
+  }
+
+  static Subnet parse(String subnetCIDR) throws UnknownHostException {
+String[] parts = subnetCIDR.split("/");

Review Comment:
   Errorprone complains about this line when building due to [known issues with 
String.split()](https://errorprone.info/bugpattern/StringSplitter).  
Recommendation is to replace this with:
   
   ```java
   List parts = Splitter.on('/').splitToList(subnetCIDR);
   ```
   
   I used something slightly different to preserve the `String[]` type 
throughout the rest of the code and it seemed to work fine:
   
   ```java
   String[] parts = Iterables.toArray(Splitter.on('/').split(subnetCIDR), 
String.class);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127385733


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/Subnet.java:
##
@@ -0,0 +1,157 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;

Review Comment:
   Need ASF license text at the top of this source file



##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddress.java:
##
@@ -0,0 +1,48 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;

Review Comment:
   Need ASF license text at the top of this source file here as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127386442


##
core/src/test/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTest.java:
##
@@ -0,0 +1,27 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;

Review Comment:
   Need ASF license text at the top of this source file here as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-06-04 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2127386825


##
core/src/test/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetTest.java:
##
@@ -0,0 +1,101 @@
+package com.datastax.oss.driver.internal.core.addresstranslation;

Review Comment:
   Last one: need ASF license text at the top of this source file



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-04-01 Thread via GitHub


absurdfarce commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2770830312

   Apologies, this is on my list but I haven't made it back to reconsider the 
updated comments in this review.  I appreciate your patience @jahstreet!
   
   FWIW I have added this to the 4.19.1 release planning doc under the working 
assumption that we'll almost certainly get this in in some form we can all 
agree on.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-23 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2009091709


##
core/pom.xml:
##
@@ -116,6 +116,11 @@
   org.reactivestreams
   reactive-streams
 
+
+  com.github.seancfoley
+  ipaddress
+  true
+

Review Comment:
   A bit of vibe-coding and we can have it with around 100 lines of code. Will 
work on integrating a change.



##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.datastax.oss.driver.internal.core.util.AddressUtils;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import edu.umd.cs.findbugs.annotations.Nullable;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+   * }
+   * 
+   *
+   * Note: subnets must be represented as prefix blocks, see {@link
+   * inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  /**
+   * Whether to resolve the addresses on initialization (if true) or on each 
node (re-)connection
+   * (if false). Defaults to false.
+   */
+  public static final String ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES =
+  "advanced.address-translator.resolve-addresses";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES;
+}
+  };

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To re

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-23 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2009087227


##
core/pom.xml:
##
@@ -116,6 +116,11 @@
   org.reactivestreams
   reactive-streams
 
+
+  com.github.seancfoley
+  ipaddress
+  true
+

Review Comment:
   We need at least the following functionality to work with subnets here:
   - Validate subnet string is in a prefix block format
   - Check if subnet contains IP address
   - All for IPv4 and IPv6
   
   The library is quite big, so copying over its parts is an overkill.
   Then the alternative is to implement these functions ourselves.
   Looking into it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-18 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2000568199


##
core/src/main/java/com/datastax/oss/driver/internal/core/ContactPoints.java:
##
@@ -41,7 +38,22 @@ public static Set merge(
 
 Set result = Sets.newHashSet(programmaticContactPoints);
 for (String spec : configContactPoints) {
-  for (InetSocketAddress address : extract(spec, resolve)) {
+
+  Set addresses = Collections.emptySet();
+  try {
+addresses = AddressUtils.extract(spec, resolve);
+  } catch (RuntimeException e) {
+LOG.warn("Ignoring invalid contact point {} ({})", spec, 
e.getMessage(), e);
+  }

Review Comment:
   Previously, `#extract` code was used only in this class and we logged errors 
together with reasons of these errors. Now the info about reasons is moved to 
until method, which is called from multiple places. In this class, I aimed to 
keep logging (as well as other functionality) as close to the origin as seemed 
possible to avoid opinionated refactoring, so I needed a way to get reasons of 
errors from the utility `#extract` to log them together with the context logs.
   Happy to agree on the way it should look like and change accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-18 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2000552728


##
core/pom.xml:
##
@@ -116,6 +116,11 @@
   org.reactivestreams
   reactive-streams
 
+
+  com.github.seancfoley
+  ipaddress
+  true
+

Review Comment:
   First thing that comes to mind is implementing it ourselves (or in other 
words copying it over from the library). Lemme evaluate how much of the util 
code is needed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-18 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2000568199


##
core/src/main/java/com/datastax/oss/driver/internal/core/ContactPoints.java:
##
@@ -41,7 +38,22 @@ public static Set merge(
 
 Set result = Sets.newHashSet(programmaticContactPoints);
 for (String spec : configContactPoints) {
-  for (InetSocketAddress address : extract(spec, resolve)) {
+
+  Set addresses = Collections.emptySet();
+  try {
+addresses = AddressUtils.extract(spec, resolve);
+  } catch (RuntimeException e) {
+LOG.warn("Ignoring invalid contact point {} ({})", spec, 
e.getMessage(), e);
+  }

Review Comment:
   Previously, `#extract` code was used only in this class and we logged errors 
together with reasons of these errors. Now the info about reasons is moved to 
util method, which is called from multiple places. In this class, I aimed to 
keep logging (as well as other functionality) as close to the origin as seemed 
possible to avoid opinionated refactoring, so I needed a way to get reasons of 
errors from the utility `#extract` to log them together with the context logs.
   Happy to agree on the way it should look like and change accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-18 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2000572246


##
core/src/main/java/com/datastax/oss/driver/internal/core/ContactPoints.java:
##
@@ -41,7 +38,22 @@ public static Set merge(
 
 Set result = Sets.newHashSet(programmaticContactPoints);
 for (String spec : configContactPoints) {
-  for (InetSocketAddress address : extract(spec, resolve)) {
+
+  Set addresses = Collections.emptySet();
+  try {
+addresses = AddressUtils.extract(spec, resolve);
+  } catch (RuntimeException e) {
+LOG.warn("Ignoring invalid contact point {} ({})", spec, 
e.getMessage(), e);
+  }
+
+  if (addresses.size() > 1) {
+LOG.info(
+"Contact point {} resolves to multiple addresses, will use them 
all ({})",
+spec,
+addresses);
+  }

Review Comment:
   Same as above, it was there so I kept it as is.
   As for me, this log is a good additional info when debugging failed to 
connect issues. Like, one could be surprised to see the client failed to 
connect logs where contact points do not match the configured ones.
   What is your opinion on the need of it?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-18 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r2000549859


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.datastax.oss.driver.internal.core.util.AddressUtils;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import edu.umd.cs.findbugs.annotations.Nullable;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+   * }
+   * 
+   *
+   * Note: subnets must be represented as prefix blocks, see {@link
+   * inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  /**
+   * Whether to resolve the addresses on initialization (if true) or on each 
node (re-)connection
+   * (if false). Defaults to false.
+   */
+  public static final String ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES =
+  "advanced.address-translator.resolve-addresses";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES;
+}
+  };

Review Comment:
   Indeed, that was derived from the example.
   I can move them, sure.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-03-17 Thread via GitHub


absurdfarce commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1999682162


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.datastax.oss.driver.internal.core.util.AddressUtils;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import edu.umd.cs.findbugs.annotations.Nullable;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+   * }
+   * 
+   *
+   * Note: subnets must be represented as prefix blocks, see {@link
+   * inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  /**
+   * Whether to resolve the addresses on initialization (if true) or on each 
node (re-)connection
+   * (if false). Defaults to false.
+   */
+  public static final String ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES =
+  "advanced.address-translator.resolve-addresses";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES;
+}
+  };

Review Comment:
   These DriverOptions need to be moved to DefaultDriverOption.  We try to keep 
all the options for the basic driver there even if they're only enabled in 
certain cases.  Note that the options for Astra connectivity [are also included 
there](https://github.com/apache/cassandra-java-driver/blob/4.19.0/core/src/main/java/com/datastax/oss/driver/api/core/config/DefaultDriverOption.java#L

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-17 Thread via GitHub


aratno commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1958529763


##
manual/core/address_resolution/README.md:
##
@@ -118,6 +118,59 @@ datastax-java-driver.advanced.address-translator.class = 
com.mycompany.MyAddress
 Note: the contact points provided while creating the `CqlSession` are not 
translated, only addresses
 retrieved from or sent by Cassandra nodes are.
 
+### Fixed proxy hostname
+
+If your client applications access Cassandra through some kind of proxy (eg. 
with AWS PrivateLink when all Cassandra
+nodes are exposed via one hostname pointing to AWS Endpoint), you can 
configure driver with
+`FixedHostNameAddressTranslator` to always translate all node addresses to 
that same proxy hostname, no matter what IP
+address a node has but still using its native transport port.
+
+To use it, specify the following in the [configuration](../configuration):
+
+```
+datastax-java-driver.advanced.address-translator.class = 
FixedHostNameAddressTranslator
+advertised-hostname = proxyhostname
+```
+
+### Fixed proxy hostname per subnet
+
+When running Cassandra in a private network and accessing it from outside of 
that private network via some kind of
+proxy, we have an option to use `FixedHostNameAddressTranslator`. But for 
multi-datacenter Cassandra deployments, we
+want to have more control over routing queries to a specific datacenter (eg. 
for optimizing latencies), which requires
+setting up a separate proxy per datacenter.
+
+Normally, each Cassandra datacenter nodes are deployed to a different subnet 
to support internode communications in the
+cluster and avoid IP address collisions. So when Cassandra broadcasts its 
nodes IP addresses, we can determine which
+datacenter that node belongs to by checking its IP address against the given 
datacenter subnet.
+
+For such scenarios you can use `SubnetAddressTranslator` to translate node IPs 
to the datacenter proxy address
+associated with it. 
+
+To use it, specify the following in the [configuration](../configuration):
+```
+datastax-java-driver.advanced.address-translator {
+  class = SubnetAddressTranslator
+  subnet-addresses {
+"100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+"100.66.0.0/15" = "cassandra.datacenter2.com:9042"

Review Comment:
   Thanks for including port configuration here - 
FixedHostNameAddressTranslator doesn't support ports, so even for 
single-hostname translation this can be a better option



##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,236 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.datastax.oss.driver.internal.core.util.AddressUtils;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import edu.umd.cs.findbugs.annotations.Nullable;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-15 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1957162913


##
core/src/main/resources/reference.conf:
##
@@ -1020,17 +1020,33 @@ datastax-java-driver {
 # the package com.datastax.oss.driver.internal.core.addresstranslation.
 #
 # The driver provides the following implementations out of the box:
-# - PassThroughAddressTranslator: returns all addresses unchanged
+# - PassThroughAddressTranslator: returns all addresses unchanged.
 # - FixedHostNameAddressTranslator: translates all addresses to a specific 
hostname.
+# - SubnetAddressTranslator: translates addresses to hostname based on the 
subnet match.
 # - Ec2MultiRegionAddressTranslator: suitable for an Amazon multi-region 
EC2 deployment where
 #   clients are also deployed in EC2. It optimizes network costs by 
favoring private IPs over
 #   public ones whenever possible.
 #
 # You can also specify a custom class that implements AddressTranslator 
and has a public
 # constructor with a DriverContext argument.
 class = PassThroughAddressTranslator
+#
 # This property has to be set only in case you use 
FixedHostNameAddressTranslator.
 # advertised-hostname = mycustomhostname
+#
+# These properties are only applicable in case you use 
SubnetAddressTranslator.
+# subnet-addresses {
+#   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+#   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+#   # IPv6 example:
+#   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+#   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+# }
+# Optional. When configured, addresses not matching the configured subnets 
are translated to this address.
+# default-address = "cassandra.datacenter1.com:9042"
+# Whether to resolve the addresses once on initialization (if true) or on 
each node (re-)connection (if false).
+# If not configured, defaults to false.
+# resolve-addresses = false

Review Comment:
   100%, I expect proxy to have at least 2 replicas eligible for "periodic" 
restarts, that was the intuition to decide here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-15 Thread via GitHub


jahstreet commented on PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#issuecomment-2661018616

   > Excellent work @jahstreet, thank you!  Have some suggestions, but I'm +1 
either way.
   
   Thanks! I will push a commit with annotations till Monday morning.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1956786827


##
core/src/main/resources/reference.conf:
##
@@ -1020,17 +1020,33 @@ datastax-java-driver {
 # the package com.datastax.oss.driver.internal.core.addresstranslation.
 #
 # The driver provides the following implementations out of the box:
-# - PassThroughAddressTranslator: returns all addresses unchanged
+# - PassThroughAddressTranslator: returns all addresses unchanged.
 # - FixedHostNameAddressTranslator: translates all addresses to a specific 
hostname.
+# - SubnetAddressTranslator: translates addresses to hostname based on the 
subnet match.
 # - Ec2MultiRegionAddressTranslator: suitable for an Amazon multi-region 
EC2 deployment where
 #   clients are also deployed in EC2. It optimizes network costs by 
favoring private IPs over
 #   public ones whenever possible.
 #
 # You can also specify a custom class that implements AddressTranslator 
and has a public
 # constructor with a DriverContext argument.
 class = PassThroughAddressTranslator
+#
 # This property has to be set only in case you use 
FixedHostNameAddressTranslator.
 # advertised-hostname = mycustomhostname
+#
+# These properties are only applicable in case you use 
SubnetAddressTranslator.
+# subnet-addresses {
+#   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+#   "100.66.0.0/15" = "cassandra.datacenter2.com:9042"
+#   # IPv6 example:
+#   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+#   # ":::6442:0/111" = "cassandra.datacenter2.com:9042"
+# }
+# Optional. When configured, addresses not matching the configured subnets 
are translated to this address.
+# default-address = "cassandra.datacenter1.com:9042"
+# Whether to resolve the addresses once on initialization (if true) or on 
each node (re-)connection (if false).
+# If not configured, defaults to false.
+# resolve-addresses = false

Review Comment:
   :+1: while `resolve-contact-points` defaults to `true`, I was thinking about 
whether we should do the same here, but I think you probably will generally use 
a DNS name that might change its backing IPs from time to time, so makes sense 
for this to be `false`.  Was that what you were thinking as well?



##
core/src/test/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslatorTest.java:
##
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static 
org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
+import com.datastax.oss.driver.internal.core.context.DefaultDriverContext;
+import 
com.datastax.oss.driver.internal.core.context.MockedDriverContextFactory;
+import com.google.common.collect.ImmutableMap;
+import java.net.InetSocketAddress;
+import java.util.Map;
+import java.util.Optional;
+import org.junit.Test;
+
+public class SubnetAddressTranslatorTest {

Review Comment:
   Mind adding:
   
   ```suggestion
   @SuppressWarnings("resource")
   public class SubnetAddressTranslatorTest {
   ```
   
   to avoid the warnings around using `new SubnetAddressTranslator` without a 
try block?  Since `AddressTranslator` is `AutoCloseable` it causes IntelliJ to 
throw some warnings, but I think its fine to use it in this way for the test.



##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,235 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in co

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1956557403


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1956486693


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1956490115


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1956486693


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-14 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1955722161


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1955680480


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1955038288


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1954257461


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1954075719


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1954021486


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1954008765


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-13 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1954008765


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6
+   * addresses.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+
.getStringMap(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).entrySet().stream()
+.map(
+e -> {
+  // Quoted and/or containing forward slashes map keys in 
reference.conf are read to
+  // strings with additional quotes, eg. 100.64.0.0/15 -> 
'100.64.0."0/15"' or
+  // "100.64.0.0/15" -> '"100.64.0.0/15"'
+  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-12 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1953990634


##
manual/core/address_resolution/README.md:
##
@@ -118,6 +118,52 @@ datastax-java-driver.advanced.address-translator.class = 
com.mycompany.MyAddress
 Note: the contact points provided while creating the `CqlSession` are not 
translated, only addresses
 retrieved from or sent by Cassandra nodes are.
 
+### Fixed proxy hostname
+
+If your client applications access Cassandra through some kind of proxy (eg. 
with AWS PrivateLink when all Cassandra
+nodes are exposed via one hostname pointing to AWS Endpoint), you can 
configure driver with
+`FixedHostNameAddressTranslator` to always translate all node addresses to 
that same proxy hostname, no matter what IP
+address a node has but still using its native transport port.
+
+To use it, specify the following in the [configuration](../configuration):
+
+```
+datastax-java-driver.advanced.address-translator.class = 
FixedHostNameAddressTranslator
+advertised-hostname = proxyhostname
+```
+
+### Fixed proxy hostname per subnet
+
+When running Cassandra in a private network and accessing it from outside of 
that private network via some kind of
+proxy, we have an option to use `FixedHostNameAddressTranslator`. But for 
multi-datacenter Cassandra deployments, we
+want to have more control over routing queries to a specific datacenter (eg. 
for optimizing latencies), which requires
+setting up a separate proxy per datacenter.
+
+Normally, each Cassandra datacenter nodes are deployed to a separate subnet to 
support internode communications in the
+cluster and avoid IP addresses collisions. So when Cassandra broadcasts its 
nodes IP addresses, we can determine which
+datacenter that node belongs to by checking its IP address against the given 
datacenter subnet.
+
+For such scenarios you can use `SubnetAddressTranslator` to translate node IPs 
to the datacenter proxy address
+associated with it. 
+
+To use it, specify the following in the [configuration](../configuration):
+```
+datastax-java-driver.advanced.address-translator {
+  class = SubnetAddressTranslator
+  subnet-addresses {
+"100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+"100.66.0.0/15" = "cassandra.datacenter2.com"  # port defaults to 9042 if 
not specified
+# IPv6 example:
+# ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+# ":::6442:0/111" = "cassandra.datacenter2.com"  # port defaults to 
9042 if not specified
+  }
+  # Optional. When configured, addresses not matching the configured subnets 
are translated to it. Port defaults to 9042 if not specified.
+  default-address = "cassandra.datacenter1.com:9042"
+}
+```
+
+Such setup is common for running Cassandra on Kubernetes with 
[k8ssandra](https://docs.k8ssandra.io/).

Review Comment:
   Right!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-12 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1953986294


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # ":::6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * 
+   *
+   * If configured without port, the default 9042 will be used. Also supports 
IPv6 subnets. Note:
+   * subnets must be represented as prefix blocks, see {@link 
inet.ipaddr.Address#isPrefixBlock()}.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used. Also 
supports IPv6

Review Comment:
   Okay, agree.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-12 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1953319078


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A map of Cassandra node subnets (CIDR notations) to target addresses, for 
example (note quoted
+   * keys):
+   *
+   * 
+   * advanced.address-translator.subnet-addresses {
+   *   "100.64.0.0/15" = "cassandra.datacenter1.com:9042"
+   *   "100.66.0.0/15" = "cassandra.datacenter2.com"
+   *   # IPv6 example:
+   *   # ":::6440:0/111" = "cassandra.datacenter1.com:9042"

Review Comment:
   :heart: appreciate the IPv6 support!



##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import com.google.common.base.Splitter;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import inet.ipaddr.IPAddress;
+import inet.ipaddr.IPAddressString;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. whe

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-12 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1952404526


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.apache.commons.net.util.SubnetUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A comma separated list with Cassandra node subnets (CIDR notations) to 
target addresses in a
+   * format `::`, for example:
+   * 
`100.64.0.0/15:cassandra.datacenter1.com:9042,100.66.0.0/15:cassandra.datacenter1.com:9042`.
 If
+   * configured without port, the default 9042 will be used.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+.getStringList(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).stream()

Review Comment:
   Okay, it is possible, just quoting works not intuitively, but for users it 
is invisible.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-11 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1950473215


##
core/pom.xml:
##
@@ -116,6 +116,10 @@
   org.reactivestreams
   reactive-streams
 
+
+  commons-net
+  commons-net
+

Review Comment:
   Changed to IPAddress and made it optional.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org



Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-10 Thread via GitHub


tolbertam commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1949652849


##
core/src/main/java/com/datastax/oss/driver/internal/core/addresstranslation/SubnetAddressTranslator.java:
##
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.datastax.oss.driver.internal.core.addresstranslation;
+
+import com.datastax.oss.driver.api.core.addresstranslation.AddressTranslator;
+import com.datastax.oss.driver.api.core.config.DriverOption;
+import com.datastax.oss.driver.api.core.context.DriverContext;
+import edu.umd.cs.findbugs.annotations.NonNull;
+import java.net.InetSocketAddress;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.apache.commons.net.util.SubnetUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This translator returns the proxy address of the private subnet containing 
the Cassandra node IP,
+ * or default address if no matching subnets, or passes through the original 
node address if no
+ * default configured.
+ *
+ * The translator can be used for scenarios when all nodes are behind some 
kind of proxy, and
+ * that proxy is different for nodes located in different subnets (eg. when 
Cassandra is deployed in
+ * multiple datacenters/regions). One can use this, for example, for Cassandra 
on Kubernetes with
+ * different Cassandra datacenters deployed to different Kubernetes clusters.
+ */
+public class SubnetAddressTranslator implements AddressTranslator {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(SubnetAddressTranslator.class);
+
+  /**
+   * A comma separated list with Cassandra node subnets (CIDR notations) to 
target addresses in a
+   * format `::`, for example:
+   * 
`100.64.0.0/15:cassandra.datacenter1.com:9042,100.66.0.0/15:cassandra.datacenter1.com:9042`.
 If
+   * configured without port, the default 9042 will be used.
+   */
+  public static final String ADDRESS_TRANSLATOR_SUBNET_ADDRESSES =
+  "advanced.address-translator.subnet-addresses";
+  /**
+   * A default address to fallback to if Cassandra node IP isn't contained in 
any of the configured
+   * subnets. If configured without port, the default 9042 will be used.
+   */
+  public static final String ADDRESS_TRANSLATOR_DEFAULT_ADDRESS =
+  "advanced.address-translator.default-address";
+
+  public static DriverOption ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_SUBNET_ADDRESSES;
+}
+  };
+  public static DriverOption ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION =
+  new DriverOption() {
+@NonNull
+@Override
+public String getPath() {
+  return ADDRESS_TRANSLATOR_DEFAULT_ADDRESS;
+}
+  };
+
+  private static final String DELIMITER = ":";
+  private static final int DEFAULT_PORT = 9042;
+
+  private final List subnetAddresses;
+  private final Optional defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+logPrefix = context.getSessionName();
+this.subnetAddresses =
+context.getConfig().getDefaultProfile()
+.getStringList(ADDRESS_TRANSLATOR_SUBNET_ADDRESSES_OPTION).stream()

Review Comment:
   From what I can tell this would work this way:
   
   ```hocon
   advanced.address-translator
   { 
 subnet-addresses = 
["100.64.0.0/15:cassandra.datacenter1.com:9042","100.66.0.0/15:cassandra.datacenter1.com:9042"]
   }
   ```
   
   One concern I have is placing multiple property values in the same string, 
which implies some kind of parsing.  I suppose that was already the case with 
address:port, but would be nice to make this structured.Using `:` as a 
delimiter would also cause some problems with IPv6.
   
   could we consider using a string map instead, e.g.:
   
   ```hocon
   advanced.address-translator
   { 
 subnet-addresses {
   100.64.0.0/15 = "cassandra.datacenter1.com:9042"
   100.66.0.0/15 = "cassandra.datacenter1.com:9042"
  

Re: [PR] Add SubnetAddressTranslator [cassandra-java-driver]

2025-02-10 Thread via GitHub


jahstreet commented on code in PR #2013:
URL: 
https://github.com/apache/cassandra-java-driver/pull/2013#discussion_r1948873334


##
core/pom.xml:
##
@@ -116,6 +116,10 @@
   org.reactivestreams
   reactive-streams
 
+
+  commons-net
+  commons-net
+

Review Comment:
   What is the strategy around adding dependencies? Is that fine to have it 
here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


-
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org