Skip to content

Commit

Permalink
JAVA-2203: Handle unresolved addresses in DefaultEndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Apr 5, 2019
1 parent d4df8f1 commit 66415c8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog/README.md
Expand Up @@ -4,6 +4,7 @@

### 4.0.1 (in progress)

- [bug] JAVA-2203: Handle unresolved addresses in DefaultEndPoint
- [bug] JAVA-2210: Add ability to set TTL for modification queries
- [improvement] JAVA-2212: Add truncate to QueryBuilder
- [improvement] JAVA-2211: Upgrade Jersey examples to fix security issue sid-3606
Expand Down
Expand Up @@ -16,20 +16,16 @@
package com.datastax.oss.driver.internal.core.metadata;

import com.datastax.oss.driver.api.core.metadata.EndPoint;
import com.datastax.oss.driver.shaded.guava.common.base.Preconditions;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Objects;

public class DefaultEndPoint implements EndPoint {

private final InetSocketAddress address;
private final String metricPrefix;

public DefaultEndPoint(InetSocketAddress address) {
Preconditions.checkNotNull(address);
this.address = address;
this.address = Objects.requireNonNull(address, "address can't be null");
this.metricPrefix = buildMetricPrefix(address);
}

Expand Down Expand Up @@ -65,20 +61,13 @@ public String asMetricPrefix() {
return metricPrefix;
}

private static String buildMetricPrefix(InetSocketAddress addressAndPort) {
StringBuilder prefix = new StringBuilder();
InetAddress address = addressAndPort.getAddress();
int port = addressAndPort.getPort();
if (address instanceof Inet4Address) {
// Metrics use '.' as a delimiter, replace so that the IP is a single path component
// (127.0.0.1 => 127_0_0_1)
prefix.append(address.getHostAddress().replace('.', '_'));
} else {
assert address instanceof Inet6Address;
// IPv6 only uses '%' and ':' as separators, so no replacement needed
prefix.append(address.getHostAddress());
private static String buildMetricPrefix(InetSocketAddress address) {
String hostString = address.getHostString();
if (hostString == null) {
throw new IllegalArgumentException(
"Could not extract a host string from provided address " + address);
}
// Append the port since Cassandra 4 supports nodes with different ports
return prefix.append(':').append(port).toString();
return hostString.replace('.', '_') + ':' + address.getPort();
}
}
@@ -0,0 +1,62 @@
/*
* Copyright DataStax, Inc.
*
* Licensed 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.metadata;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.InetSocketAddress;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class DefaultEndPointTest {

@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void should_create_from_host_name() {
DefaultEndPoint endPoint = new DefaultEndPoint(new InetSocketAddress("localhost", 9042));
assertThat(endPoint.asMetricPrefix()).isEqualTo("localhost:9042");
}

@Test
public void should_create_from_literal_ipv4_address() {
DefaultEndPoint endPoint = new DefaultEndPoint(new InetSocketAddress("127.0.0.1", 9042));
assertThat(endPoint.asMetricPrefix()).isEqualTo("127_0_0_1:9042");
}

@Test
public void should_create_from_literal_ipv6_address() {
DefaultEndPoint endPoint = new DefaultEndPoint(new InetSocketAddress("::1", 9042));
assertThat(endPoint.asMetricPrefix()).isEqualTo("0:0:0:0:0:0:0:1:9042");
}

@Test
public void should_create_from_unresolved_address() {
InetSocketAddress address = InetSocketAddress.createUnresolved("test.com", 9042);
DefaultEndPoint endPoint = new DefaultEndPoint(address);
assertThat(endPoint.asMetricPrefix()).isEqualTo("test_com:9042");
assertThat(address.isUnresolved()).isTrue();
}

@Test
public void should_reject_null_address() {
thrown.expect(NullPointerException.class);
thrown.expectMessage("address can't be null");

new DefaultEndPoint(null);
}
}

0 comments on commit 66415c8

Please sign in to comment.