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.
+ *
+ * <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"
+   *   # IPv6 example:
+   *   # "::ffff:6440:0/111" = "cassandra.datacenter1.com:9042"
+   *   # "::ffff:6442:0/111" = "cassandra.datacenter2.com"
+   * }
+   * </pre>
+   *
+   * 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<SubnetAddress> subnetAddresses;
+  private final Optional<InetSocketAddress> 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"'
+                  String subnet = e.getKey().replaceAll("\"", "");
+                  String address = e.getValue();
+                  return new SubnetAddress(subnet, address);
+                })
+            .collect(Collectors.toList());
+    this.defaultAddress =
+        Optional.ofNullable(
+                context
+                    .getConfig()
+                    .getDefaultProfile()
+                    .getString(ADDRESS_TRANSLATOR_DEFAULT_ADDRESS_OPTION, 
null))
+            .map(SubnetAddressTranslator::parseAddress);
+    
SubnetAddressTranslator.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("[{}] Resolved {} to {}", logPrefix, address, translatedAddress);
+    return translatedAddress;
+  }
+
+  @Override
+  public void close() {}
+
+  private static InetSocketAddress parseAddress(String address) {
+    List<String> addressTuple = 
Splitter.onPattern(DELIMITER).splitToList(address);
+    if (addressTuple.size() == 2) {
+      return new InetSocketAddress(addressTuple.get(0), 
Integer.parseInt(addressTuple.get(1)));

Review Comment:
   👍 the way it exists now look good to me.  Will give this another review pass 
and likely +1 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

Reply via email to