Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imlement issue #480 : UDP target host address is cached #502

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -42,7 +41,10 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -78,7 +80,7 @@ public class InfluxDBImpl implements InfluxDB {
*/
private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY));

private final InetAddress hostAddress;
private final String hostName;
private String version;
private final Retrofit retrofit;
private final InfluxDBService influxDBService;
Expand Down Expand Up @@ -116,7 +118,7 @@ public class InfluxDBImpl implements InfluxDB {
public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client,
final ResponseFormat responseFormat) {
this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat);
this.hostAddress = parseHostAddress(url);
this.hostName = parseHost(url);

this.loggingInterceptor = new HttpLoggingInterceptor();
setLogLevel(LOG_LEVEL);
Expand Down Expand Up @@ -162,7 +164,7 @@ public InfluxDBImpl(final String url, final String username, final String passwo
final InfluxDBService influxDBService, final JsonAdapter<QueryResult> adapter) {
super();
this.messagePack = false;
this.hostAddress = parseHostAddress(url);
this.hostName = parseHost(url);

this.loggingInterceptor = new HttpLoggingInterceptor();
setLogLevel(LOG_LEVEL);
Expand All @@ -187,18 +189,25 @@ public InfluxDBImpl(final String url, final String username, final String passwo
setRetentionPolicy(retentionPolicy);
}

private InetAddress parseHostAddress(final String url) {
HttpUrl httpUrl = HttpUrl.parse(url);
private String parseHost(final String url) {
String hostName;
try {
URI uri = new URI(url);
hostName = uri.getHost();
} catch (URISyntaxException e1) {
throw new IllegalArgumentException("Unable to parse url: " + url, e1);
}

if (httpUrl == null) {
throw new IllegalArgumentException("Unable to parse url: " + url);
}
if (hostName == null) {
throw new IllegalArgumentException("Unable to parse url: " + url);
}

try {
return InetAddress.getByName(httpUrl.host());
} catch (UnknownHostException e) {
throw new InfluxDBIOException(e);
}
try {
InetAddress.getByName(hostName);
} catch (UnknownHostException e) {
throw new InfluxDBIOException(e);
}
return hostName;
}

@Override
Expand Down Expand Up @@ -465,7 +474,7 @@ public void write(final int udpPort, final String records) {
initialDatagramSocket();
byte[] bytes = records.getBytes(StandardCharsets.UTF_8);
try {
datagramSocket.send(new DatagramPacket(bytes, bytes.length, hostAddress, udpPort));
datagramSocket.send(new DatagramPacket(bytes, bytes.length, new InetSocketAddress(hostName, udpPort)));
} catch (IOException e) {
throw new InfluxDBIOException(e);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,24 @@ public void testWrongHostForInfluxdb(){
Assertions.assertThrows(RuntimeException.class, () -> {
InfluxDBFactory.connect("http://" + errorHost + ":" + TestUtils.getInfluxPORT(true));
});

String unresolvableHost = "a.b.c";
Assertions.assertThrows(InfluxDBIOException.class, () -> {
InfluxDBFactory.connect("http://" + unresolvableHost + ":" + TestUtils.getInfluxPORT(true));
});
}

@Test
public void testInvalidUrlHandling(){
Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("@@@http://@@@");
});

Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("http://@@@abc");
});
}

@Test
public void testBatchEnabledTwice() {
this.influxDB.enableBatch(1, 1, TimeUnit.SECONDS);
Expand Down