kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 3e4679db605a231bafc4220104c4e6ba2e950276d32d2250ce992af16f46adb9 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-01-13 14:28 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-01-13 14:28 +01:00 |
| Parents | 13cd4939edec, a9fb235b6d73 |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | 2.0.10: |
| 5 | 5 | ------- |
| 6 | 6 | |
| 7 | +- [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518) | |
| 7 | 8 | |
| 8 | 9 | 2.0.9: |
| 9 | 10 | ------ |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +4 −0
| @@ -1316,6 +1316,10 @@ | ||
| 1316 | 1316 | if (loadBalancingPolicy instanceof CloseableLoadBalancingPolicy) |
| 1317 | 1317 | ((CloseableLoadBalancingPolicy)loadBalancingPolicy).close(); |
| 1318 | 1318 | |
| 1319 | + AddressTranslater translater = configuration.getPolicies().getAddressTranslater(); | |
| 1320 | + if (translater instanceof CloseableAddressTranslater) | |
| 1321 | + ((CloseableAddressTranslater)translater).close(); | |
| 1322 | + | |
| 1319 | 1323 | // Then we shutdown all connections |
| 1320 | 1324 | List<CloseFuture> futures = new ArrayList<CloseFuture>(sessions.size() + 1); |
| 1321 | 1325 | futures.add(controlConnection.closeAsync()); |
added driver-core/src/main/java/com/datastax/driver/core/policies/CloseableAddressTranslater.java +32 −0
| @@ -0,0 +1,32 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.Cluster; | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Extends {@link AddressTranslater} for implementations that need to free some resources | |
| 22 | + * at {@link Cluster} shutdown. | |
| 23 | + * <p> | |
| 24 | + * Note: the only reason {@link #close()} was not added directly to {@code AddressTranslater} | |
| 25 | + * is backward-compatibility. | |
| 26 | + */ | |
| 27 | +public interface CloseableAddressTranslater extends AddressTranslater { | |
| 28 | + /** | |
| 29 | + * Called at {@link Cluster} shutdown. | |
| 30 | + */ | |
| 31 | + void close(); | |
| 32 | +} |
added driver-core/src/main/java/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslater.java +144 −0
| @@ -0,0 +1,144 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2014 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.policies; | |
| 17 | + | |
| 18 | +import java.net.InetAddress; | |
| 19 | +import java.net.InetSocketAddress; | |
| 20 | +import java.util.Enumeration; | |
| 21 | +import java.util.Hashtable; | |
| 22 | + | |
| 23 | +import com.google.common.annotations.VisibleForTesting; | |
| 24 | +import javax.naming.Context; | |
| 25 | +import javax.naming.NamingEnumeration; | |
| 26 | +import javax.naming.NamingException; | |
| 27 | +import javax.naming.directory.Attribute; | |
| 28 | +import javax.naming.directory.Attributes; | |
| 29 | +import javax.naming.directory.DirContext; | |
| 30 | +import javax.naming.directory.InitialDirContext; | |
| 31 | +import org.slf4j.Logger; | |
| 32 | +import org.slf4j.LoggerFactory; | |
| 33 | + | |
| 34 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 35 | + | |
| 36 | +/** | |
| 37 | + * {@link AddressTranslater} implementation for a multi-region EC2 deployment <b>where clients are also deployed in EC2</b>. | |
| 38 | + * <p> | |
| 39 | + * Its distinctive feature is that it translates addresses according to the location of the Cassandra host: | |
| 40 | + * <ul> | |
| 41 | + * <li>addresses in different EC2 regions (than the client) are unchanged;</li> | |
| 42 | + * <li>addresses in the same EC2 region are <b>translated to private IPs</b>.</li> | |
| 43 | + * </ul> | |
| 44 | + * This optimizes network costs, because Amazon charges more for communication over public IPs. | |
| 45 | + * | |
| 46 | + * <p> | |
| 47 | + * Implementation note: this class performs a reverse DNS lookup of the origin address, to find the domain name of the target | |
| 48 | + * instance. Then it performs a forward DNS lookup of the domain name; the EC2 DNS does the private/public switch automatically | |
| 49 | + * based on location. | |
| 50 | + */ | |
| 51 | +public class EC2MultiRegionAddressTranslater implements CloseableAddressTranslater { | |
| 52 | + | |
| 53 | + private static final Logger logger = LoggerFactory.getLogger(EC2MultiRegionAddressTranslater.class); | |
| 54 | + | |
| 55 | + // TODO when we switch to Netty 4.1, we can replace this with the Netty built-in DNS client | |
| 56 | + private final DirContext ctx; | |
| 57 | + | |
| 58 | + public EC2MultiRegionAddressTranslater() { | |
| 59 | + Hashtable<Object, Object> env = new Hashtable<Object, Object>(); | |
| 60 | + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); | |
| 61 | + try { | |
| 62 | + ctx = new InitialDirContext(env); | |
| 63 | + } catch (NamingException e) { | |
| 64 | + throw new DriverException("Could not create translater", e); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + @VisibleForTesting | |
| 69 | + EC2MultiRegionAddressTranslater(DirContext ctx) { | |
| 70 | + this.ctx = ctx; | |
| 71 | + } | |
| 72 | + | |
| 73 | + @Override | |
| 74 | + public InetSocketAddress translate(InetSocketAddress socketAddress) { | |
| 75 | + InetAddress address = socketAddress.getAddress(); | |
| 76 | + try { | |
| 77 | + // InetAddress#getHostName() is supposed to perform a reverse DNS lookup, but for some reason it doesn't work | |
| 78 | + // within the same EC2 region (it returns the IP address itself). | |
| 79 | + // We use an alternate implementation: | |
| 80 | + String domainName = lookupPtrRecord(reverse(address)); | |
| 81 | + if (domainName == null) { | |
| 82 | + logger.warn("Found no domain name for {}, returning it as-is", address); | |
| 83 | + return socketAddress; | |
| 84 | + } | |
| 85 | + | |
| 86 | + InetAddress translatedAddress = InetAddress.getByName(domainName); | |
| 87 | + logger.debug("Resolved {} to {}", address, translatedAddress); | |
| 88 | + return new InetSocketAddress(translatedAddress, socketAddress.getPort()); | |
| 89 | + } catch (Exception e) { | |
| 90 | + logger.warn("Error resolving " + address + ", returning it as-is", e); | |
| 91 | + return socketAddress; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + private String lookupPtrRecord(String reversedDomain) throws Exception { | |
| 96 | + Attributes attrs = ctx.getAttributes(reversedDomain, new String[]{ "PTR" }); | |
| 97 | + for (NamingEnumeration ae = attrs.getAll(); ae.hasMoreElements(); ) { | |
| 98 | + Attribute attr = (Attribute)ae.next(); | |
| 99 | + for (Enumeration<?> vals = attr.getAll(); vals.hasMoreElements(); ) | |
| 100 | + return vals.nextElement().toString(); | |
| 101 | + } | |
| 102 | + return null; | |
| 103 | + } | |
| 104 | + | |
| 105 | + public void close() { | |
| 106 | + try { | |
| 107 | + ctx.close(); | |
| 108 | + } catch (NamingException e) { | |
| 109 | + logger.warn("Error closing translater", e); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Builds the "reversed" domain name in the ARPA domain to perform the reverse lookup | |
| 114 | + @VisibleForTesting | |
| 115 | + static String reverse(InetAddress address) { | |
| 116 | + byte[] bytes = address.getAddress(); | |
| 117 | + if (bytes.length == 4) | |
| 118 | + return reverseIpv4(bytes); | |
| 119 | + else | |
| 120 | + return reverseIpv6(bytes); | |
| 121 | + } | |
| 122 | + | |
| 123 | + private static String reverseIpv4(byte[] bytes) { | |
| 124 | + StringBuilder builder = new StringBuilder(); | |
| 125 | + for (int i = bytes.length - 1; i >= 0; i--) { | |
| 126 | + builder.append(bytes[i] & 0xFF).append('.'); | |
| 127 | + } | |
| 128 | + builder.append("in-addr.arpa"); | |
| 129 | + return builder.toString(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + private static String reverseIpv6(byte[] bytes) { | |
| 133 | + StringBuilder builder = new StringBuilder(); | |
| 134 | + for (int i = bytes.length - 1; i >= 0; i--) { | |
| 135 | + byte b = bytes[i]; | |
| 136 | + int lowNibble = b & 0x0F; | |
| 137 | + int highNibble = b >> 4 & 0x0F; | |
| 138 | + builder.append(Integer.toHexString(lowNibble)).append('.') | |
| 139 | + .append(Integer.toHexString(highNibble)).append('.'); | |
| 140 | + } | |
| 141 | + builder.append("ip6.arpa"); | |
| 142 | + return builder.toString(); | |
| 143 | + } | |
| 144 | +} |
added driver-core/src/test/java/com/datastax/driver/core/policies/EC2MultiRegionAddressTranslaterTest.java +82 −0
| @@ -0,0 +1,82 @@ | ||
| 1 | +package com.datastax.driver.core.policies; | |
| 2 | + | |
| 3 | +import org.testng.annotations.Test; | |
| 4 | + | |
| 5 | +import javax.naming.NamingException; | |
| 6 | +import javax.naming.directory.BasicAttributes; | |
| 7 | +import javax.naming.directory.InitialDirContext; | |
| 8 | +import java.net.InetAddress; | |
| 9 | +import java.net.InetSocketAddress; | |
| 10 | + | |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 12 | +import static org.mockito.Mockito.*; | |
| 13 | + | |
| 14 | +public class EC2MultiRegionAddressTranslaterTest { | |
| 15 | + | |
| 16 | + @Test(groups = "unit") | |
| 17 | + public void should_return_same_address_when_no_entry_found() throws Exception { | |
| 18 | + InitialDirContext mock = mock(InitialDirContext.class); | |
| 19 | + when(mock.getAttributes(anyString(), any(String[].class))) | |
| 20 | + .thenReturn(new BasicAttributes()); | |
| 21 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); | |
| 22 | + | |
| 23 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); | |
| 24 | + assertThat(translater.translate(address)).isEqualTo(address); | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Test(groups = "unit") | |
| 28 | + public void should_return_same_address_when_exception_encountered() throws Exception { | |
| 29 | + InitialDirContext mock = mock(InitialDirContext.class); | |
| 30 | + when(mock.getAttributes(anyString(), any(String[].class))) | |
| 31 | + .thenThrow(new NamingException("Problem resolving address (not really).")); | |
| 32 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); | |
| 33 | + | |
| 34 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); | |
| 35 | + assertThat(translater.translate(address)).isEqualTo(address); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Test(groups = "unit") | |
| 39 | + public void should_return_new_address_when_match_found() throws Exception { | |
| 40 | + InetSocketAddress expectedAddress = new InetSocketAddress("54.32.55.66", 9042); | |
| 41 | + | |
| 42 | + InitialDirContext mock = mock(InitialDirContext.class); | |
| 43 | + when(mock.getAttributes("5.2.0.192.in-addr.arpa", new String[] { "PTR" })) | |
| 44 | + .thenReturn(new BasicAttributes("PTR", expectedAddress.getHostName())); | |
| 45 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); | |
| 46 | + | |
| 47 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); | |
| 48 | + assertThat(translater.translate(address)).isEqualTo(expectedAddress); | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Test(groups = "unit") | |
| 52 | + public void should_close_context_when_closed() throws Exception { | |
| 53 | + InitialDirContext mock = mock(InitialDirContext.class); | |
| 54 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); | |
| 55 | + | |
| 56 | + // ensure close has not been called to this point. | |
| 57 | + verify(mock, times(0)).close(); | |
| 58 | + translater.close(); | |
| 59 | + // ensure close is closed. | |
| 60 | + verify(mock).close(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Test(groups = "unit") | |
| 64 | + public void should_build_reversed_domain_name_for_ip_v4() throws Exception { | |
| 65 | + InetAddress address = InetAddress.getByName("192.0.2.5"); | |
| 66 | + assertThat( | |
| 67 | + EC2MultiRegionAddressTranslater.reverse(address) | |
| 68 | + ).isEqualTo( | |
| 69 | + "5.2.0.192.in-addr.arpa" | |
| 70 | + ); | |
| 71 | + } | |
| 72 | + | |
| 73 | + @Test(groups = "unit") | |
| 74 | + public void should_build_reversed_domain_name_for_ip_v6() throws Exception { | |
| 75 | + InetAddress address = InetAddress.getByName("2001:db8::567:89ab"); | |
| 76 | + assertThat( | |
| 77 | + EC2MultiRegionAddressTranslater.reverse(address) | |
| 78 | + ).isEqualTo( | |
| 79 | + "b.a.9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa" | |
| 80 | + ); | |
| 81 | + } | |
| 82 | +} | |
| No newline at the end of the file |