com.datastax.oss.driver.internal.core.addresstranslation.Ec2MultiRegionAddressTranslator Maven / Gradle / Ivy
/*
* 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.context.DriverContext;
import com.datastax.oss.driver.internal.core.util.Loggers;
import com.datastax.oss.driver.shaded.guava.common.annotations.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* {@link AddressTranslator} implementation for a multi-region EC2 deployment where clients are
* also deployed in EC2.
*
* Its distinctive feature is that it translates addresses according to the location of the
* Cassandra host:
*
*
* - addresses in different EC2 regions (than the client) are unchanged;
*
- addresses in the same EC2 region are translated to private IPs.
*
*
* This optimizes network costs, because Amazon charges more for communication over public IPs.
*
* Implementation note: this class performs a reverse DNS lookup of the origin address, to find
* the domain name of the target instance. Then it performs a forward DNS lookup of the domain name;
* the EC2 DNS does the private/public switch automatically based on location.
*/
public class Ec2MultiRegionAddressTranslator implements AddressTranslator {
private static final Logger LOG = LoggerFactory.getLogger(Ec2MultiRegionAddressTranslator.class);
private final DirContext ctx;
private final String logPrefix;
public Ec2MultiRegionAddressTranslator(
@SuppressWarnings("unused") @NonNull DriverContext context) {
this.logPrefix = context.getSessionName();
@SuppressWarnings("JdkObsolete")
Hashtable