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.
+ *
+ * <p>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):
+   *
+   * <pre>
+   * 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:
+   *   # "::ffff:6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # "::ffff:6442:0/111" = "cassandra.datacenter2.com:9042"
+   * }
+   * </pre>
+   *
+   * 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;
+        }
+      };
+
+  private final List<SubnetAddress> subnetAddresses;
+  @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
+  private final Optional<InetSocketAddress> defaultAddress;
+  private final String logPrefix;
+
+  public SubnetAddressTranslator(@NonNull DriverContext context) {
+    logPrefix = context.getSessionName();
+    boolean resolveAddresses =
+        context
+            .getConfig()
+            .getDefaultProfile()
+            .getBoolean(ADDRESS_TRANSLATOR_RESOLVE_ADDRESSES_OPTION, false);
+    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"'
+                  String subnetCIDR = e.getKey().replaceAll("\"", "");
+                  String address = e.getValue();
+                  return new SubnetAddress(subnetCIDR, parseAddress(address, 
resolveAddresses));
+                })
+            .collect(Collectors.toList());
+    this.defaultAddress =
+        Optional.ofNullable(
+                context
+                    .getConfig()
+                    .getDefaultProfile()
+                    .getString(ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION, 
null))
+            .map(address -> parseAddress(address, resolveAddresses));
+
+    validateSubnetsAreNotOverlapping(this.subnetAddresses);
+  }
+
+  @NonNull
+  @Override
+  public InetSocketAddress translate(@NonNull InetSocketAddress address) {
+    InetSocketAddress translatedAddress = null;
+    for (SubnetAddress subnetAddress : subnetAddresses) {
+      if (subnetAddress.contains(address)) {
+        translatedAddress = subnetAddress.address;
+      }
+    }
+    if (translatedAddress == null && defaultAddress.isPresent()) {
+      translatedAddress = defaultAddress.get();
+    }
+    if (translatedAddress == null) {
+      translatedAddress = address;
+    }
+    LOG.debug("[{}] Translated {} to {}", logPrefix, address, 
translatedAddress);
+    return translatedAddress;
+  }
+
+  @Override
+  public void close() {}
+
+  @Nullable
+  private InetSocketAddress parseAddress(String address, boolean resolve) {
+    try {
+      InetSocketAddress parsedAddress = AddressUtils.extract(address, 
resolve).iterator().next();
+      LOG.debug("[{}] Parsed {} to {}", logPrefix, address, parsedAddress);
+      return parsedAddress;
+    } catch (RuntimeException e) {
+      throw new IllegalArgumentException(
+          String.format("Invalid address %s (%s)", address, e.getMessage()));

Review Comment:
   Another exception handling nit - could you attach e as the cause here?



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

Review Comment:
   Minor nit, could you include the entire exception here, rather than just the 
message? Stacktrace could be helpful for debugging.



##########
core/src/main/java/com/datastax/oss/driver/internal/core/util/AddressUtils.java:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.util;
+
+import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableSet;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
+import java.util.HashSet;
+import java.util.Set;
+
+public class AddressUtils {
+
+  public static Set<InetSocketAddress> extract(String address, boolean 
resolve) {
+    int separator = address.lastIndexOf(':');
+    if (separator < 0) {
+      throw new IllegalArgumentException("expecting format host:port");
+    }
+
+    String host = address.substring(0, separator);
+    String portString = address.substring(separator + 1);
+    int port;
+    try {
+      port = Integer.parseInt(portString);
+    } catch (NumberFormatException e) {
+      throw new IllegalArgumentException("expecting port to be a number, got " 
+ portString, e);
+    }
+    if (!resolve) {
+      return ImmutableSet.of(InetSocketAddress.createUnresolved(host, port));
+    } else {
+      InetAddress[] inetAddresses;
+      try {
+        inetAddresses = InetAddress.getAllByName(host);
+      } catch (UnknownHostException e) {
+        throw new RuntimeException("unknown host " + host, e);

Review Comment:
   Really minor nit, but could make the UnknownHostException a cause for the 
RuntimeException here, without the custom message, the stacktrace should be 
clear



-- 
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

Reply via email to